Example usage for org.objectweb.asm Opcodes ASM5

List of usage examples for org.objectweb.asm Opcodes ASM5

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ASM5.

Prototype

int ASM5

To view the source code for org.objectweb.asm Opcodes ASM5.

Click Source Link

Usage

From source file:de.loskutov.bco.asm.CommentedASMifierClassVisitor.java

License:Open Source License

private CommentedASMifierClassVisitor(ClassNode classNode, final DecompilerOptions options, String name,
        int id) {
    super(Opcodes.ASM5, name, id);
    this.classNode = classNode;
    this.options = options;
    showLines = options.modes.get(BCOConstants.F_SHOW_LINE_INFO);
    showLocals = options.modes.get(BCOConstants.F_SHOW_VARIABLES);
    showStackMap = options.modes.get(BCOConstants.F_SHOW_STACKMAP);
}

From source file:de.loskutov.bco.asm.CommentedASMifierClassVisitor.java

License:Open Source License

private ASMifier getDummyVisitor() {
    if (dummyAnnVisitor == null) {
        dummyAnnVisitor = new ASMifier(Opcodes.ASM5, "", -1) {
            @Override/*from   w  ww  .j  ava 2s  .  c  om*/
            public void visitAnnotationEnd() {
                text.clear();
            }

            @Override
            public void visitClassEnd() {
                text.clear();
            }

            @Override
            public void visitFieldEnd() {
                text.clear();
            }

            @Override
            public void visitMethodEnd() {
                text.clear();
            }
        };
    }
    return dummyAnnVisitor;
}

From source file:de.loskutov.bco.asm.DecompilerHelper.java

License:Open Source License

public static DecompiledClass getDecompiledClass(final InputStream is, DecompilerOptions options)
        throws IOException, UnsupportedClassVersionError {
    ClassReader cr = new ClassReader(is);
    ClassNode cn = new ClassNode(Opcodes.ASM5);
    int crFlags = 0;
    if (options.modes.get(BCOConstants.F_EXPAND_STACKMAP)) {
        crFlags |= ClassReader.EXPAND_FRAMES;
    }//from   w  w w . j  av  a 2 s  . c  o m
    cr.accept(cn, crFlags);
    ICommentedClassVisitor printer;
    if (options.modes.get(BCOConstants.F_SHOW_ASMIFIER_CODE)) {
        printer = new CommentedASMifierClassVisitor(cn, options);
    } else {
        printer = new CommentedClassVisitor(cn, options);
    }
    TraceClassVisitor dcv = new TraceClassVisitor(null, (Printer) printer, null);
    cn.accept(dcv);
    return getResult(printer, options, cn);
}

From source file:de.thetaphi.forbiddenapis.ClassScanner.java

License:Apache License

public ClassScanner(RelatedClassLookup lookup, final Map<String, String> forbiddenClasses,
        final Iterable<ClassPatternRule> forbiddenClassPatterns, final Map<String, String> forbiddenMethods,
        final Map<String, String> forbiddenFields, final Pattern suppressAnnotations,
        final boolean internalRuntimeForbidden) {
    super(Opcodes.ASM5);
    this.lookup = lookup;
    this.forbiddenClasses = forbiddenClasses;
    this.forbiddenClassPatterns = forbiddenClassPatterns;
    this.forbiddenMethods = forbiddenMethods;
    this.forbiddenFields = forbiddenFields;
    this.suppressAnnotations = suppressAnnotations;
    this.internalRuntimeForbidden = internalRuntimeForbidden;
}

From source file:de.thetaphi.forbiddenapis.ClassScanner.java

License:Apache License

