* 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>
*/
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
--- /dev/null
+/**
+ * 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);
+ }
+
+}