From 9308710319a346bfb59f8c14e0675e5d91beff39 Mon Sep 17 00:00:00 2001 From: chiba Date: Sat, 14 Aug 2004 15:48:30 +0000 Subject: [PATCH] implemented getMethodIndex() in javassist.reflect.ClassMetaobject git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@122 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- .../javassist/CannotCompileException.java | 25 ++++++-- .../javassist/reflect/ClassMetaobject.java | 60 +++++++++++++++++++ src/main/javassist/reflect/Metaobject.java | 7 +++ 3 files changed, 88 insertions(+), 4 deletions(-) diff --git a/src/main/javassist/CannotCompileException.java b/src/main/javassist/CannotCompileException.java index 0a575577..6bb5c038 100644 --- a/src/main/javassist/CannotCompileException.java +++ b/src/main/javassist/CannotCompileException.java @@ -22,6 +22,7 @@ import javassist.compiler.CompileError; */ public class CannotCompileException extends Exception { private String message; + private Throwable cause; public String getReason() { if (message != null) @@ -36,6 +37,7 @@ public class CannotCompileException extends Exception { public CannotCompileException(String msg) { super(msg); message = msg; + cause = null; } /** @@ -44,6 +46,12 @@ public class CannotCompileException extends Exception { public CannotCompileException(Throwable e) { super("by " + e.toString()); message = null; + cause = e; + } + + public CannotCompileException(String msg, Throwable e) { + this(msg); + cause = e; } /** @@ -51,14 +59,14 @@ public class CannotCompileException extends Exception { * NotFoundException. */ public CannotCompileException(NotFoundException e) { - this("cannot find " + e.getMessage()); + this("cannot find " + e.getMessage(), e); } /** * Constructs a CannotCompileException with an CompileError. */ public CannotCompileException(CompileError e) { - super("[source error] " + e.getMessage()); + super("[source error] " + e.getMessage(), e); message = null; } @@ -67,13 +75,22 @@ public class CannotCompileException extends Exception { * with a ClassNotFoundException. */ 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); } } diff --git a/src/main/javassist/reflect/ClassMetaobject.java b/src/main/javassist/reflect/ClassMetaobject.java index ef21bafc..7a149e7d 100644 --- a/src/main/javassist/reflect/ClassMetaobject.java +++ b/src/main/javassist/reflect/ClassMetaobject.java @@ -16,6 +16,7 @@ package javassist.reflect; import java.lang.reflect.*; +import java.util.Arrays; import java.io.Serializable; import java.io.IOException; import java.io.ObjectInputStream; @@ -28,7 +29,14 @@ import java.io.ObjectOutputStream; * class of reflective objects. It can be used to hold values * shared among the reflective objects of the same class. * + *

To obtain a class metaobject, calls _getClass() + * on a reflective object. For example, + * + *

+ * * @see javassist.reflect.Metaobject + * @see javassist.reflect.Metalevel */ public class ClassMetaobject implements Serializable { /** @@ -266,6 +274,22 @@ public class ClassMetaobject implements Serializable { return methods; } + /** + * Returns the java.lang.reflect.Method object representing + * the method specified by identifier. + * + *

Note that the actual method returned will be have an altered, + * reflective name i.e. _m_2_... + * + * @param identifier the identifier index + * given to trapMethodcall() 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 identifier. @@ -298,4 +322,40 @@ public class ClassMetaobject implements Serializable { public final Class getReturnType(int identifier) { return getReflectiveMethods()[identifier].getReturnType(); } + + /** + * Returns the identifier index of the method, as identified by its + * original name. + * + *

This method is useful, in conjuction with + * ClassMetaobject#getMethod(), 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"); + } } diff --git a/src/main/javassist/reflect/Metaobject.java b/src/main/javassist/reflect/Metaobject.java index 250293da..20d23433 100644 --- a/src/main/javassist/reflect/Metaobject.java +++ b/src/main/javassist/reflect/Metaobject.java @@ -33,7 +33,14 @@ import java.io.ObjectOutputStream; * of the method calls, a subclass of Metaobject * should be defined. * + *

To obtain a metaobject, calls _getMetaobject() + * on a reflective object. For example, + * + *

+ * * @see javassist.reflect.ClassMetaobject + * @see javassist.reflect.Metalevel */ public class Metaobject implements Serializable { protected ClassMetaobject classmetaobject; -- 2.39.5