Search in sources :

Example 66 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project LanternServer by LanternPowered.

the class SubtypeFilterDelegate method createFields.

public void createFields(ClassWriter cw) {
    FieldVisitor fv = cw.visitField(0, "classes", "Ljava/util/Set;", "Ljava/util/Set<Ljava/lang/Class<*>;>;", null);
    fv.visitEnd();
}
Also used : FieldVisitor(org.objectweb.asm.FieldVisitor)

Example 67 with FieldVisitor

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

the class SystemClassReflectionGenerator method generateJLRM_Invoke.

public static void generateJLRM_Invoke(ClassWriter cw, String classname) {
    FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, jlrmInvokeMember, "Ljava/lang/reflect/Method;", null, null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, jlrmInvokeMember, jlrmInvokeDescriptor, null, new String[] { "java/lang/IllegalAccessException", "java/lang/reflect/InvocationTargetException" });
    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, jlrmInvokeMember, "Ljava/lang/reflect/Method;");
    mv.visitJumpInsn(IFNONNULL, l0);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l0);
    mv.visitFieldInsn(GETSTATIC, classname, jlrmInvokeMember, "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);
    // target method
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_1);
    // instance on which to call the method
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_2);
    // arguments to method call
    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.visitLabel(l1);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l2);
    mv.visitVarInsn(ASTORE, 3);
    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)

Example 68 with FieldVisitor

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

the class SystemClassReflectionGenerator method generateJLCGDFS.

public static void generateJLCGDFS(ClassWriter cw, String classname) {
    FieldVisitor fv = cw.visitField(ACC_PUBLIC_STATIC, "__sljlcgdfs", "Ljava/lang/reflect/Method;", null, null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, "__sljlcgdfs", "(Ljava/lang/Class;)[Ljava/lang/reflect/Field;", null, null);
    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, "__sljlcgdfs", "Ljava/lang/reflect/Method;");
    mv.visitJumpInsn(IFNONNULL, l0);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredFields", "()[Ljava/lang/reflect/Field;", false);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l0);
    mv.visitFieldInsn(GETSTATIC, classname, "__sljlcgdfs", "Ljava/lang/reflect/Method;");
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ICONST_1);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(AASTORE);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitTypeInsn(CHECKCAST, "[Ljava/lang/reflect/Field;");
    mv.visitLabel(l1);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l2);
    mv.visitVarInsn(ASTORE, 1);
    Label l5 = new Label();
    mv.visitLabel(l5);
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    Label l6 = new Label();
    mv.visitLabel(l6);
    // mv.visitLocalVariable("clazz", "Ljava/lang/Class;", "Ljava/lang/Class<*>;", l3, l6, 0);
    // mv.visitLocalVariable("e", "Ljava/lang/Exception;", null, l5, l6, 1);
    mv.visitMaxs(6, 2);
    mv.visitEnd();
}
Also used : Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 69 with FieldVisitor

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

the class SystemClassReflectionGenerator method generateJLC_GetDeclaredConstructors.

public static void generateJLC_GetDeclaredConstructors(ClassWriter cw, String classname) {
    FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, jlcGetDeclaredConstructorsMember, "Ljava/lang/reflect/Method;", null, null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, jlcGetDeclaredConstructorsMember, jlcGetDeclaredConstructorsDescriptor, null, null);
    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, jlcGetDeclaredConstructorsMember, "Ljava/lang/reflect/Method;");
    mv.visitJumpInsn(IFNONNULL, l0);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredConstructors", "()[Ljava/lang/reflect/Constructor;", false);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l0);
    mv.visitFieldInsn(GETSTATIC, classname, jlcGetDeclaredConstructorsMember, "Ljava/lang/reflect/Method;");
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ICONST_1);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(AASTORE);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitTypeInsn(CHECKCAST, "[Ljava/lang/reflect/Constructor;");
    mv.visitLabel(l1);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l2);
    mv.visitVarInsn(ASTORE, 1);
    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.visitLocalVariable("clazz", "Ljava/lang/Class;", "Ljava/lang/Class<*>;", l3, l7, 0);
    // mv.visitLocalVariable("e", "Ljava/lang/Exception;", null, l5, l7, 1);
    mv.visitMaxs(6, 2);
    mv.visitEnd();
}
Also used : Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 70 with FieldVisitor

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

the class SystemClassReflectionGenerator method generateJLObjectStream_hasStaticInitializer.

/**
 * Create a method that can be used to intercept the calls to hasStaticInitializer made in the ObjectStreamClass.
 * The method will ask SpringLoaded whether the type has a static initializer. SpringLoaded will be able to answer
 * if it is a reloadable type. If it is not a reloadable type then springloaded will throw an exception which will
 * be caught here and the 'regular' call to hasStaticInitializer will be made.
 *
 * @param cw the classwriter to create the method in
 * @param classname the name of the class being visited
 */
public static void generateJLObjectStream_hasStaticInitializer(ClassWriter cw, String classname) {
    FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, jloObjectStream_hasInitializerMethod, "Ljava/lang/reflect/Method;", null, null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, jloObjectStream_hasInitializerMethod, "(Ljava/lang/Class;)Z", null, null);
    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    mv.visitLabel(l0);
    mv.visitFieldInsn(GETSTATIC, classname, jloObjectStream_hasInitializerMethod, "Ljava/lang/reflect/Method;");
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ICONST_1);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ALOAD, 0);
    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/Boolean");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false);
    mv.visitLabel(l1);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l2);
    // If not a reloadable type, we'll end up here (the method we called threw IllegalStateException), just make that native method call
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, classname, "hasStaticInitializer", "(Ljava/lang/Class;)Z");
    mv.visitInsn(IRETURN);
    mv.visitMaxs(3, 1);
    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