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

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

Introduction

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

Prototype

@Override
    public void visit(final int version, final int access, final String name, final String signature,
            final String superName, final String[] interfaces) 

Source Link

Usage

From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.ByteCodeVisitor.java

License:Open Source License

public void toAsm(JarOutputStream jos) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    //        ClassWriter cw = new ClassWriter(0);
    CheckClassAdapter cca = new CheckClassAdapter(cw);

    cca.visit(version, access, name, sig, superName, interfaces);
    Iterator it = fieldVisitors.entrySet().iterator();

    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry) it.next();
        ByteCodeFieldVisitor fv = (ByteCodeFieldVisitor) pairs.getValue();
        fv.toAsm(cca);//  w  ww  .  jav  a  2  s.co  m
    }

    it = methodVisitors.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry) it.next();
        ByteCodeMethodVisitor mv = (ByteCodeMethodVisitor) pairs.getValue();
        mv.toAsm(cca);
    }

    byte[] bytes = cw.toByteArray();
    if (ProjectProperties.getBoolean("fortress.bytecode.verify", false)) {
        PrintWriter pw = new PrintWriter(System.out);
        CheckClassAdapter.verify(new ClassReader(bytes), true, pw);
    }
    System.out.println("About to write " + name);
    ByteCodeWriter.writeJarredClass(jos, name, bytes);
}

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 w  w  w  . j a v  a2 s .  c  om*/

    // 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.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  w  w w  .  j  a v a  2  s.com*/

    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;// w  ww.  j a  va 2 s .c  om
    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());
}