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.android.build.gradle.internal2.incremental.IncrementalChangeVisitor.java

License:Apache License

/**
 * Turns this class into an override class that can be loaded by our custom class loader:
 *<ul>//  w w  w.j a v a 2  s  .  c om
 *   <li>Make the class name be OriginalName$override</li>
 *   <li>Ensure the class derives from java.lang.Object, no other inheritance</li>
 *   <li>Ensure the class has a public parameterless constructor that is a noop.</li>
 *</ul>
 */
@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {
    // TODO: modified by achellies, ?,??,?ClassClass,Opcodes.INVOKESPECIAL?super
    super.visit(version, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, name + OVERRIDE_SUFFIX, signature, superName, // TODO modified by achellies, "java/lang/Object" -> superName
            new String[] { CHANGE_TYPE.getInternalName() });

    if (DEBUG) {
        System.out.println(">>>>>>>> Processing " + name + "<<<<<<<<<<<<<");
    }

    visitedClassName = name;
    visitedSuperName = superName;
    instanceToStaticDescPrefix = "(L" + visitedClassName + ";";

    // Create empty constructor
    MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC, ByteCodeUtils.CONSTRUCTOR, "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, ByteCodeUtils.CONSTRUCTOR, "()V", // TODO modified by achellies, "java/lang/Object" -> superName
            false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    super.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_STATIC, "$obsolete", "Z", null,
            null);
}

From source file:com.android.build.gradle.internal2.incremental.IncrementalSupportVisitor.java

License:Apache License

/**
 * Ensures that the class contains a $change field used for referencing the IncrementalChange
 * dispatcher./*  w w  w  .j  a v a2s .  c o m*/
 *
 * <p>Also updates package_private visibility to public so we can call into this class from
 * outside the package.
 *
 * <p>All classes will have a serialVersionUID added (if one does not already exist), as
 * otherwise, serialVersionUID would be different for instrumented and non-instrumented classes.
 * We do this for all classes. Due to incremental changes, there could be a class that starts
 * implementing {@link java.io.Serializable}, thus making all of its subclasses serializable as
 * well. Those subclasses might be used for persistence, and we need to make sure their
 * serialVersionUID are stable across instant run and non-instant run builds.
 */
@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {
    visitedClassName = name;
    visitedSuperName = superName;

    // TODO: disabled by achellies
    //        addSerialUidIfMissing();
    super.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_VOLATILE | Opcodes.ACC_SYNTHETIC
            | Opcodes.ACC_TRANSIENT, "$change", getRuntimeTypeName(CHANGE_TYPE), null, null);
    access = transformClassAccessForInstantRun(access);
    super.visit(version, access, name, signature, superName, interfaces);
}

From source file:com.android.build.gradle.internal2.incremental.IncrementalSupportVisitor.java

License:Apache License

