Search in sources :

Example 11 with FieldVisitor

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

the class AbstractDataTypeGenerator method generateClasses.

@Override
<M extends DataManipulator<M, I>, I extends ImmutableDataManipulator<I, M>> void generateClasses(ClassWriter cwM, ClassWriter cwI, String mutableClassName, String immutableClassName, Class<M> manipulatorType, Class<I> immutableManipulatorType, @Nullable Class<? extends M> mutableExpansion, @Nullable Class<? extends I> immutableExpansion, @Nullable List<Method> mutableMethods, @Nullable List<Method> immutableMethods) {
    FieldVisitor fv;
    MethodVisitor mv;
    final String dManipulatorType = Type.getDescriptor(manipulatorType);
    final String dImmutableManipulatorType = Type.getDescriptor(immutableManipulatorType);
    final String dMutableExpansion = mutableExpansion == null ? null : Type.getDescriptor(mutableExpansion);
    final String dImmutableExpansion = immutableExpansion == null ? null : Type.getDescriptor(immutableExpansion);
    final String nManipulatorType = Type.getInternalName(manipulatorType);
    final String nImmutableManipulatorType = Type.getInternalName(immutableManipulatorType);
    final String nMutableExpansion = mutableExpansion == null ? null : Type.getInternalName(mutableExpansion);
    final String nImmutableExpansion = immutableExpansion == null ? null : Type.getInternalName(immutableExpansion);
    // Mutable class
    {
        final String[] interfaces = new String[mutableExpansion != null ? 2 : 1];
        final StringBuilder signBuilder = new StringBuilder();
        interfaces[0] = nManipulatorType;
        signBuilder.append(dManipulatorType);
        if (mutableExpansion != null) {
            interfaces[1] = nMutableExpansion;
            signBuilder.append(dMutableExpansion);
        }
        cwM.visit(V1_8, ACC_PUBLIC + ACC_SUPER, mutableClassName, format("L%s<%s%s>;", nAbstractData, dManipulatorType, dImmutableManipulatorType) + signBuilder.toString(), nAbstractData, interfaces);
        {
            fv = cwM.visitField(ACC_PUBLIC + ACC_STATIC, REGISTRATION_CONSUMER, "Ljava/util/function/Consumer;", format("Ljava/util/function/Consumer<%s>;", dValueCollection), null);
            fv.visitEnd();
        }
        {
            mv = cwM.visitMethod(ACC_PROTECTED, "<init>", "()V", null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitLdcInsn(Type.getType(manipulatorType));
            mv.visitLdcInsn(Type.getType(immutableManipulatorType));
            mv.visitMethodInsn(INVOKESPECIAL, nAbstractData, "<init>", "(Ljava/lang/Class;Ljava/lang/Class;)V", false);
            mv.visitInsn(RETURN);
            mv.visitMaxs(3, 1);
            mv.visitEnd();
        }
        {
            mv = cwM.visitMethod(ACC_PROTECTED, "<init>", format("(%s)V", dImmutableManipulatorType), null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitMethodInsn(INVOKESPECIAL, nAbstractData, "<init>", format("(%s)V", dImmutableDataManipulator), false);
            mv.visitInsn(RETURN);
            mv.visitMaxs(2, 2);
            mv.visitEnd();
        }
        {
            mv = cwM.visitMethod(ACC_PROTECTED, "<init>", format("(%s)V", dManipulatorType), null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitMethodInsn(INVOKESPECIAL, nAbstractData, "<init>", format("(%s)V", dDataManipulator), false);
            mv.visitInsn(RETURN);
            mv.visitMaxs(2, 2);
            mv.visitEnd();
        }
        {
            mv = cwM.visitMethod(ACC_PROTECTED, "<init>", format("(L%s;)V", nIDataManipulatorBase), format("(L%s<%s%s>;)V", nIDataManipulatorBase, dManipulatorType, dImmutableManipulatorType), null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitMethodInsn(INVOKESPECIAL, nAbstractData, "<init>", format("(L%s;)V", nIDataManipulatorBase), false);
            mv.visitInsn(RETURN);
            mv.visitMaxs(2, 2);
            mv.visitEnd();
        }
        {
            mv = cwM.visitMethod(ACC_PUBLIC, "registerKeys", "()V", null, null);
            mv.visitCode();
            mv.visitFieldInsn(GETSTATIC, mutableClassName, REGISTRATION_CONSUMER, "Ljava/util/function/Consumer;");
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKEVIRTUAL, mutableClassName, "getValueCollection", format("()%s", dValueCollection), false);
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/function/Consumer", "accept", "(Ljava/lang/Object;)V", true);
            mv.visitInsn(RETURN);
            mv.visitMaxs(2, 1);
            mv.visitEnd();
        }
    }
    // Immutable class
    {
        final String[] interfaces = new String[mutableExpansion != null ? 2 : 1];
        final StringBuilder signBuilder = new StringBuilder();
        interfaces[0] = nImmutableManipulatorType;
        signBuilder.append(dImmutableManipulatorType);
        if (mutableExpansion != null) {
            interfaces[1] = nImmutableExpansion;
            signBuilder.append(dImmutableExpansion);
        }
        cwI.visit(V1_8, ACC_PUBLIC + ACC_SUPER, immutableClassName, format("L%s<%s%s>;", nAbstractImmutableData, dImmutableManipulatorType, dManipulatorType) + signBuilder.toString(), nAbstractImmutableData, interfaces);
        {
            fv = cwI.visitField(ACC_PUBLIC + ACC_STATIC, REGISTRATION_CONSUMER, "Ljava/util/function/Consumer;", format("Ljava/util/function/Consumer<%s>;", dValueCollection), null);
            fv.visitEnd();
        }
        {
            mv = cwI.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitLdcInsn(Type.getType(immutableManipulatorType));
            mv.visitLdcInsn(Type.getType(manipulatorType));
            mv.visitMethodInsn(INVOKESPECIAL, nAbstractImmutableData, "<init>", "(Ljava/lang/Class;Ljava/lang/Class;)V", false);
            mv.visitInsn(RETURN);
            mv.visitMaxs(3, 1);
            mv.visitEnd();
        }
        {
            mv = cwI.visitMethod(ACC_PUBLIC, "<init>", format("(%s)V", dManipulatorType), null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitMethodInsn(INVOKESPECIAL, nAbstractImmutableData, "<init>", format("(%s)V", dDataManipulator), false);
            mv.visitInsn(RETURN);
            mv.visitMaxs(2, 2);
            mv.visitEnd();
        }
        {
            mv = cwI.visitMethod(ACC_PUBLIC, "registerKeys", "()V", null, null);
            mv.visitCode();
            mv.visitFieldInsn(GETSTATIC, immutableClassName, REGISTRATION_CONSUMER, "Ljava/util/function/Consumer;");
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKEVIRTUAL, immutableClassName, "getValueCollection", format("()%s", dValueCollection), false);
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/function/Consumer", "accept", "(Ljava/lang/Object;)V", true);
            mv.visitInsn(RETURN);
            mv.visitMaxs(2, 1);
            mv.visitEnd();
        }
    }
    visitMethods(mutableClassName, cwM, "getValue", mutableMethods);
    visitMethods(immutableClassName, cwI, "getImmutableValue", immutableMethods);
}
Also used : FieldVisitor(org.objectweb.asm.FieldVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 12 with FieldVisitor

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

the class TypeGenerator method visitMethods.

static void visitMethods(String className, ClassWriter cw, String methodName, @Nullable List<Method> methods) {
    if (methods != null) {
        final FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, KEYS, format("[%s", dKey), null, null);
        fv.visitEnd();
        for (int i = 0; i < methods.size(); i++) {
            final Method method = methods.get(i);
            final TypeToken<?> typeToken = TypeToken.of(method.getGenericReturnType());
            final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, method.getName(), Type.getMethodDescriptor(method), format("()%s", getDescriptor(typeToken)), null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETSTATIC, className, KEYS, format("[%s", dKey));
            visitIntInsn(mv, i);
            mv.visitInsn(AALOAD);
            mv.visitMethodInsn(INVOKEVIRTUAL, className, methodName, format("(%s)Ljava/util/Optional;", dKey), false);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Optional", "get", "()Ljava/lang/Object;", false);
            mv.visitTypeInsn(CHECKCAST, Type.getInternalName(method.getReturnType()));
            mv.visitInsn(ARETURN);
            mv.visitMaxs(3, 1);
            mv.visitEnd();
        }
    }
}
Also used : Method(java.lang.reflect.Method) FieldVisitor(org.objectweb.asm.FieldVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 13 with FieldVisitor

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

the class ScriptFunctionGenerator method generateScriptFunctionClass.

/**
 * Generates a {@link ScriptFunction} class for the specified {@link ScriptFunctionMethod}.
 *
 * @param functionMethod The script function method
 * @return The script function class
 */
private Class<? extends ScriptFunction> generateScriptFunctionClass(ScriptFunctionMethod<?> functionMethod) {
    final Class<?> functionClass = functionMethod.getFunctionClass();
    final String name = functionClass.getName() + ScriptFunction.class.getSimpleName() + "Impl35962493";
    final String internalName = name.replace('.', '/');
    final String superClass;
    final String[] interfaces;
    if (functionMethod.getFunctionClass().isInterface()) {
        interfaces = new String[2];
        interfaces[1] = Type.getInternalName(functionClass);
        superClass = Type.getInternalName(Object.class);
    } else {
        interfaces = new String[1];
        superClass = Type.getInternalName(functionClass);
    }
    interfaces[0] = Type.getInternalName(ScriptFunction.class);
    // public class <I>ScriptReferencedImpl35962493 implements <I>, ScriptFunction {
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, internalName, null, superClass, interfaces);
    // private final LanternScript script;
    FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, "script", Type.getDescriptor(LanternScript.class), null, null);
    fv.visitEnd();
    // public <I>ScriptReferencedImpl35962493(LanternScript script) {
    // this.script = script;
    // }
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(" + Type.getDescriptor(LanternScript.class) + ")V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, internalName, "script", Type.getDescriptor(LanternScript.class));
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    // @Override
    // public LanternScript getScript() {
    // return this.script;
    // }
    mv = cw.visitMethod(ACC_PUBLIC, "getScript", "()" + Type.getDescriptor(LanternScript.class), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalName, "script", Type.getDescriptor(LanternScript.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    // The script method
    @SuppressWarnings("ConstantConditions") @Nonnull final Method method = functionMethod.getMethod();
    mv = cw.visitMethod(ACC_PUBLIC, method.getName(), Type.getMethodDescriptor(method), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalName, "script", Type.getDescriptor(LanternScript.class));
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(LanternScript.class), "getRaw", "()" + Type.getDescriptor(Object.class), false);
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(functionClass));
    // Load all the method parameters to pass them through the delegated method
    for (int i = 1; i <= method.getParameterTypes().length; i++) {
        mv.visitVarInsn(ALOAD, i);
    }
    mv.visitMethodInsn(getInvokeMethodInsnOpcode(method), Type.getInternalName(method.getDeclaringClass()), method.getName(), Type.getMethodDescriptor(method), true);
    final Class<?> returnType = method.getReturnType();
    mv.visitInsn(getReturnInsnOpcode(returnType));
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] byteCode = cw.toByteArray();
    // noinspection unchecked
    return this.classLoader.defineClass(name, byteCode);
}
Also used : Nonnull(javax.annotation.Nonnull) Method(java.lang.reflect.Method) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 14 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project runelite by runelite.

