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:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterServices.java

License:Open Source License

private static MethodHandle createNoPermissionsInvoker() {
    final String className = "NoPermissionsInvoker";

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_7, ACC_PUBLIC | ACC_SUPER | ACC_FINAL, className, null, "java/lang/Object", null);
    final Type objectType = Type.getType(Object.class);
    final Type methodHandleType = Type.getType(MethodHandle.class);
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "invoke",
            Type.getMethodDescriptor(Type.VOID_TYPE, methodHandleType, objectType), null, null));
    mv.visitCode();/*from  w  ww  .j  a  v a  2s.c om*/
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.invokevirtual(methodHandleType.getInternalName(), "invokeExact",
            Type.getMethodDescriptor(Type.VOID_TYPE, objectType), false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] bytes = cw.toByteArray();

    final ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
        @Override
        public ClassLoader run() {
            return new SecureClassLoader(null) {
                @Override
                protected Class<?> findClass(final String name) throws ClassNotFoundException {
                    if (name.equals(className)) {
                        return defineClass(name, bytes, 0, bytes.length, new ProtectionDomain(
                                new CodeSource(null, (CodeSigner[]) null), new Permissions()));
                    }
                    throw new ClassNotFoundException(name);
                }
            };
        }
    });

    try {
        return MethodHandles.lookup().findStatic(Class.forName(className, true, loader), "invoke",
                MethodType.methodType(void.class, MethodHandle.class, Object.class));
    } catch (final ReflectiveOperationException e) {
        throw new AssertionError(e.getMessage(), e);
    }
}

From source file:com.github.fge.grappa.transform.generate.ClassNodeInitializer.java

License:Open Source License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {/*  w  w  w .  ja  v a 2  s . c  o  m*/
    if (ownerClass == classNode.getParentClass()) {
        if ((access & ACC_PRIVATE) != 0)
            throw new InvalidGrammarException("a parser class cannot be " + "private");
        if ((access & ACC_FINAL) != 0)
            throw new InvalidGrammarException("a parser class cannot be " + "final");
        String className = getExtendedParserClassName(name);
        classNode.visit(Opcodes.V1_7, ACC_PUBLIC, className, null, classNode.getParentType().getInternalName(),
                null);
    }
}

From source file:com.github.fge.grappa.transform.process.GroupClassGenerator.java

License:Apache License

private void generateClassBasics(InstructionGroup group, ClassWriter cw) {
    cw.visit(Opcodes.V1_7, ACC_PUBLIC + ACC_FINAL + ACC_SYNTHETIC, group.getGroupClassType().getInternalName(),
            null, getBaseType().getInternalName(), null);
    cw.visitSource(classNode.sourceFile, null);
}

From source file:com.google.devtools.build.android.desugar.CoreLibrarySupport.java

License:Open Source License

public void makeDispatchHelpers(GeneratedClassStore store) {
    HashMap<Class<?>, ClassVisitor> dispatchHelpers = new HashMap<>();
    for (Collection<EmulatedMethod> group : emulatedDefaultMethods.asMap().values()) {
        checkState(!group.isEmpty());/*from  w  ww . j a  v  a  2  s  .c  om*/
        Class<?> root = group.stream().map(EmulatedMethod::owner)
                .max(DefaultMethodClassFixer.SubtypeComparator.INSTANCE).get();
        checkState(group.stream().map(m -> m.owner()).allMatch(o -> root.isAssignableFrom(o)),
                "Not a single unique method: %s", group);
        String methodName = group.stream().findAny().get().name();

        ImmutableList<Class<?>> customOverrides = findCustomOverrides(root, methodName);

        for (EmulatedMethod methodDefinition : group) {
            Class<?> owner = methodDefinition.owner();
            ClassVisitor dispatchHelper = dispatchHelpers.computeIfAbsent(owner, clazz -> {
                String className = clazz.getName().replace('.', '/') + "$$Dispatch";
                ClassVisitor result = store.add(className);
                result.visit(Opcodes.V1_7,
                        // Must be public so dispatch methods can be called from anywhere
                        Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PUBLIC, className, /*signature=*/ null,
                        "java/lang/Object", EMPTY_LIST);
                return result;
            });

            // Types to check for before calling methodDefinition's companion, sub- before super-types
            ImmutableList<Class<?>> typechecks = concat(group.stream().map(EmulatedMethod::owner),
                    customOverrides.stream()).filter(o -> o != owner && owner.isAssignableFrom(o)).distinct() // should already be but just in case
                            .sorted(DefaultMethodClassFixer.SubtypeComparator.INSTANCE)
                            .collect(ImmutableList.toImmutableList());
            makeDispatchHelperMethod(dispatchHelper, methodDefinition, typechecks);
        }
    }
}

From source file:com.google.devtools.build.android.desugar.Java7Compatibility.java

License:Open Source License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {/*  w  ww  .  j  a v  a  2  s  .  co m*/
    internalName = name;
    isInterface = BitFlags.isSet(access, Opcodes.ACC_INTERFACE);
    super.visit(Math.min(version, Opcodes.V1_7), access, name, signature, superName, interfaces);
}

From source file:com.google.devtools.build.android.desugar.Java7CompatibilityTest.java

License:Open Source License

