]> source.dussan.org Git - aspectj.git/commitdiff
add support and tests for isAnonymous in JavaClass
authoracolyer <acolyer>
Fri, 4 Nov 2005 13:06:27 +0000 (13:06 +0000)
committeracolyer <acolyer>
Fri, 4 Nov 2005 13:06:27 +0000 (13:06 +0000)
bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java
bcel-builder/testdata/AnonymousClassTest.java [new file with mode: 0644]
bcel-builder/testdata/testcode.jar
bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/AllTests.java
bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/AnonymousClassTest.java [new file with mode: 0644]
lib/bcel/bcel-src.zip
lib/bcel/bcel.jar

index e92f6a798c89001245e4a17d10b491a6b0143518..2a1bf15260287595959226cea2fa4a58277408d0 100644 (file)
@@ -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 (file)
index 0000000..ca1c501
--- /dev/null
@@ -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
index 884ed866fd65ab5421e60f4962affe8275db6fa9..6bac0c9fb2eeb0259d2ee8c2560b7daedcbcecfa 100644 (file)
Binary files a/bcel-builder/testdata/testcode.jar and b/bcel-builder/testdata/testcode.jar differ
index f7ef2ab46edaa8fb0f7798adf0216eed1a4a1c45..52113f5706f8cfac8a7d519888825b8d13d5bfe0 100644 (file)
@@ -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 (file)
index 0000000..34cc013
--- /dev/null
@@ -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);
+       }
+       
+}
index 29cadc20a067ae1f0d5c0019ff809a2c7b45de93..02a8ca37c4a099af52d6663b58dd267fa6b6e3b8 100644 (file)
Binary files a/lib/bcel/bcel-src.zip and b/lib/bcel/bcel-src.zip differ
index 716f4518e5e29bd71762c730588e4f9ea9356aca..fe3553cb257b0ea1f47db80532f066ed459218ef 100644 (file)
Binary files a/lib/bcel/bcel.jar and b/lib/bcel/bcel.jar differ