Example usage for org.objectweb.asm Opcodes V1_6

List of usage examples for org.objectweb.asm Opcodes V1_6

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes V1_6.

Prototype

int V1_6

To view the source code for org.objectweb.asm Opcodes V1_6.

Click Source Link

Usage

From source file:blusunrize.immersiveengineering.common.util.compat.jei.arcfurnace.ArcFurnaceRecipeWrapper.java

private static Class<? extends ArcFurnaceRecipeWrapper> createSubWrapper(String subtype) throws Exception {
    String entitySuperClassName = Type.getInternalName(ArcFurnaceRecipeWrapper.class);
    String entityProxySubClassName = ArcFurnaceRecipeWrapper.class.getSimpleName().concat(subtype);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, entityProxySubClassName, null,
            entitySuperClassName, null);
    cw.visitSource(entityProxySubClassName.concat(".java"), null);
    //create constructor
    String methodDescriptor = "(L" + Type.getInternalName(ArcFurnaceRecipe.class) + ";)V";
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, null, null);
    mv.visitCode();/*w ww  .  jav  a  2  s .c  o m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, entitySuperClassName, "<init>", methodDescriptor, false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    return (Class<? extends ArcFurnaceRecipeWrapper>) new ProxyClassLoader(
            Thread.currentThread().getContextClassLoader(), cw.toByteArray())
                    .loadClass(entityProxySubClassName.replaceAll("/", "."));
}

From source file:br.usp.each.saeg.badua.core.internal.ContentTypeDetector.java

License:Open Source License

private static int determineType(final InputStream in) throws IOException {
    final int header = readInt(in);
    switch (header) {
    case ZIPFILE:
        return ZIPFILE;
    case PACK200FILE:
        return PACK200FILE;
    case CLASSFILE:
        // also verify version to distinguish from Mach Object files:
        switch (readInt(in)) {
        case Opcodes.V1_1:
        case Opcodes.V1_2:
        case Opcodes.V1_3:
        case Opcodes.V1_4:
        case Opcodes.V1_5:
        case Opcodes.V1_6:
        case Opcodes.V1_7:
        case Opcodes.V1_8:
            return CLASSFILE;
        }/*from  w  ww  .  j av  a  2s.  co m*/
    }
    if ((header & 0xffff0000) == GZFILE) {
        return GZFILE;
    }
    return UNKNOWN;
}

From source file:br.usp.each.saeg.badua.core.internal.instr.ClassInstrumenter.java

License:Open Source License

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

    classProbeCount = 0;//from w  w w.j  ava 2s. c om
    className = name;
    withFrames = (version & 0xff) >= Opcodes.V1_6;
    interfaceType = (access & Opcodes.ACC_INTERFACE) != 0;

    super.visit(version, access, name, signature, superName, interfaces);
}

From source file:br.usp.each.saeg.badua.test.validation.MaxTest.java

License:Open Source License