@Override
public FieldVisitor visitField(final int access, final String name, final String desc, String signature,
        Object value) {/*from ww  w  .j  ava2  s . com*/
    currentGroupId++;
    if (classSuppressed) {
        return null;
    }
    return new FieldVisitor(Opcodes.ASM5) {
        final boolean isDeprecated = (access & Opcodes.ACC_DEPRECATED) != 0;
        {
            // only check signature, if field is not synthetic
            if ((access & Opcodes.ACC_SYNTHETIC) == 0) {
                reportFieldViolation(checkDescriptor(desc), "field declaration");
            }
            if (this.isDeprecated) {
                maybeSuppressCurrentGroup(DEPRECATED_TYPE);
                reportFieldViolation(checkType(DEPRECATED_TYPE), "deprecation on field declaration");
            }
        }

        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            if (this.isDeprecated && DEPRECATED_DESCRIPTOR.equals(desc)) {
                // don't report 2 times!
                return null;
            }
            final Type type = Type.getType(desc);
            maybeSuppressCurrentGroup(type);
            reportFieldViolation(checkAnnotationDescriptor(type, visible), "annotation on field declaration");
            return null;
        }

        @Override
        public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc,
                boolean visible) {
            reportFieldViolation(checkAnnotationDescriptor(Type.getType(desc), visible),
                    "type annotation on field declaration");
            return null;
        }

        private void reportFieldViolation(String violation, String where) {
            if (violation != null) {
                violations.add(new ForbiddenViolation(currentGroupId, violation,
                        String.format(Locale.ENGLISH, "%s of '%s'", where, name), -1));
            }
        }
    };
}

From source file:de.thetaphi.forbiddenapis.ClassScanner.java

License:Apache License

