Example usage for org.objectweb.asm Opcodes ACC_FINAL

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

Introduction

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

Prototype

int ACC_FINAL

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

Click Source Link

Usage

From source file:com.asakusafw.dag.compiler.codegen.OperationGenerator.java

License:Apache License

private static void addContextField(ClassWriter writer) {
    writer.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, FIELD_CONTEXT,
            typeOf(OperationAdapter.Context.class).getDescriptor(), null, null);
}

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  .ja  va2  s .com
 *
 *  @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  ww w . j a  v  a 2s  .  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);/*  w w  w  . j  a  v  a 2 s. c  o  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.codename1.tools.translator.ByteCodeField.java

License:Open Source License

public ByteCodeField(String clsName, int access, String name, String desc, String signature, Object value) {
    this.clsName = clsName;
    this.value = value;
    privateField = (access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE;
    if (value != null && value instanceof String) {
        Parser.addToConstantPool((String) value);
    }//ww w. j  a  v  a 2s.c o m
    staticField = (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC;
    finalField = (access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL;
    fieldName = name.replace('$', '_');

    arrayDimensions = 0;
    while (desc.startsWith("[")) {
        desc = desc.substring(1);
        arrayDimensions++;
    }
    char currentType = desc.charAt(0);
    switch (currentType) {
    case 'L':
        // Object skip until ;
        int idx = desc.indexOf(';');
        String objectType = desc.substring(1, idx);
        objectType = objectType.replace('/', '_').replace('$', '_');
        if (!dependentClasses.contains(objectType)) {
            dependentClasses.add(objectType);
        }
        //if (!privateField && !exportedClasses.contains(objectType)) {
        //    exportedClasses.add(objectType);
        //}

        type = objectType;
        break;
    case 'I':
        primitiveType = Integer.TYPE;
        break;
    case 'J':
        primitiveType = Long.TYPE;
        break;
    case 'B':
        primitiveType = Byte.TYPE;
        break;
    case 'S':
        primitiveType = Short.TYPE;
        break;
    case 'F':
        primitiveType = Float.TYPE;
        break;
    case 'D':
        primitiveType = Double.TYPE;
        break;
    case 'Z':
        primitiveType = Boolean.TYPE;
        break;
    case 'C':
        primitiveType = Character.TYPE;
        break;
    }
}

From source file:com.codename1.tools.translator.BytecodeMethod.java

License:Open Source License

public BytecodeMethod(String clsName, int access, String name, String desc, String signature,
        String[] exceptions) {/*  w ww  .j  a  v a2  s . c  o  m*/
    methodName = name;
    this.clsName = clsName;
    this.desc = desc;
    privateMethod = (access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE;
    nativeMethod = (access & Opcodes.ACC_NATIVE) == Opcodes.ACC_NATIVE;
    staticMethod = (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC;
    finalMethod = (access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL;
    synchronizedMethod = (access & Opcodes.ACC_SYNCHRONIZED) == Opcodes.ACC_SYNCHRONIZED;
    int pos = desc.lastIndexOf(')');
    if (!staticMethod) {
        if (!dependentClasses.contains("java_lang_NullPointerException")) {
            dependentClasses.add("java_lang_NullPointerException");
        }
    } // 
    if (methodName.equals("<init>")) {
        methodName = "__INIT__";
        constructor = true;
        returnType = new ByteCodeMethodArg(Void.TYPE, 0);
    } else {
        if (methodName.equals("<clinit>")) {
            methodName = "__CLINIT__";
            returnType = new ByteCodeMethodArg(Void.TYPE, 0);
            staticMethod = true;
        } else {
            String retType = desc.substring(pos + 1);
            if (retType.equals("V")) {
                returnType = new ByteCodeMethodArg(Void.TYPE, 0);
            } else {
                int dim = 0;
                while (retType.startsWith("[")) {
                    retType = retType.substring(1);
                    dim++;
                }
                char currentType = retType.charAt(0);
                switch (currentType) {
                case 'L':
                    // Object skip until ;
                    int idx = retType.indexOf(';');
                    String objectType = retType.substring(1, idx);
                    objectType = objectType.replace('/', '_').replace('$', '_');
                    if (!dependentClasses.contains(objectType)) {
                        dependentClasses.add(objectType);
                    }
                    //if (!this.isPrivate() && !exportedClasses.contains(objectType)) {
                    //    exportedClasses.add(objectType);
                    //}
                    returnType = new ByteCodeMethodArg(objectType, dim);
                    break;
                case 'I':
                    returnType = new ByteCodeMethodArg(Integer.TYPE, dim);
                    break;
                case 'J':
                    returnType = new ByteCodeMethodArg(Long.TYPE, dim);
                    break;
                case 'B':
                    returnType = new ByteCodeMethodArg(Byte.TYPE, dim);
                    break;
                case 'S':
                    returnType = new ByteCodeMethodArg(Short.TYPE, dim);
                    break;
                case 'F':
                    returnType = new ByteCodeMethodArg(Float.TYPE, dim);
                    break;
                case 'D':
                    returnType = new ByteCodeMethodArg(Double.TYPE, dim);
                    break;
                case 'Z':
                    returnType = new ByteCodeMethodArg(Boolean.TYPE, dim);
                    break;
                case 'C':
                    returnType = new ByteCodeMethodArg(Character.TYPE, dim);
                    break;
                }
            }
        }
    }
    int currentArrayDim = 0;
    desc = desc.substring(1, pos);
    for (int i = 0; i < desc.length(); i++) {
        char currentType = desc.charAt(i);
        switch (currentType) {
        case '[':
            // array of...
            currentArrayDim++;
            continue;
        case 'L':
            // Object skip until ;
            int idx = desc.indexOf(';', i);
            String objectType = desc.substring(i + 1, idx);
            objectType = objectType.replace('/', '_').replace('$', '_');
            if (!dependentClasses.contains(objectType)) {
                dependentClasses.add(objectType);
            }
            //if (!this.isPrivate() && !exportedClasses.contains(objectType)) {
            //    exportedClasses.contains(objectType);
            //}
            i = idx;
            arguments.add(new ByteCodeMethodArg(objectType, currentArrayDim));
            break;
        case 'I':
            arguments.add(new ByteCodeMethodArg(Integer.TYPE, currentArrayDim));
            break;
        case 'J':
            arguments.add(new ByteCodeMethodArg(Long.TYPE, currentArrayDim));
            break;
        case 'B':
            arguments.add(new ByteCodeMethodArg(Byte.TYPE, currentArrayDim));
            break;
        case 'S':
            arguments.add(new ByteCodeMethodArg(Short.TYPE, currentArrayDim));
            break;
        case 'F':
            arguments.add(new ByteCodeMethodArg(Float.TYPE, currentArrayDim));
            break;
        case 'D':
            arguments.add(new ByteCodeMethodArg(Double.TYPE, currentArrayDim));
            break;
        case 'Z':
            arguments.add(new ByteCodeMethodArg(Boolean.TYPE, currentArrayDim));
            break;
        case 'C':
            arguments.add(new ByteCodeMethodArg(Character.TYPE, currentArrayDim));
            break;
        }
        currentArrayDim = 0;
    }
}

From source file:com.codename1.tools.translator.Parser.java

License:Open Source License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {//  ww  w.  ja v a 2s  . com
    cls.setBaseClass(superName);
    cls.setBaseInterfaces(interfaces);
    if ((access & Opcodes.ACC_ABSTRACT) == Opcodes.ACC_ABSTRACT) {
        cls.setIsAbstract(true);
    }
    if ((access & Opcodes.ACC_INTERFACE) == Opcodes.ACC_INTERFACE) {
        cls.setIsInterface(true);
    }
    if ((access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL) {
        cls.setFinalClass(true);
    }
    if ("com/codename1/testing/UnitTest".equals(superName)
            || "com/codename1/testing/AbstractTest".equals(superName)) {
        cls.setIsUnitTest(true);
    }
    if ((access & Opcodes.ACC_ENUM) == Opcodes.ACC_ENUM) {
        cls.setIsEnum(true);
    }
    super.visit(version, access, name, signature, superName, interfaces);
}

From source file:com.datatorrent.stram.webapp.asm.ASMUtil.java

License:Apache License

public static boolean isFinal(int opCode) {
    return (opCode & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL;
}

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

License:Apache License

/** Gets the access flag (see JVMS8 4.1, 4.5, 4.6) corresponding to the given modifier. */
private static int modifierToAccessFlag(Modifier modifier) {
    switch (modifier) {
    case PUBLIC://from   w  w  w. ja va  2  s .  c  o m
        return Opcodes.ACC_PUBLIC;
    case PROTECTED:
        return Opcodes.ACC_PROTECTED;
    case PRIVATE:
        return Opcodes.ACC_PRIVATE;
    case ABSTRACT:
        return Opcodes.ACC_ABSTRACT;
    case DEFAULT:
        return 0;
    case STATIC:
        return Opcodes.ACC_STATIC;
    case FINAL:
        return Opcodes.ACC_FINAL;
    case TRANSIENT:
        return Opcodes.ACC_TRANSIENT;
    case VOLATILE:
        return Opcodes.ACC_VOLATILE;
    case SYNCHRONIZED:
        return Opcodes.ACC_SYNCHRONIZED;
    case NATIVE:
        return Opcodes.ACC_NATIVE;
    case STRICTFP:
        return Opcodes.ACC_STRICT;
    default:
        throw new IllegalArgumentException(String.format("Unexpected modifier: %s", modifier));
    }
}

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

License:Apache License

@Test
public void testFinalFlagOnField() throws IOException {
    testFieldFlags("final", Opcodes.ACC_FINAL);
}