@Override
@Before/*from w  w w  . j av a2s  .  c om*/
public void setUp() throws Exception {
    super.setUp();

    final int classVersion = Opcodes.V1_6;
    final int classAccessor = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER;
    final String className = "Max";
    final String superName = "java/lang/Object";

    final int methodAccessor = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC;
    final String methodName = "max";
    final String methodDesc = "([II)I";

    final ClassWriter cw = new ClassWriter(0);
    final MethodVisitor mw;

    cw.visit(classVersion, classAccessor, className, null, superName, null);
    mw = cw.visitMethod(methodAccessor, methodName, methodDesc, null, null);
    mw.visitCode();
    // block 0 (definitions {0, 1, 2, 3})
    mw.visitInsn(Opcodes.ICONST_0);
    mw.visitVarInsn(Opcodes.ISTORE, 2);
    mw.visitVarInsn(Opcodes.ALOAD, 0);
    mw.visitVarInsn(Opcodes.ILOAD, 2);
    mw.visitIincInsn(2, 1);
    mw.visitInsn(Opcodes.IALOAD);
    mw.visitVarInsn(Opcodes.ISTORE, 3);
    // block 1 (p-uses {1, 2})
    final Label backLoop = new Label();
    mw.visitLabel(backLoop);
    mw.visitVarInsn(Opcodes.ILOAD, 2);
    mw.visitVarInsn(Opcodes.ILOAD, 1);
    final Label breakLoop = new Label();
    mw.visitJumpInsn(Opcodes.IF_ICMPGE, breakLoop);
    // block 3 (p-uses {0, 2, 3})
    mw.visitVarInsn(Opcodes.ALOAD, 0);
    mw.visitVarInsn(Opcodes.ILOAD, 2);
    mw.visitInsn(Opcodes.IALOAD);
    mw.visitVarInsn(Opcodes.ILOAD, 3);
    final Label jump = new Label();
    mw.visitJumpInsn(Opcodes.IF_ICMPLE, jump);
    // block 5 (definitions {3}, uses {0, 2})
    mw.visitVarInsn(Opcodes.ALOAD, 0);
    mw.visitVarInsn(Opcodes.ILOAD, 2);
    mw.visitInsn(Opcodes.IALOAD);
    mw.visitVarInsn(Opcodes.ISTORE, 3);
    // block 4 (definitions {2}, uses {2})
    mw.visitLabel(jump);
    mw.visitIincInsn(2, 1);
    mw.visitJumpInsn(Opcodes.GOTO, backLoop);
    // block 2 ( uses {3})
    mw.visitLabel(breakLoop);
    mw.visitVarInsn(Opcodes.ILOAD, 3);
    mw.visitInsn(Opcodes.IRETURN);
    mw.visitMaxs(2, 4);
    mw.visitEnd();
    cw.visitEnd();

    final byte[] bytes = cw.toByteArray();
    klass = addClass(className, bytes);
    method = klass.getMethod(methodName, int[].class, int.class);
    classId = CRC64.checksum(bytes);

    RT.init(new RuntimeData());
}

From source file:bytecode.ClassExporter.java

License:Apache License

/**
 * Exports the given class node to a file. The location of this file is
 * determined by the output classpath. This will cause a
 * <code>RuntimeException</code> if the class is not deeply loaded.
 *
 * @param  c     Class node to be exported.
 *//* w w  w  . j a v a 2s . c  o m*/
public static void export(ClassNode c) {
    // Form array of interface names.
    String[] interfaces = new String[c.getInterfaces().size()];
    int index = 0;

    for (ClassNode iface : c.getInterfaces()) {
        interfaces[index++] = iface.getName();
    }

    // Export Class Header
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    cw.visit(Opcodes.V1_6, Modifier.getBitField(c.getModifiers()), c.getName(), null, // NOTE: Generic signature (for debugging/reflection).
            (c.getSuperClass() == null) ? null : c.getSuperClass().getName(), interfaces);

    // TODO: Inner / Outer Classes

    // Debug Information
    if (c.getSourceFile() != null) {
        cw.visitSource(c.getSourceFile(), null);
    }

    // Export Fields
    for (Field f : c.getFields()) {
        cw.visitField(Modifier.getBitField(f.getModifiers()), f.getName(), f.getType().getDescriptor(), null,
                f.getDefaultValue());
    }

    // Export Methods
    MethodVisitor mv;

    for (Method m : c.getMethods()) {
        if (!m.getModifiers().contains(Modifier.INHERITED)) {
            mv = cw.visitMethod(Modifier.getBitField(m.getModifiers()), m.getName(), m.getDescriptor(), null, // NOTE: Generic signature (for debugging/reflection).
                    null // TODO: Exceptions
            );

            // Export code unless NATIVE or ABSTRACT.
            if (!m.getModifiers().contains(Modifier.NATIVE) && !m.getModifiers().contains(Modifier.ABSTRACT)) {
                mv.visitCode();
                m.getImplementation().accept(new BlockExporter(mv));

                // Arguments are just dummys, the ClassWriter recalculates.
                mv.visitMaxs(0, 0);
            }

            mv.visitEnd();
        }
    }

    cw.visitEnd();

    File f = new File(outputDirectory, c.getName() + ".class");
    try {
        f.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(cw.toByteArray());
    } catch (IOException e) {
        throw new RuntimeException("Could not create class file: " + f);
    }
}

