Example usage for org.objectweb.asm Opcodes ACC_STATIC

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

Introduction

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

Prototype

int ACC_STATIC

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

Click Source Link

Usage

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

License:Open Source License

/**
 * Builds the bytecode and writes out the R.class file, and R$inner.class files.
 *///w  ww. j a v a 2  s .  c  o  m
public void write() throws IOException {
    Iterable<String> folders = PACKAGE_SPLITTER.split(packageName);
    Path packageDir = outFolder;
    for (String folder : folders) {
        packageDir = packageDir.resolve(folder);
    }
    // At least create the outFolder that was requested. However, if there are no symbols, don't
    // create the R.class and inner class files (no need to have an empty class).
    Files.createDirectories(packageDir);
    if (initializers.isEmpty()) {
        return;
    }
    Path rClassFile = packageDir.resolve(SdkConstants.FN_COMPILED_RESOURCE_CLASS);

    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, /* signature */
            SUPER_CLASS, null /* interfaces */);
    classWriter.visitSource(SdkConstants.FN_RESOURCE_CLASS, null);
    writeConstructor(classWriter);

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

    // Now generate the R$inner.class files.
    for (Map.Entry<ResourceType, List<FieldInitializer>> entry : initializers.entrySet()) {
        writeInnerClass(entry.getValue(), packageDir, rClassName, entry.getKey().toString());
    }
}

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

License:Open Source License

private void writeInnerClass(List<FieldInitializer> initializers, Path packageDir,
        String fullyQualifiedOuterClass, String innerClass) throws IOException {
    ClassWriter innerClassWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    String fullyQualifiedInnerClass = writeInnerClassHeader(fullyQualifiedOuterClass, innerClass,
            innerClassWriter);/*from ww  w  .  ja va 2  s  .  c  om*/

    List<FieldInitializer> deferredInitializers = new ArrayList<>();
    int fieldAccessLevel = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC;
    if (finalFields) {
        fieldAccessLevel |= Opcodes.ACC_FINAL;
    }
    for (FieldInitializer init : initializers) {
        if (init.writeFieldDefinition(innerClassWriter, fieldAccessLevel, finalFields)) {
            deferredInitializers.add(init);
        }
    }
    if (!deferredInitializers.isEmpty()) {
        writeStaticClassInit(innerClassWriter, fullyQualifiedInnerClass, deferredInitializers);
    }

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

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

License:Open Source License

private String writeInnerClassHeader(String fullyQualifiedOuterClass, String innerClass,
        ClassWriter innerClassWriter) {/*  w ww  .  jav a 2  s .com*/
    String fullyQualifiedInnerClass = fullyQualifiedOuterClass + "$" + innerClass;
    innerClassWriter.visit(JAVA_VERSION, Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SUPER,
            fullyQualifiedInnerClass, null, /* signature */
            SUPER_CLASS, null /* interfaces */);
    innerClassWriter.visitSource(SdkConstants.FN_RESOURCE_CLASS, null);
    writeConstructor(innerClassWriter);
    innerClassWriter.visitInnerClass(fullyQualifiedInnerClass, fullyQualifiedOuterClass, innerClass,
            Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC);
    return fullyQualifiedInnerClass;
}

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

License:Open Source License

private static void writeStaticClassInit(ClassWriter classWriter, String className,
        List<FieldInitializer> initializers) {
    MethodVisitor visitor = classWriter.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, /* signature */
            null /* exceptions */);
    visitor.visitCode();/* w ww  .  jav a 2 s .  co  m*/
    int stackSlotsNeeded = 0;
    InstructionAdapter insts = new InstructionAdapter(visitor);
    for (FieldInitializer fieldInit : initializers) {
        stackSlotsNeeded = Math.max(stackSlotsNeeded, fieldInit.writeCLInit(insts, className));
    }
    insts.areturn(Type.VOID_TYPE);
    visitor.visitMaxs(stackSlotsNeeded, 0);
    visitor.visitEnd();
}

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.
 *///w  ww .  ja  v a2  s .  c om
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 w w.  j  a  va2 s.  c om
    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 writeStaticClassInit(ClassWriter classWriter, String className,
        List<DeferredInitializer> deferredInitializers) {
    MethodVisitor visitor = classWriter.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
    visitor.visitCode();//from   w ww  .  j  a  va 2s .c  o m
    int stackSlotsNeeded = 0;
    InstructionAdapter insts = new InstructionAdapter(visitor);
    for (DeferredInitializer fieldInit : deferredInitializers) {
        stackSlotsNeeded = Math.max(stackSlotsNeeded, fieldInit.writeCLInit(className, insts));
    }
    insts.areturn(Type.VOID_TYPE);
    visitor.visitMaxs(stackSlotsNeeded, 0);
    visitor.visitEnd();
}

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

