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:org.jacoco.core.internal.instr.ProbeArrayStrategyFactoryTest.java

License:Open Source License

@Test
public void testEmptyInterface7() {
    final IProbeArrayStrategy strategy = test(Opcodes.V1_7, Opcodes.ACC_INTERFACE, false, false, false);
    assertEquals(NoneProbeArrayStrategy.class, strategy.getClass());
    assertNoDataField();//  ww w  . j a v a 2s .c om
    assertNoInitMethod();
}

From source file:org.jacoco.core.internal.instr.ProbeArrayStrategyFactoryTest.java

License:Open Source License

@Test(expected = UnsupportedOperationException.class)
public void testEmptyInterface7StoreInstance() {
    IProbeArrayStrategy strategy = test(Opcodes.V1_7, Opcodes.ACC_INTERFACE, false, false, false);
    strategy.storeInstance(null, false, 0);
}

From source file:org.jacoco.core.test.validation.BootstrapMethodReferenceTest.java

License:Open Source License

@Test
public void test() throws Exception {
    final String className = "Example";

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null);

    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "run", "()I", null, null);
    mv.visitCode();//from   w ww .  j a  v  a  2 s.com
    addCauseOfResizeInstructions(mv);
    final MethodType methodType = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class,
            String.class, MethodType.class);
    final Handle handle = new Handle(Opcodes.H_INVOKESTATIC,
            this.getClass().getCanonicalName().replace('.', '/'), "bootstrap",
            methodType.toMethodDescriptorString(), false);
    mv.visitInvokeDynamicInsn("invoke", "()I", handle);
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(1, 0);
    mv.visitEnd();

    cw.visitEnd();

    final byte[] original = cw.toByteArray();
    assertEquals(42, run(className, original));

    final byte[] instrumented = instrumenter.instrument(original, className);
    assertEquals(42, run(className, instrumented));
}

From source file:org.jruby.anno.IndyBinder.java

License:LGPL

