From: chiba Date: Mon, 31 Oct 2005 16:48:44 +0000 (+0000) Subject: added makeUniqueName() in CtClass. X-Git-Tag: rel_3_17_1_ga~411 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=bfdfdff690cdf56edb25ef28740da13dc2c86785;p=javassist.git added makeUniqueName() in CtClass. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@215 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- diff --git a/Readme.html b/Readme.html index e5dec48f..8cbfa2b9 100644 --- a/Readme.html +++ b/Readme.html @@ -287,6 +287,8 @@ see javassist.Dump.
  • getEnclosingClass() in javassist.CtClass was renamed to getEnclosingMethod().
  • toMethod() in javassist.CtConstructor has been implemented. +
  • javassist.preproc package was elminated and the source was + moved to the sample directory.

    - version 3.1 RC2 in September 7, 2005 diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 6e5196e5..c0962808 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -1199,4 +1199,17 @@ public abstract class CtClass { { throw new CannotCompileException("not a class"); } + + /** + * Makes a unique member name. This method guarantees that + * the returned name is not used as a prefix of any methods + * or fields visible in this class. + * If the returned name is XYZ, then any method or field names + * in this class do not start with XYZ. + * + * @param prefix the prefix of the member name. + */ + public String makeUniqueName(String prefix) { + throw new RuntimeException("not available in " + getName()); + } } diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 0fb42def..bdf389cd 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -26,6 +26,8 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Hashtable; import java.util.List; +import java.util.Set; + import javassist.bytecode.AccessFlag; import javassist.bytecode.AttributeInfo; import javassist.bytecode.AnnotationsAttribute; @@ -1256,6 +1258,72 @@ class CtClassType extends CtClass { } int getUniqueNumber() { return uniqueNumberSeed++; } + + public String makeUniqueName(String prefix) { + HashMap table = new HashMap(); + makeMemberList(table); + Set keys = table.keySet(); + String[] methods = new String[keys.size()]; + keys.toArray(methods); + + if (notFindInArray(prefix, methods)) + return prefix; + + int i = 100; + String name; + do { + if (i > 999) + throw new RuntimeException("too many unique name"); + + name = prefix + i++; + } while (!notFindInArray(name, methods)); + return name; + } + + private static boolean notFindInArray(String prefix, String[] values) { + int len = values.length; + for (int i = 0; i < len; i++) + if (values[i].startsWith(prefix)) + return false; + + return true; + } + + private void makeMemberList(HashMap table) { + int mod = getModifiers(); + if (Modifier.isAbstract(mod) || Modifier.isInterface(mod)) + try { + CtClass[] ifs = getInterfaces(); + int size = ifs.length; + for (int i = 0; i < size; i++) { + CtClass ic =ifs[i]; + if (ic != null && ic instanceof CtClassType) + ((CtClassType)ic).makeMemberList(table); + } + } + catch (NotFoundException e) {} + + try { + CtClass s = getSuperclass(); + if (s != null && s instanceof CtClassType) + ((CtClassType)s).makeMemberList(table); + } + catch (NotFoundException e) {} + + List list = getClassFile2().getMethods(); + int n = list.size(); + for (int i = 0; i < n; i++) { + MethodInfo minfo = (MethodInfo)list.get(i); + table.put(minfo.getName(), this); + } + + list = getClassFile2().getFields(); + n = list.size(); + for (int i = 0; i < n; i++) { + FieldInfo finfo = (FieldInfo)list.get(i); + table.put(finfo.getName(), this); + } + } } class FieldInitLink { diff --git a/src/main/javassist/CtConstructor.java b/src/main/javassist/CtConstructor.java index 7bcf2622..fa139c79 100644 --- a/src/main/javassist/CtConstructor.java +++ b/src/main/javassist/CtConstructor.java @@ -153,6 +153,27 @@ public final class CtConstructor extends CtBehavior { return false; } + /** + * Returns true if this constructor calls a constructor + * of the super class. This method returns false if it + * calls another constructor of this class by this(). + */ + public boolean callsSuper() throws CannotCompileException { + CodeAttribute codeAttr = methodInfo.getCodeAttribute(); + if (codeAttr != null) { + CodeIterator it = codeAttr.iterator(); + try { + int index = it.skipSuperConstructor(); + return index >= 0; + } + catch (BadBytecode e) { + throw new CannotCompileException(e); + } + } + + return false; + } + /** * Sets a constructor body. * diff --git a/src/main/javassist/Modifier.java b/src/main/javassist/Modifier.java index 01ac1425..e41c7259 100644 --- a/src/main/javassist/Modifier.java +++ b/src/main/javassist/Modifier.java @@ -175,6 +175,12 @@ public class Modifier { return mod & ~clearBit; } + /** + * Return a string describing the access modifier flags in + * the specified modifier. + * + * @param mod modifier flags. + */ public static String toString(int mod) { return java.lang.reflect.Modifier.toString(mod); } diff --git a/tutorial/tutorial.html b/tutorial/tutorial.html index c2d54a87..f591abe7 100644 --- a/tutorial/tutorial.html +++ b/tutorial/tutorial.html @@ -76,7 +76,9 @@ specified key. If such a CtClass object is not found, then returned as the resulting value of get().

    The CtClass object obtained from a ClassPool -object can be modified. +object can be modified +(details of how to modify +a CtClass will be presented later). In the example above, it is modified so that the superclass of test.Rectangle is changed into a class test.Point. This change is reflected on the original @@ -101,7 +103,7 @@ Class clazz = cc.toClass();

    toClass() requests the context class loader for the current thread to load the class file represented by the CtClass. It returns a java.lang.Class object representing the loaded class. -For more details, please see the following section. +For more details, please see this section below.

    Defining a new class

    diff --git a/tutorial/tutorial2.html b/tutorial/tutorial2.html index 50ee8ec0..484e85ec 100644 --- a/tutorial/tutorial2.html +++ b/tutorial/tutorial2.html @@ -102,7 +102,7 @@ representing a method_info structure included in a class file. The low-level API uses the vocabulary from the Java Virtual machine specification. The users must have the knowledge about class files and bytecode. For more details, the users should see the -javassist.bytecode package. +javassist.bytecode package.

    The class files modified by Javassist requires the javassist.runtime package for runtime support