Example usage for org.objectweb.asm Opcodes ACC_SYNTHETIC

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

Introduction

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

Prototype

int ACC_SYNTHETIC

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

Click Source Link

Usage

From source file:com.thomas15v.packetlib.codegenerator.asm.ASMHelper.java

License:MIT License

/**
 * Generate a forwarding method of the form
 * "T name() { return Class.forward(this); }".
 *
 * @param clazz Class to generate new method on
 * @param name Name of method to generate
 * @param forwardname Name of method to call
 * @param rettype Return type of method//from w  w  w .j  av a  2s.c o m
 * @param fowardtype Forward type
 * @param thistype Type to treat 'this' as for overload searching purposes
 */
public static void generateForwardingToStaticMethod(ClassNode clazz, String name, String forwardname,
        Type rettype, Type fowardtype, Type thistype) {
    MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name,
            "()" + rettype.getDescriptor(), null, null);

    ASMHelper.populateForwardingToStaticMethod(method, forwardname, rettype, thistype, fowardtype);

    clazz.methods.add(method);
}

From source file:com.thomas15v.packetlib.codegenerator.asm.ASMHelper.java

License:MIT License

/**
 * Generate a forwarding method of the form
 * "T name(S object) { return object.forward(); }".
 *
 * @param clazz Class to generate new method on
 * @param name Name of method to generate
 * @param forwardname Name of method to call
 * @param rettype Return type of method//from   w  w w  . j a  v a 2s .  c  o m
 * @param argtype Type of object to call method on
 */
public static void generateForwardingMethod(ClassNode clazz, String name, String forwardname, Type rettype,
        Type argtype) {
    MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name,
            "()" + rettype.getDescriptor(), null, null);

    ASMHelper.populateForwardingMethod(method, forwardname, rettype, argtype, Type.getObjectType(clazz.name));

    clazz.methods.add(method);
}

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  .  ja  v  a 2  s .co m
    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  a  va 2  s.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.unisb.cs.st.javalanche.mutation.bytecodeMutations.MutationsCollectorClassAdapter.java

License:Open Source License

public MethodVisitor visitMethod(int access, String name, String desc, String signature,
        final String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    if ((access & Opcodes.ACC_SYNTHETIC) > 0 || (access & Opcodes.ACC_BRIDGE) > 0 || name.equals("<clinit>")) {
        return mv;
    }/*  w  w w .  j a v  a2 s .c o m*/
    if (debug) {
        mv = new CheckMethodAdapter(mv);
    }
    JavalancheConfiguration configuration = ConfigurationLocator.getJavalancheConfiguration();
    if (configuration.enableMutationType(REPLACE_CONSTANT)) {
        mv = new PossibilitiesRicMethodAdapter(mv, className, name, mpc, ricPossibilities, desc);
    }
    if (configuration.enableMutationType(NEGATE_JUMP)) {
        mv = new NegateJumpsPossibilitiesMethodAdapter(mv, className, name, mpc, negatePossibilities, desc);
    }
    if (configuration.enableMutationType(ARITHMETIC_REPLACE)) {
        mv = new PossibilitiesArithmeticReplaceMethodAdapter(mv, className, name, mpc, arithmeticPossibilities,
                desc);
    }
    if (configuration.enableMutationType(REMOVE_CALL)) {
        mv = new RemoveCallsPossibilitiesMethodAdapter(new MyAdviceAdapter(mv, access, name, desc), className,
                name, mpc, removeCallsPossibilities, desc);
    }
    if (configuration.enableMutationType(REPLACE_VARIABLE)) {
        ReplaceVariablesPossibilitiesMethodAdapter rvAdapter = new ReplaceVariablesPossibilitiesMethodAdapter(
                mv, className, name, mpc, replaceVariablesPossibilities, desc,
                projectVariables.getStaticVariables(className), projectVariables.getClassVariables(className));
        mv = rvAdapter;
        AnalyzerAdapter analyzerAdapter = new AnalyzerAdapter(className, access, name, desc, mv);
        rvAdapter.setAnlyzeAdapter(analyzerAdapter);
        mv = analyzerAdapter;
    }
    if (configuration.enableMutationType(ABSOLUTE_VALUE)) {
        mv = new AbsoluteValuePossibilitiesAdapter(mv, className, name, absoluteValuePossibilities, desc, mpc);
    }
    if (configuration.enableMutationType(UNARY_OPERATOR)) {
        mv = new UnaryOperatorPossibilitiesAdapter(mv, className, name, unaryOperatorPossibilities, desc, mpc);
    }

    if (configuration.enableMutationType(MONITOR_REMOVE)) {
        mv = new MonitorRemovePossibilitiesMethodAdapter(mv, className, name, mpc, monitorPossibilities, desc);
    }
    if (configuration.enableMutationType(REPLACE_THREAD_CALL)) {
        mv = new ReplaceThreadCallsPossibilitiesMethodAdapter(new ReplaceAdviceAdapter(mv, access, name, desc),
                className, name, mpc, replaceThreadCallsPossibilities, desc);
    }
    return mv;
}

