Example usage for org.objectweb.asm Opcodes V1_5

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

Introduction

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

Prototype

int V1_5

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

Click Source Link

Usage

From source file:com.facebook.buck.jvm.java.abi.SourceVersionUtils.java

License:Apache License

/** Gets the class file version corresponding to the given source version constant. */
public static int sourceVersionToClassFileVersion(SourceVersion version) {
    switch (version) {
    case RELEASE_0:
        return Opcodes.V1_1; // JVMS8 4.1: 1.0 and 1.1 both support version 45.3 (Opcodes.V1_1)
    case RELEASE_1:
        return Opcodes.V1_1;
    case RELEASE_2:
        return Opcodes.V1_2;
    case RELEASE_3:
        return Opcodes.V1_3;
    case RELEASE_4:
        return Opcodes.V1_4;
    case RELEASE_5:
        return Opcodes.V1_5;
    case RELEASE_6:
        return Opcodes.V1_6;
    case RELEASE_7:
        return Opcodes.V1_7;
    case RELEASE_8:
        return Opcodes.V1_8;
    default:/* ww w .  j a  va 2 s .  co m*/
        throw new IllegalArgumentException(String.format("Unexpected source version: %s", version));
    }
}

From source file:com.geeksaga.light.profiler.util.ASMUtil.java

License:Apache License

public static byte[] toBytes(ClassNodeWrapper clazz) {
    int flags = ClassWriter.COMPUTE_MAXS;

    if (clazz.version > Opcodes.V1_5) {
        flags |= ClassWriter.COMPUTE_FRAMES;
    }/*from   w  w  w .java 2 s .co  m*/

    ClassWriter classWriter = new ClassWriterWrapper(flags);

    clazz.accept(useJSRInlinerAdapter(classWriter));

    return classWriter.toByteArray();
}

From source file:com.github.rgcjonas.kuemmelgtr.core.Compiler.java

License:Open Source License

public static byte[] compileFunc(TokenSource<RPNToken> source, String variable) throws ParsingError {
    // initialize class
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "com/github/rgcjonas/kuemmelgtr/jit/BogusName", null,
            CompiledClassBase.class.getName().replace('.', '/'),
            new String[] { Evaluator.Function.class.getName().replace('.', '/') });

    // create constructor
    MethodVisitor construct = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    construct.visitCode();/* www.ja va  2s. c om*/
    construct.visitVarInsn(Opcodes.ALOAD, 0);
    construct.visitMethodInsn(Opcodes.INVOKESPECIAL, CompiledClassBase.class.getName().replace('.', '/'),
            "<init>", "()V");
    construct.visitInsn(Opcodes.RETURN);
    construct.visitMaxs(0, 0);
    construct.visitEnd();

    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "evaluate", "(D)D", null,
            new String[] { "com/github/rgcjonas/kuemmelgtr/core/RuntimeError" });

    compileCommon(source, method, variable);

    writer.visitEnd();
    return writer.toByteArray();
}

From source file:com.github.rgcjonas.kuemmelgtr.core.Compiler.java

License:Open Source License

public static byte[] compileBasic(TokenSource<RPNToken> source) throws ParsingError {
    // initialize class
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "com/github/rgcjonas/kuemmelgtr/jit/BogusName", null,
            CompiledClassBase.class.getName().replace('.', '/'),
            new String[] { Evaluator.Basic.class.getName().replace('.', '/') });

    // create constructor
    MethodVisitor construct = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    construct.visitCode();//from   w w w.j a  va  2s .co m
    construct.visitVarInsn(Opcodes.ALOAD, 0);
    construct.visitMethodInsn(Opcodes.INVOKESPECIAL, CompiledClassBase.class.getName().replace('.', '/'),
            "<init>", "()V");
    construct.visitInsn(Opcodes.RETURN);
    construct.visitMaxs(0, 0);
    construct.visitEnd();

    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "evaluate", "()D", null,
            new String[] { "com/github/rgcjonas/kuemmelgtr/core/ParsingError",
                    "com/github/rgcjonas/kuemmelgtr/core/RuntimeError" });

    compileCommon(source, method, null);

    writer.visitEnd();
    return writer.toByteArray();
}

From source file:com.google.code.nanorm.internal.introspect.asm.AccessorBuilder.java

License:Apache License

/**
 * Constructor. Starts generating the Java class code.
 * /*w  w  w  . jav  a  2 s  .  com*/
 * @param name accessor class name
 * @param isSetter if we building accessor for setter.
 */
public AccessorBuilder(String name, boolean isSetter) {
    this.isSetter = isSetter;

    cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);

    Type owner = Type.getType('L' + name + ';');
    String iface = isSetter ? "com/google/code/nanorm/internal/introspect/Setter"
            : "com/google/code/nanorm/internal/introspect/Getter";
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object", new String[] { iface });

    cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "type", "Ljava/lang/reflect/Type;", null, null);

    visitConstructor(owner);
    visitGetType(owner);
}

From source file:com.google.code.nanorm.internal.introspect.asm.MapperBuilder.java

License:Apache License

/**
 * Build mapper./*from   w w  w.j  av  a2  s. c o m*/
 * 
 * @param name class name
 * @param mapper mapper interface or base class
 * @param configs method configurations
 * @return mapper byte-code
 */