/***
 * Inserts a trampoline to this class so that the updated methods can make calls to super
 * class methods./* w w  w. j a v a2  s  .  com*/
 * <p>
 * Pseudo code for this trampoline:
 * <code>
 *   Object access$super($classType instance, String name, object[] args) {
 *      switch(name) {
 *          case "firstMethod.(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;":
 *            return super~instance.firstMethod((String)arg[0], arg[1]);
 *          case "secondMethod.(Ljava/lang/String;I)V":
 *            return super~instance.firstMethod((String)arg[0], arg[1]);
 *
 *          default:
 *            StringBuilder $local1 = new StringBuilder();
 *            $local1.append("Method not found ");
 *            $local1.append(name);
 *            $local1.append(" in " $classType $super implementation");
 *            throw new $package/InstantReloadException($local1.toString());
 *      }
 * </code>
 */
private void createAccessSuper() {
    int access = Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_VARARGS;
    Method m = new Method("access$super",
            "(L" + visitedClassName + ";Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;");
    MethodVisitor visitor = super.visitMethod(access, m.getName(), m.getDescriptor(), null, null);

    final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor);

    // Gather all methods from itself and its superclasses to generate a giant access$super
    // implementation.
    // This will work fine as long as we don't support adding methods to a class.
    final Map<String, MethodReference> uniqueMethods = new HashMap<>();
    if (parentNodes.isEmpty()) {
        // if we cannot determine the parents for this class, let's blindly add all the
        // method of the current class as a gateway to a possible parent version.
        addAllNewMethods(classNode, classNode, uniqueMethods);
    } else {
        // otherwise, use the parent list.
        for (ClassNode parentNode : parentNodes) {
            addAllNewMethods(classNode, parentNode, uniqueMethods);
        }
    }

    new StringSwitch() {
        @Override
        void visitString() {
            mv.visitVarInsn(Opcodes.ALOAD, 1);
        }

        @Override
        void visitCase(String methodName) {
            MethodReference methodRef = uniqueMethods.get(methodName);

            mv.visitVarInsn(Opcodes.ALOAD, 0);

            Type[] args = Type.getArgumentTypes(methodRef.method.desc);
            int argc = 0;
            for (Type t : args) {
                mv.visitVarInsn(Opcodes.ALOAD, 2);
                mv.push(argc);
                mv.visitInsn(Opcodes.AALOAD);
                ByteCodeUtils.unbox(mv, t);
                argc++;
            }

            if (TRACING_ENABLED) {
                trace(mv, "super selected ", methodRef.owner.name, methodRef.method.name,
                        methodRef.method.desc);
            }
            String parentName = findParentClassForMethod(methodRef);
            LOG.verbose("Generating access$super for " + methodRef.method.name + " recv " + parentName);

            // Call super on the other object, yup this works cos we are on the right place to
            // call from.
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, parentName, methodRef.method.name, methodRef.method.desc,
                    false);

            Type ret = Type.getReturnType(methodRef.method.desc);
            if (ret.getSort() == Type.VOID) {
                mv.visitInsn(Opcodes.ACONST_NULL);
            } else {
                mv.box(ret);
            }
            mv.visitInsn(Opcodes.ARETURN);
        }

        @Override
        void visitDefault() {
            writeMissingMessageWithHash(mv, visitedClassName);
        }
    }.visit(mv, uniqueMethods.keySet());

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:com.android.mkstubs.sourcer.ClassSourcer.java

License:Apache License

@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
    // skip synthetic fields
    if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
        return null;
    }/*from ww w .  jav  a 2 s .  co m*/

    return new FieldSourcer(mOutput, access, name, desc, signature);
}

From source file:com.android.tools.klint.detector.api.LintUtils.java

License:Apache License

/**
 * Returns true if the given class node represents a static inner class.
 *
 * @param classNode the inner class to be checked
 * @return true if the class node represents an inner class that is static
 *//*from  w  w w .ja  v  a 2s .  co m*/
public static boolean isStaticInnerClass(@NonNull ClassNode classNode) {
    // Note: We can't just filter out static inner classes like this:
    //     (classNode.access & Opcodes.ACC_STATIC) != 0
    // because the static flag only appears on methods and fields in the class
    // file. Instead, look for the synthetic this pointer.

    @SuppressWarnings("rawtypes") // ASM API
    List fieldList = classNode.fields;
    for (Object f : fieldList) {
        FieldNode field = (FieldNode) f;
        if (field.name.startsWith("this$") && (field.access & Opcodes.ACC_SYNTHETIC) != 0) {
            return false;
        }
    }

    return true;
}

From source file:com.changingbits.Builder.java

License:Apache License

/** Build a {@link LongRangeMultiSet} implementation to
 *  lookup intervals for a given point.//from   ww w  .  java 2s  .  c o  m
 *
 *  @param useAsm If true, the tree will be compiled to
 *  java bytecodes using the {@code asm} library; typically
 *  this results in a faster (~3X) implementation. */