From source file:dodola.anole.lib.IncrementalSupportVisitor.java

License:Apache License

/**
 * Ensures that the class contains a $change field used for referencing the
 * IncrementalChange dispatcher.//  w  w w. j  a v a  2  s .co m
 * <p/>
 * Also updates package_private visiblity to public so we can call into this class from
 * outside the package.
 */
@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {
    visitedClassName = name;
    visitedSuperName = superName;

    super.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_VOLATILE | Opcodes.ACC_SYNTHETIC,
            "$change", getRuntimeTypeName(CHANGE_TYPE), null, null);
    access = transformClassAccessForInstantRun(access);
    super.visit(version, access, name, signature, superName, interfaces);
}

From source file:dodola.anole.lib.IncrementalSupportVisitor.java

License:Apache License

/***
 * Inserts a trampoline to this class so that the updated methods can make calls to super
 * class methods.//from  www.j  a  va 2s. com
 * <p/>
 * Pseudo code for this trampoline:
 * <code>
 * Object access$super($classType instance, String name, object[] args) {
 * switch(name) {
 * case "firstMethod.(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;":
 * return super~instance.firstMethod((String)arg[0], arg[1]);
 * case "secondMethod.(Ljava/lang/String;I)V":
 * return super~instance.firstMethod((String)arg[0], arg[1]);
 * <p>
 * default:
 * StringBuilder $local1 = new StringBuilder();
 * $local1.append("Method not found ");
 * $local1.append(name);
 * $local1.append(" in " $classType $super implementation");
 * throw new $package/InstantReloadException($local1.toString());
 * }
 * </code>
 */
private void createAccessSuper() {
    int access = Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_VARARGS;
    Method m = new Method("access$super",
            "(L" + visitedClassName + ";Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;");
    MethodVisitor visitor = super.visitMethod(access, m.getName(), m.getDescriptor(), null, null);

    final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor);

    // Gather all methods from itself and its superclasses to generate a giant access$super
    // implementation.
    // This will work fine as long as we don't support adding methods to a class.
    final Map<String, MethodReference> uniqueMethods = new HashMap<String, MethodReference>();
    if (parentNodes.isEmpty()) {
        // if we cannot determine the parents for this class, let's blindly add all the
        // method of the current class as a gateway to a possible parent version.
        addAllNewMethods(uniqueMethods, classNode);
    } else {
        // otherwise, use the parent list.
        for (ClassNode parentNode : parentNodes) {
            addAllNewMethods(uniqueMethods, parentNode);
        }
    }

    new StringSwitch() {
        @Override
        void visitString() {
            mv.visitVarInsn(Opcodes.ALOAD, 1);
        }

        @Override
        void visitCase(String methodName) {
            MethodReference methodRef = uniqueMethods.get(methodName);

            mv.visitVarInsn(Opcodes.ALOAD, 0);

            Type[] args = Type.getArgumentTypes(methodRef.method.desc);
            int argc = 0;
            for (Type t : args) {
                mv.visitVarInsn(Opcodes.ALOAD, 2);
                mv.push(argc);
                mv.visitInsn(Opcodes.AALOAD);
                ByteCodeUtils.unbox(mv, t);
                argc++;
            }

            if (TRACING_ENABLED) {
                trace(mv, "super selected ", methodRef.owner.name, methodRef.method.name,
                        methodRef.method.desc);
            }
            // Call super on the other object, yup this works cos we are on the right place to
            // call from.
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, methodRef.owner.name, methodRef.method.name,
                    methodRef.method.desc, false);

            Type ret = Type.getReturnType(methodRef.method.desc);
            if (ret.getSort() == Type.VOID) {
                mv.visitInsn(Opcodes.ACONST_NULL);
            } else {
                mv.box(ret);
            }
            mv.visitInsn(Opcodes.ARETURN);
        }

        @Override
        void visitDefault() {
            writeMissingMessageWithHash(mv, visitedClassName);
        }
    }.visit(mv, uniqueMethods.keySet());

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:dodola.anole.lib.IncrementalSupportVisitor.java

