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:net.sourceforge.cobertura.instrument.pass3.AtomicArrayCodeProvider.java

License:GNU General Public License

/**
 * <pre>//from  ww w  . j  av  a 2 s.  c  om
 * int[] __cobertura_get_and_reset_counters() {
 * int[] res = new int[counters.length()];
 * for(int i=0; i<counters.length(); i++){
 * res[i]=counters.getAndSet(i, 0);
 * }
 * return res;
 * }
 * </pre>
 */
public void generateCoberturaGetAndResetCountersMethod(ClassVisitor cv, String className) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
            COBERTURA_GET_AND_RESET_COUNTERS_METHOD_NAME, "()[I", null, null);

    mv.visitCode();
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicIntegerArray", "length",
            "()I");
    mv.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT);
    mv.visitVarInsn(Opcodes.ASTORE, 0);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ISTORE, 1);
    Label l3 = new Label();
    mv.visitJumpInsn(Opcodes.GOTO, l3);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicIntegerArray", "getAndSet",
            "(II)I");
    mv.visitInsn(Opcodes.IASTORE);
    mv.visitIincInsn(1, 1);
    mv.visitLabel(l3);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicIntegerArray", "length",
            "()I");
    mv.visitJumpInsn(Opcodes.IF_ICMPLT, l4);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(0, 0);//will be recalculated by writer
    mv.visitEnd();
}

From source file:net.sourceforge.cobertura.instrument.pass3.FastArrayCodeProvider.java

License:GNU General Public License

public void generateCountersField(ClassVisitor cv) {
    /*final tooks 270ms, no-modifier 310ms, volatile 500ms*/
    FieldVisitor fv = cv.visitField(//from   w w w  .  ja  va 2 s  .  com
            Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_TRANSIENT,
            COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE, null, null);
    fv.visitEnd();
}

From source file:net.sourceforge.cobertura.instrument.pass3.FastArrayCodeProvider.java

License:GNU General Public License

public void generateCoberturaGetAndResetCountersMethod(ClassVisitor cv, String className) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
            COBERTURA_GET_AND_RESET_COUNTERS_METHOD_NAME, "()[I", null, null);
    mv.visitCode();// w w  w .  j  ava 2  s .  c  om
    /*cobertura_counters.*/
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitVarInsn(Opcodes.ASTORE, 0);
    /*cobertura_counters.*/
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitInsn(Opcodes.ARRAYLENGTH);

    mv.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT);
    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(0, 0);//will be recalculated by writer
    mv.visitEnd();
}

From source file:net.sourceforge.cobertura.instrument.pass3.InjectCodeClassInstrumenter.java

License:GNU General Public License

/**
 * <p>Instrumenting a code in a single method. Special conditions for processing 'static initialization block'.</p>
 * <p/>/*  w w  w  . j  av a  2s  .c o  m*/
 * <p>This method also uses {@link ShiftVariableMethodAdapter} that is used firstly to calculate the index of internal
 * variable injected to store information about last 'processed' jump or switch in runtime ( {@link ShiftVariableMethodAdapter#calculateFirstStackVariable(int, String)} ),
 * and then is used to inject code responsible for keeping the variable and shifting (+1) all previously seen variables.
 */
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    if (ignoredMethods.contains(name + desc)) {
        return mv;
    }
    if ((access & Opcodes.ACC_STATIC) != 0) {
        mv = new GenerateCallCoberturaInitMethodVisitor(mv, classMap.getClassName());
        if ("<clinit>".equals(name)) {
            wasStaticInitMethodVisited = true;
        }
    }
    FindTouchPointsMethodAdapter instrumenter = new FindTouchPointsMethodAdapter(mv, classMap.getClassName(),
            name, desc, eventIdGenerator, duplicatedLinesMap, lineIdGenerator);
    instrumenter.setTouchPointListener(touchPointListener);
    instrumenter.setIgnoreRegexp(getIgnoreRegexp());
    LocalVariablesSorter sorter = new LocalVariablesSorter(access, desc, instrumenter);
    int variable = sorter.newLocal(Type.INT_TYPE);
    touchPointListener.setLastJumpIdVariableIndex(variable);
    return sorter;
    //return new ShiftVariableMethodAdapter(instrumenter, access, desc, 1);
}

From source file:net.sourceforge.cobertura.instrument.pass3.InjectCodeClassInstrumenter.java

License:GNU General Public License

/**
 * <p>If there was no 'static initialization block' in the class, the method is responsible for generating the method.<br/>
 * It is also responsible for generating method that keeps mapping of counterIds into source places connected to them</p>
 *///from ww  w.  ja  v  a  2  s  . c  o  m
@Override
public void visitEnd() {
    if (!wasStaticInitMethodVisited) {
        //We need to generate new method
        MethodVisitor mv = super.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        codeProvider.generateCallCoberturaInitMethod(mv, classMap.getClassName());
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(/*stack*/3, /*local*/0);
        mv.visitEnd();
        wasStaticInitMethodVisited = true;
    }

    codeProvider.generateCoberturaInitMethod(cv, classMap.getClassName(), classMap.getMaxCounterId() + 1);
    codeProvider.generateCoberturaClassMapMethod(cv, classMap);
    codeProvider.generateCoberturaGetAndResetCountersMethod(cv, classMap.getClassName());

    super.visitEnd();
}

From source file:net.sourceforge.cobertura.instrument.pass3.TestUnitCodeProvider.java

License:GNU General Public License

/**
 * Generates:/*w w w.ja  v  a 2 s. co  m*/
 * public static final transient Map __cobertura_counters;
 */
public void generateCountersField(ClassVisitor cv) {
    FieldVisitor fv = cv.visitField(
            Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_TRANSIENT,
            COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE, null, null);
    fv.visitEnd();
}

From source file:net.sourceforge.cobertura.instrument.pass3.TestUnitCodeProvider.java

License:GNU General Public License

/**
 * Generates:// ww w .  j a va2s .com
 *   public static TestUnitInformationHolder[] __cobertura_get_and_reset_counters()
 *   {
 *     TestUnitInformationHolder[] local = __cobertura_counters;
 *     __cobertura_counters = new TestUnitInformationHolder[__cobertura_counters.length];
 *     return local;
 *   }
 */
public void generateCoberturaGetAndResetCountersMethod(ClassVisitor cv, String className) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
            COBERTURA_GET_AND_RESET_COUNTERS_METHOD_NAME, "()" + COBERTURA_COUNTERS_FIELD_TYPE, null, null);
    mv.visitCode();
    /*cobertura_counters.*/
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitVarInsn(Opcodes.ASTORE, 0);
    /*cobertura_counters.*/
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitInsn(Opcodes.ARRAYLENGTH);

    mv.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(TestUnitInformationHolder.class));
    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(0, 0);//will be recalculated by writer
    mv.visitEnd();
}