public LongRangeMultiSet getMultiSet(boolean useAsm, boolean useArrayImpl) {

    finish(useArrayImpl);

    if (useAsm) {
        StringBuilder sb = new StringBuilder();
        sb.append('\n');
        int count = 0;
        for (LongRange range : ranges) {
            sb.append("// range ");
            sb.append(count++);
            sb.append(": ");
            sb.append(range);
            sb.append('\n');
        }
        sb.append('\n');
        sb.append("int upto = 0;\n");
        buildJavaSource(root, 0, sb);
        String javaSource = sb.toString();
        //System.out.println("java: " + javaSource);

        ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
        classWriter.visit(Opcodes.V1_7,
                Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC,
                COMPILED_TREE_CLASS.replace('.', '/'), null, LONG_RANGE_MULTI_SET_TYPE.getInternalName(), null);
        classWriter.visitSource(javaSource, null);

        Method m = Method.getMethod("void <init> ()");
        GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, m, null,
                null, classWriter);
        constructor.loadThis();
        constructor.loadArgs();
        constructor.invokeConstructor(LONG_RANGE_MULTI_SET_TYPE, m);
        constructor.returnValue();
        constructor.endMethod();

        GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, LOOKUP_METHOD,
                null, null, classWriter);
        //Label labelTop = new Label();
        //Label labelEnd = new Label();
        //gen.visitLabel(labelTop);
        int uptoLocal = gen.newLocal(Type.INT_TYPE);
        //System.out.println("uptoLocal=" + uptoLocal);
        // nocommit is this not needed!?
        //gen.visitLocalVariable("upto", "I", null, labelTop, labelEnd, uptoLocal);
        gen.push(0);
        gen.storeLocal(uptoLocal, Type.INT_TYPE);
        buildAsm(gen, root, uptoLocal);
        // Return upto:
        gen.loadLocal(uptoLocal, Type.INT_TYPE);
        gen.returnValue();
        //gen.visitLabel(labelEnd);
        gen.endMethod();
        classWriter.visitEnd();

        byte[] bytes = classWriter.toByteArray();

        // javap -c /x/tmp/my.class
        /*
        try {
          FileOutputStream fos = new FileOutputStream(new File("/x/tmp/my.class"));
          fos.write(bytes);
          fos.close();
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
        */

        // nocommit allow changing the class loader
        Class<? extends LongRangeMultiSet> treeClass = new Loader(LongRangeMultiSet.class.getClassLoader())
                .define(COMPILED_TREE_CLASS, classWriter.toByteArray());
        try {
            return treeClass.getConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException
                | InvocationTargetException e) {
            throw new RuntimeException(e);
        }

    } else if (useArrayImpl) {
        return new ArrayLongRangeMultiSet(root);
    } else {
        return new SimpleLongRangeMultiSet(root);
    }
}

From source file:com.changingbits.Builder.java

License:Apache License

public LongRangeCounter getCounter(boolean useAsm) {
    finish(false);/*from   w  w w .j  av  a2 s .  c  o m*/
    if (useAsm) {
        StringBuilder sb = new StringBuilder();
        sb.append('\n');
        int count = 0;
        for (LongRange range : ranges) {
            sb.append("// range ");
            sb.append(count++);
            sb.append(": ");
            sb.append(range);
            sb.append('\n');
        }
        sb.append('\n');
        buildJavaCounterSource(root, 0, sb, false);
        String javaSource = sb.toString();
        //System.out.println("javaSource:\n" + javaSource);

        ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
        classWriter.visit(Opcodes.V1_7,
                Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC,
                COMPILED_COUNTER_CLASS.replace('.', '/'), null, BASE_LONG_RANGE_COUNTER_TYPE.getInternalName(),
                null);
        classWriter.visitSource(javaSource, null);
        Method m = Method.getMethod("void <init> (com.changingbits.Node, int, int)");
        GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, m, null,
                null, classWriter);
        constructor.loadThis();
        constructor.loadArgs();
        constructor.invokeConstructor(Type.getType(BaseLongRangeCounter.class), m);
        constructor.returnValue();
        constructor.endMethod();

        GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, ADD_METHOD,
                null, null, classWriter);
        buildCounterAsm(gen, root, false);
        gen.returnValue();
        gen.endMethod();
        classWriter.visitEnd();

        byte[] bytes = classWriter.toByteArray();

        // javap -c /x/tmp/my.class
        /*
        try {
          FileOutputStream fos = new FileOutputStream(new File("/x/tmp/counter.class"));
          fos.write(bytes);
          fos.close();
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
        */

        // nocommit allow changing the class loader
        Class<? extends LongRangeCounter> cl = new CounterLoader(LongRangeCounter.class.getClassLoader())
                .define(COMPILED_COUNTER_CLASS, classWriter.toByteArray());
        try {
            return cl.getConstructor(Node.class, int.class, int.class).newInstance(root,
                    elementaryIntervals.size(), ranges.length);
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException
                | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    } else {
        return new SimpleLongRangeCounter(root, elementaryIntervals, ranges.length);
    }
}

