Example usage for org.objectweb.asm Opcodes ACC_PUBLIC

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

Introduction

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

Prototype

int ACC_PUBLIC

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

Click Source Link

Usage

From source file:groovy.transform.builder.SimpleStrategy.java

License:Apache License

public void build(BuilderASTTransformation transform, AnnotatedNode annotatedNode, AnnotationNode anno) {
    if (!(annotatedNode instanceof ClassNode)) {
        transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME
                + " processing: building for " + annotatedNode.getClass().getSimpleName() + " not supported by "
                + getClass().getSimpleName(), annotatedNode);
        return;/*from w  w  w .  j  a v  a 2 s . c  o  m*/
    }
    ClassNode buildee = (ClassNode) annotatedNode;
    if (unsupportedAttribute(transform, anno, "builderClassName"))
        return;
    if (unsupportedAttribute(transform, anno, "buildMethodName"))
        return;
    if (unsupportedAttribute(transform, anno, "builderMethodName"))
        return;
    if (unsupportedAttribute(transform, anno, "forClass"))
        return;
    if (unsupportedAttribute(transform, anno, "includeSuperProperties"))
        return;
    if (unsupportedAttribute(transform, anno, "allProperties"))
        return;
    if (unsupportedAttribute(transform, anno, "force"))
        return;
    boolean useSetters = transform.memberHasValue(anno, "useSetters", true);
    boolean allNames = transform.memberHasValue(anno, "allNames", true);

    List<String> excludes = new ArrayList<String>();
    List<String> includes = new ArrayList<String>();
    includes.add(Undefined.STRING);
    if (!getIncludeExclude(transform, anno, buildee, excludes, includes))
        return;
    if (includes.size() == 1 && Undefined.isUndefined(includes.get(0)))
        includes = null;
    String prefix = getMemberStringValue(anno, "prefix", "set");
    List<FieldNode> fields = getFields(transform, anno, buildee);
    if (includes != null) {
        for (String name : includes) {
            checkKnownField(transform, anno, name, fields);
        }
    }
    for (FieldNode field : fields) {
        String fieldName = field.getName();
        if (!AbstractASTTransformation.shouldSkipUndefinedAware(fieldName, excludes, includes, allNames)) {
            String methodName = getSetterName(prefix, fieldName);
            Parameter parameter = param(field.getType(), fieldName);
            addGeneratedMethod(buildee, methodName, Opcodes.ACC_PUBLIC, newClass(buildee), params(parameter),
                    NO_EXCEPTIONS,
                    block(stmt(useSetters && !field.isFinal()
                            ? callThisX(getSetterName("set", fieldName), varX(parameter))
                            : assignX(fieldX(field), varX(parameter))), returnS(varX("this"))));
        }
    }
}

From source file:io.awacs.plugin.stacktrace.ClassTransformer.java

License:Apache License

public void visit(ClassNode cn) {
    cn.check(Opcodes.ASM5);/* www.j a  va2s.c  o m*/
    //??
    List<MethodNode> appended = new ArrayList<>(cn.methods.size());
    //??
    for (Object mn : cn.methods) {
        MethodNode src = (MethodNode) mn;
        //
        if (!filterMethod(cn, src)) {
            continue;
        }
        boolean terminated = isTerminatedMethod(src);

        if (terminated) {
            //copy exceptions
            String[] exceptions = null;
            if (src.exceptions != null) {
                exceptions = new String[src.exceptions.size()];
                for (int i = 0; i < src.exceptions.size(); i++) {
                    exceptions[i] = src.exceptions.get(i).toString();
                }
            }
            //declare method
            MethodNode proxy = new MethodNode(src.access, src.name, src.desc, src.signature, exceptions);
            appended.add(proxy);
            //copy method annotations
            List<AnnotationNode> methodAnns = null;
            if (src.visibleAnnotations != null) {
                methodAnns = new ArrayList<>(src.visibleAnnotations.size());
                methodAnns.addAll(src.visibleAnnotations);
            }
            proxy.visibleAnnotations = methodAnns;
            //copy parameter annotations
            List[] parameterAnns = null;
            if (src.visibleParameterAnnotations != null) {
                parameterAnns = new List[src.visibleParameterAnnotations.length];
                System.arraycopy(src.visibleParameterAnnotations, 0, parameterAnns, 0,
                        src.visibleParameterAnnotations.length);
            }
            proxy.visibleParameterAnnotations = parameterAnns;
            //clear origin method's annotation and change name
            int _slash = cn.name.lastIndexOf('/');
            //??
            src.name = src.name + "_origin_" + cn.name.substring(_slash + 1);
            src.access = src.access & ~Opcodes.ACC_PUBLIC | Opcodes.ACC_PRIVATE;
            src.visibleAnnotations = null;
            src.visibleLocalVariableAnnotations = null;
            transformTerminatedMethod(src, proxy, cn);
        } else {
            transformPlainMethod(src, cn);
        }
    }
    cn.methods.addAll(appended);
}

