Java Code Examples for org.apache.bcel.generic.MethodGen#isStatic()

The following examples show how to use org.apache.bcel.generic.MethodGen#isStatic() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValueNumberAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public ValueNumberAnalysis(MethodGen methodGen, DepthFirstSearch dfs, LoadedFieldSet loadedFieldSet,
        RepositoryLookupFailureCallback lookupFailureCallback) {

    super(dfs);
    this.methodGen = methodGen;
    this.factory = new ValueNumberFactory();
    ValueNumberCache cache = new ValueNumberCache();
    this.visitor = new ValueNumberFrameModelingVisitor(methodGen, factory, cache, loadedFieldSet, lookupFailureCallback);

    int numLocals = methodGen.getMaxLocals();
    this.entryLocalValueList = new ValueNumber[numLocals];
    for (int i = 0; i < numLocals; ++i) {
        this.entryLocalValueList[i] = factory.createFreshValue();
    }

    this.exceptionHandlerValueNumberMap = new IdentityHashMap<>();

    // For non-static methods, keep track of which value represents the
    // "this" reference
    if (!methodGen.isStatic()) {
        this.thisValue = entryLocalValueList[0];
    }

    this.factAtLocationMap = new HashMap<>();
    this.factAfterLocationMap = new HashMap<>();
    if (DEBUG) {
        System.out.println("VNA Analysis " + methodGen.getClassName() + "." + methodGen.getName() + " : "
                + methodGen.getSignature());
    }

}
 
Example 2
Source File: LockAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public LockAnalysis(MethodGen methodGen, ValueNumberDataflow vnaDataflow, DepthFirstSearch dfs) {
    super(dfs);
    this.methodGen = methodGen;
    this.vnaDataflow = vnaDataflow;
    this.vna = vnaDataflow.getAnalysis();
    this.isSynchronized = methodGen.isSynchronized();
    this.isStatic = methodGen.isStatic();
    if (DEBUG) {
        System.out.println("Analyzing Locks in " + methodGen.getClassName() + "." + methodGen.getName());
    }
}
 
Example 3
Source File: BCELPerfTest.java    From annotation-tools with MIT License 4 votes vote down vote up
byte[] counterAdaptClass(final InputStream is, final String name)
        throws Exception
{
    JavaClass jc = new ClassParser(is, name + ".class").parse();
    ClassGen cg = new ClassGen(jc);
    ConstantPoolGen cp = cg.getConstantPool();
    if (!cg.isInterface()) {
        FieldGen fg = new FieldGen(ACC_PUBLIC,
                Type.getType("I"),
                "_counter",
                cp);
        cg.addField(fg.getField());
    }
    Method[] ms = cg.getMethods();
    for (int j = 0; j < ms.length; ++j) {
        MethodGen mg = new MethodGen(ms[j], cg.getClassName(), cp);
        if (!mg.getName().equals("<init>") && !mg.isStatic()
                && !mg.isAbstract() && !mg.isNative())
        {
            if (mg.getInstructionList() != null) {
                InstructionList il = new InstructionList();
                il.append(new ALOAD(0));
                il.append(new ALOAD(0));
                il.append(new GETFIELD(cp.addFieldref(name, "_counter", "I")));
                il.append(new ICONST(1));
                il.append(new IADD());
                il.append(new PUTFIELD(cp.addFieldref(name, "_counter", "I")));
                mg.getInstructionList().insert(il);
                mg.setMaxStack(Math.max(mg.getMaxStack(), 2));
                boolean lv = ms[j].getLocalVariableTable() == null;
                boolean ln = ms[j].getLineNumberTable() == null;
                if (lv) {
                    mg.removeLocalVariables();
                }
                if (ln) {
                    mg.removeLineNumbers();
                }
                cg.replaceMethod(ms[j], mg.getMethod());
            }
        }
    }
    return cg.getJavaClass().getBytes();
}
 
Example 4
Source File: BCELPerfTest.java    From annotation-tools with MIT License 4 votes vote down vote up
byte[] counterAdaptClass(final InputStream is, final String name)
        throws Exception
{
    JavaClass jc = new ClassParser(is, name + ".class").parse();
    ClassGen cg = new ClassGen(jc);
    ConstantPoolGen cp = cg.getConstantPool();
    if (!cg.isInterface()) {
        FieldGen fg = new FieldGen(ACC_PUBLIC,
                Type.getType("I"),
                "_counter",
                cp);
        cg.addField(fg.getField());
    }
    Method[] ms = cg.getMethods();
    for (int j = 0; j < ms.length; ++j) {
        MethodGen mg = new MethodGen(ms[j], cg.getClassName(), cp);
        if (!mg.getName().equals("<init>") && !mg.isStatic()
                && !mg.isAbstract() && !mg.isNative())
        {
            if (mg.getInstructionList() != null) {
                InstructionList il = new InstructionList();
                il.append(new ALOAD(0));
                il.append(new ALOAD(0));
                il.append(new GETFIELD(cp.addFieldref(name, "_counter", "I")));
                il.append(new ICONST(1));
                il.append(new IADD());
                il.append(new PUTFIELD(cp.addFieldref(name, "_counter", "I")));
                mg.getInstructionList().insert(il);
                mg.setMaxStack(Math.max(mg.getMaxStack(), 2));
                boolean lv = ms[j].getLocalVariableTable() == null;
                boolean ln = ms[j].getLineNumberTable() == null;
                if (lv) {
                    mg.removeLocalVariables();
                }
                if (ln) {
                    mg.removeLineNumbers();
                }
                cg.replaceMethod(ms[j], mg.getMethod());
            }
        }
    }
    return cg.getJavaClass().getBytes();
}
 