License:Apache License

/**
 * Process method description to retrieve informations about arguments and
 * initial stack size.//  www .  ja  v  a2 s  .co m
 * 
 * <p>Double and Long takes two slots in the initial frame size. Any other 
 * type including arrays and classes takes one single slot.
 * 
 * <p>If the method is not static a pointer to the current class is passed as
 * an implicit argument which is placed at the beginning of the frame at 
 * index 0 and which takes one slot (because it is a reference to an 
 * instance).
 * 
 * @param access Method's flags.
 * @param description A string containing the description of the method.
 * 
 * TODO} 
 */
protected void processParams(int access, String description) {

    if (description == null) {
        throw new RuntimeException("Description cannot be null.");
    }

    String argument = description.substring(1, description.indexOf(')'));
    Matcher matcher = StackServant.TYPE_PATTERN.matcher(argument);
    int groupCount = matcher.groupCount();

    if ((access & Opcodes.ACC_STATIC) > 0) {
        staticFlag = true;
    }
    initialLocalFrameSize = getParamInitialIndex();

    /*
     * Process all the parameters and set the initial frame's size.
     * This servant is in charge for handling any increase to the size of the 
     * frame.
     */
    while (matcher.find()) {
        String s = matcher.group();
        params.add(s);
        initialLocalFrameSize += getFrameSizeForType(s);
    }
    instrumentedLocalFrameSize = initialLocalFrameSize;
}

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

License:Apache License

/**
 * Tests {@link StackServant#increaseInstrumentedStack(int)}.
 */// w w w  . j  a va  2 s. c o  m
public void testIncreaseInstrumentedStack() {
    String testName = "testIncreaseInstrumentedStack";

    MethodVisitor mv = null;
    int access = Opcodes.ACC_STATIC;
    String description = null;
    StackServant servant = null;
    int size = -1;

    description = "()V";
    servant = new StackServant(mv, access, description);
    size = servant.getInstrumentedFrameSize();
    assertEquals(testName, 0, size);
    for (int i = 1; i < 20; i++) {
        servant.increaseInstrumentedStack(1);
        size = servant.getInstrumentedFrameSize();
        assertEquals(testName, i, size);
    }

    description = "(IZI)V";
    servant = new StackServant(mv, access, description);
    size = servant.getInstrumentedFrameSize();
    assertEquals(testName, 3, size);
    for (int i = 1; i < 20; i++) {
        servant.increaseInstrumentedStack(1);
        size = servant.getInstrumentedFrameSize();
        assertEquals(testName, 3 + i, size);
    }
}

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

License:Apache License

/**
 * Tests {@link StackServant#getInitialFrameSize()}.
 *//*from ww w .  java  2 s.c o m*/
public void testGetInitialFrameSize() {
    String testName = "testGetInitialFrameSize";

    MethodVisitor mv = null;
    String description = null;
    StackServant servant = null;
    int size = -1;

    description = "()V";
    servant = new StackServant(mv, Opcodes.ACC_STATIC, description);
    size = servant.getInitialFrameSize();
    assertEquals(testName, 0, size);
    servant = new StackServant(mv, 0, description);
    size = servant.getInitialFrameSize();
    assertEquals(testName, 1, size);

    description = "()D";
    servant = new StackServant(mv, Opcodes.ACC_STATIC, description);
    size = servant.getInitialFrameSize();
    assertEquals(testName, 0, size);
    servant = new StackServant(mv, 0, description);
    size = servant.getInitialFrameSize();
    assertEquals(testName, 1, size);

    // "Z", "B", "S", "C", "I", "F", "L", "["
    String[] symbols1 = new String[] { "Z", "B", "S", "C", "I", "F", "Lasd;", "[I" };
    for (String s : symbols1) {
        servant = new StackServant(mv, Opcodes.ACC_STATIC, "(" + s + ")V");
        size = servant.getInitialFrameSize();
        assertEquals(testName, 1, size);
    }

    // "J", "D"
    String[] symbols2 = new String[] { "J", "D" };
    for (String s : symbols2) {
        servant = new StackServant(mv, Opcodes.ACC_STATIC, "(" + s + ")V");
        size = servant.getInitialFrameSize();
        assertEquals(testName, 2, size);
    }

    StringBuffer sb = new StringBuffer("(");
    int finalSize = 0;
    for (int i = 1; i < 5; i++) {
        sb.append(symbols1[(int) (symbols1.length * Math.random())]);
        sb.append(symbols2[(int) (symbols2.length * Math.random())]);
        finalSize += 3;
    }
    sb.append(")J");
    servant = new StackServant(mv, Opcodes.ACC_STATIC, sb.toString());
    size = servant.getInitialFrameSize();
    assertEquals(testName, finalSize, size);
}