Example usage for org.objectweb.asm Opcodes ACC_SYNTHETIC

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

Introduction

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

Prototype

int ACC_SYNTHETIC

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

Click Source Link

Usage

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

License:Open Source License

public static boolean isSynthetic(int access) {
    return isSet(access, Opcodes.ACC_SYNTHETIC);
}

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

License:Open Source License

private static byte[] convertClass(ZipFile file, ZipEntry entry) throws IOException {
    try (InputStream content = file.getInputStream(entry)) {
        ClassReader reader = new ClassReader(content);
        ClassWriter writer = new ClassWriter(0);
        ClassVisitor converter = new ClassVisitor(Opcodes.ASM5, writer) {
            @Override/*from  w w w  . ja  v a 2  s. co m*/
            public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                    String[] exceptions) {
                if (name.startsWith("lambda$") && (access & Opcodes.ACC_SYNTHETIC) == 0) {
                    access |= Opcodes.ACC_SYNTHETIC;
                }
                return super.visitMethod(access, name, desc, signature, exceptions);
            }
        };
        reader.accept(converter, 0);
        return writer.toByteArray();
    }
}

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());/* w w w . 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.InterfaceDesugaring.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor result;/*from  w w  w .j  a  v a 2s  .co m*/
    if (BitFlags.isSet(accessFlags, Opcodes.ACC_INTERFACE)
            && BitFlags.noneSet(access, Opcodes.ACC_ABSTRACT | Opcodes.ACC_BRIDGE)
            && !"<clinit>".equals(name)) {
        checkArgument(BitFlags.noneSet(access, Opcodes.ACC_NATIVE), "Forbidden per JLS ch 9.4");

        boolean isLambdaBody = name.startsWith("lambda$") && BitFlags.isSet(access, Opcodes.ACC_SYNTHETIC);
        if (isLambdaBody) {
            access &= ~Opcodes.ACC_PUBLIC; // undo visibility change from LambdaDesugaring
            // Rename lambda method to reflect the new owner.  Not doing so confuses LambdaDesugaring
            // if it's run over this class again.
            name += COMPANION_SUFFIX;
        }
        if (BitFlags.isSet(access, Opcodes.ACC_STATIC)) {
            // Completely move static interface methods, which requires rewriting call sites
            result = companion().visitMethod(access & ~Opcodes.ACC_PRIVATE, name, desc, signature, exceptions);
        } else {
            MethodVisitor abstractDest;
            if (isLambdaBody) {
                // Completely move lambda bodies, which requires rewriting call sites
                access &= ~Opcodes.ACC_PRIVATE;
                abstractDest = null;
            } else {
                // Make default methods abstract but move their implementation into a static method with
                // corresponding signature.  Doesn't require callsite rewriting but implementing classes
                // may need to implement default methods explicitly.
                checkArgument(BitFlags.noneSet(access, Opcodes.ACC_PRIVATE),
                        "Unexpected private interface method %s.%s : %s", name, internalName, desc);
                abstractDest = super.visitMethod(access | Opcodes.ACC_ABSTRACT, name, desc, signature,
                        exceptions);
            }

            // TODO(b/37110951): adjust signature with explicit receiver type, which may be generic
            MethodVisitor codeDest = companion().visitMethod(access | Opcodes.ACC_STATIC, name,
                    companionDefaultMethodDescriptor(internalName, desc), (String) null, // drop signature, since given one doesn't include the new param
                    exceptions);

            result = abstractDest != null ? new MultiplexAnnotations(codeDest, abstractDest) : codeDest;
        }
    } else {
        result = super.visitMethod(access, name, desc, signature, exceptions);
    }
    return result != null ? new InterfaceInvocationRewriter(result) : null;
}

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

License:Open Source License

private ClassVisitor companion() {
    if (companion == null) {
        checkState(BitFlags.isSet(accessFlags, Opcodes.ACC_INTERFACE));
        String companionName = internalName + COMPANION_SUFFIX;

        companion = store.add(companionName);
        companion.visit(bytecodeVersion,
                // Companion class must be public so moved methods can be called from anywhere
                (accessFlags | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PUBLIC) & ~Opcodes.ACC_INTERFACE,
                companionName, (String) null, // signature
                "java/lang/Object", new String[0]);
    }// w w w  . java 2  s . c o m
    return companion;
}

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

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    // Remove bridge default methods in interfaces; javac generates them again for implementing
    // classes anyways.
    if (isInterface && (access
            & (Opcodes.ACC_BRIDGE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_STATIC)) == Opcodes.ACC_BRIDGE) {
        return null;
    }/*from  w w  w. ja  v  a2 s  . c  o m*/
    if (isInterface && "$jacocoInit".equals(name)
            && BitFlags.isSet(access, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_STATIC)) {
        // Drop static interface method that Jacoco generates--we'll inline it into the static
        // initializer instead
        return null;
    }
    // TODO(b/31547323): Avoid stack trace and report errors more user-friendly
    checkArgument(!isInterface || BitFlags.isSet(access, Opcodes.ACC_ABSTRACT) || "<clinit>".equals(name),
            "Interface %s defines non-abstract method %s%s, which is not supported", internalName, name, desc);
    MethodVisitor result = super.visitMethod(access, name, desc, signature, exceptions);
    return (isInterface && "<clinit>".equals(name)) ? new InlineJacocoInit(result) : result;
}

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

License:Open Source License

