Example usage for org.objectweb.asm Opcodes V1_7

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

Introduction

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

Prototype

int V1_7

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

Click Source Link

Usage

From source file:de.javanarior.vo.generator.ByteCodeGenerator.java

License:Apache License

/**
 * Generate a implementation of a value object.
 *
 * @param valueType/*w  ww.  j a v  a2 s . co  m*/
 *            - value object type
 * @param technicalType
 *            - to which the value object is mapped
 * @param wrapperClass
 *            - abstract wrapper class, correspond to the technical type
 * @return class name and byte code in a container
 */
/* CHECKSTYLE:OFF */
public static ByteCodeContainer generate(Class<?> valueType, Class<? extends Comparable<?>> technicalType,
        @SuppressWarnings("rawtypes") Class<? extends AbstractValue> wrapperClass) {
    /* CHECKSTYLE:ON */

    if (!isInterface(valueType)) {
        throw new IllegalArgumentException("Could not generate implementation for class " + valueType.getName()
                + ". Please provide interface");
    }

    ClassWriter classWriter = new ClassWriter(0);
    MethodVisitor methodVisitor;

    classWriter.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, implementationClassName(valueType),
            "L" + parentClassName(wrapperClass) + "<" + addTypeDiscriptor(valueType) + ">;"
                    + addTypeDiscriptor(valueType),
            parentClassName(wrapperClass), implementedInterfaces(valueType));

    methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR, methodDescriptor(technicalType),
            null, null);
    methodVisitor.visitCode();
    Label label0 = new Label();
    methodVisitor.visitLabel(label0);
    /* CHECKSTYLE:OFF */
    methodVisitor.visitLineNumber(8, label0);
    /* CHECKSTYLE:ON */
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitVarInsn(getILOADOpCode(technicalType), 1);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, parentClassName(wrapperClass), CONSTRUCTOR,
            methodDescriptor(technicalType), false);
    Label label1 = new Label();
    methodVisitor.visitLabel(label1);
    /* CHECKSTYLE:OFF */
    methodVisitor.visitLineNumber(9, label1);
    /* CHECKSTYLE:ON */
    methodVisitor.visitInsn(Opcodes.RETURN);
    Label label2 = new Label();
    methodVisitor.visitLabel(label2);
    methodVisitor.visitLocalVariable("this", addTypeSignature(implementationClassName(valueType)), null, label0,
            label2, 0);
    methodVisitor.visitLocalVariable("value", getType(technicalType), null, label0, label2, 1);
    int stackSize = getStackSize(Type.getDescriptor(technicalType));
    methodVisitor.visitMaxs(stackSize, stackSize);
    methodVisitor.visitEnd();
    classWriter.visitEnd();

    return new ByteCodeContainer(binaryClassName(valueType), classWriter.toByteArray());
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

private ClassNodeBuilder(final String className, final String superClass, final String constructorDesc,
        final String superConstructorDesc, final boolean isInterface) {
    this.isInterface = isInterface;
    classNode = new ClassNode(Opcodes.ASM5);
    classNode.access = ACC_PUBLIC;//  www. j  a v  a 2 s  .  c o m
    classNode.name = className.replace(".", "/");
    if (superClass != null) {
        classNode.superName = superClass.replace(".", "/");
    }
    classNode.version = Opcodes.V1_7;
    if (!isInterface) {
        addConstructor(constructorDesc, superConstructorDesc);
    } else {
        classNode.access |= ACC_INTERFACE;
        classNode.access |= ACC_ABSTRACT;
    }
    lastElement = null;
}

From source file:edu.mit.streamjit.util.bytecode.KlassUnresolver.java

License:Open Source License

private byte[] unresolve() {
    this.classNode.version = Opcodes.V1_7;
    this.classNode.access = Modifier.toBits(klass.modifiers());
    this.classNode.name = internalName(klass);
    assert klass.getSuperclass() != null || Object.class.equals(klass.getBackingClass()) : klass;
    this.classNode.superName = internalName(klass.getSuperclass());
    for (Klass k : klass.interfaces())
        this.classNode.interfaces.add(internalName(k));

    for (Field f : klass.fields()) {
        FieldNode fn = new FieldNode(Opcodes.ASM4, Modifier.toBits(f.modifiers()), f.getName(),
                f.getType().getFieldType().getDescriptor(), null, null);
        this.classNode.fields.add(fn);
    }/*from w  w w  .  j  av  a2  s  .c  om*/

    for (Method m : klass.methods())
        this.classNode.methods.add(MethodUnresolver.unresolve(m));

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    ClassVisitor cv = cw;
    //      boolean assertionsEnabled = false;
    //      assert assertionsEnabled = true; //intentional side effect
    //      if (assertionsEnabled)
    //         cv = new CheckClassAdapter(cv, true);
    classNode.accept(cv);
    return cw.toByteArray();
}

From source file:fr.insalyon.telecom.jooflux.InvokeClassAdapter.java

License:Mozilla Public License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {/*ww  w.  ja  v  a 2  s . c  o m*/
    this.cv.visit(Opcodes.V1_7, access, name, signature, superName, interfaces);
    this.isInterface = (access & Opcodes.ACC_INTERFACE) != 0;
    //        this.isClassLoader = "java/lang/ClassLoader".equals(superName);
}

