Search in sources :

Example 1 with InnerClassNode

use of org.objectweb.asm.tree.InnerClassNode in project bytecode-viewer by Konloch.

the class ClassNodeDecompiler method decompile.

protected static PrefixedStringBuilder decompile(PrefixedStringBuilder sb, ArrayList<String> decompiledClasses, String containerName, ClassNode cn) {
    ArrayList<String> unableToDecompile = new ArrayList<String>();
    decompiledClasses.add(cn.name);
    sb.append(getAccessString(cn.access));
    sb.append(" ");
    sb.append(cn.name);
    if (cn.superName != null && !cn.superName.equals("java/lang/Object")) {
        sb.append(" extends ");
        sb.append(cn.superName);
    }
    int amountOfInterfaces = cn.interfaces.size();
    if (amountOfInterfaces > 0) {
        sb.append(" implements ");
        sb.append(cn.interfaces.get(0));
        if (amountOfInterfaces > 1) {
        // sb.append(",");
        }
        for (int i = 1; i < amountOfInterfaces; i++) {
            sb.append(", ");
            sb.append(cn.interfaces.get(i));
        }
    }
    sb.append(" {");
    sb.append(BytecodeViewer.nl);
    for (FieldNode fn : (List<FieldNode>) cn.fields) {
        sb.append(BytecodeViewer.nl);
        sb.append("     ");
        FieldNodeDecompiler.decompile(sb, fn);
    }
    if (cn.fields.size() > 0) {
        sb.append(BytecodeViewer.nl);
    }
    for (MethodNode mn : (List<MethodNode>) cn.methods) {
        sb.append(BytecodeViewer.nl);
        MethodNodeDecompiler.decompile(sb, mn, cn);
    }
    for (Object o : cn.innerClasses) {
        InnerClassNode innerClassNode = (InnerClassNode) o;
        String innerClassName = innerClassNode.name;
        if ((innerClassName != null) && !decompiledClasses.contains(innerClassName)) {
            decompiledClasses.add(innerClassName);
            ClassNode cn1 = BytecodeViewer.getClassNode(containerName, innerClassName);
            if (cn1 != null) {
                sb.appendPrefix("     ");
                sb.append(BytecodeViewer.nl + BytecodeViewer.nl);
                sb = decompile(sb, decompiledClasses, containerName, cn1);
                sb.trimPrefix(5);
                sb.append(BytecodeViewer.nl);
            } else {
                unableToDecompile.add(innerClassName);
            }
        }
    }
    if (!unableToDecompile.isEmpty()) {
        sb.append("//the following inner classes couldn't be decompiled: ");
        for (String s : unableToDecompile) {
            sb.append(s);
            sb.append(" ");
        }
        sb.append(BytecodeViewer.nl);
    }
    sb.append("}");
    // " with prefix length: " + sb.prefix.length());
    return sb;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) InnerClassNode(org.objectweb.asm.tree.InnerClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) InnerClassNode(org.objectweb.asm.tree.InnerClassNode)

Example 2 with InnerClassNode

use of org.objectweb.asm.tree.InnerClassNode in project openj9 by eclipse.

the class ClassFileCompare method compareInnerClasses.

private void compareInnerClasses(ClassNode class1, ClassNode class2) {
    if (nullCheck(class1.innerClasses, class2.innerClasses, "Inner classes")) {
        return;
    }
    /*
		 * Copy and sort inner classes by name, innerName, outerName
		 */
    @SuppressWarnings("unchecked") List<InnerClassNode> innerClasses1 = new ArrayList<>(class1.innerClasses);
    @SuppressWarnings("unchecked") List<InnerClassNode> innerClasses2 = new ArrayList<>(class2.innerClasses);
    Comparator<InnerClassNode> comparator = new Comparator<InnerClassNode>() {

        @Override
        public int compare(InnerClassNode innerClass1, InnerClassNode innerClass2) {
            int name = innerClass1.name.compareTo(innerClass2.name);
            if (0 != name) {
                return name;
            }
            int innerName = innerClass1.innerName.compareTo(innerClass2.innerName);
            if (0 != innerName) {
                return innerName;
            }
            return innerClass1.outerName.compareTo(innerClass2.outerName);
        }
    };
    Collections.sort(innerClasses1, comparator);
    Collections.sort(innerClasses2, comparator);
    /*
		 * Compare inner classes
		 */
    if (innerClasses1.size() != innerClasses2.size()) {
        reportDifference("Inner classes differ: " + innerClasses1 + ", " + innerClasses2);
    } else {
        Iterator<InnerClassNode> iter = innerClasses1.iterator();
        for (InnerClassNode innerClass2 : innerClasses2) {
            InnerClassNode innerClass1 = iter.next();
            /*
				 * ASM would not add any extra flag like ACC_SYNTHETIC_ATTRIBUTE to the inner_class_access_flags 
				 * since it may not have access to the class file of inner class when inspecting its attributes.
				 * Thus, there is no need to use compareAccessFlag() in inner class.
				 */
            compare(innerClass1.access, innerClass2.access, "Inner class access flags", true);
            compare(innerClass1.name, innerClass2.name, "Inner class name");
            compare(innerClass1.innerName, innerClass2.innerName, "Inner class inner name");
            compare(innerClass1.outerName, innerClass2.outerName, "Inner class outer name");
        }
    }
}
Also used : ArrayList(java.util.ArrayList) InnerClassNode(org.objectweb.asm.tree.InnerClassNode) Comparator(java.util.Comparator)

