<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
{
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());
+ }
}
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;
}
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 {
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 <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.
*
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);
}
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
<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>
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