From source file:io.syncframework.optimizer.OControllerClassVisitor.java

License:Apache License

/**
 * Generates _asActions() method as following:
 * //from  w w  w  . j  av a  2 s  .c  om
 * if(name.equals("upload"))
 *    return upload();
 * else if(name.equals("save"))
 *    return save();
 * else if(name.equals("main"))
 *    return main();
 * else if(name.equals("redir"))
 *    return redir();
 * else
 *    throw new RuntimeException("no action named "+name);
 */
private void createActionMethod() {
    StringBuilder sb = new StringBuilder();
    sb.append("(").append(Type.getType(String.class)).append(")").append(Type.getType(Result.class));
    String desc = sb.toString();

    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asAction", desc, null, null);

    Label start = new Label();
    Label next = new Label();
    Label variable = new Label();
    boolean first = true;

    for (String name : reflector.getActions().keySet()) {
        Label l0 = null;
        Label l1 = new Label();

        if (first) {
            l0 = new Label();
            first = false;
        } else {
            l0 = next;
            next = new Label();
        }

        mv.visitLabel(l0);
        if (!first) {
            mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        }
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
        mv.visitJumpInsn(Opcodes.IFEQ, next);

        mv.visitLabel(l1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, reflector.getClazzInternalName(), name,
                "()" + Type.getType(Result.class), false);
        mv.visitInsn(Opcodes.ARETURN);
    }

    mv.visitLabel(next);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/NoSuchMethodException");
    mv.visitInsn(Opcodes.DUP);
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("no @Action named ");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V",
            false);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;",
            false);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/NoSuchMethodException", "<init>",
            "(Ljava/lang/String;)V", false);
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(variable);
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, start, next, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, start, next, 1);

    mv.visitMaxs(5, 2);
    mv.visitEnd();
}

From source file:io.syncframework.optimizer.OControllerClassVisitor.java

License:Apache License

/**
 * Generates code:/*from w w  w .  j a va 2 s.  c o  m*/
 * 
 * public Class<?>[] _asActionInterceptors(String name) {
 *    return _asInterceptors.get(name);
 * }
 */
public void createActionInterceptorsMethod() {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asActionInterceptors",
            "(Ljava/lang/String;)[Ljava/lang/Class;", null, null);

    Label l0 = new Label();
    Label l1 = new Label();
    mv.visitLabel(l0);
    mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asInterceptors",
            "Ljava/util/Map;");
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get",
            "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Class;");
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitLabel(l1);
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, l0, l1, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, l0, l1, 1);
    mv.visitMaxs(2, 2);

    mv.visitEnd();
}

From source file:io.syncframework.optimizer.OControllerClassVisitor.java

License:Apache License

/**
 * Generates this code://from   w  w w  . j a va  2 s  .co  m
 * 
 * public boolean _asActionIsDefined(String name) {
 *    if(_asActions.containsKey(name))
 *       return true;
 *    return false;
 * }
 */
public void createActionIsDefinedMethod() {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asActionIsDefined", "(Ljava/lang/String;)Z", null,
            null);
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label l3 = new Label();

    mv.visitLabel(l0);
    mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asActions", "Ljava/util/Map;");
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "containsKey", "(Ljava/lang/Object;)Z", true);
    mv.visitJumpInsn(Opcodes.IFEQ, l1);

    mv.visitLabel(l2);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitLabel(l1);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitLabel(l3);
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, l0, l3, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, l0, l3, 1);
    mv.visitMaxs(2, 2);

    mv.visitEnd();
}

From source file:io.syncframework.optimizer.OControllerClassVisitor.java

License:Apache License

/**
 * Generates context method setter: //  w ww.  j  av  a  2  s .co m
 * 
 * public void _as(Application|Error|Message|...)Context((Application|Error|Message|...)Context variable) {
 *    this.variable = variable
 *       or
 *    // do nothing
 * }
 */
private void createContextMethod(String methodName, String typeDescriptor, String variableName) {
    if (variableName != null) {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, methodName, "(" + typeDescriptor + ")V", null,
                null);
        Label l0 = new Label();
        Label l1 = new Label();
        Label l2 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, reflector.getClazzInternalName(), variableName, typeDescriptor);
        mv.visitLabel(l1);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, l0, l2, 0);
        mv.visitLocalVariable(variableName, typeDescriptor, null, l0, l2, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    } else {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, methodName, "(" + typeDescriptor + ")V", null,
                null);
        Label l0 = new Label();
        Label l1 = new Label();
        mv.visitLabel(l0);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, l0, l1, 0);
        mv.visitLocalVariable("argument", typeDescriptor, null, l0, l1, 1);
    }
}

From source file:io.syncframework.optimizer.OControllerClassVisitor.java

License:Apache License

/**
 * Generates the code://from  w  w w.  ja  va 2s.  c  om
 * public Map<String, Class<?>> _asParameters() {
 *    return _asParameters;
 * }
 */
private void createParametersMethod() {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asParameters", "()Ljava/util/Map;",
            "()Ljava/util/Map<Ljava/lang/String;Ljava/lang/Class<*>;>;", null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asParameters", "Ljava/util/Map;");
    mv.visitInsn(Opcodes.ARETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:io.syncframework.optimizer.OControllerClassVisitor.java

License:Apache License

/**
 * Generates the _asParameter() getter as
 * /*from  ww w . j  a v  a2 s . co m*/
 * public Object _asParameter(String name) {
 *    if(name.equals("name"))
 *       return getName();
 *    if(name.equals("date"))
 *       return getDate();
 *    ...
 *    return null;
 * }
 */
private void createParametersGetterMethod() {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asParameter",
            "(Ljava/lang/String;)Ljava/lang/Object;", null, null);

    Label start = new Label();
    Label next = new Label();
    Label variable = new Label();
    boolean first = true;

    for (String name : reflector.getParameters().keySet()) {
        Label l0 = null;
        Label l1 = new Label();

        if (first) {
            l0 = new Label();
            first = false;
        } else {
            l0 = next;
            next = new Label();
        }

        mv.visitLabel(l0);
        if (!first)
            mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
        mv.visitJumpInsn(Opcodes.IFEQ, next);

        String methodGetterName = reflector.getGetters().get(name).getName();
        String methodGetterDesc = "()" + Type.getDescriptor(reflector.getParameters().get(name));

        mv.visitLabel(l1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, reflector.getClazzInternalName(), methodGetterName,
                methodGetterDesc, false);
        mv.visitInsn(Opcodes.ARETURN);
    }

    mv.visitLabel(next);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitLabel(variable);
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, start, variable, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, start, variable, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:io.syncframework.optimizer.OControllerClassVisitor.java

License:Apache License

/**
 * Creates the code as://  w  w  w  .j a  v  a  2s.  c  o m
 * 
 * public void _asParameter(String name, Object value) {
 *    if(name.equals("name") {
 *       setName((String)value);
 *       return;
 *    }
 *   if(name.equals("date") {
 *      setDate((Date)value);
 *      return;
 *   }
 *   ...
 *    return;
 * } 
 */
private void createParametersSetterMethod() {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asParameter",
            "(Ljava/lang/String;Ljava/lang/Object;)V", null, null);

    Label start = new Label();
    Label next = new Label();
    Label variable = new Label();
    boolean first = true;

    for (String name : reflector.getParameters().keySet()) {
        Label l0 = null;
        Label l1 = new Label();
        Label l2 = new Label();

        if (first) {
            l0 = new Label();
            first = false;
        } else {
            l0 = next;
            next = new Label();
        }

        mv.visitLabel(l0);
        if (!first)
            mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
        mv.visitJumpInsn(Opcodes.IFEQ, next);

        Class<?> parameterType = reflector.getParameters().get(name);

        String setterMethodName = reflector.getSetters().get(name).getName();
        String setterMethodDesc = "(" + Type.getDescriptor(parameterType) + ")V";

        mv.visitLabel(l1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(parameterType));
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, reflector.getClazzInternalName(), setterMethodName,
                setterMethodDesc, false);

        mv.visitLabel(l2);
        mv.visitInsn(Opcodes.RETURN);
    }

    mv.visitLabel(next);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitInsn(Opcodes.RETURN);

    mv.visitLabel(variable);
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, start, next, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, start, next, 1);
    mv.visitLocalVariable("value", "Ljava/lang/Object;", null, start, next, 2);
    mv.visitMaxs(2, 3);
    mv.visitEnd();
}

From source file:io.syncframework.optimizer.OControllerClassVisitor.java

License:Apache License

/**
 * public Class<?> _asParameterConverter(String name) {
 *    return _asConverters.get(name);/*  w  w w  .ja  v  a 2s  . c o m*/
 * }
 */
public void createParameterConverterMethod() {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asParameterConverter",
            "(Ljava/lang/String;)Ljava/lang/Class;", null, null);
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asConverters", "Ljava/util/Map;");
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get",
            "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Class");
    mv.visitInsn(Opcodes.ARETURN);

    Label l1 = new Label();
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, l0, l1, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, l0, l1, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}