Example 5
Source File: Pass3bVerifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Pass 3b implements the data flow analysis as described in the Java Virtual
 * Machine Specification, Second Edition.
 * Later versions will use LocalVariablesInfo objects to verify if the
 * verifier-inferred types and the class file's debug information (LocalVariables
 * attributes) match [TODO].
 *
 * @see org.apache.bcel.verifier.statics.LocalVariablesInfo
 * @see org.apache.bcel.verifier.statics.Pass2Verifier#getLocalVariablesInfo(int)
 */
@Override
public VerificationResult do_verify() {
    if (! myOwner.doPass3a(methodNo).equals(VerificationResult.VR_OK)) {
        return VerificationResult.VR_NOTYET;
    }

    // Pass 3a ran before, so it's safe to assume the JavaClass object is
    // in the BCEL repository.
    JavaClass jc;
    try {
        jc = Repository.lookupClass(myOwner.getClassName());
    } catch (final ClassNotFoundException e) {
        // FIXME: maybe not the best way to handle this
        throw new AssertionViolatedException("Missing class: " + e, e);
    }

    final ConstantPoolGen constantPoolGen = new ConstantPoolGen(jc.getConstantPool());
    // Init Visitors
    final InstConstraintVisitor icv = new InstConstraintVisitor();
    icv.setConstantPoolGen(constantPoolGen);

    final ExecutionVisitor ev = new ExecutionVisitor();
    ev.setConstantPoolGen(constantPoolGen);

    final Method[] methods = jc.getMethods(); // Method no "methodNo" exists, we ran Pass3a before on it!

    try{

        final MethodGen mg = new MethodGen(methods[methodNo], myOwner.getClassName(), constantPoolGen);

        icv.setMethodGen(mg);

        ////////////// DFA BEGINS HERE ////////////////
        if (! (mg.isAbstract() || mg.isNative()) ) { // IF mg HAS CODE (See pass 2)

            final ControlFlowGraph cfg = new ControlFlowGraph(mg);

            // Build the initial frame situation for this method.
            final Frame f = new Frame(mg.getMaxLocals(),mg.getMaxStack());
            if ( !mg.isStatic() ) {
                if (mg.getName().equals(Const.CONSTRUCTOR_NAME)) {
                    Frame.setThis(new UninitializedObjectType(ObjectType.getInstance(jc.getClassName())));
                    f.getLocals().set(0, Frame.getThis());
                }
                else{
                    Frame.setThis(null);
                    f.getLocals().set(0, ObjectType.getInstance(jc.getClassName()));
                }
            }
            final Type[] argtypes = mg.getArgumentTypes();
            int twoslotoffset = 0;
            for (int j=0; j<argtypes.length; j++) {
                if (argtypes[j] == Type.SHORT || argtypes[j] == Type.BYTE ||
                    argtypes[j] == Type.CHAR || argtypes[j] == Type.BOOLEAN) {
                    argtypes[j] = Type.INT;
                }
                f.getLocals().set(twoslotoffset + j + (mg.isStatic()?0:1), argtypes[j]);
                if (argtypes[j].getSize() == 2) {
                    twoslotoffset++;
                    f.getLocals().set(twoslotoffset + j + (mg.isStatic()?0:1), Type.UNKNOWN);
                }
            }
            circulationPump(mg,cfg, cfg.contextOf(mg.getInstructionList().getStart()), f, icv, ev);
        }
    }
    catch (final VerifierConstraintViolatedException ce) {
        ce.extendMessage("Constraint violated in method '"+methods[methodNo]+"':\n","");
        return new VerificationResult(VerificationResult.VERIFIED_REJECTED, ce.getMessage());
    }
    catch (final RuntimeException re) {
        // These are internal errors

        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);
        re.printStackTrace(pw);

        throw new AssertionViolatedException("Some RuntimeException occured while verify()ing class '"+jc.getClassName()+
            "', method '"+methods[methodNo]+"'. Original RuntimeException's stack trace:\n---\n"+sw+"---\n", re);
    }
    return VerificationResult.VR_OK;
}
 
Example 6
Source File: BugInstance.java    From spotbugs with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Add a method annotation. If this is the first method annotation added, it
 * becomes the primary method annotation. If the method has source line
 * information, then a SourceLineAnnotation is added to the method.
 *
 * @param methodGen
 *            the MethodGen object for the method
 * @param sourceFile
 *            source file method is defined in
 * @return this object
 */
@Nonnull
public BugInstance addMethod(MethodGen methodGen, String sourceFile) {
    String className = methodGen.getClassName();
    MethodAnnotation methodAnnotation = new MethodAnnotation(className, methodGen.getName(), methodGen.getSignature(),
            methodGen.isStatic());
    addMethod(methodAnnotation);
    addSourceLinesForMethod(methodAnnotation, SourceLineAnnotation.fromVisitedMethod(methodGen, sourceFile));
    return this;
}