@Test
public void testJava7CompatibleInterface() throws Exception {
    ClassReader reader = new ClassReader(ExtendsDefault.class.getName());
    ClassTester tester = new ClassTester();
    reader.accept(new Java7Compatibility(tester, null, null), 0);
    assertThat(tester.version).isEqualTo(Opcodes.V1_7);
    assertThat(tester.bridgeMethods).isEqualTo(0); // make sure we strip bridge methods
    assertThat(tester.clinitMethods).isEqualTo(1); // make sure we don't strip <clinit>
}

From source file:com.google.devtools.build.android.desugar.Java7CompatibilityTest.java

License:Open Source License

/**
 * Tests that a class implementing interfaces with bridge methods redeclares those bridges.
 * This is behavior of javac that we rely on.
 *//* ww w.  j  a v  a 2  s.c o m*/
@Test
public void testConcreteClassRedeclaresBridges() throws Exception {
    ClassReader reader = new ClassReader(Impl.class.getName());
    ClassTester tester = new ClassTester();
    reader.accept(new Java7Compatibility(tester, null, null), 0);
    assertThat(tester.version).isEqualTo(Opcodes.V1_7);
    assertThat(tester.bridgeMethods).isEqualTo(2);
}

From source file:com.google.template.soy.jbcsrc.internal.SoyClassWriter.java

License:Apache License

private SoyClassWriter(Writer writer, Builder builder) {
    super(writer.api(), Flags.DEBUG ? new CheckClassAdapter(writer, false) : writer);
    this.writer = writer;
    this.typeInfo = builder.type;
    super.visit(Opcodes.V1_7, builder.access, builder.type.internalName(), null /* not generic */,
            builder.baseClass.internalName(),
            builder.interfaces.toArray(new String[builder.interfaces.size()]));
    if (builder.fileName != null) {
        super.visitSource(builder.fileName,
                // No JSR-45 style source maps, instead we write the line numbers in the normal locations.
                null);/*  w w w .  j  a  v  a  2s .c  o  m*/
    }
}

From source file:com.mogujie.instantrun.SuperHelperVisitor.java

License:Apache License

public void start() {
    visit(Opcodes.V1_7, ACC_PUBLIC + ACC_SUPER, visitor.visitedClassName + "$helper", null,
            visitor.visitedSuperName, null);
    for (int nodeIndex = 0; nodeIndex < superNode.methods.size(); nodeIndex++) {
        MethodNode methodNode = (MethodNode) superNode.methods.get(nodeIndex);
        if ("<init>".equals(methodNode.name)) {
            String[] exceptions = null;
            if (methodNode.exceptions != null) {
                exceptions = (String[]) methodNode.exceptions.toArray(new String[0]);
            }//from  www  .  j  a  v a 2s . c  om
            MethodVisitor mv = visitMethod(ACC_PUBLIC, methodNode.name, methodNode.desc, methodNode.signature,
                    exceptions);
            mv.visitCode();
            Type[] args = Type.getArgumentTypes(methodNode.desc);
            List<LocalVariable> variables = ByteCodeUtils.toLocalVariables(Arrays.asList(args));
            mv.visitVarInsn(ALOAD, 0);
            int local = 1;
            for (int i = 0; i < variables.size(); i++) {
                mv.visitVarInsn(variables.get(i).type.getOpcode(Opcodes.ILOAD), variables.get(i).var + 1);
                local = variables.get(i).var + 1 + variables.get(i).type.getSize();
            }
            mv.visitMethodInsn(INVOKESPECIAL, superNode.name, methodNode.name, methodNode.desc, false);
            mv.visitInsn(RETURN);
            mv.visitMaxs(local, local);
            mv.visitEnd();
        }
    }

    for (InstantMethod method : visitor.superMethods) {
        MethodVisitor mv = visitMethod(ACC_PUBLIC + ACC_STATIC, method.getName(), method.getDescriptor(), null,
                null);
        mv.visitCode();
        Type[] args = Type.getArgumentTypes(method.getDescriptor());

        List<LocalVariable> variables = ByteCodeUtils.toLocalVariables(Arrays.asList(args));
        int totSize = 1;
        for (LocalVariable variable : variables) {

            mv.visitVarInsn(variable.type.getOpcode(Opcodes.ILOAD), variable.var);
            totSize = variable.var;
        }

        mv.visitMethodInsn(INVOKESPECIAL, method.getOwner(), method.getName(), method.getOriDesc(), false);

        Type returnType = Type.getReturnType(method.getDescriptor());
        mv.visitInsn(returnType.getOpcode(Opcodes.IRETURN));
        mv.visitMaxs(totSize + 1, totSize + 1);
        mv.visitEnd();
    }

    visitEnd();
}

From source file:com.tencent.tinker.build.auxiliaryclass.AuxiliaryClassGenerator.java

License:Open Source License

private static void generateClass(String dotClassName, File fileOut) throws IOException {
    final String classDesc = dotClassName.replace('.', '/');
    ClassWriter cw = new ClassWriter(0);
    cw.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, classDesc, null, "java/lang/Object", null);
    cw.visitSource(fileOut.getName(), null);
    {// ww  w  . j  av  a  2s .  c  om
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();
    byte[] classBytes = cw.toByteArray();

    OutputStream os = null;
    try {
        os = new BufferedOutputStream(new FileOutputStream(fileOut));
        os.write(classBytes);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (Exception e) {
                // Ignored.
            }
        }
    }
}