1 package net.sf.clirr.core.internal.bcel; 2 3 import net.sf.clirr.core.CheckerException; 4 import net.sf.clirr.core.spi.Scope; 5 6 import org.apache.bcel.Constants; 7 import org.apache.bcel.classfile.Attribute; 8 import org.apache.bcel.classfile.ConstantClass; 9 import org.apache.bcel.classfile.ConstantPool; 10 import org.apache.bcel.classfile.ConstantUtf8; 11 import org.apache.bcel.classfile.InnerClass; 12 import org.apache.bcel.classfile.InnerClasses; 13 import org.apache.bcel.classfile.JavaClass; 14 import org.apache.bcel.util.Repository; 15 16 final class BcelScopeHelper 17 { 18 19 23 private BcelScopeHelper() 24 { 25 } 26 27 34 public static Scope getScope(int accessFlags) 35 { 36 if ((accessFlags & Constants.ACC_PUBLIC) > 0) 37 { 38 return Scope.PUBLIC; 39 } 40 41 if ((accessFlags & Constants.ACC_PROTECTED) > 0) 42 { 43 return Scope.PROTECTED; 44 } 45 46 if ((accessFlags & Constants.ACC_PRIVATE) > 0) 47 { 48 return Scope.PRIVATE; 49 } 50 51 return Scope.PACKAGE; 52 } 53 54 77 public static Scope getClassScope(JavaClass jclass) throws CheckerException 78 { 79 int dollarPos = jclass.getClassName().lastIndexOf('$'); 80 if (dollarPos == -1) 81 { 82 return getScope(jclass.getAccessFlags()); 84 } 85 86 String jclassName = jclass.getClassName(); 88 String enclosingClassName = jclassName.substring(0, dollarPos); 89 Repository repo = jclass.getRepository(); 90 JavaClass enclosingClass = repo.findClass(enclosingClassName); 91 92 if (enclosingClass == null) 93 { 94 throw new CheckerException( 95 "Unable to locate enclosing class " + enclosingClassName 96 + " for nested class " + jclassName); 97 } 98 99 ConstantPool pool = enclosingClass.getConstantPool(); 100 Attribute[] attrs = enclosingClass.getAttributes(); 101 for (int i = 0; i < attrs.length; ++i) 102 { 103 if (attrs[i] instanceof InnerClasses) 104 { 105 InnerClasses ics = (InnerClasses) attrs[i]; 106 InnerClass[] icarray = ics.getInnerClasses(); 107 for (int j = 0; j < icarray.length; ++j) 108 { 109 InnerClass ic = icarray[j]; 113 int classIndex = ic.getInnerClassIndex(); 114 ConstantClass constClass = (ConstantClass) pool.getConstant(classIndex); 115 int nameIndex = constClass.getNameIndex(); 116 ConstantUtf8 nameconst = (ConstantUtf8) pool.getConstant(nameIndex); 117 String classname = nameconst.getBytes().replace('/', '.'); 118 if (jclassName.equals(classname)) 119 { 120 return getScope(ic.getInnerAccessFlags()); 121 } 122 } 123 } 124 } 125 126 throw new CheckerException( 128 "Unable to find information in class " + enclosingClass.getClassName() 129 + " referring back to nested class " + jclassName); 130 131 } 132 133 } 134 | Popular Tags |