Example usage for org.objectweb.asm Opcodes ACC_PUBLIC

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

Introduction

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

Prototype

int ACC_PUBLIC

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

Click Source Link

Usage

From source file:com.google.devtools.build.android.resources.RClassWriter.java

License:Open Source License

/**
 * Builds the bytecode and writes out the R.class file, and R$inner.class files.
 *///from w  ww  .  j a v  a 2 s. c o m
public void write() throws IOException {
    Splitter splitter = Splitter.on('.');
    Iterable<String> folders = splitter.split(packageName);
    File packageDir = outFolder;
    for (String folder : folders) {
        packageDir = new File(packageDir, folder);
    }
    File rClassFile = new File(packageDir, SdkConstants.FN_COMPILED_RESOURCE_CLASS);
    Files.createParentDirs(rClassFile);
    String packageWithSlashes = packageName.replaceAll("\\.", "/");
    String rClassName = packageWithSlashes + "/R";
    ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classWriter.visit(JAVA_VERSION, Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SUPER, rClassName,
            null, SUPER_CLASS, null);
    classWriter.visitSource(SdkConstants.FN_RESOURCE_CLASS, null);
    writeConstructor(classWriter);

    Table<String, String, SymbolEntry> symbols = getAllSymbols();
    Table<String, String, SymbolEntry> values = getSymbols(symbolValues);

    Set<String> rowSet = symbols.rowKeySet();
    List<String> rowList = new ArrayList<>(rowSet);
    Collections.sort(rowList);

    // Build the R.class w/ the inner classes, then later build the individual R$inner.class.
    for (String row : rowList) {
        String innerClassName = rClassName + "$" + row;
        classWriter.visitInnerClass(innerClassName, rClassName, row,
                Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC);
    }
    classWriter.visitEnd();
    Files.write(classWriter.toByteArray(), rClassFile);

    // Now generate the R$inner.class files.
    for (String row : rowList) {
        writeInnerClass(symbols, values, packageDir, rClassName, row);
    }
}

From source file:com.google.devtools.build.android.resources.RClassWriter.java

License:Open Source License

private void writeInnerClass(Table<String, String, SymbolEntry> symbols,
        Table<String, String, SymbolEntry> values, File packageDir, String fullyQualifiedOuterClass,
        String innerClass) throws IOException {
    ClassWriter innerClassWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    String fullyQualifiedInnerClass = fullyQualifiedOuterClass + "$" + innerClass;
    innerClassWriter.visit(JAVA_VERSION, Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SUPER,
            fullyQualifiedInnerClass, null, SUPER_CLASS, null);
    innerClassWriter.visitSource("R.java", null);
    writeConstructor(innerClassWriter);/*  w ww  . ja v  a2s  . c o m*/
    innerClassWriter.visitInnerClass(fullyQualifiedInnerClass, fullyQualifiedOuterClass, innerClass,
            Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC);

    Map<String, SymbolEntry> rowMap = symbols.row(innerClass);
    Set<String> symbolSet = rowMap.keySet();
    List<String> symbolList = new ArrayList<>(symbolSet);
    Collections.sort(symbolList);
    List<DeferredInitializer> deferredInitializers = new ArrayList<>();
    int fieldAccessLevel = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC;
    if (finalFields) {
        fieldAccessLevel |= Opcodes.ACC_FINAL;
    }
    for (String symbolName : symbolList) {
        // get the matching SymbolEntry from the values Table.
        SymbolEntry value = values.get(innerClass, symbolName);
        if (value != null) {
            String desc;
            Object initializer = null;
            if (value.getType().equals("int")) {
                desc = "I";
                if (finalFields) {
                    initializer = Integer.decode(value.getValue());
                } else {
                    deferredInitializers.add(IntDeferredInitializer.of(value.getName(), value.getValue()));
                }
            } else {
                Preconditions.checkArgument(value.getType().equals("int[]"));
                desc = "[I";
                deferredInitializers.add(IntArrayDeferredInitializer.of(value.getName(), value.getValue()));
            }
            innerClassWriter.visitField(fieldAccessLevel, value.getName(), desc, null, initializer).visitEnd();
        }
    }

    if (!deferredInitializers.isEmpty()) {
        // build the <clinit> method.
        writeStaticClassInit(innerClassWriter, fullyQualifiedInnerClass, deferredInitializers);
    }

    innerClassWriter.visitEnd();
    File innerFile = new File(packageDir, "R$" + innerClass + ".class");
    Files.write(innerClassWriter.toByteArray(), innerFile);
}