Example 3 with InnerClassNode

use of org.objectweb.asm.tree.InnerClassNode in project pinpoint by naver.

the class ASMClassNodeAdapter method getInnerClasses.

public List<ASMClassNodeAdapter> getInnerClasses() {
    if (this.classNode.innerClasses == null) {
        return Collections.emptyList();
    }
    final List<ASMClassNodeAdapter> innerClasses = new ArrayList<>();
    final List<InnerClassNode> innerClassNodes = this.classNode.innerClasses;
    for (InnerClassNode node : innerClassNodes) {
        if (node.name == null) {
            continue;
        }
        // skip code.
        ASMClassNodeAdapter adapter = get(this.pluginInputStreamProvider, this.classLoader, this.protectionDomain, node.name, true);
        if (adapter != null) {
            innerClasses.add(adapter);
        }
    }
    return innerClasses;
}
Also used : ArrayList(java.util.ArrayList) InnerClassNode(org.objectweb.asm.tree.InnerClassNode)

Example 4 with InnerClassNode

use of org.objectweb.asm.tree.InnerClassNode in project bytecode-viewer by Konloch.

the class ClassNodeDecompiler method decompile.

public static PrefixedStringBuilder decompile(PrefixedStringBuilder sb, List<String> decompiledClasses, ClassNode cn) {
    List<String> unableToDecompile = new ArrayList<>();
    decompiledClasses.add(cn.name);
    sb.append(getAccessString(cn.access));
    sb.append(" ");
    sb.append(cn.name);
    if (cn.superName != null && !cn.superName.equals("java/lang/Object")) {
        sb.append(" extends ");
        sb.append(cn.superName);
    }
    int amountOfInterfaces = cn.interfaces.size();
    if (amountOfInterfaces > 0) {
        sb.append(" implements ");
        sb.append(cn.interfaces.get(0));
        for (int i = 1; i < amountOfInterfaces; i++) {
            sb.append(", ");
            sb.append(cn.interfaces.get(i));
        }
    }
    sb.append(" {");
    sb.append(nl);
    sb.append("     ");
    sb.append("<ClassVersion=" + cn.version + ">");
    sb.append(nl);
    if (cn.sourceDebug != null) {
        sb.append("     ");
        sb.append("<SourceDebug=" + cn.sourceDebug + ">");
        sb.append(nl);
    }
    if (cn.sourceFile != null) {
        sb.append("     ");
        sb.append("<SourceFile=" + cn.sourceFile + ">");
        sb.append(nl);
    }
    if (cn.signature != null) {
        sb.append("     ");
        sb.append("<Sig=" + cn.signature + ">");
    }
    for (FieldNode fn : cn.fields) {
        sb.append(nl);
        sb.append("     ");
        FieldNodeDecompiler.decompile(sb, fn);
    }
    if (cn.fields.size() > 0) {
        sb.append(nl);
    }
    for (MethodNode mn : cn.methods) {
        sb.append(nl);
        MethodNodeDecompiler.decompile(sb, mn, cn);
    }
    for (InnerClassNode o : cn.innerClasses) {
        String innerClassName = o.name;
        if ((innerClassName != null) && !decompiledClasses.contains(innerClassName)) {
            decompiledClasses.add(innerClassName);
            ClassNode cn1 = BytecodeViewer.blindlySearchForClassNode(innerClassName);
            if (cn1 != null) {
                sb.appendPrefix("     ");
                sb.append(nl + nl);
                sb = decompile(sb, decompiledClasses, cn1);
                sb.trimPrefix(5);
                sb.append(nl);
            } else {
                unableToDecompile.add(innerClassName);
            }
        }
    }
    if (!unableToDecompile.isEmpty()) {
        sb.append("// The following inner classes couldn't be decompiled: ");
        for (String s : unableToDecompile) {
            sb.append(s);
            sb.append(" ");
        }
        sb.append(nl);
    }
    if (cn.attrs != null) {
        sb.append(nl);
        for (Attribute attr : cn.attrs) {
            // TODO: finish attributes
            // + attr.content.toString());
            sb.append(attr.type + ": ");
        }
    }
    // sb.append(BytecodeViewer.nl);
    sb.append("}");
    // " with prefix length: " + sb.prefix.length());
    return sb;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) InnerClassNode(org.objectweb.asm.tree.InnerClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode) Attribute(org.objectweb.asm.Attribute) ArrayList(java.util.ArrayList) InnerClassNode(org.objectweb.asm.tree.InnerClassNode)