From source file:co.cask.cdap.internal.app.runtime.service.http.HttpHandlerGenerator.java

License:Apache License

/**
 * Generates a new class that implements {@link HttpHandler} by copying methods signatures from the given
 * {@link HttpServiceHandler} class. Calls to {@link HttpServiceHandler} methods are transactional.
 *
 * @param delegateType type of the {@link HttpServiceHandler}
 * @param pathPrefix prefix for all {@code @PATH} annotation
 * @return A {@link ClassDefinition} containing information of the newly generated class.
 * @throws IOException if failed to generate the class.
 *//*from   w  w w. j  a v  a  2s.co  m*/
ClassDefinition generate(TypeToken<? extends HttpServiceHandler> delegateType, String pathPrefix)
        throws IOException {
    Class<?> rawType = delegateType.getRawType();
    List<Class<?>> preservedClasses = Lists.newArrayList();
    preservedClasses.add(rawType);

    ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    String internalName = Type.getInternalName(rawType);
    String className = internalName + Hashing.md5().hashString(internalName);

    // Generate the class
    Type classType = Type.getObjectType(className);
    classWriter.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className,
            getClassSignature(delegateType), Type.getInternalName(AbstractHttpHandlerDelegator.class), null);

    // Inspect the delegate class hierarchy to generate public handler methods.
    for (TypeToken<?> type : delegateType.getTypes().classes()) {
        if (!Object.class.equals(type.getRawType())) {
            inspectHandler(delegateType, type, pathPrefix, classType, classWriter, preservedClasses);
        }
    }

    generateConstructor(delegateType, classWriter);
    generateLogger(classType, classWriter);

    ClassDefinition classDefinition = new ClassDefinition(classWriter.toByteArray(), className,
            preservedClasses);
    // DEBUG block. Uncomment for debug
    // co.cask.cdap.internal.asm.Debugs.debugByteCode(classDefinition, new java.io.PrintWriter(System.out));
    // End DEBUG block
    return classDefinition;
}

From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java

License:Apache License

/**
 * Generates a {@link DatumWriter} class for encoding data of the given output type with the given schema.
 * @param outputType Type information of the output data type.
 * @param schema Schema of the output data type.
 * @return A {@link co.cask.cdap.internal.asm.ClassDefinition} that contains generated class information.
 *//*w  w  w.  j a  va2 s  . c o m*/
ClassDefinition generate(TypeToken<?> outputType, Schema schema) {
    classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    preservedClasses = Lists.newArrayList();

    TypeToken<?> interfaceType = getInterfaceType(outputType);

    // Generate the class
    String className = getClassName(interfaceType, schema);
    classType = Type.getObjectType(className);
    classWriter.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className,
            Signatures.getClassSignature(interfaceType), Type.getInternalName(Object.class),
            new String[] { Type.getInternalName(interfaceType.getRawType()) });

    // Static schema hash field, for verification
    classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "SCHEMA_HASH",
            Type.getDescriptor(String.class), null, schema.getSchemaHash().toString()).visitEnd();

    // Schema field
    classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "schema", Type.getDescriptor(Schema.class),
            null, null).visitEnd();

    // Encode method
    generateEncode(outputType, schema);

    // Constructor
    generateConstructor();

    ClassDefinition classDefinition = new ClassDefinition(classWriter.toByteArray(), className,
            preservedClasses);
    // DEBUG block. Uncomment for debug
    //    co.cask.cdap.internal.asm.Debugs.debugByteCode(classDefinition, new java.io.PrintWriter(System.out));
    // End DEBUG block
    return classDefinition;
}

From source file:co.cask.cdap.internal.io.FieldAccessorGenerator.java

License:Apache License

