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:org.evosuite.instrumentation.BooleanTestabilityTransformation.java

License:Open Source License

/**
 * Apply testability transformation to an individual method
 * /* w  w w. j a v  a  2 s .  c  om*/
 * @param mn
 */
private void transformMethod(MethodNode mn) {
    logger.info("Transforming method {}{}", mn.name, mn.desc);

    //currentCFG = GraphPool.getActualCFG(className, mn.name + mn.desc);

    // TODO: Skipping interfaces for now, but will need to handle Booleans in interfaces!
    if ((mn.access & Opcodes.ACC_ABSTRACT) == Opcodes.ACC_ABSTRACT)
        return;

    String origDesc = getOriginalDesc(className, mn.name, mn.desc);
    logger.info("Analyzing {} for TT, signature {}/{}", mn.name, origDesc, mn.desc);
    try {
        Analyzer a = new Analyzer(
                new BooleanValueInterpreter(origDesc, (mn.access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC));
        a.analyze(className, mn);
        currentFrames = a.getFrames();
    } catch (Exception e) {
        logger.info("1. Error during analysis: {}", e);
        //e.printStackTrace();
        // TODO: Handle error
    }
    generateCDG(mn);
    currentMethodNode = mn;
    // First expand ifs without else/*
    new ImplicitElseTransformer(this).transform(mn);
    try {
        Analyzer a = new Analyzer(
                new BooleanValueInterpreter(origDesc, (mn.access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC));
        a.analyze(className, mn);
        currentFrames = a.getFrames();
    } catch (Exception e) {
        logger.info("2. Error during analysis: {}", e);
        //e.printStackTrace();
        // TODO: Handle error
    }

    //      BytecodeInstructionPool.reRegisterMethodNode(mn, className, mn.name + mn.desc);
    // Replace all bitwise operators
    logger.info("Transforming Boolean bitwise operators");
    new BitwiseOperatorTransformer(this).transform(mn);

    // Transform IFEQ/IFNE to IFLE/IFGT
    logger.info("Transforming Boolean IFs");
    new BooleanIfTransformer(this).transform(mn);

    // Insert call to BooleanHelper.get after ICONST_0/1 or Boolean fields
    logger.info("Transforming Boolean definitions");
    new BooleanDefinitionTransformer(this).transform(mn);

    // Replace all instanceof comparisons
    logger.info("Transforming instanceof");
    new InstanceOfTransformer().transform(mn);

    // Replace all calls to methods/fields returning booleans
    new BooleanCallsTransformer(this).transform(mn);

    // Transform all flag based comparisons
    logger.info("Transforming Boolean distances");
    new BooleanDistanceTransformer(this).transform(mn);
    mn.maxStack += 3;

    // Replace all boolean arrays
    new BooleanArrayTransformer().transform(mn);

    new BooleanArrayIndexTransformer(getArrayFrames(mn)).transform(mn);

    // Replace all boolean return values
    logger.info("Transforming Boolean return values");
    new BooleanReturnTransformer(this).transform(mn);

    //      GraphPool.clear(className, mn.name + mn.desc);
    //      BytecodeInstructionPool.clear(className, mn.name + mn.desc);
    //      BranchPool.clear(className, mn.name + mn.desc);

    // Actually this should be done automatically by the ClassWriter...
    // +2 because we might do a DUP2
    mn.maxStack += 1;
}

From source file:org.evosuite.instrumentation.coverage.DefUseInstrumentation.java

License:Open Source License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override/*from   w ww.  j ava 2 s.  c o  m*/
public void analyze(ClassLoader classLoader, MethodNode mn, String className, String methodName, int access) {
    RawControlFlowGraph completeCFG = GraphPool.getInstance(classLoader).getRawCFG(className, methodName);
    logger.info("Applying DefUse instrumentation on CFG with " + completeCFG.vertexCount() + " nodes");
    Iterator<AbstractInsnNode> j = mn.instructions.iterator();
    while (j.hasNext()) {
        AbstractInsnNode in = j.next();
        for (BytecodeInstruction v : completeCFG.vertexSet()) {

            if ((ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE)
                    || ArrayUtil.contains(Properties.CRITERION, Criterion.ALLDEFS)) && in.equals(v.getASMNode())
                    && v.isDefUse()) {

                boolean isValidDU = false;

                //               if(v.isLocalArrayDefinition()) {
                //                  LoggingUtils.getEvoLogger().info(
                //                     "LOCAL ARRAY VAR DEF " + v.toString()+" loaded by "+v.getSourceOfStackInstruction(2).toString());
                //               }

                if (v.isMethodCallOfField()) {
                    // keep track of field method calls, though we do not
                    // know
                    // how to handle them at this point during the analysis
                    // (need complete CCFGs first)
                    isValidDU = DefUsePool.addAsFieldMethodCall(v);
                } else {
                    // keep track of uses
                    if (v.isUse())
                        isValidDU = DefUsePool.addAsUse(v);
                    // keep track of definitions
                    if (v.isDefinition())
                        isValidDU = DefUsePool.addAsDefinition(v) || isValidDU;
                }
                if (isValidDU) {
                    boolean staticContext = v.isStaticDefUse() || ((access & Opcodes.ACC_STATIC) > 0);
                    // adding instrumentation for defuse-coverage
                    InsnList instrumentation = getInstrumentation(v, staticContext, className, methodName, mn);
                    if (instrumentation == null)
                        throw new IllegalStateException("error instrumenting node " + v.toString());

                    if (v.isMethodCallOfField())
                        mn.instructions.insertBefore(v.getASMNode(), instrumentation);
                    else if (v.isArrayStoreInstruction())
                        mn.instructions.insertBefore(v.getSourceOfArrayReference().getASMNode(),
                                instrumentation);

                    // Loading of an array is already handled by ALOAD
                    // AILOAD would only be needed if we define DU pairs on 
                    // array indices
                    //                  else if(v.isArrayLoadInstruction())
                    //                     mn.instructions.insertBefore(v.getSourceOfArrayReference().getASMNode(), instrumentation);
                    else if (v.isUse())
                        mn.instructions.insert(v.getASMNode(), instrumentation);
                    else
                        mn.instructions.insertBefore(v.getASMNode(), instrumentation);
                }
            }
        }
    }
}

From source file:org.evosuite.instrumentation.coverage.MutationInstrumentation.java

License:Open Source License

private void getFrames(MethodNode mn, String className) {
    try {/*w  w w.j  a  va2s . c o  m*/
        Analyzer a = new Analyzer(
                new BooleanValueInterpreter(mn.desc, (mn.access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC));
        a.analyze(className, mn);
        this.frames = a.getFrames();
    } catch (Exception e) {
        logger.info("1. Error during analysis: " + e);
        //e.printStackTrace();
        // TODO: Handle error
    }

}

From source file:org.evosuite.instrumentation.CreateClassResetClassAdapter.java

License:Open Source License

private boolean hasStaticModifier(int access) {
    return (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC;
}

From source file:org.evosuite.instrumentation.CreateClassResetClassAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from  w  w w  .  j  ava 2  s .  c om*/
public MethodVisitor visitMethod(int methodAccess, String methodName, String descriptor, String signature,
        String[] exceptions) {

    MethodVisitor mv = super.visitMethod(methodAccess, methodName, descriptor, signature, exceptions);

    if (methodName.equals("<clinit>") && !isInterface) {
        clinitFound = true;
        logger.info("Found static initializer in class {}", className);
        //determineSerialisableUID();

        // duplicates existing <clinit>
        MethodVisitor visitMethod = super.visitMethod(methodAccess | Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
                ClassResetter.STATIC_RESET, descriptor, signature, exceptions);

        CreateClassResetMethodAdapter staticResetMethodAdapter = new CreateClassResetMethodAdapter(visitMethod,
                className, this.static_fields, finalFields);

        if (this.removeUpdatesOnFinalFields) {
            MethodVisitor mv2 = new RemoveFinalMethodAdapter(className, staticResetMethodAdapter, finalFields);

            return new MultiMethodVisitor(mv2, mv);
        } else {
            return new MultiMethodVisitor(staticResetMethodAdapter, mv);
        }
    }
    return mv;
}

From source file:org.evosuite.instrumentation.CreateClassResetClassAdapter.java

License:Open Source License

private void createSerialisableUID() {
    // Only add this for serialisable classes
    if (serialUID < 0)
        return;/*w w  w  .  ja  v  a2  s .  c o  m*/
    /*
     * If the class is serializable, then adding a hashCode will change the serialVersionUID
     * if it is not defined in the class. Hence, if it is not defined, we have to define it to
     * avoid problems in serialising the class.
     */
    logger.info("Adding serialId to class {}", className);
    visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "serialVersionUID", "J", null,
            serialUID);
}

From source file:org.evosuite.instrumentation.CreateClassResetClassAdapter.java

License:Open Source License

private void createEmptyStaticReset() {
    logger.info("Creating brand-new static initializer in class {}", className);
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, ClassResetter.STATIC_RESET,
            "()V", null, null);
    mv.visitCode();//from  w w  w.  j  a v  a2 s .  com
    for (StaticField staticField : static_fields) {

        if (!finalFields.contains(staticField.name)) {

            logger.info("Adding bytecode for initializing field {}", staticField.name);

            if (staticField.value != null) {
                mv.visitLdcInsn(staticField.value);
            } else {
                Type type = Type.getType(staticField.desc);
                switch (type.getSort()) {
                case Type.BOOLEAN:
                case Type.BYTE:
                case Type.CHAR:
                case Type.SHORT:
                case Type.INT:
                    mv.visitInsn(Opcodes.ICONST_0);
                    break;
                case Type.FLOAT:
                    mv.visitInsn(Opcodes.FCONST_0);
                    break;
                case Type.LONG:
                    mv.visitInsn(Opcodes.LCONST_0);
                    break;
                case Type.DOUBLE:
                    mv.visitInsn(Opcodes.DCONST_0);
                    break;
                case Type.ARRAY:
                case Type.OBJECT:
                    mv.visitInsn(Opcodes.ACONST_NULL);
                    break;
                }
            }
            mv.visitFieldInsn(Opcodes.PUTSTATIC, className, staticField.name, staticField.desc);

        }
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

}

From source file:org.evosuite.instrumentation.EndOfClassInitializerVisitor.java

License:Open Source License

private static boolean isStatic(int access) {
    return (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC;
}

From source file:org.evosuite.instrumentation.EndOfClassInitializerVisitor.java

License:Open Source License

private void createEmptyClassInit() {
    logger.info("Creating <clinit> in class " + className);
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();// w w  w.  ja  va2s  . c o m

    String executionTracerClassName = ExecutionTracer.class.getName().replace('.', '/');
    String executionTracerDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class));

    String classNameWithDots = className.replace('/', '.');
    mv.visitLdcInsn(classNameWithDots);
    mv.visitMethodInsn(INVOKESTATIC, executionTracerClassName, EXIT_CLASS_INIT, executionTracerDescriptor,
            false);

    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

}

From source file:org.evosuite.instrumentation.MethodEntryAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override//w w  w.  j ava  2 s  . co m
public void onMethodEnter() {

    if (methodName.equals("<clinit>"))
        return; // FIXXME: Should we call super.onMethodEnter() here?

    mv.visitLdcInsn(className);
    mv.visitLdcInsn(fullMethodName);
    if ((access & Opcodes.ACC_STATIC) > 0) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
    }
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ExecutionTracer.class),
            "enteredMethod", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V", false);

    super.onMethodEnter();
}