diff options
author | acolyer <acolyer> | 2005-11-04 13:06:27 +0000 |
---|---|---|
committer | acolyer <acolyer> | 2005-11-04 13:06:27 +0000 |
commit | 590f81aacbd8fdc9cb3aebef76a3f2da1034a7f8 (patch) | |
tree | 166328d7302d4c30a4adf68445b751e5f0258ab8 /bcel-builder | |
parent | f74be5e86f34b9ced867c4687da11caf390532a0 (diff) | |
download | aspectj-590f81aacbd8fdc9cb3aebef76a3f2da1034a7f8.tar.gz aspectj-590f81aacbd8fdc9cb3aebef76a3f2da1034a7f8.zip |
add support and tests for isAnonymous in JavaClass
Diffstat (limited to 'bcel-builder')
-rw-r--r-- | bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java | 20 | ||||
-rw-r--r-- | bcel-builder/testdata/AnonymousClassTest.java | 16 | ||||
-rw-r--r-- | bcel-builder/testdata/testcode.jar | bin | 16210 -> 17736 bytes | |||
-rw-r--r-- | bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/AllTests.java | 1 | ||||
-rw-r--r-- | bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/AnonymousClassTest.java | 57 |
5 files changed, 93 insertions, 1 deletions
diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java index e92f6a798..2a1bf1526 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java @@ -77,7 +77,7 @@ import java.util.StringTokenizer; * class file. Those interested in programatically generating classes * should see the <a href="../generic/ClassGen.html">ClassGen</a> class. - * @version $Id: JavaClass.java,v 1.7 2005/09/21 15:02:05 acolyer Exp $ + * @version $Id: JavaClass.java,v 1.8 2005/11/04 13:06:27 acolyer Exp $ * @see org.aspectj.apache.bcel.generic.ClassGen * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> */ @@ -723,6 +723,24 @@ public class JavaClass extends AccessFlags implements Cloneable, Node { return (access_flags & Constants.ACC_INTERFACE) == 0; } + public final boolean isAnonymous() { + for (int i = 0; i < this.attributes.length; i++) { + if (this.attributes[i] instanceof InnerClasses) { + InnerClass[] innerClasses = ((InnerClasses) this.attributes[i]).getInnerClasses(); + for (int j = 0; j < innerClasses.length; j++) { + if (innerClasses[j].getInnerNameIndex() == 0) { + // this is an anonymous class, but is it me, or a class contained in me?? + String inner_class_name = constant_pool.getConstantString(innerClasses[j].getInnerClassIndex(), + Constants.CONSTANT_Class); + inner_class_name = Utility.compactClassName(inner_class_name); + if (inner_class_name.equals(getClassName())) return true; + } + } + } + } + return false; + } + // J5SUPPORT: /** * Returns true if this class represents an annotation, i.e. it was a diff --git a/bcel-builder/testdata/AnonymousClassTest.java b/bcel-builder/testdata/AnonymousClassTest.java new file mode 100644 index 000000000..ca1c50101 --- /dev/null +++ b/bcel-builder/testdata/AnonymousClassTest.java @@ -0,0 +1,16 @@ +public class AnonymousClassTest { + + public void foo() { + + new Runnable() { + public void run() {}; + }.run(); + + + } + + class X {} + + static class Y {} + +}
\ No newline at end of file diff --git a/bcel-builder/testdata/testcode.jar b/bcel-builder/testdata/testcode.jar Binary files differindex 884ed866f..6bac0c9fb 100644 --- a/bcel-builder/testdata/testcode.jar +++ b/bcel-builder/testdata/testcode.jar diff --git a/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/AllTests.java b/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/AllTests.java index f7ef2ab46..52113f570 100644 --- a/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/AllTests.java +++ b/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/AllTests.java @@ -51,6 +51,7 @@ public class AllTests { suite.addTestSuite(UtilTests.class); suite.addTestSuite(GenericSignatureParsingTest.class); suite.addTestSuite(GenericsErasureTesting.class); + suite.addTestSuite(AnonymousClassTest.class); //$JUnit-END$ return suite; } diff --git a/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/AnonymousClassTest.java b/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/AnonymousClassTest.java new file mode 100644 index 000000000..34cc01352 --- /dev/null +++ b/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/AnonymousClassTest.java @@ -0,0 +1,57 @@ +/** + * Copyright (c) 2005 Contributors + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Adrian Colyer - initial implementation + */ +package org.aspectj.apache.bcel.classfile.tests; + +import java.io.File; + +import org.aspectj.apache.bcel.classfile.JavaClass; +import org.aspectj.apache.bcel.util.ClassPath; +import org.aspectj.apache.bcel.util.SyntheticRepository; + +import junit.framework.TestCase; + +/** + * @author adrian colyer + * + */ +public class AnonymousClassTest extends TestCase { + + private SyntheticRepository repos; + + public void testRegularClassIsNotAnonymous() throws ClassNotFoundException { + JavaClass clazz = repos.loadClass("AnonymousClassTest"); + assertFalse("regular outer classes are not anonymous",clazz.isAnonymous()); + } + + public void testNamedInnerClassIsNotAnonymous() throws ClassNotFoundException { + JavaClass clazz = repos.loadClass("AnonymousClassTest$X"); + assertFalse("regular inner classes are not anonymous",clazz.isAnonymous()); + } + + public void testStaticInnerClassIsNotAnonymous() throws ClassNotFoundException { + JavaClass clazz = repos.loadClass("AnonymousClassTest$Y"); + assertFalse("regular static inner classes are not anonymous",clazz.isAnonymous()); + } + + public void testAnonymousInnerClassIsAnonymous() throws ClassNotFoundException { + JavaClass clazz = repos.loadClass("AnonymousClassTest$1"); + assertTrue("anonymous inner classes are anonymous",clazz.isAnonymous()); + + } + + protected void setUp() throws Exception { + ClassPath cp = + new ClassPath("testdata"+File.separator+"testcode.jar"+File.pathSeparator+System.getProperty("java.class.path")); + repos = SyntheticRepository.getInstance(cp); + } + +} |