the class ClassFile method accept.

public void accept(ClassVisitor visitor) {
    String[] ints = interfaces.getInterfaces().stream().map(i -> i.getName()).toArray(String[]::new);
    visitor.visit(version, access, name.getName(), null, super_class.getName(), ints);
    visitor.visitSource(source, null);
    for (Annotation annotation : annotations.getAnnotations()) {
        AnnotationVisitor av = visitor.visitAnnotation(annotation.getType().toString(), true);
        annotation.accept(av);
    }
    for (Field field : fields) {
        FieldVisitor fv = visitor.visitField(field.getAccessFlags(), field.getName(), field.getType().toString(), null, field.getValue());
        field.accept(fv);
    }
    for (Method method : methods) {
        String[] exceptions = method.getExceptions().getExceptions().stream().map(cl -> cl.getName()).toArray(String[]::new);
        if (exceptions.length == 0) {
            exceptions = null;
        }
        MethodVisitor mv = visitor.visitMethod(method.getAccessFlags(), method.getName(), method.getDescriptor().toString(), null, exceptions);
        method.accept(mv);
    }
    visitor.visitEnd();
}
Also used : Annotations(net.runelite.asm.attributes.Annotations) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) List(java.util.List) Annotation(net.runelite.asm.attributes.annotation.Annotation) Opcodes(org.objectweb.asm.Opcodes) MethodVisitor(org.objectweb.asm.MethodVisitor) FieldVisitor(org.objectweb.asm.FieldVisitor) Signature(net.runelite.asm.signature.Signature) ClassVisitor(org.objectweb.asm.ClassVisitor) ArrayList(java.util.ArrayList) Class(net.runelite.asm.pool.Class) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) FieldVisitor(org.objectweb.asm.FieldVisitor) Annotation(net.runelite.asm.attributes.annotation.Annotation) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 15 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project openj9 by eclipse.