License:Apache License

/***
 * Inserts a trampoline to this class so that the updated methods can make calls to
 * constructors.//from  w ww  .j a v a 2  s  .  c  o  m
 * <p>
 * <p/>
 * Pseudo code for this trampoline:
 * <code>
 * ClassName(Object[] args, Marker unused) {
 * String name = (String) args[0];
 * if (name.equals(
 * "java/lang/ClassName.(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;")) {
 * this((String)arg[1], arg[2]);
 * return
 * }
 * if (name.equals("SuperClassName.(Ljava/lang/String;I)V")) {
 * super((String)arg[1], (int)arg[2]);
 * return;
 * }
 * ...
 * StringBuilder $local1 = new StringBuilder();
 * $local1.append("Method not found ");
 * $local1.append(name);
 * $local1.append(" in " $classType $super implementation");
 * throw new $package/InstantReloadException($local1.toString());
 * }
 * </code>
 */
private void createDispatchingThis() {
    // Gather all methods from itself and its superclasses to generate a giant constructor
    // implementation.
    // This will work fine as long as we don't support adding constructors to classes.
    final Map<String, MethodNode> uniqueMethods = new HashMap<String, MethodNode>();

    addAllNewConstructors(uniqueMethods, classNode, true /*keepPrivateConstructors*/);
    for (ClassNode parentNode : parentNodes) {
        addAllNewConstructors(uniqueMethods, parentNode, false /*keepPrivateConstructors*/);
    }

    int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC;

    Method m = new Method(AsmUtils.CONSTRUCTOR, ConstructorArgsRedirection.DISPATCHING_THIS_SIGNATURE);
    MethodVisitor visitor = super.visitMethod(0, m.getName(), m.getDescriptor(), null, null);
    final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor);

    mv.visitCode();
    // Mark this code as redirection code
    Label label = new Label();
    mv.visitLineNumber(0, label);

    // Get and store the constructor canonical name.
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.push(0);
    mv.visitInsn(Opcodes.AALOAD);
    mv.unbox(Type.getType("Ljava/lang/String;"));
    final int constructorCanonicalName = mv.newLocal(Type.getType("Ljava/lang/String;"));
    mv.storeLocal(constructorCanonicalName);

    new StringSwitch() {

        @Override
        void visitString() {
            mv.loadLocal(constructorCanonicalName);
        }

        @Override
        void visitCase(String canonicalName) {
            MethodNode methodNode = uniqueMethods.get(canonicalName);
            String owner = canonicalName.split("\\.")[0];

            // Parse method arguments and
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            Type[] args = Type.getArgumentTypes(methodNode.desc);
            int argc = 0;
            for (Type t : args) {
                mv.visitVarInsn(Opcodes.ALOAD, 1);
                mv.push(argc + 1);
                mv.visitInsn(Opcodes.AALOAD);
                ByteCodeUtils.unbox(mv, t);
                argc++;
            }

            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, owner, AsmUtils.CONSTRUCTOR, methodNode.desc, false);

            mv.visitInsn(Opcodes.RETURN);
        }

        @Override
        void visitDefault() {
            writeMissingMessageWithHash(mv, visitedClassName);
        }
    }.visit(mv, uniqueMethods.keySet());

    mv.visitMaxs(1, 3);
    mv.visitEnd();
}

From source file:erjang.ETuple.java

License:Apache License

private static void make_blank_bridge(ClassAdapter cw, String this_class_name, String super_class_name) {
    MethodVisitor mv;/*from  w  ww .  j  ava2s. c o  m*/
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE, "blank",
            "()L" + super_class_name + ";", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this_class_name, "blank", "()L" + this_class_name + ";");
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:fi.luontola.buildtest.AsmUtils.java

License:Open Source License

public static boolean isSynthetic(ClassNode cn) {
    return hasFlag(cn.access, Opcodes.ACC_SYNTHETIC);
}