*/
public class CannotCompileException extends Exception {
private String message;
+ private Throwable cause;
public String getReason() {
if (message != null)
public CannotCompileException(String msg) {
super(msg);
message = msg;
+ cause = null;
}
/**
public CannotCompileException(Throwable e) {
super("by " + e.toString());
message = null;
+ cause = e;
+ }
+
+ public CannotCompileException(String msg, Throwable e) {
+ this(msg);
+ cause = e;
}
/**
* <code>NotFoundException</code>.
*/
public CannotCompileException(NotFoundException e) {
- this("cannot find " + e.getMessage());
+ this("cannot find " + e.getMessage(), e);
}
/**
* Constructs a CannotCompileException with an <code>CompileError</code>.
*/
public CannotCompileException(CompileError e) {
- super("[source error] " + e.getMessage());
+ super("[source error] " + e.getMessage(), e);
message = null;
}
* with a <code>ClassNotFoundException</code>.
*/
public CannotCompileException(ClassNotFoundException e, String name) {
- this("cannot find " + name);
+ this("cannot find " + name, e);
}
/**
* Constructs a CannotCompileException with a ClassFormatError.
*/
public CannotCompileException(ClassFormatError e, String name) {
- this("invalid class format: " + name);
+ this("invalid class format: " + name, e);
+ }
+
+ /**
+ * Prints this exception and its backtrace.
+ */
+ public void printStackTrace(java.io.PrintWriter w) {
+ super.printStackTrace(w);
+ w.println("Caused by:");
+ cause.printStackTrace(w);
}
}
package javassist.reflect;
import java.lang.reflect.*;
+import java.util.Arrays;
import java.io.Serializable;
import java.io.IOException;
import java.io.ObjectInputStream;
* class of reflective objects. It can be used to hold values
* shared among the reflective objects of the same class.
*
+ * <p>To obtain a class metaobject, calls <code>_getClass()</code>
+ * on a reflective object. For example,
+ *
+ * <ul><pre>ClassMetaobject cm = ((Metalevel)reflectiveObject)._getClass();
+ * </pre></ul>
+ *
* @see javassist.reflect.Metaobject
+ * @see javassist.reflect.Metalevel
*/
public class ClassMetaobject implements Serializable {
/**
return methods;
}
+ /**
+ * Returns the <code>java.lang.reflect.Method</code> object representing
+ * the method specified by <code>identifier</code>.
+ *
+ * <p>Note that the actual method returned will be have an altered,
+ * reflective name i.e. <code>_m_2_..</code>.
+ *
+ * @param identifier the identifier index
+ * given to <code>trapMethodcall()</code> etc.
+ * @see Metaobject#trapMethodcall(int,Object[])
+ * @see #trapMethodcall(int,Object[])
+ */
+ public final Method getMethod(int identifier) {
+ return getReflectiveMethods()[identifier];
+ }
+
/**
* Returns the name of the method specified
* by <code>identifier</code>.
public final Class getReturnType(int identifier) {
return getReflectiveMethods()[identifier].getReturnType();
}
+
+ /**
+ * Returns the identifier index of the method, as identified by its
+ * original name.
+ *
+ * <p>This method is useful, in conjuction with
+ * <link>ClassMetaobject#getMethod()</link>, to obtain a quick reference
+ * to the original method in the reflected class (i.e. not the proxy
+ * method), using the original name of the method.
+ *
+ * @param originalName The original name of the reflected method
+ * @param argTypes array of Class specifying the method signature
+ * @return the identifier index of the original method
+ * @throws NoSuchMethodException if the method does not exist
+ *
+ * @see ClassMetaobject#getMethod(int)
+ * @author Brett Randall
+ * @author Shigeru Chiba
+ */
+ public final int getMethodIndex(String originalName, Class[] argTypes)
+ throws NoSuchMethodException
+ {
+ Method[] mthds = getReflectiveMethods();
+ for (int i = 0; i < mthds.length; i++) {
+ if (mthds[i] == null)
+ continue;
+
+ // check name and parameter types match
+ if (getMethodName(i).equals(originalName)
+ && Arrays.equals(argTypes, mthds[i].getParameterTypes()))
+ return i;
+ }
+
+ throw new NoSuchMethodException("Method " + originalName
+ + " not found");
+ }
}