Example 5 with InnerClassNode

use of org.objectweb.asm.tree.InnerClassNode in project dex2jar by pxb1988.

the class Dex2Asm method convertClass.

public void convertClass(DexClassNode classNode, ClassVisitorFactory cvf, Map<String, Clz> classes) {
    ClassVisitor cv = cvf.create(toInternalName(classNode.className));
    if (cv == null) {
        return;
    }
    // the default value of static-final field are omitted by dex, fix it
    DexFix.fixStaticFinalFieldValue(classNode);
    String signature = null;
    if (classNode.anns != null) {
        for (DexAnnotationNode ann : classNode.anns) {
            if (ann.visibility == Visibility.SYSTEM) {
                switch(ann.type) {
                    case DexConstants.ANNOTATION_SIGNATURE_TYPE:
                        {
                            Object[] strs = (Object[]) findAnnotationAttribute(ann, "value");
                            if (strs != null) {
                                StringBuilder sb = new StringBuilder();
                                for (Object str : strs) {
                                    sb.append(str);
                                }
                                signature = sb.toString();
                            }
                        }
                        break;
                }
            }
        }
    }
    String[] interfaceInterNames = null;
    if (classNode.interfaceNames != null) {
        interfaceInterNames = new String[classNode.interfaceNames.length];
        for (int i = 0; i < classNode.interfaceNames.length; i++) {
            interfaceInterNames[i] = toInternalName(classNode.interfaceNames[i]);
        }
    }
    Clz clzInfo = classes.get(classNode.className);
    int access = classNode.access;
    boolean isInnerClass = false;
    if (clzInfo != null) {
        isInnerClass = clzInfo.enclosingClass != null || clzInfo.enclosingMethod != null;
    }
    access = clearClassAccess(isInnerClass, access);
    cv.visit(Opcodes.V1_6, access, toInternalName(classNode.className), signature, classNode.superClass == null ? null : toInternalName(classNode.superClass), interfaceInterNames);
    List<InnerClassNode> innerClassNodes = new ArrayList<InnerClassNode>(5);
    if (clzInfo != null) {
        searchInnerClass(clzInfo, innerClassNodes, classNode.className);
    }
    if (isInnerClass) {
        // build Outer Clz
        if (clzInfo.innerName == null) {
            // anonymous Innerclass
            Method enclosingMethod = clzInfo.enclosingMethod;
            if (enclosingMethod != null) {
                cv.visitOuterClass(toInternalName(enclosingMethod.getOwner()), enclosingMethod.getName(), enclosingMethod.getDesc());
            } else {
                Clz enclosingClass = clzInfo.enclosingClass;
                cv.visitOuterClass(toInternalName(enclosingClass.name), null, null);
            }
        }
        searchEnclosing(clzInfo, innerClassNodes);
    }
    Collections.sort(innerClassNodes, INNER_CLASS_NODE_COMPARATOR);
    for (InnerClassNode icn : innerClassNodes) {
        if (icn.innerName != null && !isJavaIdentifier(icn.innerName)) {
            System.err.println("WARN: ignored invalid inner class name " + ", treat as anonymous inner class.");
            icn.innerName = null;
            icn.outerName = null;
        }
        icn.accept(cv);
    }
    accept(classNode.anns, cv);
    if (classNode.fields != null) {
        for (DexFieldNode fieldNode : classNode.fields) {
            convertField(classNode, fieldNode, cv);
        }
    }
    if (classNode.methods != null) {
        for (DexMethodNode methodNode : classNode.methods) {
            convertMethod(classNode, methodNode, cv);
        }
    }
    cv.visitEnd();
}
Also used : IrMethod(com.googlecode.dex2jar.ir.IrMethod) InnerClassNode(org.objectweb.asm.tree.InnerClassNode)

Aggregations

InnerClassNode (org.objectweb.asm.tree.InnerClassNode)14 ArrayList (java.util.ArrayList)6 ClassNode (org.objectweb.asm.tree.ClassNode)5 MethodNode (org.objectweb.asm.tree.MethodNode)4 FieldNode (org.objectweb.asm.tree.FieldNode)3 IrMethod (com.googlecode.dex2jar.ir.IrMethod)2 PrintWriter (java.io.PrintWriter)2 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)2 StringWriter (java.io.StringWriter)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Comparator (java.util.Comparator)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1 MethodIdentifier (org.evosuite.setup.PutStaticMethodCollector.MethodIdentifier)1 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)1 GenericClass (org.evosuite.utils.generic.GenericClass)1 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)1