fin = new BufferedInputStream(fin);
classfile = new ClassFile(new DataInputStream(fin));
if (!classfile.getName().equals(qualifiedName))
- throw new RuntimeException(classfile.getName() + " in "
- + qualifiedName.replace('.', '/') + ".java");
+ throw new RuntimeException("cannot find " + qualifiedName + ": "
+ + classfile.getName() + " found in "
+ + qualifiedName.replace('.', '/') + ".class");
return classfile;
}
* @param desc the descriptor of the field type.
*/
public void addPutstatic(CtClass c, String name, String desc) {
+ addPutstatic0(c, null, name, desc);
+ }
+
+ /**
+ * Appends PUTSTATIC.
+ *
+ * @param classname the fully-qualified name of the target class.
+ * @param filedName the field name.
+ * @param desc the descriptor of the field type.
+ */
+ public void addPutstatic(String classname, String fieldName, String desc) {
+ // if classname is null, the target class is THIS.
+ addPutstatic0(null, classname, fieldName, desc);
+ }
+
+ private void addPutstatic0(CtClass target, String classname,
+ String fieldName, String desc) {
add(PUTSTATIC);
- int ci = constPool.addClassInfo(c);
- addIndex(constPool.addFieldrefInfo(ci, name, desc));
+
+ // target is null if it represents THIS.
+ int ci = classname == null ? constPool.addClassInfo(target)
+ : constPool.addClassInfo(classname);
+ addIndex(constPool.addFieldrefInfo(ci, fieldName, desc));
growStack(-Descriptor.dataSize(desc));
}
* <p>If the descriptor represents a method type, this method returns
* (the size of the returned value) - (the sum of the data sizes
* of all the parameters). For example, if the descriptor is
- * "(I)D", then this method returns 1 (= 2 - 1).
+ * <code>"(I)D"</code>, then this method returns 1 (= 2 - 1).
*
* @param desc descriptor
*/
public static int dataSize(String desc) {
+ return dataSize(desc, true);
+ }
+
+ /**
+ * Computes the data size of parameters.
+ * If one of the parameters is double type, the size of that parameter
+ * is 2 words. For example, if the given descriptor is
+ * <code>"(IJ)D"</code>, then this method returns 3. The size of the
+ * return type is not computed.
+ *
+ * @param desc a method descriptor.
+ */
+ public static int paramSize(String desc) {
+ return -dataSize(desc, false);
+ }
+
+ private static int dataSize(String desc, boolean withRet) {
int n = 0;
char c = desc.charAt(0);
if (c == '(') {
}
}
- if (c == 'J' || c == 'D')
- n += 2;
- else if (c != 'V')
- ++n;
+ if (withRet)
+ if (c == 'J' || c == 'D')
+ n += 2;
+ else if (c != 'V')
+ ++n;
return n;
}
pruning on again (if it was initially on).
</ul>
+
<h4>Class search path</h4>
<p>The default <code>ClassPool</code> returned
<p>You must not call any method on that
<code>CtClass</code> object after <code>detach()</code> is called.
+However, you can call <code>get()</code> on <code>ClassPool</code>
+to make a new instance of <code>CtClass</code> representing
+the same class. If you call <code>get()</code>, the <code>ClassPool</code>
+reads a class file again and newly creates a <code>CtClass</code>
+object, which is returned by <code>get()</code>.
<p>
Another idea is to occasionally replace a <code>ClassPool</code> with