aboutsummaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/javassist/CtClass.java23
-rw-r--r--src/main/javassist/CtClassType.java18
-rw-r--r--src/main/javassist/bytecode/EnclosingMethodAttribute.java12
3 files changed, 45 insertions, 8 deletions
diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java
index f34a565a..9e4ca20c 100644
--- a/src/main/javassist/CtClass.java
+++ b/src/main/javassist/CtClass.java
@@ -762,8 +762,29 @@ public abstract class CtClass {
*
* @return null if this class is not a local class or an anonymous
* class.
+ * @deprecated The enclosing method might be a constructor.
+ * Use {@link #getEnclosingBehavior()}.
+ * @see #getEnclosingBehavior()
*/
- public CtMethod getEnclosingMethod() throws NotFoundException {
+ public final CtMethod getEnclosingMethod() throws NotFoundException {
+ CtBehavior b = getEnclosingBehavior();
+ if (b == null)
+ return null;
+ else if (b instanceof CtMethod)
+ return (CtMethod)b;
+ else
+ throw new NotFoundException(b.getLongName() + " is enclosing " + getName());
+ }
+
+ /**
+ * Returns the immediately enclosing method of this class.
+ * It might be not a method but a constructor.
+ * This method works only with JDK 1.5 or later.
+ *
+ * @return null if this class is not a local class or an anonymous
+ * class.
+ */
+ public CtBehavior getEnclosingBehavior() throws NotFoundException {
return null;
}
diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java
index 4d10b5dd..4df382a9 100644
--- a/src/main/javassist/CtClassType.java
+++ b/src/main/javassist/CtClassType.java
@@ -772,17 +772,25 @@ class CtClassType extends CtClass {
return null;
}
- public CtMethod getEnclosingMethod() throws NotFoundException {
+ public CtBehavior getEnclosingBehavior() throws NotFoundException {
ClassFile cf = getClassFile2();
EnclosingMethodAttribute ema
= (EnclosingMethodAttribute)cf.getAttribute(
EnclosingMethodAttribute.tag);
- if (ema != null) {
+ if (ema == null)
+ return null;
+ else {
CtClass enc = classPool.get(ema.className());
- return enc.getMethod(ema.methodName(), ema.methodDescriptor());
+ String name = ema.methodName();
+ switch (name) {
+ case MethodInfo.nameInit:
+ return enc.getConstructor(ema.methodDescriptor());
+ case MethodInfo.nameClinit:
+ return enc.getClassInitializer();
+ default:
+ return enc.getMethod(name, ema.methodDescriptor());
+ }
}
-
- return null;
}
public CtClass makeNestedClass(String name, boolean isStatic) {
diff --git a/src/main/javassist/bytecode/EnclosingMethodAttribute.java b/src/main/javassist/bytecode/EnclosingMethodAttribute.java
index d59955dd..4f422fd7 100644
--- a/src/main/javassist/bytecode/EnclosingMethodAttribute.java
+++ b/src/main/javassist/bytecode/EnclosingMethodAttribute.java
@@ -20,6 +20,8 @@ import java.io.DataInputStream;
import java.io.IOException;
import java.util.Map;
+import javassist.CtConstructor;
+
/**
* <code>EnclosingMethod_attribute</code>.
*/
@@ -98,12 +100,18 @@ public class EnclosingMethodAttribute extends AttributeInfo {
/**
* Returns the method name specified by <code>method_index</code>.
+ * If the method is a class initializer (static constructor),
+ * {@link MethodInfo#nameClinit} is returned.
*/
public String methodName() {
ConstPool cp = getConstPool();
int mi = methodIndex();
- int ni = cp.getNameAndTypeName(mi);
- return cp.getUtf8Info(ni);
+ if (mi == 0)
+ return MethodInfo.nameClinit;
+ else {
+ int ni = cp.getNameAndTypeName(mi);
+ return cp.getUtf8Info(ni);
+ }
}
/**