@Override
public MethodVisitor visitMethod(final int access, final String name, final String desc, String signature,
        String[] exceptions) {// w  w w . j ava  2s  .  com
    currentGroupId++;
    if (classSuppressed) {
        return null;
    }
    return new MethodVisitor(Opcodes.ASM5) {
        private final Method myself = new Method(name, desc);
        private final boolean isDeprecated = (access & Opcodes.ACC_DEPRECATED) != 0;
        private int lineNo = -1;

        {
            // only check signature, if method is not synthetic
            if ((access & Opcodes.ACC_SYNTHETIC) == 0) {
                reportMethodViolation(checkDescriptor(desc), "method declaration");
            }
            if (this.isDeprecated) {
                maybeSuppressCurrentGroup(DEPRECATED_TYPE);
                reportMethodViolation(checkType(DEPRECATED_TYPE), "deprecation on method declaration");
            }
        }

        private String checkMethodAccess(String owner, Method method) {
            String violation = checkClassUse(owner, "class/interface");
            if (violation != null) {
                return violation;
            }
            final String printout = forbiddenMethods.get(owner + '\000' + method);
            if (printout != null) {
                return "Forbidden method invocation: " + printout;
            }
            final ClassSignature c = lookup.lookupRelatedClass(owner);
            if (c != null && !c.methods.contains(method)) {
                if (c.superName != null && (violation = checkMethodAccess(c.superName, method)) != null) {
                    return violation;
                }
                // JVM spec says: interfaces after superclasses
                if (c.interfaces != null) {
                    for (String intf : c.interfaces) {
                        if (intf != null && (violation = checkMethodAccess(intf, method)) != null) {
                            return violation;
                        }
                    }
                }
            }
            return null;
        }

        private String checkFieldAccess(String owner, String field) {
            String violation = checkClassUse(owner, "class/interface");
            if (violation != null) {
                return violation;
            }
            final String printout = forbiddenFields.get(owner + '\000' + field);
            if (printout != null) {
                return "Forbidden field access: " + printout;
            }
            final ClassSignature c = lookup.lookupRelatedClass(owner);
            if (c != null && !c.fields.contains(field)) {
                if (c.interfaces != null) {
                    for (String intf : c.interfaces) {
                        if (intf != null && (violation = checkFieldAccess(intf, field)) != null) {
                            return violation;
                        }
                    }
                }
                // JVM spec says: superclasses after interfaces
                if (c.superName != null && (violation = checkFieldAccess(c.superName, field)) != null) {
                    return violation;
                }
            }
            return null;
        }

        private String checkHandle(Handle handle, boolean checkLambdaHandle) {
            switch (handle.getTag()) {
            case Opcodes.H_GETFIELD:
            case Opcodes.H_PUTFIELD:
            case Opcodes.H_GETSTATIC:
            case Opcodes.H_PUTSTATIC:
                return checkFieldAccess(handle.getOwner(), handle.getName());
            case Opcodes.H_INVOKEVIRTUAL:
            case Opcodes.H_INVOKESTATIC:
            case Opcodes.H_INVOKESPECIAL:
            case Opcodes.H_NEWINVOKESPECIAL:
            case Opcodes.H_INVOKEINTERFACE:
                final Method m = new Method(handle.getName(), handle.getDesc());
                if (checkLambdaHandle && handle.getOwner().equals(internalMainClassName)
                        && handle.getName().startsWith(LAMBDA_METHOD_NAME_PREFIX)) {
                    // as described in <http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.html>,
                    // we will record this metafactory call as "lambda" invokedynamic,
                    // so we can assign the called lambda with the same groupId like *this* method:
                    lambdas.put(m, currentGroupId);
                }
                return checkMethodAccess(handle.getOwner(), m);
            }
            return null;
        }

        private String checkConstant(Object cst, boolean checkLambdaHandle) {
            if (cst instanceof Type) {
                return checkType((Type) cst);
            } else if (cst instanceof Handle) {
                return checkHandle((Handle) cst, checkLambdaHandle);
            }
            return null;
        }

        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            if (this.isDeprecated && DEPRECATED_DESCRIPTOR.equals(desc)) {
                // don't report 2 times!
                return null;
            }
            final Type type = Type.getType(desc);
            maybeSuppressCurrentGroup(type);
            reportMethodViolation(checkAnnotationDescriptor(type, visible), "annotation on method declaration");
            return null;
        }

        @Override
        public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
            reportMethodViolation(checkAnnotationDescriptor(Type.getType(desc), visible),
                    "parameter annotation on method declaration");
            return null;
        }

        @Override
        public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc,
                boolean visible) {
            reportMethodViolation(checkAnnotationDescriptor(Type.getType(desc), visible),
                    "type annotation on method declaration");
            return null;
        }

        @Override
        public AnnotationVisitor visitInsnAnnotation(int typeRef, TypePath typePath, String desc,
                boolean visible) {
            reportMethodViolation(checkAnnotationDescriptor(Type.getType(desc), visible),
                    "annotation in method body");
            return null;
        }

        @Override
        public AnnotationVisitor visitLocalVariableAnnotation(int typeRef, TypePath typePath, Label[] start,
                Label[] end, int[] index, String desc, boolean visible) {
            reportMethodViolation(checkAnnotationDescriptor(Type.getType(desc), visible),
                    "annotation in method body");
            return null;
        }

        @Override
        public AnnotationVisitor visitTryCatchAnnotation(int typeRef, TypePath typePath, String desc,
                boolean visible) {
            reportMethodViolation(checkAnnotationDescriptor(Type.getType(desc), visible),
                    "annotation in method body");
            return null;
        }

        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
            reportMethodViolation(checkMethodAccess(owner, new Method(name, desc)), "method body");
        }

        @Override
        public void visitFieldInsn(int opcode, String owner, String name, String desc) {
            reportMethodViolation(checkFieldAccess(owner, name), "method body");
        }

        @Override
        public void visitTypeInsn(int opcode, String type) {
            if (opcode == Opcodes.ANEWARRAY) {
                reportMethodViolation(checkType(Type.getObjectType(type)), "method body");
            }
        }

        @Override
        public void visitMultiANewArrayInsn(String desc, int dims) {
            reportMethodViolation(checkDescriptor(desc), "method body");
        }

        @Override
        public void visitLdcInsn(Object cst) {
            reportMethodViolation(checkConstant(cst, false), "method body");
        }

        @Override
        public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) {
            final boolean isLambdaMetaFactory = LAMBDA_META_FACTORY_INTERNALNAME.equals(bsm.getOwner());
            reportMethodViolation(checkHandle(bsm, false), "method body");
            for (final Object cst : bsmArgs) {
                reportMethodViolation(checkConstant(cst, isLambdaMetaFactory), "method body");
            }
        }

        private String getHumanReadableMethodSignature() {
            final Type[] args = Type.getType(myself.getDescriptor()).getArgumentTypes();
            final StringBuilder sb = new StringBuilder(myself.getName()).append('(');
            boolean comma = false;
            for (final Type t : args) {
                if (comma)
                    sb.append(',');
                sb.append(t.getClassName());
                comma = true;
            }
            sb.append(')');
            return sb.toString();
        }

        private void reportMethodViolation(String violation, String where) {
            if (violation != null) {
                violations.add(new ForbiddenViolation(currentGroupId, myself, violation,
                        String.format(Locale.ENGLISH, "%s of '%s'", where, getHumanReadableMethodSignature()),
                        lineNo));
            }
        }

        @Override
        public void visitLineNumber(int lineNo, Label start) {
            this.lineNo = lineNo;
        }
    };
}

From source file:de.thetaphi.forbiddenapis.ClassSignature.java

