All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testifyproject.ClassMap Maven / Gradle / Ivy

The newest version!
/*
 * Javassist, a Java-bytecode translator toolkit.
 * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in org.testifyproject.testifyprojectpliance with
 * the License.  Alternatively, the contents of this file may be used under
 * the terms of the GNU Lesser General Public License Version 2.1 or later,
 * or the Apache License Version 2.0.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 */

package org.testifyproject;

import org.testifyproject.bytecode.Descriptor;

/**
 * A hash table associating class names with different names.
 *
 * 

This hashtable is used for replacing class names in a class * definition or a method body. Define a subclass of this class * if a more org.testifyproject.testifyprojectplex mapping algorithm is needed. For example, * *

class MyClassMap extends ClassMap {
 *   public Object get(Object jvmClassName) {
 *     String name = toJavaName((String)jvmClassName);
 *     if (name.startsWith("java."))
 *         return toJvmName("java2." + name.substring(5));
 *     else
 *         return super.get(jvmClassName);
 *   }
 * }
* *

This subclass maps java.lang.String to * java2.lang.String. Note that get() * receives and returns the internal representation of a class name. * For example, the internal representation of java.lang.String * is java/lang/String. * *

Note that this is a map from String to String. * * @see #get(Object) * @see CtClass#replaceClassName(ClassMap) * @see CtNewMethod#copy(CtMethod,String,CtClass,ClassMap) */ public class ClassMap extends java.util.HashMap { private ClassMap parent; /** * Constructs a hash table. */ public ClassMap() { parent = null; } ClassMap(ClassMap map) { parent = map; } /** * Maps a class name to another name in this hashtable. * The names are obtained with calling Class.getName(). * This method translates the given class names into the * internal form used in the JVM before putting it in * the hashtable. * * @param oldname the original class name * @param newname the substituted class name. */ public void put(CtClass oldname, CtClass newname) { put(oldname.getName(), newname.getName()); } /** * Maps a class name to another name in this hashtable. * If the hashtable contains another mapping from the same * class name, the old mapping is replaced. * This method translates the given class names into the * internal form used in the JVM before putting it in * the hashtable. * *

If oldname is identical to * newname, then this method does not * perform anything; it does not record the mapping from * oldname to newname. See * fix method. * * @param oldname the original class name. * @param newname the substituted class name. * @see #fix(String) */ public void put(String oldname, String newname) { if (oldname == newname) return; String oldname2 = toJvmName(oldname); String s = (String)get(oldname2); if (s == null || !s.equals(oldname2)) super.put(oldname2, toJvmName(newname)); } /** * Is equivalent to put() except that * the given mapping is not recorded into the hashtable * if another mapping from oldname is * already included. * * @param oldname the original class name. * @param newname the substituted class name. */ public void putIfNone(String oldname, String newname) { if (oldname == newname) return; String oldname2 = toJvmName(oldname); String s = (String)get(oldname2); if (s == null) super.put(oldname2, toJvmName(newname)); } protected final void put0(Object oldname, Object newname) { super.put(oldname, newname); } /** * Returns the class name to wihch the given jvmClassName * is mapped. A subclass of this class should override this method. * *

This method receives and returns the internal representation of * class name used in the JVM. * * @see #toJvmName(String) * @see #toJavaName(String) */ public Object get(Object jvmClassName) { Object found = super.get(jvmClassName); if (found == null && parent != null) return parent.get(jvmClassName); else return found; } /** * Prevents a mapping from the specified class name to another name. */ public void fix(CtClass clazz) { fix(clazz.getName()); } /** * Prevents a mapping from the specified class name to another name. */ public void fix(String name) { String name2 = toJvmName(name); super.put(name2, name2); } /** * Converts a class name into the internal representation used in * the JVM. */ public static String toJvmName(String classname) { return Descriptor.toJvmName(classname); } /** * Converts a class name from the internal representation used in * the JVM to the normal one used in Java. */ public static String toJavaName(String classname) { return Descriptor.toJavaName(classname); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy