Search in sources :

Example 31 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project atlas by alibaba.

the class AsmTest method test.

@Test
public void test() throws Throwable {
    ClassReader cr = new ClassReader(Config.class.getName());
    ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
    ClassVisitor cv = new MethodChangeClassAdapter(cw);
    cr.accept(cv, Opcodes.ASM5);
    // Add a new method
    MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "add", "([Ljava/lang/String;)V", null, null);
    // pushes the 'out' field (of type PrintStream) of the System class
    mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    // pushes the "Hello World!" String constant
    mw.visitLdcInsn("this is add method print!");
    // invokes the 'println' method (defined in the PrintStream class)
    mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
    mw.visitInsn(RETURN);
    // this code uses a maximum of two stack elements and two local
    // variables
    mw.visitMaxs(0, 0);
    mw.visitEnd();
    // Type.getDescriptor(AdviceFlowOuterHolder.class)
    FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "age", Type.INT_TYPE.toString(), null, 1);
    fv.visitEnd();
    FieldVisitor fv2 = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "name2", Type.getDescriptor(String.class), null, "name2");
    fv2.visitEnd();
    ModifyClassVisiter cv2 = new ModifyClassVisiter(Opcodes.ASM5);
    cv2.addRemoveField("name");
    cr.accept(cv2, Opcodes.ASM5);
    FieldVisitor fv3 = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "name", Type.getDescriptor(String.class), null, "name");
    fv3.visitEnd();
    byte[] code = cw.toByteArray();
    File file = new File("Config.class");
    System.out.println(file.getAbsolutePath());
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(code);
    fos.close();
}
Also used : ModifyClassVisiter(com.taobao.android.builder.tools.asm.field.ModifyClassVisiter) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) MethodChangeClassAdapter(com.taobao.asm.AsmExample.MethodChangeClassAdapter) ClassVisitor(org.objectweb.asm.ClassVisitor) FieldVisitor(org.objectweb.asm.FieldVisitor) File(java.io.File) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.Test)

Example 32 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project groovy by apache.

the class AsmClassGenerator method visitField.

@Override
public void visitField(final FieldNode fieldNode) {
    onLineNumber(fieldNode, "visitField: " + fieldNode.getName());
    ClassNode t = fieldNode.getType();
    String signature = BytecodeHelper.getGenericsBounds(t);
    Expression initialValueExpression = fieldNode.getInitialValueExpression();
    ConstantExpression cexp = initialValueExpression instanceof ConstantExpression ? (ConstantExpression) initialValueExpression : null;
    if (cexp != null) {
        cexp = Verifier.transformToPrimitiveConstantIfPossible(cexp);
    }
    Object value = cexp != null && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(t) && fieldNode.isStatic() && fieldNode.isFinal() ? cexp.getValue() : // GROOVY-5150
    null;
    if (value != null) {
        // byte, char and short require an extra cast
        if (isPrimitiveByte(t) || isPrimitiveShort(t)) {
            value = ((Number) value).intValue();
        } else if (isPrimitiveChar(t)) {
            value = Integer.valueOf((Character) value);
        }
    }
    FieldVisitor fv = classVisitor.visitField(fieldNode.getModifiers(), fieldNode.getName(), BytecodeHelper.getTypeDescription(t), signature, value);
    visitAnnotations(fieldNode, fv);
    visitType(fieldNode.getType(), fv, newTypeReference(FIELD), "", true);
    fv.visitEnd();
}
Also used : InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodReferenceExpression(org.codehaus.groovy.ast.expr.MethodReferenceExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) Expression(org.codehaus.groovy.ast.expr.Expression) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MethodPointerExpression(org.codehaus.groovy.ast.expr.MethodPointerExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) LambdaExpression(org.codehaus.groovy.ast.expr.LambdaExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) FieldVisitor(org.objectweb.asm.FieldVisitor)

Example 33 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project aries by apache.

the class AbstractWovenProxyAdapter method generateField.

/**
 * Generate an instance field that should be "invisible" to normal code
 *
 * @param fieldName
 * @param fieldDescriptor
 */
private final void generateField(String fieldName, String fieldDescriptor) {
    FieldVisitor fv = cv.visitField(ACC_PROTECTED | ACC_TRANSIENT | ACC_SYNTHETIC | ACC_FINAL, fieldName, fieldDescriptor, null, null);
    for (String s : annotationTypeDescriptors) fv.visitAnnotation(s, true).visitEnd();
    fv.visitEnd();
}
Also used : FieldVisitor(org.objectweb.asm.FieldVisitor)

Example 34 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project spring-loaded by spring-projects.