ClassDefinition generate(Class<?> classType, Field field, boolean publicOnly) {
    String name = String.format("%s$GeneratedAccessor%s", classType.getName(), field.getName());
    if (name.startsWith("java.") || name.startsWith("javax.")) {
        name = "co.cask.cdap." + name;
        publicOnly = true;//from   w  w  w  .  j av a  2  s.c  om
    }
    this.className = name.replace('.', '/');

    if (publicOnly) {
        isPrivate = !Modifier.isPublic(field.getModifiers())
                || !Modifier.isPublic(field.getDeclaringClass().getModifiers());
    } else {
        isPrivate = Modifier.isPrivate(field.getModifiers())
                || Modifier.isPrivate(field.getDeclaringClass().getModifiers());
    }

    // Generate the class
    classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    classWriter.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className, null,
            Type.getInternalName(AbstractFieldAccessor.class), new String[0]);

    generateConstructor(field);
    generateGetter(field);
    generateSetter(field);

    classWriter.visitEnd();

    ClassDefinition classDefinition = new ClassDefinition(classWriter.toByteArray(), className);
    // DEBUG block. Uncomment for debug
    //    co.cask.cdap.internal.asm.Debugs.debugByteCode(classDefinition, new java.io.PrintWriter(System.out));
    // End DEBUG block
    return classDefinition;
}

From source file:co.cask.common.internal.io.DatumWriterGenerator.java

License:Apache License

/**
 * Generates a {@link DatumWriter} class for encoding data of the given output type with the given schema.
 * @param outputType Type information of the output data type.
 * @param schema Schema of the output data type.
 * @return A {@link co.cask.common.internal.asm.ClassDefinition} that contains generated class information.
 *///from ww  w  .  j  a va 2  s.  com
ClassDefinition generate(TypeToken<?> outputType, Schema schema) {
    classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    preservedClasses = Lists.newArrayList();

    TypeToken<?> interfaceType = getInterfaceType(outputType);

    // Generate the class
    String className = getClassName(interfaceType, schema);
    classType = Type.getObjectType(className);
    classWriter.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className,
            Signatures.getClassSignature(interfaceType), Type.getInternalName(Object.class),
            new String[] { Type.getInternalName(interfaceType.getRawType()) });

    // Static schema hash field, for verification
    classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "SCHEMA_HASH",
            Type.getDescriptor(String.class), null, schema.getSchemaHash().toString()).visitEnd();

    // Schema field
    classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "schema", Type.getDescriptor(Schema.class),
            null, null).visitEnd();

    // Encode method
    generateEncode(outputType, schema);

    // Constructor
    generateConstructor();

    ClassDefinition classDefinition = new ClassDefinition(classWriter.toByteArray(), className,
            preservedClasses);
    // DEBUG block. Uncomment for debug
    //    co.cask.common.internal.asm.Debugs.debugByteCode(classDefinition, new java.io.PrintWriter(System.out));
    // End DEBUG block
    return classDefinition;
}

From source file:co.cask.common.internal.io.FieldAccessorGenerator.java

License:Apache License

ClassDefinition generate(TypeToken<?> classType, Field field, boolean publicOnly) {
    String name = String.format("%s$GeneratedAccessor%s", classType.getRawType().getName(), field.getName());
    if (name.startsWith("java.") || name.startsWith("javax.")) {
        name = "co.cask.cdap." + name;
        publicOnly = true;//from  w ww  . j  ava2 s  . co  m
    }
    this.className = name.replace('.', '/');

    if (publicOnly) {
        isPrivate = !Modifier.isPublic(field.getModifiers())
                || !Modifier.isPublic(field.getDeclaringClass().getModifiers());
    } else {
        isPrivate = Modifier.isPrivate(field.getModifiers())
                || Modifier.isPrivate(field.getDeclaringClass().getModifiers());
    }

    // Generate the class
    classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    classWriter.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className, null,
            Type.getInternalName(AbstractFieldAccessor.class), new String[0]);

    generateConstructor(field);
    generateGetter(field);
    generateSetter(field);

    classWriter.visitEnd();

    ClassDefinition classDefinition = new ClassDefinition(classWriter.toByteArray(), className);
    // DEBUG block. Uncomment for debug
    //    co.cask.common.internal.asm.Debugs.debugByteCode(classDefinition, new java.io.PrintWriter(System.out));
    // End DEBUG block
    return classDefinition;
}