--- /dev/null
+package javassist.expr;
+
+import javassist.CtClass;
+import javassist.CtConstructor;
+import javassist.CtMethod;
+import javassist.NotFoundException;
+import javassist.bytecode.CodeIterator;
+import javassist.bytecode.MethodInfo;
+
+/**
+ * Constructor call such as <code>this()</code> and <code>super()</code>
+ * within a constructor body.
+ *
+ * @see NewExpr
+ */
+public class ConstructorCall extends MethodCall {
+ /**
+ * Undocumented constructor. Do not use; internal-use only.
+ */
+ protected ConstructorCall(int pos, CodeIterator i, CtClass decl, MethodInfo m) {
+ super(pos, i, decl, m);
+ }
+
+ /**
+ * Returns <code>"super"</code> or "<code>"this"</code>.
+ */
+ public String getMethodName() {
+ return isSuper() ? "super" : "this";
+ }
+
+ /**
+ * Always throws a <code>NotFoundException</code>.
+ *
+ * @see #getConstructor()
+ */
+ public CtMethod getMethod() throws NotFoundException {
+ throw new NotFoundException("this is a constructor call. Call getConstructor().");
+ }
+
+ /**
+ * Returns the called constructor.
+ */
+ public CtConstructor getConstructor() throws NotFoundException {
+ return getCtClass().getConstructor(getMethodDesc());
+ }
+
+ /**
+ * Returns true if the called constructor is not <code>this()</code>
+ * but <code>super()</code> (a constructor declared in the super class).
+ */
+ public boolean isSuper() {
+ return super.isSuper();
+ }
+}
newList = newList.next;
}
else {
- expr = new MethodCall(pos, iterator, clazz, minfo);
- MethodCall mcall = (MethodCall)expr;
- if (!mcall.getMethodName().equals(
- MethodInfo.nameInit))
+ MethodCall mcall = new MethodCall(pos, iterator, clazz, minfo);
+ if (mcall.getMethodName().equals(MethodInfo.nameInit)) {
+ ConstructorCall ccall = new ConstructorCall(pos, iterator, clazz, minfo);
+ expr = ccall;
+ edit(ccall);
+ }
+ else {
+ expr = mcall;
edit(mcall);
+ }
}
}
}
/**
* Edits a method call (overridable).
+ *
* The default implementation performs nothing.
*/
public void edit(MethodCall m) throws CannotCompileException {}
+ /**
+ * Edits a constructor call (overridable).
+ * The constructor call is either
+ * <code>super()</code> or <code>this()</code>
+ * included in a constructor body.
+ *
+ * The default implementation performs nothing.
+ *
+ * @see #edit(NewExpr)
+ */
+ public void edit(ConstructorCall c) throws CannotCompileException {}
+
/**
* Edits a field-access expression (overridable).
* Field access means both read and write.
* Returns the class of the target object,
* which the method is called on.
*/
- private CtClass getCtClass() throws NotFoundException {
+ protected CtClass getCtClass() throws NotFoundException {
return thisClass.getClassPool().get(getClassName());
}
}
/**
- * Returns the name of the called method.
+ * Returns the name of the called method.
*/
public String getMethodName() {
ConstPool cp = getConstPool();
return getCtClass().getMethod(getMethodName(), getMethodDesc());
}
- private String getMethodDesc() {
+ /**
+ * Returns the descriptor of the called method.
+ */
+ protected String getMethodDesc() {
ConstPool cp = getConstPool();
int nt = getNameAndType(cp);
return cp.getUtf8Info(cp.getNameAndTypeDescriptor(nt));