the class SystemClassReflectionGenerator method generateJLCMethod.

public static void generateJLCMethod(ClassWriter cw, String classname, String membername, String methodname) {
    FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, membername, "Ljava/lang/reflect/Method;", null, null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC + ACC_VARARGS, membername, "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", null, new String[] { "java/lang/NoSuchMethodException" });
    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/reflect/InvocationTargetException");
    Label l3 = new Label();
    mv.visitTryCatchBlock(l0, l1, l3, "java/lang/Exception");
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitFieldInsn(GETSTATIC, classname, membername, "Ljava/lang/reflect/Method;");
    mv.visitJumpInsn(IFNONNULL, l0);
    Label l5 = new Label();
    mv.visitLabel(l5);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", methodname, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l0);
    mv.visitFieldInsn(GETSTATIC, classname, membername, "Ljava/lang/reflect/Method;");
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ICONST_3);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_2);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitInsn(AASTORE);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitTypeInsn(CHECKCAST, "java/lang/reflect/Method");
    mv.visitLabel(l1);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l2);
    mv.visitVarInsn(ASTORE, 3);
    Label l6 = new Label();
    mv.visitLabel(l6);
    // Don't print the exception if just unwrapping it
    // mv.visitVarInsn(ALOAD, 3);
    // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "printStackTrace", "()V");
    Label l7 = new Label();
    mv.visitLabel(l7);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
    mv.visitTypeInsn(INSTANCEOF, "java/lang/NoSuchMethodException");
    Label l8 = new Label();
    mv.visitJumpInsn(IFEQ, l8);
    Label l9 = new Label();
    mv.visitLabel(l9);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
    mv.visitTypeInsn(CHECKCAST, "java/lang/NoSuchMethodException");
    mv.visitInsn(ATHROW);
    mv.visitLabel(l3);
    mv.visitVarInsn(ASTORE, 3);
    Label l10 = new Label();
    mv.visitLabel(l10);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "()V", false);
    mv.visitLabel(l8);
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    Label l11 = new Label();
    mv.visitLabel(l11);
    // mv.visitLocalVariable("clazz", "Ljava/lang/Class;", "Ljava/lang/Class<*>;", l4, l11, 0);
    // mv.visitLocalVariable("methodname", "Ljava/lang/String;", null, l4, l11, 1);
    // mv.visitLocalVariable("parameterTypes", "[Ljava/lang/Class;", null, l4, l11, 2);
    // mv.visitLocalVariable("ite", "Ljava/lang/reflect/InvocationTargetException;", null, l6, l3, 3);
    // mv.visitLocalVariable("e", "Ljava/lang/Exception;", null, l10, l8, 3);
    mv.visitMaxs(6, 4);
    mv.visitEnd();
}
Also used : Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 35 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project spring-loaded by spring-projects.

the class SystemClassReflectionGenerator method generateJLRF_Get.

public static void generateJLRF_Get(ClassWriter cw, String classname) {
    FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, jlrfGetMember, "Ljava/lang/reflect/Method;", null, null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, jlrfGetMember, jlrfGetDescriptor, null, new String[] { "java/lang/IllegalAccessException", "java/lang/IllegalArgumentException" });
    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitFieldInsn(GETSTATIC, classname, jlrfGetMember, "Ljava/lang/reflect/Method;");
    mv.visitJumpInsn(IFNONNULL, l0);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Field", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l0);
    mv.visitFieldInsn(GETSTATIC, classname, jlrfGetMember, "Ljava/lang/reflect/Method;");
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ICONST_2);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_0);
    // target field
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_1);
    // instance on which to get the field
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(AASTORE);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitLabel(l1);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l2);
    mv.visitVarInsn(ASTORE, 2);
    Label l5 = new Label();
    mv.visitLabel(l5);
    Label l6 = new Label();
    mv.visitLabel(l6);
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    Label l7 = new Label();
    mv.visitLabel(l7);
    mv.visitMaxs(8, 4);
    mv.visitEnd();
}
Also used : Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

FieldVisitor (org.objectweb.asm.FieldVisitor)90 MethodVisitor (org.objectweb.asm.MethodVisitor)59 Label (org.objectweb.asm.Label)33 ClassWriter (org.objectweb.asm.ClassWriter)28 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)9 ClassReader (org.objectweb.asm.ClassReader)8 Type (org.objectweb.asm.Type)7 Method (java.lang.reflect.Method)6 ClassVisitor (org.objectweb.asm.ClassVisitor)6 IOException (java.io.IOException)4 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 LitString (lucee.transformer.expression.literal.LitString)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 PrintWriter (java.io.PrintWriter)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URL (java.net.URL)2