Search in sources :

Example 1 with Attribute

use of org.objectweb.asm.Attribute in project sonar-java by SonarSource.

the class AsmExample method main.

public static void main(String[] args) throws Exception {
    ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) {

        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            System.out.println("CLASS");
            System.out.println("access: " + asFlagSet(access));
            System.out.println("name: " + name);
            System.out.println("signature: " + signature);
            System.out.println("superName: " + superName);
            System.out.println("interfaces: " + Arrays.toString(interfaces));
            System.out.println();
        }

        @Override
        public void visitSource(String source, String debug) {
            System.out.println("SOURCE");
            System.out.println();
        }

        @Override
        public void visitOuterClass(String owner, String name, String desc) {
            System.out.println("OUTER CLASS");
            System.out.println("owner: " + owner);
            System.out.println("name: " + name);
            System.out.println("desc: " + desc);
            System.out.println();
        }

        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            System.out.println("ANNOTATION");
            System.out.println("desc: " + desc);
            System.out.println("visible: " + visible);
            return null;
        }

        @Override
        public void visitAttribute(Attribute attr) {
            System.out.println("ATTRIBUTE");
            System.out.println();
        }

        @Override
        public void visitInnerClass(String name, String outerName, String innerName, int access) {
            System.out.println("INNER CLASS");
            System.out.println("access: " + asFlagSet(access));
            System.out.println("name: " + name);
            System.out.println("outerName: " + outerName);
            System.out.println("innerName: " + innerName);
            System.out.println();
        }

        @Override
        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
            System.out.println("FIELD");
            System.out.println("access: " + asFlagSet(access));
            System.out.println("name: " + name);
            System.out.println("desc: " + desc);
            System.out.println("signature: " + desc);
            System.out.println();
            return null;
        }

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            System.out.println("METHOD");
            System.out.println("access: " + asFlagSet(access));
            System.out.println("name: " + name);
            System.out.println("desc: " + desc);
            System.out.println("signature: " + desc);
            System.out.println("exceptions: " + Arrays.toString(exceptions));
            System.out.println();
            return null;
        }

        @Override
        public void visitEnd() {
            System.out.println("END");
        }

        public EnumSet<Flag> asFlagSet(int flags) {
            EnumSet<Flag> result = EnumSet.noneOf(Flag.class);
            int mask = 1;
            for (int i = 0; i < 15; i++) {
                if ((flags & mask) != 0) {
                    result.add(Flag.values()[i]);
                }
                mask = mask << 1;
            }
            return result;
        }
    };
    InputStream in = AsmExample.class.getResourceAsStream("/org/sonar/java/resolve/AsmExample.class");
    ClassReader classReader = new ClassReader(in);
    classReader.accept(cv, 0);
}
Also used : Attribute(org.objectweb.asm.Attribute) InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor)

Example 2 with Attribute

use of org.objectweb.asm.Attribute 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 3 with Attribute

use of org.objectweb.asm.Attribute in project robovm by robovm.

the class ObjCProtocolProxyPlugin method generateProxyMethods.

