Example usage for org.objectweb.asm.util CheckClassAdapter visitEnd

List of usage examples for org.objectweb.asm.util CheckClassAdapter visitEnd

Introduction

In this page you can find the example usage for org.objectweb.asm.util CheckClassAdapter visitEnd.

Prototype

@Override
    public void visitEnd() 

Source Link

Usage

From source file:erjang.ETuple.java

License:Apache License

static byte[] make_tuple_class_data(int num_cells) {
    ClassWriter cww = new ClassWriter(true);

    CheckClassAdapter cw = new CheckClassAdapter(cww);

    String this_class_name = ETUPLE_NAME + num_cells;
    String super_class_name = ETUPLE_NAME;
    cw.visit(Opcodes.V1_4, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, this_class_name, null, super_class_name,
            null);/*from   ww  w.ja v  a  2 s . c o  m*/

    // create fields

    for (int i = 1; i <= num_cells; i++) {
        cw.visitField(Opcodes.ACC_PUBLIC, "elem" + i, ETERM_TYPE.getDescriptor(), null, null);
    }

    // create count method
    create_count(cw, num_cells);

    // create cast method
    create_cast(cw, num_cells);
    create_cast2(cw, num_cells);

    // create constructor
    create_constructor(cw, super_class_name);

    // create copy
    create_tuple_copy(num_cells, cw, this_class_name, super_class_name);

    // create nth
    create_tuple_nth(num_cells, cw, this_class_name);

    // create set
    create_tuple_set(num_cells, cw, this_class_name);

    cw.visitEnd();
    byte[] data = cww.toByteArray();

    // dump(this_class_name, data);

    return data;
}

From source file:org.smallmind.nutsnbolts.reflection.ProxyGenerator.java

License:Open Source License

public static <T> T createProxy(Class<T> parseClass, InvocationHandler handler,
        Class<? extends Annotation>... allowedAnnotationClasses) {

    Class<T> extractedClass;
    ParseKey parseKey;/*w ww .  ja v a2s . co  m*/
    String[] allowedAnnotationSignatures = null;

    if (handler == null) {
        throw new IllegalArgumentException("You must supply a non-null InvocationHandler");
    }

    int parseClassModifiers = parseClass.getModifiers();

    if (!Modifier.isPublic(parseClassModifiers)) {
        throw new ByteCodeManipulationException("The proxy class(%s) must be 'public'", parseClass.getName());
    }
    if (Modifier.isStatic(parseClassModifiers)) {
        throw new ByteCodeManipulationException("The proxy class(%s) must not be 'static'",
                parseClass.getName());
    }
    if ((!parseClass.isInterface()) && Modifier.isAbstract(parseClassModifiers)) {
        throw new ByteCodeManipulationException("A concrete proxy class(%s) must not be 'abstract'",
                parseClass.getName());
    }

    if (allowedAnnotationClasses != null) {
        allowedAnnotationSignatures = new String[allowedAnnotationClasses.length];
        for (int index = 0; index < allowedAnnotationClasses.length; index++) {
            allowedAnnotationSignatures[index] = "L"
                    + allowedAnnotationClasses[index].getName().replace('.', '/') + ";";
        }
    }

    parseKey = new ParseKey(parseClass, allowedAnnotationSignatures);
    if ((extractedClass = INTERFACE_MAP.get(parseKey)) == null) {
        synchronized (INTERFACE_MAP) {
            if ((extractedClass = INTERFACE_MAP.get(parseKey)) == null) {

                Class currentClass;
                ClassReader classReader;
                ClassWriter classWriter;
                CheckClassAdapter checkClassAdapter;
                ProxyClassVisitor proxyClassVisitor;
                ClassLoader parseClassLoader;
                ProxyClassLoader proxyClassLoader;
                HashSet<MethodTracker> methodTrackerSet;
                boolean initialized = false;

                classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
                checkClassAdapter = new CheckClassAdapter(classWriter, true);

                currentClass = parseClass;
                methodTrackerSet = new HashSet<>();
                do {
                    if (currentClass.equals(Object.class)) {
                        currentClass = ObjectImpersonator.class;
                    }

                    try {
                        classReader = new ClassReader(currentClass.getClassLoader().getResourceAsStream(
                                currentClass.getCanonicalName().replace('.', '/') + ".class"));
                    } catch (IOException ioException) {
                        throw new ByteCodeManipulationException(ioException);
                    }

                    proxyClassVisitor = new ProxyClassVisitor(checkClassAdapter, parseClass, currentClass,
                            allowedAnnotationSignatures, methodTrackerSet, initialized);
                    classReader.accept(proxyClassVisitor, 0);
                    initialized = true;
                } while ((currentClass = currentClass.equals(ObjectImpersonator.class) ? null
                        : currentClass.getSuperclass()) != null);

                checkClassAdapter.visitEnd();

                synchronized (LOADER_MAP) {
                    if ((proxyClassLoader = LOADER_MAP
                            .get(parseClassLoader = parseClass.getClassLoader())) == null) {
                        LOADER_MAP.put(parseClassLoader,
                                proxyClassLoader = new ProxyClassLoader(parseClassLoader));
                    }
                }

                INTERFACE_MAP.put(parseKey, extractedClass = proxyClassLoader.extractInterface(
                        parseClass.getName() + "$Proxy$_ExtractedSubclass", classWriter.toByteArray()));
            }
        }
    }

    try {
        return extractedClass.getConstructor(InvocationHandler.class).newInstance(handler);
    } catch (Exception exception) {
        throw new ByteCodeManipulationException(exception);
    }
}