public static byte[] buildMapper(String name, Class<?> mapper, MethodConfig[] configs) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);

    Type owner = Type.getType('L' + name + ';');

    if (mapper.isInterface()) {
        cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object",
                new String[] { mapper.getName().replace('.', '/') });
    } else {
        cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, mapper.getName().replace('.', '/'), null);
    }

    // Copy the annotations
    copyAnnotations(mapper, cw, null);

    cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "delegate", QUERY_DELEGATE_TYPE.getDescriptor(),
            null, null);

    cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "configs",
            STATEMENT_CONFIGS_ARR_TYPE.getDescriptor(), null, null);

    if (mapper.isInterface()) {
        visitConstructor(cw, owner, OBJECT_TYPE);
    } else {
        visitConstructor(cw, owner, Type.getType(mapper));
    }

    for (MethodConfig cfg : configs) {
        visitMethod(owner, cw, cfg);
    }

    cw.visitEnd();

    return cw.toByteArray();
}

From source file:com.google.devtools.build.wireless.testing.java.injector.coverage.CodeCoverageClassAdapterTest.java

License:Apache License

/**
 * Tests /*from  w ww  .  j  a v a  2s  .  com*/
 * {@link CodeCoverageClassAdapter#visitMethod(int, String, String, String, String[])}
 * .
 * 
 * <p>Tests that the right method visitor would be returned.
 * The correct chain would be 
 * <br>|DummyMethodAdapter|<br>
 * if the method does not have 
 * to be mapped or 
 * <br>|CodeCoverageClassAdapter.MethodCoverage|DummyMethodAdapter|<br>
 * if the method has to be covered. This allows to make 
 * assertion on the correct usage by checking the type of the first element in 
 * the chain.
 */
public void testVisitMethod() {
    String name = null;
    classAdapter.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Foo", null, ClassNames.JAVA_LANG_OBJECT, null);
    classAdapter.visitSource("Foo.java", null);

    for (int i = 0; i < MAX; i++) {
        name = "dummeyMethod" + i;
        MethodVisitor mv = classAdapter.visitMethod(Opcodes.ACC_PUBLIC, name, "()V", null, null);
        if (classAdapter.isIncluded()) {
            assertTrue("Mapped as covered.", !(mv instanceof EmptyVisitor));
        } else {
            assertTrue("Mapped as covered.", (mv instanceof EmptyVisitor));
        }
    }
}

From source file:com.google.devtools.build.wireless.testing.java.injector.coverage.CodeCoverageClassAdapterTest.java

License:Apache License

/**
 * Tests /*  www.  j  av a 2s.  co m*/
 * {@link CoverageStatisticContainer#addInstrumentedLineAndGetLineIndex(String, int)}
 * .
 * 
 * <p>Simulates the adding of a full method starting from line 0 until a 
 * value.
 */
public void testIncludeLines_sequence() {
    String className = "com/Foo";
    String sourceName = "Foo.java";
    String producedFilename = "com/Foo";
    String methodName = "method";
    String methodDesc = "()V";
    int lines = 12345;

    String[] inclusion = new String[] { "+com" };
    classAdapter = new CodeCoverageClassAdapter(new EmptyVisitor(), statisticContainer, inclusion,
            CoverageMode.LINE);
    classAdapter.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, ClassNames.JAVA_LANG_OBJECT, null);
    classAdapter.visitSource(sourceName, null);

    MethodVisitor mv = classAdapter.visitMethod(Opcodes.ACC_PUBLIC, methodName, methodDesc, null, null);
    mv.visitCode();
    for (int i = 0; i < lines; i++) {
        mv.visitLineNumber(i, null);
    }
    mv.visitEnd();
    assertEquals("Wrong line size.", lines, statisticContainer.getNumberOfLinesForFile(producedFilename));
}

From source file:com.google.gwt.dev.shell.rewrite.ForceClassVersion15.java

License:Apache License

@Override
public void visit(final int version, final int access, final String name, final String signature,
        final String superName, final String[] interfaces) {
    assert (version >= Opcodes.V1_5);
    super.visit(Opcodes.V1_5, access, name, signature, superName, interfaces);
}

From source file:com.google.gwt.dev.shell.rewrite.HostedModeClassRewriter.java

License:Apache License

public byte[] writeJsoIntf(final String className, byte classBytes[]) {
    String desc = toDescriptor(className);
    assert (jsoIntfDescs.contains(desc));
    assert (jsoSuperDescs.containsKey(desc));
    List<String> superDescs = jsoSuperDescs.get(desc);
    assert (superDescs != null);
    assert (superDescs.size() > 0);

    // The ASM model is to chain a bunch of visitors together.
    ClassWriter writer = new ClassWriter(0);
    final ClassVisitor v = writer;

    // v = new CheckClassAdapter(v);
    // v = new TraceClassVisitor(v, new PrintWriter(System.out));

    String[] interfaces;//from  ww w.  java 2 s .  com
    // TODO(bov): something better than linear?
    if (superDescs.contains("java/lang/Object")) {
        interfaces = null;
    } else {
        interfaces = superDescs.toArray(new String[superDescs.size()]);
    }
    v.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC | Opcodes.ACC_INTERFACE, desc, null, "java/lang/Object",
            interfaces);
    if (classBytes != null) {
        // Java7 enforces innerclass/outerclass consistency. In order to fix this, copy from original
        ClassVisitor cv = new EmptyVisitor() {
            @Override
            public void visitInnerClass(String name, String outerName, String innerName, int access) {
                // copy inner class table from original JSO to synthetic interface
                v.visitInnerClass(name, outerName, innerName, access);
            }

            @Override
            public void visitOuterClass(String owner, String name, String desc) {
                // copy outer class table from original JSO to synthetic interface
                v.visitOuterClass(owner, name, desc);
            }
        };
        new ClassReader(classBytes).accept(cv, 0);
    }
    v.visitEnd();
    return writer.toByteArray();
}