private void generateProxyMethods(Config config, List<String> interfazes, ClassWriter cw) throws IOException {
    Clazzes clazzes = config.getClazzes();
    final Set<String> addedMethods = new HashSet<>();
    for (String interfaze : interfazes) {
        Clazz clazz = clazzes.load(interfaze);
        if (clazz == null) {
            continue;
        }
        // Copy all abstract method (we skip default methods) to the proxy 
        // and make them native instead of abstract.
        ClassReader classReader = new ClassReader(clazz.getBytes());
        classReader.accept(new ClassVisitor(ASM4, cw) {

            @Override
            public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
                String key = name + desc;
                if ((access & ACC_ABSTRACT) > 0 && !addedMethods.contains(key)) {
                    access &= ~ACC_ABSTRACT;
                    access |= ACC_NATIVE;
                    addedMethods.add(key);
                    return super.visitMethod(access, name, desc, signature, exceptions);
                }
                return null;
            }

            @Override
            public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            // Ignored
            }

            @Override
            public void visitEnd() {
            // Ignored
            }

            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                // Ignored
                return null;
            }

            @Override
            public void visitAttribute(Attribute attr) {
            // Ignored
            }

            @Override
            public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
                // Ignored
                return null;
            }

            @Override
            public void visitInnerClass(String name, String outerName, String innerName, int access) {
            // Ignored
            }

            @Override
            public void visitOuterClass(String owner, String name, String desc) {
            // Ignored
            }

            @Override
            public void visitSource(String source, String debug) {
            // Ignored
            }
        }, 0);
    }
}
Also used : Attribute(org.objectweb.asm.Attribute) ClassVisitor(org.objectweb.asm.ClassVisitor) FieldVisitor(org.objectweb.asm.FieldVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassReader(org.objectweb.asm.ClassReader) Clazz(org.robovm.compiler.clazz.Clazz) Clazzes(org.robovm.compiler.clazz.Clazzes) HashSet(java.util.HashSet)

Example 4 with Attribute

use of org.objectweb.asm.Attribute in project jacoco by jacoco.

the class ClassAnalyzerTest method should_collect_attributes.

@Test
public void should_collect_attributes() {
    assertTrue(analyzer.getClassAttributes().isEmpty());
    analyzer.visitAttribute(new Attribute("foo") {
    });
    assertTrue(analyzer.getClassAttributes().contains("foo"));
}
Also used : Attribute(org.objectweb.asm.Attribute) Test(org.junit.Test)

Example 5 with Attribute

use of org.objectweb.asm.Attribute in project maple-ir by LLVM-but-worse.

the class ASMPrinter method emitLiteral.

@Override
public void emitLiteral(Object o) {
    if (o instanceof AnnotationNode) {
        AnnotationNode an = (AnnotationNode) o;
        Map<Object, Object> map = new HashMap<>();
        map.put("desc", an.desc);
        if (isNonEmpty(an.values)) {
            List<Object> valuesList = new ArrayList<>();
            List<Object> realValues = an.values;
            if ((realValues.size() % 2) != 0) {
                throw new IllegalStateException(String.format("AN values: %s", realValues));
            }
            for (int i = 0; i < realValues.size() / 2; i++) {
                int j = (i * 2);
                String key = (String) realValues.get(j);
                Object val = realValues.get(j + 1);
                Map<String, Object> innerMap = new HashMap<>();
                innerMap.put(key, val);
                valuesList.add(innerMap);
            }
            map.put("values", valuesList);
        }
        this.emitDirectiveValue(map);
    } else if (o instanceof Attribute) {
        Attribute attr = (Attribute) o;
        Map<String, Object> map = new HashMap<>();
        map.put("type", attr.type);
        // TODO: check
        map.put("values", new String(attr.value));
        this.emitDirectiveValue(map);
        throw new UnsupportedOperationException("TODO");
    } else if (o instanceof Type) {
        this.sw.print("T\"").print(o.toString()).print("\"");
    } else {
        super.emitLiteral(o);
    }
}
Also used : HashMap(java.util.HashMap) Attribute(org.objectweb.asm.Attribute) ArrayList(java.util.ArrayList) Type(org.objectweb.asm.Type) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Attribute (org.objectweb.asm.Attribute)5 ArrayList (java.util.ArrayList)2 ClassReader (org.objectweb.asm.ClassReader)2 ClassVisitor (org.objectweb.asm.ClassVisitor)2 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Test (org.junit.Test)1 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)1 FieldVisitor (org.objectweb.asm.FieldVisitor)1 MethodVisitor (org.objectweb.asm.MethodVisitor)1 Type (org.objectweb.asm.Type)1 AnnotationNode (org.objectweb.asm.tree.AnnotationNode)1 ClassNode (org.objectweb.asm.tree.ClassNode)1 FieldNode (org.objectweb.asm.tree.FieldNode)1 InnerClassNode (org.objectweb.asm.tree.InnerClassNode)1 MethodNode (org.objectweb.asm.tree.MethodNode)1 Clazz (org.robovm.compiler.clazz.Clazz)1 Clazzes (org.robovm.compiler.clazz.Clazzes)1