From source file:org.spongepowered.mod.asm.util.ASMEventListenerFactory.java

License:MIT License

@SuppressWarnings("unchecked")
private static <T> Class<T> createClass(Class<T> interf, Method input, Method output) {

    String className = getClassName(interf, input, output);

    ClassWriter cwBase = new ClassWriter(0);
    CheckClassAdapter cw = new CheckClassAdapter(cwBase);

    MethodVisitor mv;/*from ww w.  j a v a  2  s.  co  m*/

    String classNameDesc = className.replace('.', '/');

    String interfaceInternalName = Type.getInternalName(interf);

    String inputName = input.getName();
    String inputMethodDescriptor = Type.getMethodDescriptor(input);

    String outputParameterTypeIntName = Type.getInternalName(output.getParameterTypes()[0]);
    String outputTargetTypeIntName = Type.getInternalName(output.getDeclaringClass());
    String outputMethodDescriptor = Type.getMethodDescriptor(output);
    String outputName = output.getName();

    boolean isOutputInterface = output.getDeclaringClass().isInterface();

    // A new class of the following form is created, with a unique name
    //
    // package org.spongepowered.mod.asm;
    // public class <className> extends java.lang.Object implements <interf>
    //
    //     private final Object target
    //
    //     public <className> (java.lang.Object target) {
    //         super();
    //         this.target = target;
    //         return;
    //     }
    //
    //     public void <inputMethod> (<inputMethodType event) {
    //         ((outputTargetType) this.target).outputMethod((outputParameteType) event);
    //         return
    //     }
    // }

    // package org.spongepowered.mod.asm;
    // public class <className> extends java.lang.Object implements <interf>
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, classNameDesc, null, "java/lang/Object",
            new String[] { interfaceInternalName });

    // private final Object target
    cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "target", "Ljava/lang/Object;", null, null);

    // Constructor

    // public UniqueClass (java.lang.Object target) {
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, null);
    mv.visitCode();

    // super();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);

    // this.target = target;
    mv.visitVarInsn(Opcodes.ALOAD, 0); // Loads this
    mv.visitVarInsn(Opcodes.ALOAD, 1); // Loads target (from input)
    mv.visitFieldInsn(Opcodes.PUTFIELD, classNameDesc, "target", "Ljava/lang/Object;");

    // return;
    mv.visitInsn(Opcodes.RETURN);

    // }
    // 2 localvars due to inputs: this, target
    // 2 items on stack after double ALOAD
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    // Callback method

    // public void <inputMethod> (<inputMethodType event) {
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, inputName, inputMethodDescriptor, null, null);
    mv.visitCode();

    // push((casted) this.target)
    mv.visitVarInsn(Opcodes.ALOAD, 0); // Loads this
    mv.visitFieldInsn(Opcodes.GETFIELD, classNameDesc, "target", "Ljava/lang/Object;");
    mv.visitTypeInsn(Opcodes.CHECKCAST, outputTargetTypeIntName);

    // push((casted) event)
    mv.visitVarInsn(Opcodes.ALOAD, 1); // Loads method parameter 0
    mv.visitTypeInsn(Opcodes.CHECKCAST, outputParameterTypeIntName);

    // ((outputTargetType) this.target).outputMethod((outputParameteType) event);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, outputTargetTypeIntName, outputName, outputMethodDescriptor,
            isOutputInterface);

    // return
    mv.visitInsn(Opcodes.RETURN);

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

    cw.visitEnd();

    byte[] bytes = cwBase.toByteArray();

    return (Class<T>) loader.defineClass(className, bytes);
}

From source file:org.spongepowered.mod.asm.util.ASMEventListenerHolderFactory.java

License:MIT License

public static Class<?> getClass(Class<?> eventClass, EventPriority priority, boolean canceled) {
    ClassWriter cwRaw = new ClassWriter(0);
    CheckClassAdapter cw = new CheckClassAdapter(cwRaw);

    MethodVisitor mv;//from  w  w  w  .  j  av  a  2  s .co m
    AnnotationVisitor av0;

    String className = getClassName(eventClass, priority, canceled);
    String classNameInternal = className.replace('.', '/');

    String invokeMethodDesc = "(" + Type.getDescriptor(eventClass) + ")V";

    String eventPriorityName = priority.name();

    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, classNameInternal, null,
            "org/spongepowered/mod/event/EventListenerHolder", null);

    cw.visitInnerClass("net/minecraftforge/event/world/BlockEvent$BreakEvent",
            "net/minecraftforge/event/world/BlockEvent", "BreakEvent", ACC_PUBLIC + ACC_STATIC);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "org/spongepowered/mod/event/EventListenerHolder", "<init>", "()V",
                false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "invoke", invokeMethodDesc, null, null);
        {
            av0 = mv.visitAnnotation("Lnet/minecraftforge/fml/common/eventhandler/SubscribeEvent;", true);
            av0.visitEnum("priority", "Lnet/minecraftforge/fml/common/eventhandler/EventPriority;",
                    eventPriorityName);
            av0.visit("receiveCanceled", canceled);
            av0.visitEnd();
        }
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, "org/spongepowered/mod/event/EventListenerHolder", "invoke",
                "(Ljava/lang/Object;)V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    if (loaderAccess == null) {
        loaderAccess = new ClassLoaderAccess();
    }
    return loaderAccess.defineClass(className, cwRaw.toByteArray());
}