summaryrefslogtreecommitdiffstats
path: root/src/main/javassist
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/javassist')
-rw-r--r--src/main/javassist/ClassPool.java3
-rw-r--r--src/main/javassist/CtClass.java12
-rw-r--r--src/main/javassist/CtClassType.java26
3 files changed, 38 insertions, 3 deletions
diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java
index 50143d6a..3e0ed6f5 100644
--- a/src/main/javassist/ClassPool.java
+++ b/src/main/javassist/ClassPool.java
@@ -248,6 +248,9 @@ public class ClassPool {
throws NotFoundException
{
CtClass clazz = get0(orgName, false);
+ if (clazz == null)
+ throw new NotFoundException(orgName);
+
if (clazz instanceof CtClassType)
((CtClassType)clazz).setClassPool(this);
diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java
index 78b40150..d3de83c5 100644
--- a/src/main/javassist/CtClass.java
+++ b/src/main/javassist/CtClass.java
@@ -531,13 +531,23 @@ public abstract class CtClass {
* If this class is a member class or interface of another class,
* then the class enclosing this class is returned.
*
- * @return null if this class is a top-level class.
+ * @return null if this class is a top-level class or an anonymous class.
*/
public CtClass getDeclaringClass() throws NotFoundException {
return null;
}
/**
+ * Returns the immediately enclosing class of this class.
+ * This method works only with JDK 1.5 or later.
+ *
+ * @return null if this class is a top-level class.
+ */
+ public CtClass getEnclosingClass() throws NotFoundException {
+ return null;
+ }
+
+ /**
* Makes a new nested class. Making a nested class modifies the
* data in this <code>CtClass</code>.
*
diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java
index d567a9b9..2993a343 100644
--- a/src/main/javassist/CtClassType.java
+++ b/src/main/javassist/CtClassType.java
@@ -24,6 +24,7 @@ import javassist.bytecode.CodeAttribute;
import javassist.bytecode.CodeIterator;
import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;
+import javassist.bytecode.EnclosingMethodAttribute;
import javassist.bytecode.FieldInfo;
import javassist.bytecode.InnerClassesAttribute;
import javassist.bytecode.MethodInfo;
@@ -186,6 +187,10 @@ class CtClassType extends CtClass {
fin = new BufferedInputStream(fin);
classfile = new ClassFile(new DataInputStream(fin));
+ if (!classfile.getName().equals(qualifiedName))
+ throw new RuntimeException(classfile.getName() + " in "
+ + qualifiedName.replace('.', '/') + ".java");
+
return classfile;
}
catch (NotFoundException e) {
@@ -421,12 +426,29 @@ class CtClassType extends CtClass {
String name = getName();
int n = ica.tableLength();
for (int i = 0; i < n; ++i)
- if (name.equals(ica.innerClass(i)))
- return classPool.get(ica.outerClass(i));
+ if (name.equals(ica.innerClass(i))) {
+ String outName = ica.outerClass(i);
+ if (outName != null)
+ return classPool.get(outName);
+ }
return null;
}
+ public CtClass getEnclosingClass() throws NotFoundException {
+ CtClass enc = getDeclaringClass();
+ if (enc == null) {
+ ClassFile cf = getClassFile2();
+ EnclosingMethodAttribute ema
+ = (EnclosingMethodAttribute)cf.getAttribute(
+ EnclosingMethodAttribute.tag);
+ if (ema != null)
+ enc = classPool.get(ema.className());
+ }
+
+ return enc;
+ }
+
public CtClass makeNestedClass(String name, boolean isStatic) {
if (!isStatic)
throw new RuntimeException(