diff options
-rw-r--r-- | Readme.html | 2 | ||||
-rw-r--r-- | src/main/javassist/CtClass.java | 13 | ||||
-rw-r--r-- | src/main/javassist/CtClassType.java | 68 | ||||
-rw-r--r-- | src/main/javassist/CtConstructor.java | 21 | ||||
-rw-r--r-- | src/main/javassist/Modifier.java | 6 | ||||
-rw-r--r-- | tutorial/tutorial.html | 6 | ||||
-rw-r--r-- | tutorial/tutorial2.html | 2 |
7 files changed, 115 insertions, 3 deletions
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. <li>getEnclosingClass() in javassist.CtClass was renamed to getEnclosingMethod(). <li>toMethod() in javassist.CtConstructor has been implemented. + <li>javassist.preproc package was elminated and the source was + moved to the sample directory. </ul> <p>- 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 @@ -154,6 +154,27 @@ public final class CtConstructor extends CtBehavior { } /** + * 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 <code>this()</code>. + */ + 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. * * @param src the source code representing the 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 <code>CtClass</code> object is not found, then returned as the resulting value of <code>get()</code>. <p>The <code>CtClass</code> object obtained from a <code>ClassPool</code> -object can be modified. +object can be modified +(<a href="tutorial2.html#intro">details of how to modify +a <code>CtClass</code></a> will be presented later). In the example above, it is modified so that the superclass of <code>test.Rectangle</code> is changed into a class <code>test.Point</code>. This change is reflected on the original @@ -101,7 +103,7 @@ Class clazz = cc.toClass(); <p><code>toClass()</code> requests the context class loader for the current thread to load the class file represented by the <code>CtClass</code>. It returns a <code>java.lang.Class</code> object representing the loaded class. -For more details, please see <a href="#toclass">the following section</a>. +For more details, please see <a href="#toclass">this section below</a>. <a name="def"> <h4>Defining a new class</h4> 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 <code>method_info</code> 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 -<code>javassist.bytecode</code> package. +<a href="tutorial3.html#intro"><code>javassist.bytecode</code> package</a>. <p>The class files modified by Javassist requires the <code>javassist.runtime</code> package for runtime support |