License:Apache License

public ClassSignature(final ClassReader classReader, boolean isRuntimeClass, boolean withReader) {
    this.reader = withReader ? classReader : null;
    this.isRuntimeClass = isRuntimeClass;
    this.className = classReader.getClassName();
    this.superName = classReader.getSuperName();
    this.interfaces = classReader.getInterfaces();
    final Set<Method> methods = new HashSet<Method>();
    final Set<String> fields = new HashSet<String>();
    classReader.accept(new ClassVisitor(Opcodes.ASM5) {
        @Override//  www  .j  ava2  s.c  o m
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            final Method m = new Method(name, desc);
            methods.add(m);
            return null;
        }

        @Override
        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
            fields.add(name);
            return null;
        }
    }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    this.methods = Collections.unmodifiableSet(methods);
    this.fields = Collections.unmodifiableSet(fields);
}

From source file:de.tuberlin.uebb.jbop.access.OptimizerUtils.java

License:Open Source License

/**
 * Read class./*from w  w  w  .j a  v  a  2  s. c om*/
 * 
 * @param input
 *          the input
 * @return the class node
 * @throws JBOPClassException
 *           the jBOP class exception
 */
public static ClassNode readClass(final Object input) throws JBOPClassException {
    final ClassReader classReader = new ClassReader(ClassAccessor.toBytes(input));
    final ClassNode classNode = new ClassNode(Opcodes.ASM5);
    classReader.accept(classNode, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    return classNode;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

private ClassNodeBuilder(final String className, final String superClass, final String constructorDesc,
        final String superConstructorDesc, final boolean isInterface) {
    this.isInterface = isInterface;
    classNode = new ClassNode(Opcodes.ASM5);
    classNode.access = ACC_PUBLIC;//w  w  w  . j  a v a 2s .  c o m
    classNode.name = className.replace(".", "/");
    if (superClass != null) {
        classNode.superName = superClass.replace(".", "/");
    }
    classNode.version = Opcodes.V1_7;
    if (!isInterface) {
        addConstructor(constructorDesc, superConstructorDesc);
    } else {
        classNode.access |= ACC_INTERFACE;
        classNode.access |= ACC_ABSTRACT;
    }
    lastElement = null;
}

From source file:de.tuberlin.uebb.jbop.optimizer.methodsplitter.MethodSplitter.java

License:Open Source License

@Override
public InsnList optimize(final InsnList original, final MethodNode methodNode) throws JBOPClassException {

    LocalVariablesSorter sorter = new LocalVariablesSorter(methodNode.access, methodNode.desc,
            new EmptyMethodVisitor(Opcodes.ASM5));
    methodNode.accept(sorter);//  ww w  . j  av  a  2  s.c  o  m

    if (getLength(methodNode) < maxInsns) {
        return clean(original);
    }

    final List<Block> blocks = getBlocks(original, methodNode);

    final String baseName = methodNode.name;
    final String[] exceptions = getExceptions(methodNode);

    final InsnList returnList = new InsnList();
    InsnList list = returnList;

    Block block = blocks.get(0);
    block.renameInsns(block.getVarMap());

    final String name = baseName + "__split__part__";
    final Type endType = Type.getReturnType(methodNode.desc);
    for (int i = 1; i < blocks.size(); ++i) {
        final Map<Integer, Integer> paramRenameMap = block.getVarMap();
        add(list, block.getInstructions(), original);
        block = blocks.get(i);
        block.renameInsns(paramRenameMap);
        final String methodDescriptor = block.getDescriptor();
        final String newMethodName = name + block.getBlockNumber();
        final MethodNode splitMethod = new MethodNode(Opcodes.ASM5, ACCESS, newMethodName, methodDescriptor,
                null, exceptions);
        additionalMethods.add(splitMethod);

        list.add(new VarInsnNode(Opcodes.ALOAD, 0));
        list.add(block.getPushParameters());
        list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, classNode.name, splitMethod.name, methodDescriptor));
        list.add(new InsnNode(endType.getOpcode(IRETURN)));
        sorter = new LocalVariablesSorter(splitMethod.access, splitMethod.desc,
                new EmptyMethodVisitor(Opcodes.ASM5));
        splitMethod.accept(sorter);
        list = splitMethod.instructions;
    }
    add(list, block.getInstructions(), original);
    return returnList;
}