@Override
public void visitEnd() {
    for (Map.Entry<Handle, MethodReferenceBridgeInfo> bridge : bridgeMethods.entrySet()) {
        Handle original = bridge.getKey();
        Handle neededMethod = bridge.getValue().bridgeMethod();
        checkState(/*from   ww  w  . ja  v a 2  s. co m*/
                neededMethod.getTag() == Opcodes.H_INVOKESTATIC
                        || neededMethod.getTag() == Opcodes.H_INVOKEVIRTUAL,
                "Cannot generate bridge method %s to reach %s", neededMethod, original);
        checkState(bridge.getValue().referenced() != null, "Need referenced method %s to generate bridge %s",
                original, neededMethod);

        int access = Opcodes.ACC_BRIDGE | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_FINAL;
        if (neededMethod.getTag() == Opcodes.H_INVOKESTATIC) {
            access |= Opcodes.ACC_STATIC;
        }
        MethodVisitor bridgeMethod = super.visitMethod(access, neededMethod.getName(), neededMethod.getDesc(),
                (String) null, toInternalNames(bridge.getValue().referenced().getExceptionTypes()));

        // Bridge is a factory method calling a constructor
        if (original.getTag() == Opcodes.H_NEWINVOKESPECIAL) {
            bridgeMethod.visitTypeInsn(Opcodes.NEW, original.getOwner());
            bridgeMethod.visitInsn(Opcodes.DUP);
        }

        int slot = 0;
        if (neededMethod.getTag() != Opcodes.H_INVOKESTATIC) {
            bridgeMethod.visitVarInsn(Opcodes.ALOAD, slot++);
        }
        Type neededType = Type.getMethodType(neededMethod.getDesc());
        for (Type arg : neededType.getArgumentTypes()) {
            bridgeMethod.visitVarInsn(arg.getOpcode(Opcodes.ILOAD), slot);
            slot += arg.getSize();
        }
        bridgeMethod.visitMethodInsn(invokeOpcode(original), original.getOwner(), original.getName(),
                original.getDesc(), original.isInterface());
        bridgeMethod.visitInsn(neededType.getReturnType().getOpcode(Opcodes.IRETURN));

        bridgeMethod.visitMaxs(0, 0); // rely on class writer to compute these
        bridgeMethod.visitEnd();
    }
    super.visitEnd();
}

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

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (name.equals("$deserializeLambda$") && BitFlags.isSet(access, Opcodes.ACC_SYNTHETIC)) {
        // Android doesn't do anything special for lambda serialization so drop the special
        // deserialization hook that javac generates.  This also makes sure we don't reference
        // java/lang/invoke/SerializedLambda, which doesn't exist on Android.
        return null;
    }// w ww. java  2  s  .co  m
    if (name.startsWith("lambda$") && BitFlags.isSet(access, Opcodes.ACC_SYNTHETIC)) {
        if (!allowDefaultMethods && isInterface && BitFlags.isSet(access, Opcodes.ACC_STATIC)) {
            // There must be a lambda in the interface (which in the absence of hand-written default or
            // static interface methods must mean it's in the <clinit> method or inside another lambda).
            // We'll move this method out of this class, so just record and drop it here.
            // (Note lambda body methods have unique names, so we don't need to remember desc here.)
            aggregateInterfaceLambdaMethods.add(internalName + '#' + name);
            return null;
        }
        if (BitFlags.isSet(access, Opcodes.ACC_PRIVATE)) {
            // Make lambda body method accessible from lambda class
            access &= ~Opcodes.ACC_PRIVATE;
            if (allowDefaultMethods && isInterface) {
                // java 8 requires interface methods to have exactly one of ACC_PUBLIC and ACC_PRIVATE
                access |= Opcodes.ACC_PUBLIC;
            } else {
                // Method was private so it can be final, which should help VMs perform dispatch.
                access |= Opcodes.ACC_FINAL;
            }
        }
        // Guarantee unique lambda body method name to avoid accidental overriding. This wouldn't be
        // be necessary for static methods but in visitOuterClass we don't know whether a potential
        // outer lambda$ method is static or not, so we just always do it.
        name = uniqueInPackage(internalName, name);
    }
    MethodVisitor dest = super.visitMethod(access, name, desc, signature, exceptions);
    return dest != null ? new InvokedynamicRewriter(dest, access, name, desc, signature, exceptions) : null;
}

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

License:Apache License

/**
 * Selects which class to map for coverage. At runtime mapped methods of 
 * mapped classes will have a position in the bitmask used for method 
 * coverage.//  w  w  w.  j  a v  a2 s . c o  m
 * 
 * <p> If the current classname points to an interface it has to be skipped 
 * because interfaces are not marked for coverage. 
 * 
 * <p> If the current class is synthetic it also has to be discarded because
 * synthetic classes do not correspond to any source class and they would 
 * be a useless overhead.  
 * 
 * @return <code>true</code> if the class has to be mapped for coverage.
 */
protected boolean isIncluded() {
    if ((classOpcode & Opcodes.ACC_INTERFACE) > 0 || (classOpcode & Opcodes.ACC_SYNTHETIC) > 0
            || isAutoGeneratedClass) {
        return false;
    }

    return shouldInstrumentClass;
}

From source file:com.google.gwt.dev.javac.asm.CollectClassData.java

License:Apache License

/**
 * Called for each field./*from   w w w.jav a2s . c o m*/
 *
 * @param access access flags for field
 * @param name field name
 * @param desc type descriptor (ie, Ljava/lang/String;)
 * @param signature generic signature (null if not generic)
 * @param value initialized value if constant
 */
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
    if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
        // if ("this$1".equals(name) && classType == ClassType.Anonymous) {
        // // TODO(jat): !!! really nasty hack
        // classType = ClassType.Inner;
        // }
        // skip synthetic fields
        return null;
    }
    CollectFieldData fv = new CollectFieldData(access, name, desc, signature, value);
    fields.add(fv);
    return fv;
}