Search in sources :

Example 16 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project felix by apache.

the class NamesTestCase method testSpecificationRecognizedOnlyMap.

public void testSpecificationRecognizedOnlyMap() throws Exception {
    MethodNode node = new MethodNode();
    node.name = "handle";
    node.desc = "(Ljava/util/Map;)V";
    assertNull(getMethodIdentifier(node));
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode)

Example 17 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project felix by apache.

the class NamesTestCase method testSpecificationRecognizedWithDictionary.

public void testSpecificationRecognizedWithDictionary() throws Exception {
    MethodNode node = new MethodNode();
    node.name = "handle";
    node.desc = "(Lmy/Service;Ljava/util/Dictionary;)V";
    assertEquals("my.Service", getMethodIdentifier(node));
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode)

Example 18 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project felix by apache.

the class ClassScanner method extractAnnotation.

/**
 * Extract annotations
 */
private final List<ScannedAnnotation> extractAnnotation(final ClassNode classNode, final Class<?> annotatedClass) throws SCRDescriptorException {
    final List<ScannedAnnotation> descriptions = new ArrayList<ScannedAnnotation>();
    // first parse class annotations
    @SuppressWarnings("unchecked") final List<AnnotationNode> annotations = getAllAnnotations(classNode.invisibleAnnotations, classNode.visibleAnnotations);
    if (annotations != null) {
        for (final AnnotationNode annotation : annotations) {
            this.parseAnnotation(descriptions, annotation, annotatedClass);
        }
        // second parse method annotations
        @SuppressWarnings("unchecked") final List<MethodNode> methods = classNode.methods;
        if (methods != null) {
            for (final MethodNode method : methods) {
                final String name = method.name;
                // check for constructor
                if (!"<init>".equals(name)) {
                    @SuppressWarnings("unchecked") final List<AnnotationNode> annos = getAllAnnotations(method.invisibleAnnotations, method.visibleAnnotations);
                    if (annos != null) {
                        final Type[] signature = Type.getArgumentTypes(method.desc);
                        final Method[] allMethods = annotatedClass.getDeclaredMethods();
                        Method found = null;
                        for (final Method m : allMethods) {
                            if (m.getName().equals(name)) {
                                if (m.getParameterTypes().length == 0 && (signature == null || signature.length == 0)) {
                                    found = m;
                                }
                                if (m.getParameterTypes().length > 0 && signature != null && m.getParameterTypes().length == signature.length) {
                                    found = m;
                                    for (int index = 0; index < m.getParameterTypes().length; index++) {
                                        String parameterTypeName = m.getParameterTypes()[index].getName();
                                        // Name of array parameters is returned with syntax [L<name>;, convert to <name>[]
                                        Matcher matcher = ARRAY_PARAM_TYPE_NAME.matcher(parameterTypeName);
                                        if (matcher.matches()) {
                                            parameterTypeName = matcher.group(1) + "[]";
                                        }
                                        if (!parameterTypeName.equals(signature[index].getClassName()) && !m.getParameterTypes()[index].getSimpleName().equals(signature[index].getClassName())) {
                                            found = null;
                                        }
                                    }
                                }
                                // if method is found return it now, to avoid resetting 'found' to null if next method has same name but different parameters
                                if (found != null) {
                                    break;
                                }
                            }
                        }
                        if (found == null) {
                            throw new SCRDescriptorException("Annotated method " + name + " not found.", annotatedClass.getName());
                        }
                        for (final AnnotationNode annotation : annos) {
                            parseAnnotation(descriptions, annotation, found);
                        }
                    }
                }
            }
        }
        // third parse field annotations
        @SuppressWarnings("unchecked") final List<FieldNode> fields = classNode.fields;
        if (fields != null) {
            for (final FieldNode field : fields) {
                @SuppressWarnings("unchecked") final List<AnnotationNode> annos = getAllAnnotations(field.invisibleAnnotations, field.visibleAnnotations);
                if (annos != null) {
                    final String name = field.name;
                    final Field[] allFields = annotatedClass.getDeclaredFields();
                    Field found = null;
                    for (final Field f : allFields) {
                        if (f.getName().equals(name)) {
                            found = f;
                            break;
                        }
                    }
                    if (found == null) {
                        throw new SCRDescriptorException("Annotated field " + name + " not found.", annotatedClass.getName());
                    }
                    for (final AnnotationNode annotation : annos) {
                        parseAnnotation(descriptions, annotation, found);
                    }
                }
            }
        }
    }
    return descriptions;
}
Also used : FieldNode(org.objectweb.asm.tree.FieldNode) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) Type(org.objectweb.asm.Type) MethodNode(org.objectweb.asm.tree.MethodNode) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) ScannedAnnotation(org.apache.felix.scrplugin.annotations.ScannedAnnotation) SCRDescriptorException(org.apache.felix.scrplugin.SCRDescriptorException)