From source file:net.orfjackal.retrolambda.Config.java

License:Open Source License

public int getBytecodeVersion() {
    return Integer.parseInt(p.getProperty(BYTECODE_VERSION, "" + Opcodes.V1_7));
}

From source file:net.orfjackal.retrolambda.SystemPropertiesConfig.java

License:Open Source License

@Override
public int getBytecodeVersion() {
    return Integer.parseInt(p.getProperty(BYTECODE_VERSION, "" + Opcodes.V1_7));
}

From source file:net.orfjackal.retrolambda.Transformers.java

License:Open Source License

private byte[] transform(String className, Consumer<ClassVisitor> reader, ClassVisitorChain chain) {
    try {/* w  w  w. j a v a2s . c  om*/
        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        ClassVisitor next = writer;

        next = new LowerBytecodeVersion(next, targetVersion);
        if (targetVersion < Opcodes.V1_7) {
            next = new SwallowSuppressedExceptions(next);
            next = new RemoveMethodHandlesLookupReferences(next);
            next = new RequireNonNull(next);
        }
        next = new FixInvokeStaticOnInterfaceMethod(next);
        next = new UpdateRenamedEnclosingMethods(next, analyzer);
        next = chain.wrap(next);

        reader.accept(next);
        return writer.toByteArray();

    } catch (Throwable t) {
        throw new RuntimeException("Failed to backport class: " + className, t);
    }
}

From source file:org.apache.aries.proxy.impl.ProxyUtils.java

License:Apache License

/**
 * Get the java version to be woven at./*from  www .  ja v  a2  s  .c  o m*/
 * @return
 */
public static int getWeavingJavaVersion() {
    if (weavingJavaVersion == -1) {
        //In order to avoid an inconsistent stack error the version of the woven byte code needs to match
        //the level of byte codes in the original class
        switch (JAVA_CLASS_VERSION) {
        case Opcodes.V1_8:
            LOGGER.debug("Weaving to Java 8");
            weavingJavaVersion = Opcodes.V1_8;
            break;
        case Opcodes.V1_7:
            LOGGER.debug("Weaving to Java 7");
            weavingJavaVersion = Opcodes.V1_7;
            break;
        case Opcodes.V1_6:
            LOGGER.debug("Weaving to Java 6");
            weavingJavaVersion = Opcodes.V1_6;
            break;
        case Opcodes.V1_5:
            LOGGER.debug("Weaving to Java 5");
            weavingJavaVersion = Opcodes.V1_5;
            break;
        default:
            //aries should work with Java 5 or above - also will highlight when a higher level (and unsupported) level of Java is released
            throw new IllegalArgumentException("Invalid Java version " + JAVA_CLASS_VERSION);
        }
    }
    return weavingJavaVersion;
}

From source file:org.apache.twill.yarn.CustomClassLoader.java

License:Apache License

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
    if (!CustomClassLoaderRunnable.GENERATED_CLASS_NAME.equals(name)) {
        return super.findClass(name);
    }/*from w ww. j a  v a 2 s .c o m*/

    // Generate a class that look like this:
    //
    // public class Generated {
    //
    //   public void announce(ServiceAnnouncer announcer, String serviceName, int port) {
    //     announcer.announce(serviceName, port);
    //   }
    // }
    Type generatedClassType = Type
            .getObjectType(CustomClassLoaderRunnable.GENERATED_CLASS_NAME.replace('.', '/'));
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, generatedClassType.getInternalName(), null,
            Type.getInternalName(Object.class), null);

    // Generate the default constructor, which just call super();
    Method constructor = new Method("<init>", Type.VOID_TYPE, new Type[0]);
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, null, null, cw);
    mg.loadThis();
    mg.invokeConstructor(Type.getType(Object.class), constructor);
    mg.returnValue();
    mg.endMethod();

    // Generate the announce method
    Method announce = new Method("announce", Type.VOID_TYPE,
            new Type[] { Type.getType(ServiceAnnouncer.class), Type.getType(String.class), Type.INT_TYPE });
    mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, announce, null, null, cw);
    mg.loadArg(0);
    mg.loadArg(1);
    mg.loadArg(2);
    mg.invokeInterface(Type.getType(ServiceAnnouncer.class), new Method("announce",
            Type.getType(Cancellable.class), new Type[] { Type.getType(String.class), Type.INT_TYPE }));
    mg.pop();
    mg.returnValue();
    mg.endMethod();
    cw.visitEnd();

    byte[] byteCode = cw.toByteArray();
    return defineClass(CustomClassLoaderRunnable.GENERATED_CLASS_NAME, byteCode, 0, byteCode.length);
}

From source file:org.elasticsearch.painless.Writer.java

License:Apache License

private void writeBegin() {
    final int compute = ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS;
    final int version = Opcodes.V1_7;
    final int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL;
    final String base = BASE_CLASS_TYPE.getInternalName();
    final String name = CLASS_TYPE.getInternalName();

    writer = new ClassWriter(compute);
    writer.visit(version, access, name, null, base, null);
    writer.visitSource(source, null);/*from   w  w w  .j a va2s .  c om*/
}