From source file:net.yrom.tools.RSymbols.java

License:Apache License

private void drainSymbols(Path file) {
    final String filename = file.getFileName().toString();
    String typeName = filename.substring(0, filename.length() - ".class".length());
    byte[] bytes;
    try {/*from  w  ww  .ja  v  a  2  s.  co  m*/
        bytes = Files.readAllBytes(file);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    ClassVisitor visitor = new ClassVisitor(Opcodes.ASM5) {
        @Override
        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
            // read constant value
            if (value instanceof Integer) {
                String key = typeName + '.' + name;
                Integer old = symbols.get(key);
                if (old != null && !old.equals(value)) {
                    throw new IllegalStateException("Value of " + key + " mismatched! " + "Excepted 0x"
                            + Integer.toHexString(old) + " but was 0x" + Integer.toHexString((Integer) value));
                } else {
                    symbols.put(key, (Integer) value);
                }
            }
            return null;
        }

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            if (access == Opcodes.ACC_STATIC && "<clinit>".equals(name)) {

                return new MethodVisitor(Opcodes.ASM5) {
                    int[] current = null;
                    LinkedList<Integer> intStack = new LinkedList<>();

                    @Override
                    public void visitIntInsn(int opcode, int operand) {
                        if (opcode == Opcodes.NEWARRAY && operand == Opcodes.T_INT) {
                            current = new int[intStack.pop()];
                        } else if (opcode == Opcodes.BIPUSH) {
                            intStack.push(operand);
                        }
                    }

                    @Override
                    public void visitLdcInsn(Object cst) {
                        if (cst instanceof Integer) {
                            intStack.push((Integer) cst);
                        }
                    }

                    @Override
                    public void visitInsn(int opcode) {
                        if (opcode >= Opcodes.ICONST_0 && opcode <= Opcodes.ICONST_5) {
                            intStack.push(opcode - Opcodes.ICONST_0);
                        } else if (opcode == Opcodes.IASTORE) {
                            int value = intStack.pop();
                            int index = intStack.pop();
                            current[index] = value;
                        }
                    }

                    @Override
                    public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                        if (opcode == Opcodes.PUTSTATIC) {
                            int[] old = styleables.get(name);
                            if (old != null && old.length != current.length && !Arrays.equals(old, current)) {
                                throw new IllegalStateException("Value of styleable." + name + " mismatched! "
                                        + "Excepted " + Arrays.toString(old) + " but was "
                                        + Arrays.toString(current));
                            } else {
                                styleables.put(name, current);
                            }
                            current = null;
                            intStack.clear();
                        }
                    }
                };
            }
            return null;
        }
    };

    new ClassReader(bytes).accept(visitor, SKIP_DEBUG | SKIP_FRAMES);
}

From source file:net.yrom.tools.WriteStyleablesProcessor.java

License:Apache License

@Override
public void proceed() {
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_SUPER,
            RSymbols.R_STYLEABLES_CLASS_NAME, null, "java/lang/Object", null);
    for (String name : symbols.getStyleables().keySet()) {
        writer.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, name, "[I", null, null);
    }/*from   w  ww.  j  av a 2s. c o  m*/

    writeClinit(writer);
    writer.visitEnd();
    byte[] bytes = writer.toByteArray();
    try {
        if (!dir.isDirectory() && !dir.mkdirs()) {
            throw new RuntimeException("Cannot mkdir " + dir);
        }
        Files.write(dir.toPath().resolve(RSymbols.R_STYLEABLES_CLASS_NAME + ".class"), bytes);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:net.yrom.tools.WriteStyleablesProcessor.java

License:Apache License

private void writeClinit(ClassWriter writer) {
    Map<String, int[]> styleables = symbols.getStyleables();
    MethodVisitor clinit = writer.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
    clinit.visitCode();/*from   w w w  .ja  v  a 2s . c  om*/

    for (Map.Entry<String, int[]> entry : styleables.entrySet()) {
        final String field = entry.getKey();
        final int[] value = entry.getValue();
        final int length = value.length;
        pushInt(clinit, length);
        clinit.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT);
        for (int i = 0; i < length; i++) {
            clinit.visitInsn(Opcodes.DUP); // dup
            pushInt(clinit, i);
            pushInt(clinit, value[i]);
            clinit.visitInsn(Opcodes.IASTORE); // iastore
        }
        clinit.visitFieldInsn(Opcodes.PUTSTATIC, RSymbols.R_STYLEABLES_CLASS_NAME, field, "[I");
    }
    clinit.visitInsn(Opcodes.RETURN);
    clinit.visitMaxs(0, 0); // auto compute
    clinit.visitEnd();
}