From source file:com.changingbits.Builder.java

License:Apache License

public LongRangeCounter getCounter2() {
    finish(false);/* ww w  .  j a  va 2  s  .  co  m*/

    // Maps each range to the leaf counts that contribute to it:
    Map<Integer, List<Integer>> rangeToLeaf = new HashMap<>();
    buildRangeToLeaf(root, new ArrayList<Integer>(), rangeToLeaf);

    StringBuilder sb = new StringBuilder();
    sb.append('\n');
    sb.append("public void add(long v) {\n");
    int count = 0;
    for (LongRange range : ranges) {
        sb.append("  // range ");
        sb.append(count++);
        sb.append(": ");
        sb.append(range);
        sb.append('\n');
    }

    buildJavaCounter2Source(root, 1, sb, false);

    sb.append("}\n\n");
    sb.append("public int[] getCounts() {\n");
    sb.append("  int[] counts = new int[");
    sb.append(ranges.length);
    sb.append("];\n");
    for (int range = 0; range < ranges.length; range++) {
        List<Integer> elements = rangeToLeaf.get(range);
        if (elements != null) {
            sb.append("  counts[");
            sb.append(range);
            sb.append("] = count");
            sb.append(elements.get(0));

            for (int i = 1; i < elements.size(); i++) {
                sb.append(" + count");
                sb.append(elements.get(i));
            }
            sb.append(";\n");
        }
    }
    sb.append("  return counts;\n}\n");

    String javaSource = sb.toString();
    //System.out.println("counter2 javaSource:\n" + javaSource);

    ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    classWriter.visit(Opcodes.V1_7,
            Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC,
            COMPILED_COUNTER_CLASS2.replace('.', '/'), null, LONG_RANGE_COUNTER_TYPE.getInternalName(), null);
    classWriter.visitSource(javaSource, null);

    // Define "int countN" members:
    int numLeaves = elementaryIntervals.size();
    for (int i = 0; i < numLeaves; i++) {
        classWriter.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_SYNTHETIC, "count" + i, "I", null, null);
    }

    // init:
    Method m = Method.getMethod("void <init> ()");
    GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, m, null,
            null, classWriter);
    // Init all counters to 0:
    for (int i = 0; i < numLeaves; i++) {
        constructor.loadThis();
        constructor.push(0);
        constructor.putField(COMPILED_COUNTER_CLASS2_TYPE, "count" + i, Type.INT_TYPE);
    }
    constructor.loadThis();
    constructor.invokeConstructor(LONG_RANGE_COUNTER_TYPE, m);
    constructor.returnValue();
    constructor.endMethod();

    // void add(long v):
    GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, ADD_METHOD, null,
            null, classWriter);
    buildCounterAsm2(gen, root, false);
    gen.returnValue();
    gen.endMethod();

    // int[] getCounts():
    gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, GET_COUNTS_METHOD, null, null,
            classWriter);
    int countsLocal = gen.newLocal(INT_ARRAY_TYPE);
    gen.push(ranges.length);
    gen.newArray(Type.INT_TYPE);
    gen.storeLocal(countsLocal);

    for (int range = 0; range < ranges.length; range++) {
        List<Integer> elements = rangeToLeaf.get(range);
        if (elements != null) {
            gen.loadLocal(countsLocal);
            gen.push(range);

            gen.loadThis();
            gen.getField(COMPILED_COUNTER_CLASS2_TYPE, "count" + elements.get(0), Type.INT_TYPE);

            for (int i = 1; i < elements.size(); i++) {
                gen.loadThis();
                gen.getField(COMPILED_COUNTER_CLASS2_TYPE, "count" + elements.get(i), Type.INT_TYPE);
                gen.visitInsn(Opcodes.IADD);
            }

            gen.arrayStore(Type.INT_TYPE);
        }
    }

    gen.loadLocal(countsLocal);
    gen.returnValue();
    gen.endMethod();

    classWriter.visitEnd();

    byte[] bytes = classWriter.toByteArray();

    // javap -c /x/tmp/my.class
    /*
    try {
      FileOutputStream fos = new FileOutputStream(new File("/x/tmp/counter2.class"));
      fos.write(bytes);
      fos.close();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    */

    // nocommit allow changing the class loader
    Class<? extends LongRangeCounter> cl = new CounterLoader(LongRangeCounter.class.getClassLoader())
            .define(COMPILED_COUNTER_CLASS2, classWriter.toByteArray());
    try {
        return cl.getConstructor().newInstance();
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException
            | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

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

License:Apache License

@Override
@Nullable/*from   w w  w .j a  v a 2  s . c o  m*/
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    // Per JVMS8 2.9, "Class and interface initialization methods are invoked
    // implicitly by the Java Virtual Machine; they are never invoked directly from any
    // Java Virtual Machine instruction, but are invoked only indirectly as part of the class
    // initialization process." Thus we don't need to emit a stub of <clinit>.
    if (!shouldInclude(access) || (name.equals("<clinit>") && (access & Opcodes.ACC_STATIC) > 0)) {
        return null;
    }

    // We don't stub private constructors, but if stripping these constructors results in no
    // constructors at all, we want to include a default private constructor. This is because
    // removing all these private methods will make the class look like it has no constructors at
    // all, which is not possible. We track if this class has a public, non-synthetic constructor
    // and is not an interface or annotation to determine if a default private constructor is
    // generated when visitEnd() is called.
    if (name.equals("<init>") && (access & Opcodes.ACC_SYNTHETIC) == 0) {
        hasVisibleConstructor = true;
    }

    // Bridge methods are created by the compiler, and don't appear in source. It would be nice to
    // skip them, but they're used by the compiler to cover the fact that type erasure has occurred.
    // Normally the compiler adds these as public methods, but if you're compiling against a stub
    // produced using our ABI generator, we don't want people calling it accidentally. Oh well, I
    // guess it happens IRL too.
    //
    // Synthetic methods are also generated by the compiler, unless it's one of the methods named in
    // section 4.7.8 of the JVM spec, which are "<init>" and "Enum.valueOf()" and "Enum.values".
    // None of these are actually harmful to the ABI, so we allow synthetic methods through.
    // http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.8
    return super.visitMethod(access, name, desc, signature, exceptions);
}

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

License:Apache License

private boolean shouldIncludeInnerClass(int access, String name, @Nullable String outerName) {
    if (referencedClassNames == null || referencedClassNames.contains(name)) {
        // Either it's the first pass, and we're not filtering inner classes yet,
        // or it's the second one, and this inner class is part of the ABI and should
        // therefore be included
        return true;
    }//from w  ww  .ja  v  a  2  s  . co  m

    String currentClassName = Preconditions.checkNotNull(this.name);
    if (name.equals(currentClassName)) {
        // Must always include the entry for our own class, since that's what makes it an inner class.
        return true;
    }

    boolean isAnonymousOrLocalClass = (outerName == null);
    if (isAnonymousOrLocalClass) {
        // Anonymous and local classes are never part of the ABI.
        return false;
    }

    if ((access & (Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE)) == Opcodes.ACC_SYNTHETIC) {
        // Don't include synthetic classes
        return false;
    }

    if (currentClassName.equals(outerName)) {
        // For now, always include our own inner classes, regardless of visibility
        return true;
    }

    return false;
}