From source file:com.google.devtools.build.android.resources.RClassWriter.java

License:Open Source License

private static void writeConstructor(ClassWriter classWriter) {
    MethodVisitor constructor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    constructor.visitCode();// ww  w.ja v a  2  s  .c o m
    constructor.visitVarInsn(Opcodes.ALOAD, 0);
    constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, SUPER_CLASS, "<init>", "()V", false);
    constructor.visitInsn(Opcodes.RETURN);
    constructor.visitMaxs(1, 1);
    constructor.visitEnd();
}

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

License:Apache License

/**
 * Tests //from  w ww.  ja 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 //from  w  w w  .j  a  v  a  2  s  .  c  o 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.devtools.build.wireless.testing.java.injector.coverage.CodeCoverageClassAdapterTest.java

License:Apache License

public void testVisitMethod_skipClassesNotIncluded() {
    String[] inclusion = new String[] { "+com/google/io" };
    classAdapter = new CodeCoverageClassAdapter(new EmptyVisitor(), statisticContainer, inclusion,
            CoverageMode.LINE);/*  w  ww .jav  a2  s . com*/

    classAdapter.visit(1, Opcodes.ACC_PUBLIC, "com/google/common/Timer", null, ClassNames.JAVA_LANG_OBJECT,
            null);
    MethodVisitor mv = classAdapter.visitMethod(Opcodes.ACC_PUBLIC, "method", "()V", null, null);

    assertFalse("Classes that do not match the include pattern should not be instrumented",
            mv instanceof CodeCoverageClassAdapter.MethodCoverage);
}

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

License:Apache License

public void testVisitMethod_skipAutoGeneratedClasses() {
    classAdapter = new CodeCoverageClassAdapter(new EmptyVisitor(), statisticContainer, null,
            CoverageMode.LINE);/*from   w  w  w . j a  v a2 s  .  c  o  m*/

    classAdapter.visit(1, Opcodes.ACC_PUBLIC, "com/google/common/Timer$1", null, ClassNames.JAVA_LANG_OBJECT,
            null);
    MethodVisitor mv = classAdapter.visitMethod(Opcodes.ACC_PUBLIC, "method", "()V", null, null);

    assertFalse("Auto-generated classes should not be instrumented",
            mv instanceof CodeCoverageClassAdapter.MethodCoverage);
}

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

License:Apache License

public void testVisitMethod_lineCoverageInstrumentationAdded() {
    final String methodName = "method";
    final String className = "com/google/common/Timer";
    final String sourceFile = "Timer.java";
    final String methodDesc = "()V";
    final String method = className + "." + methodName + methodDesc;

    String[] inclusion = new String[] { "+com" };
    classAdapter = new CodeCoverageClassAdapter(new EmptyVisitor(), statisticContainer, inclusion,
            CoverageMode.LINE);/* ww  w  .  j ava 2s . com*/

    classAdapter.visit(1, Opcodes.ACC_PUBLIC, className, null, ClassNames.JAVA_LANG_OBJECT, null);
    classAdapter.visitSource(sourceFile, null);

    MethodVisitor mv = classAdapter.visitMethod(Opcodes.ACC_PUBLIC, methodName, "()V", null, null);
    final int lineNumber = 20;
    mv.visitLineNumber(20, new Label());

    assertEquals(1, statisticContainer.getInstrumentedLines(className).size());
    assertTrue(statisticContainer.getInstrumentedLines(className).get(0).equals(lineNumber));
}

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

License:Apache License