Example 19 with MethodNode

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

the class CodeSequenceDiagram method execute.

@Override
public void execute(ArrayList<ClassNode> classNodeList) {
    if (BytecodeViewer.viewer.workPane.getCurrentViewer() == null || !(BytecodeViewer.viewer.workPane.getCurrentViewer() instanceof ClassViewer)) {
        BytecodeViewer.showMessage("First open a class file.");
        return;
    }
    ClassNode c = BytecodeViewer.viewer.workPane.getCurrentViewer().cn;
    if (c == null) {
        BytecodeViewer.showMessage("ClassNode is null for CodeSequenceDiagram. Please report to @Konloch");
        return;
    }
    JFrame frame = null;
    if (c.name != null)
        frame = new JFrame("Code Sequence Diagram - " + c.name);
    else
        frame = new JFrame("Code Sequence Diagram - Unknown Name");
    frame.setIconImages(Resources.iconList);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(400, 320);
    mxGraph graph = new mxGraph();
    graph.setVertexLabelsMovable(false);
    graph.setGridEnabled(true);
    graph.setEnabled(false);
    graph.setCellsEditable(false);
    graph.setCellsSelectable(false);
    graph.setCellsMovable(false);
    graph.setCellsLocked(true);
    Object parent = graph.getDefaultParent();
    Font font = UIManager.getDefaults().getFont("TabbedPane.font");
    AffineTransform affinetransform = new AffineTransform();
    FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
    graph.getModel().beginUpdate();
    try {
        int testX = 10;
        int testY = 0;
        double magicNumber = 5.8;
        for (MethodNode m : (ArrayList<MethodNode>) c.methods) {
            String mIdentifier = c.name + "." + m.name + m.desc;
            Object node = graph.insertVertex(parent, null, mIdentifier, testX, testY, mIdentifier.length() * magicNumber, 30);
            Object attach = node;
            testX += (int) (font.getStringBounds(mIdentifier, frc).getWidth()) + 60;
            for (AbstractInsnNode i : m.instructions.toArray()) {
                if (i instanceof MethodInsnNode) {
                    MethodInsnNode mi = (MethodInsnNode) i;
                    String identifier = mi.owner + "." + mi.name + mi.desc;
                    Object node2 = graph.insertVertex(parent, null, identifier, testX, testY, identifier.length() * 5, 30);
                    testX += (int) (font.getStringBounds(identifier, frc).getWidth()) + 60;
                    graph.insertEdge(parent, null, null, attach, node2);
                    attach = node2;
                }
            }
            testY += 60;
            testX = 10;
        }
    } finally {
        graph.getModel().endUpdate();
    }
    mxGraphComponent graphComponent = new mxGraphComponent(graph);
    frame.getContentPane().add(graphComponent);
    frame.setVisible(true);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ArrayList(java.util.ArrayList) com.mxgraph.view.mxGraph(com.mxgraph.view.mxGraph) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) AffineTransform(java.awt.geom.AffineTransform) ClassViewer(the.bytecode.club.bytecodeviewer.gui.ClassViewer) FontRenderContext(java.awt.font.FontRenderContext) com.mxgraph.swing.mxGraphComponent(com.mxgraph.swing.mxGraphComponent)

Example 20 with MethodNode

use of org.objectweb.asm.tree.MethodNode 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)

Aggregations

MethodNode (org.objectweb.asm.tree.MethodNode)322 ClassNode (org.objectweb.asm.tree.ClassNode)123 Test (org.junit.Test)94 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)59 ClassReader (org.objectweb.asm.ClassReader)57 InsnList (org.objectweb.asm.tree.InsnList)49 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)47 Label (org.objectweb.asm.Label)44 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)41 InsnNode (org.objectweb.asm.tree.InsnNode)34 ClassWriter (org.objectweb.asm.ClassWriter)26 FieldNode (org.objectweb.asm.tree.FieldNode)26 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 ArrayList (java.util.ArrayList)24 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)24 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)21 LabelNode (org.objectweb.asm.tree.LabelNode)19 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)19 List (java.util.List)17 Type (org.objectweb.asm.Type)17