Search in sources :

Example 6 with ClassRef

use of com.jopdesign.common.type.ClassRef in project jop by jop-devel.

the class InnerClassesInfo method buildInnerClasses.

/**
     * Build a map of InnerClasses containing all entries for all inner classes in the 'classes'
     * parameter and all their enclosing inner classes.
     *
     * @param classes a collection of classes the result should contain.
     * @return the map of className->InnerClass containing all inner classes in the 'classes' parameter.
     */
private Collection<InnerClass> buildInnerClasses(Collection<ClassRef> classes) {
    List<ClassRef> queue = new LinkedList<ClassRef>(classes);
    Set<String> visited = new HashSet<String>();
    List<InnerClass> inner = new LinkedList<InnerClass>();
    ConstantPoolGen cpg = classGen.getConstantPool();
    InnerClasses ic = getInnerClassesAttribute();
    while (!queue.isEmpty()) {
        ClassRef ref = queue.remove(0);
        ClassInfo cls = ref.getClassInfo();
        int innerClassIdx;
        int outerClassIdx = 0;
        int innerNameIdx = 0;
        int flags = 0;
        String enclosingClass = null;
        if (cls != null) {
            InnerClassesInfo info = cls.getInnerClassesInfo();
            // only nested classes have an entry
            if (!info.isNestedClass()) {
                continue;
            }
            enclosingClass = info.getEnclosingClassName();
            innerClassIdx = cpg.addClass(ref.getClassName());
            if (!info.isLocalInnerClass()) {
                // class is a member, add outer class reference
                outerClassIdx = cpg.addClass(enclosingClass);
            }
            if (!info.isAnonymousInnerClass()) {
                // class has a simple name
                innerNameIdx = cpg.addUtf8(info.getInnerName());
            }
            flags = cls.getAccessFlags();
        } else {
            // unknown class, need to check the existing InnerClass array, if it exists
            InnerClass i = findInnerClass(ic, ref.getClassName());
            if (i == null) {
                // class is not an innerclass according to our old InnerClasses table
                continue;
            }
            innerClassIdx = i.getInnerClassIndex();
            outerClassIdx = i.getOuterClassIndex();
            innerNameIdx = i.getInnerNameIndex();
            flags = i.getInnerAccessFlags();
        }
        // will add a reference to the enclosing class anyway
        if (enclosingClass != null && !visited.contains(enclosingClass)) {
            queue.add(classInfo.getAppInfo().getClassRef(enclosingClass));
        }
        visited.add(ref.getClassName());
        inner.add(new InnerClass(innerClassIdx, outerClassIdx, innerNameIdx, flags));
    }
    return inner;
}
Also used : ClassRef(com.jopdesign.common.type.ClassRef) InnerClasses(org.apache.bcel.classfile.InnerClasses) LinkedList(java.util.LinkedList) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) InnerClass(org.apache.bcel.classfile.InnerClass) HashSet(java.util.HashSet) ConstantClassInfo(com.jopdesign.common.type.ConstantClassInfo)

Aggregations

ClassRef (com.jopdesign.common.type.ClassRef)6 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 InnerClass (org.apache.bcel.classfile.InnerClass)2 InnerClasses (org.apache.bcel.classfile.InnerClasses)2 ClassInfo (com.jopdesign.common.ClassInfo)1 MethodInfo (com.jopdesign.common.MethodInfo)1 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)1 ConstantClassInfo (com.jopdesign.common.type.ConstantClassInfo)1 ConstantMethodInfo (com.jopdesign.common.type.ConstantMethodInfo)1 FieldRef (com.jopdesign.common.type.FieldRef)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 ClassFormatException (org.apache.bcel.classfile.ClassFormatException)1 Constant (org.apache.bcel.classfile.Constant)1 CPInstruction (org.apache.bcel.generic.CPInstruction)1 InstructionHandle (org.apache.bcel.generic.InstructionHandle)1 InstructionList (org.apache.bcel.generic.InstructionList)1 InstructionFinder (org.apache.bcel.util.InstructionFinder)1