public void testVisitMethod_lineCoverageInstrumentationNotAdded() {
    final String methodName = "method";
    final String className = "com/google/common/Timer";
    final String sourceFile = "Timer.java";
    final String methodDesc = "()V";
    final String method = className + "." + methodName + methodDesc;

    String[] inclusion = new String[] { "+com" };
    classAdapter = new CodeCoverageClassAdapter(new EmptyVisitor(), statisticContainer, inclusion,
            CoverageMode.SUMMARY);/*from   w  w  w  .  ja v a2  s.  c o m*/

    classAdapter.visit(1, Opcodes.ACC_PUBLIC, className, null, ClassNames.JAVA_LANG_OBJECT, null);
    classAdapter.visitSource(sourceFile, null);

    MethodVisitor mv = classAdapter.visitMethod(Opcodes.ACC_PUBLIC, methodName, "()V", null, null);
    final int lineNumber = 20;
    mv.visitLineNumber(20, new Label());

    assertEquals(0, statisticContainer.getInstrumentedLines(method).size());
}

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

License:Apache License

/**
 * Tests /*from   www .  j  av  a  2 s .  c o m*/
 * {@link CodeCoverageClassAdapter#visit(int, int, String, String, String, String[])}
 * .
 * 
 * <p>
 * Verifies:
 * <ul> 
 * <li>that source file would be collected correctly.
 * <li>that duplicated source file would be collected only once.
 * <li>that classes in the default package would be considered.
 * <li>that classes with the same name but in different packages would be 
 *    collected correctly.
 * </ul> 
 * In case of failure the problem is probably located in the class adapter or 
 * in the statistic container.
 */
public void testVisit() {
    int currentClassCovered = 0;
    String name = null;
    int currentCoveredPkg = 0;

    /*
     * Visit several different classes in the default package and verify that 
     * they have all been mapped.
     */
    for (int i = 0; i < MAX; i++) {
        name = "Dummy" + i;
        classAdapter.visit(1, Opcodes.ACC_PUBLIC, name, null, ClassNames.JAVA_LANG_OBJECT, null);
        if (classAdapter.isIncluded()) {
            currentClassCovered++;
            // All the classes are in the default package
            currentCoveredPkg = 1;
        }
        assertEquals("Number of classes.", currentClassCovered, statisticContainer.getClassSize());
        assertEquals("Number of packages.", currentCoveredPkg, statisticContainer.getPackageSize());
    }

    /*
     * Visits the same class several times and check that it will be added only
     * once.
     */
    currentClassCovered = statisticContainer.getClassSize();
    currentCoveredPkg = statisticContainer.getPackageSize();
    name = "duplicated/DuplicatedDummy";
    if (classAdapter.isIncluded()) {
        currentClassCovered++;
        currentCoveredPkg++;
    }
    for (int i = 0; i < MAX; i++) {
        classAdapter.visit(1, Opcodes.ACC_PUBLIC, name, null, ClassNames.JAVA_LANG_OBJECT, null);
        assertEquals("Duplicated classes.", currentClassCovered, statisticContainer.getClassSize());
        assertEquals("Number of packages.", currentCoveredPkg, statisticContainer.getPackageSize());
    }

    /*
     * Visits classes with the same name but in different packages and verify 
     * that all of them have been covered.
     */
    currentClassCovered = statisticContainer.getClassSize();
    currentCoveredPkg = statisticContainer.getPackageSize();
    for (int i = 0; i < MAX; i++) {
        name = "org/foo" + i + "/Dummy";
        classAdapter.visit(1, Opcodes.ACC_PUBLIC, name, null, ClassNames.JAVA_LANG_OBJECT, null);
        if (classAdapter.isIncluded()) {
            currentClassCovered++;
            // All the classes are in different packages
            currentCoveredPkg++;
        }
        assertEquals("Package handling in class counting.", currentClassCovered,
                statisticContainer.getClassSize());
        assertEquals("Number of packages.", currentCoveredPkg, statisticContainer.getPackageSize());
    }
}