the class InvokeDynamicTestGenerator method generateClassData.

public static byte[] generateClassData(int value) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    MethodVisitor mv;
    FieldVisitor fv;
    cw.visit(V1_7, ACC_PUBLIC, GENERATED_CLASS_NAME.replace('.', '/'), null, "java/lang/Object", null);
    {
        fv = cw.visitField(ACC_STATIC, "random", "I", null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(0, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "addIntegers", "(II)I", null, null);
        mv.visitCode();
        // mv.visitIntInsn(BIPUSH, value);
        mv.visitLdcInsn(new Integer(value));
        mv.visitFieldInsn(PUTSTATIC, GENERATED_CLASS_NAME.replace('.', '/'), "random", "I");
        mv.visitVarInsn(ILOAD, 0);
        mv.visitVarInsn(ILOAD, 1);
        MethodType mt1 = MethodType.methodType(int.class, int.class, int.class);
        MethodType mt2 = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);
        Handle handle = new Handle(H_INVOKESTATIC, InvokeDynamicTestGenerator.class.getCanonicalName().replace('.', '/'), "bootstrapAddInts", mt2.toMethodDescriptorString());
        mv.visitInvokeDynamicInsn("addInts", mt1.toMethodDescriptorString(), handle);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : MethodType(java.lang.invoke.MethodType) CallSite(java.lang.invoke.CallSite) ConstantCallSite(java.lang.invoke.ConstantCallSite) Lookup(java.lang.invoke.MethodHandles.Lookup) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) Handle(org.objectweb.asm.Handle) MethodHandle(java.lang.invoke.MethodHandle)

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