public void processType(TypeElement cd) {
    // process inner classes
    for (TypeElement innerType : ElementFilter.typesIn(cd.getEnclosedElements())) {
        processType(innerType);/*  w ww .  j  av a  2 s  .  c o  m*/
    }

    try {
        String qualifiedName = cd.getQualifiedName().toString().replace('.', '$');

        // skip anything not related to jruby
        if (!qualifiedName.contains("org$jruby")) {
            return;
        }

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);

        cw.visitAnnotation(p(Generated.class), true);

        cw.visit(Opcodes.V1_7, ACC_PUBLIC,
                ("org.jruby.gen." + qualifiedName + POPULATOR_SUFFIX).replace('.', '/'), null,
                "org/jruby/anno/TypePopulator", null);

        mv = new SkinnyMethodAdapter(cw, ACC_PUBLIC, "<init>", "()V", null, null);
        mv.start();
        mv.aload(0);
        mv.invokespecial("org/jruby/anno/TypePopulator", "<init>", "()V");
        mv.voidreturn();
        mv.end();

        mv = new SkinnyMethodAdapter(cw, ACC_PUBLIC, "populate", "(Lorg/jruby/RubyModule;Ljava/lang/Class;)V",
                null, null);

        mv.start();

        if (DEBUG) {
            mv.ldc("Using pregenerated populator: " + qualifiedName + POPULATOR_SUFFIX);
            mv.aprintln();
        }

        // scan for meta, compat, etc to reduce findbugs complaints about "dead assignments"
        boolean hasAnno = false;
        boolean hasMeta = false;
        boolean hasModule = false;
        for (ExecutableElement method : ElementFilter.methodsIn(cd.getEnclosedElements())) {
            JRubyMethod anno = method.getAnnotation(JRubyMethod.class);
            if (anno == null) {
                continue;
            }
            hasAnno = true;
            hasMeta |= anno.meta();
            hasModule |= anno.module();
        }

        if (!hasAnno)
            return;

        //            mv.local(BASEMETHOD, "javaMethod", "Lorg/jruby/internal/runtime/methods/HandleMethod;");
        //            mv.local(MODULEMETHOD, "moduleMethod", "Lorg/jruby/internal/runtime/methods/HandleMethod;");
        //
        //            mv.local(RUNTIME, "runtime", RUBY_TYPE);
        mv.aload(RUBYMODULE);
        mv.invokevirtual("org/jruby/RubyModule", "getRuntime", "()Lorg/jruby/Ruby;");
        mv.astore(RUNTIME);

        if (hasMeta || hasModule) {
            //                mv.local(SINGLETONCLASS, "singletonClass", RUBYMODULE_TYPE);
            mv.aload(1);
            mv.invokevirtual("org/jruby/RubyModule", "getSingletonClass", "()Lorg/jruby/RubyClass;");
            mv.astore(SINGLETONCLASS);
        }

        Map<CharSequence, List<ExecutableElement>> annotatedMethods = new HashMap<>();
        Map<CharSequence, List<ExecutableElement>> staticAnnotatedMethods = new HashMap<>();

        Set<String> frameAwareMethods = null; // lazy init - there's usually none
        Set<String> scopeAwareMethods = null; // lazy init - there's usually none

        int methodCount = 0;
        for (ExecutableElement method : ElementFilter.methodsIn(cd.getEnclosedElements())) {
            JRubyMethod anno = method.getAnnotation(JRubyMethod.class);
            if (anno == null)
                continue;

            methodCount++;

            // warn if the method raises any exceptions (JRUBY-4494)
            if (method.getThrownTypes().size() != 0) {
                System.err.print(
                        "Method " + cd.toString() + "." + method.toString() + " should not throw exceptions: ");
                boolean comma = false;
                for (TypeMirror thrownType : method.getThrownTypes()) {
                    if (comma)
                        System.err.print(", ");
                    System.err.print(thrownType);
                    comma = true;
                }
                System.err.print("\n");
            }

            final String[] names = anno.name();
            CharSequence name = names.length == 0 ? method.getSimpleName() : names[0];

            final Map<CharSequence, List<ExecutableElement>> methodsHash;
            if (method.getModifiers().contains(Modifier.STATIC)) {
                methodsHash = staticAnnotatedMethods;
            } else {
                methodsHash = annotatedMethods;
            }

            List<ExecutableElement> methodDescs = methodsHash.get(name);
            if (methodDescs == null) {
                methodsHash.put(name, methodDescs = new ArrayList<>(4));
            }

            methodDescs.add(method);

            // check for caller frame field reads or writes
            boolean frame = false;
            boolean scope = false;
            for (FrameField field : anno.reads()) {
                frame |= field.needsFrame();
                scope |= field.needsScope();
            }
            for (FrameField field : anno.writes()) {
                frame |= field.needsFrame();
                scope |= field.needsScope();
            }

            if (frame) {
                if (frameAwareMethods == null)
                    frameAwareMethods = new HashSet<>(4, 1);
                AnnotationHelper.addMethodNamesToSet(frameAwareMethods, method.getSimpleName().toString(),
                        names, anno.alias());
            }
            if (scope) {
                if (scopeAwareMethods == null)
                    scopeAwareMethods = new HashSet<>(4, 1);
                AnnotationHelper.addMethodNamesToSet(scopeAwareMethods, method.getSimpleName().toString(),
                        names, anno.alias());
            }
        }

        if (methodCount == 0) {
            // no annotated methods found, skip
            return;
        }

        classNames.add(getActualQualifiedName(cd));

        processMethodDeclarations(staticAnnotatedMethods);
        for (Map.Entry<CharSequence, List<ExecutableElement>> entry : staticAnnotatedMethods.entrySet()) {
            ExecutableElement decl = entry.getValue().get(0);
            if (!decl.getAnnotation(JRubyMethod.class).omit())
                addCoreMethodMapping(entry.getKey(), decl);
        }

        processMethodDeclarations(annotatedMethods);
        for (Map.Entry<CharSequence, List<ExecutableElement>> entry : annotatedMethods.entrySet()) {
            ExecutableElement decl = entry.getValue().get(0);
            if (!decl.getAnnotation(JRubyMethod.class).omit())
                addCoreMethodMapping(entry.getKey(), decl);
        }

        mv.voidreturn();
        mv.end();

        // write out a static initializer for frame names, so it only fires once
        mv = new SkinnyMethodAdapter(cw, ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);

        mv.start();

        if (frameAwareMethods != null && !frameAwareMethods.isEmpty()) {
            mv.ldc(frameAwareMethods.size());
            mv.anewarray("java/lang/String");
            int index = 0;
            for (CharSequence name : frameAwareMethods) {
                mv.dup();
                mv.ldc(index++);
                mv.ldc(name);
                mv.aastore();
            }
            mv.invokestatic("org/jruby/runtime/MethodIndex", "addFrameAwareMethods", "([Ljava/lang/String;)V");
        }
        if (scopeAwareMethods != null && !scopeAwareMethods.isEmpty()) {
            mv.ldc(frameAwareMethods.size());
            mv.anewarray("java/lang/String");
            int index = 0;
            for (CharSequence name : scopeAwareMethods) {
                mv.dup();
                mv.ldc(index++);
                mv.ldc(name);
                mv.aastore();
            }
            mv.invokestatic("org/jruby/runtime/MethodIndex", "addScopeAwareMethods", "([Ljava/lang/String;)V");
        }

        mv.voidreturn();
        mv.end();

        cw.visitEnd();

        new File(SRC_GEN_DIR).mkdirs();
        FileOutputStream fos = new FileOutputStream(SRC_GEN_DIR + qualifiedName + POPULATOR_SUFFIX + ".class");
        fos.write(cw.toByteArray());
        fos.close();
    } catch (IOException ex) {
        ex.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:org.jruby.RubyInstanceConfig.java

License:LGPL

private static int initGlobalJavaVersion() {
    String specVersion = specVersion = Options.BYTECODE_VERSION.load();

    // stack map calculation is failing for some compilation scenarios, so
    // forcing both 1.5 and 1.6 to use 1.5 bytecode for the moment.
    if (specVersion.equals("1.5")) {// || specVersion.equals("1.6")) {
        return Opcodes.V1_5;
    } else if (specVersion.equals("1.6")) {
        return Opcodes.V1_6;
    } else if (specVersion.equals("1.7") || specVersion.equals("1.8")) {
        return Opcodes.V1_7;
    } else {//from   w w w.  jav a  2 s  . com
        throw new RuntimeException("unsupported Java version: " + specVersion);
    }
}

From source file:org.jruby.runtime.opto.OptoFactory.java

License:LGPL

public static Invalidator newConstantInvalidator() {
    if (RubyInstanceConfig.JAVA_VERSION == Opcodes.V1_7 && RubyInstanceConfig.INVOKEDYNAMIC_CONSTANTS) {
        try {/* w w w.java  2  s  . co m*/
            return (Invalidator) Class.forName("org.jruby.runtime.opto.SwitchPointInvalidator").newInstance();
        } catch (Throwable t) {
            // ignore
        }
    }
    return new ObjectIdentityInvalidator();
}

From source file:org.jruby.runtime.opto.OptoFactory.java

License:LGPL

public static Invalidator newMethodInvalidator(RubyModule module) {
    if (RubyInstanceConfig.JAVA_VERSION == Opcodes.V1_7
            && RubyInstanceConfig.INVOKEDYNAMIC_INVOCATION_SWITCHPOINT) {
        try {/* w w w  .j  a v  a 2  s . c  om*/
            return (Invalidator) Class.forName("org.jruby.runtime.opto.GenerationAndSwitchPointInvalidator")
                    .getConstructor(RubyModule.class).newInstance(module);
        } catch (Throwable t) {
            // ignore
        }
    }
    return new GenerationInvalidator(module);
}

From source file:org.netbeans.html.boot.impl.FnUtils.java

License:Open Source License

/** Seeks for {@link JavaScriptBody} and {@link JavaScriptResource} annotations
 * in the bytecode and converts them into real code. Used by Maven plugin
 * postprocessing classes.//  w  ww. j av  a 2 s. c  o  m
 * 
 * @param bytecode the original bytecode with javascript specific annotations
 * @param loader the loader to load resources (scripts and classes) when needed
 * @return the transformed bytecode
 * @since 0.7
 */
public static byte[] transform(byte[] bytecode, ClassLoader loader) {
    ClassReader cr = new ClassReader(bytecode) {
        // to allow us to compile with -profile compact1 on 
        // JDK8 while processing the class as JDK7, the highest
        // class format asm 4.1 understands to
        @Override
        public short readShort(int index) {
            short s = super.readShort(index);
            if (index == 6 && s > Opcodes.V1_7) {
                return Opcodes.V1_7;
            }
            return s;
        }
    };
    FindInClass tst = new FindInClass(loader, null);
    cr.accept(tst, 0);
    if (tst.found > 0) {
        ClassWriter w = new ClassWriterEx(loader, cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
        FindInClass fic = new FindInClass(loader, w);
        cr.accept(fic, 0);
        bytecode = w.toByteArray();
    }
    return bytecode;
}

From source file:se.eris.asm.AsmUtilsTest.java

License:Apache License

@Test
void asmOpcodeToJavaVersion() {
    assertEquals(1, AsmUtils.asmOpcodeToJavaVersion(Opcodes.V1_1));
    assertEquals(5, AsmUtils.asmOpcodeToJavaVersion(Opcodes.V1_5));
    assertEquals(6, AsmUtils.asmOpcodeToJavaVersion(Opcodes.V1_6));
    assertEquals(7, AsmUtils.asmOpcodeToJavaVersion(Opcodes.V1_7));
    assertEquals(8, AsmUtils.asmOpcodeToJavaVersion(Opcodes.V1_8));

    assertEquals(9, AsmUtils.asmOpcodeToJavaVersion(Opcodes.V9));
    assertEquals(10, AsmUtils.asmOpcodeToJavaVersion(Opcodes.V10));
    assertEquals(11, AsmUtils.asmOpcodeToJavaVersion(Opcodes.V11));
}

From source file:uk.co.unclealex.executable.generator.jar.JarServiceImplTest.java

License:Apache License

protected byte[] generateClass(String className) {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;/*from w w w.  j av  a2  s.c om*/

    cw.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, className.replace('.', '/'), null,
            "java/lang/Object", null);

    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "execute", "()Ljava/lang/String;", null, null);
    mv.visitCode();
    mv.visitLdcInsn(className);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    cw.visitEnd();

    return cw.toByteArray();

}