org.apache.bcel.generic.PUTFIELD Java Examples

The following examples show how to use org.apache.bcel.generic.PUTFIELD. 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: ValueNumberFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitPUTFIELD(PUTFIELD obj) {
    if (doForwardSubstitution()) {
        XField xfield = Hierarchy.findXField(obj, getCPG());
        if (xfield != null) {
            storeInstanceField(xfield, obj, false);
            return;
        }

    }
    handleNormalInstruction(obj);
}
 
Example #2
Source File: IsNullValueFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitPUTFIELD(PUTFIELD obj) {
    if (getNumWordsConsumed(obj) != 2) {
        super.visitPUTFIELD(obj);
        return;
    }

    IsNullValue nullValueStored = null;
    try {
        nullValueStored = getFrame().getTopValue();
    } catch (DataflowAnalysisException e1) {
        AnalysisContext.logError("Oops", e1);
    }
    super.visitPUTFIELD(obj);
    XField field = XFactory.createXField(obj, cpg);
    if (nullValueStored != null && ValueNumberAnalysisFeatures.REDUNDANT_LOAD_ELIMINATION) {
        try {
            ValueNumberFrame vnaFrameBefore = vnaDataflow.getFactAtLocation(getLocation());
            ValueNumber refValue = vnaFrameBefore.getStackValue(1);
            AvailableLoad load = new AvailableLoad(refValue, field);
            ValueNumberFrame vnaFrameAfter = vnaDataflow.getFactAfterLocation(getLocation());
            ValueNumber[] newValueNumbersForField = vnaFrameAfter.getAvailableLoad(load);
            if (newValueNumbersForField != null && trackValueNumbers) {
                for (ValueNumber v : newValueNumbersForField) {
                    getFrame().setKnownValue(v, nullValueStored);
                }
            }
        } catch (DataflowAnalysisException e) {
            AnalysisContext.logError("Oops", e);
        }
    }
}
 
Example #3
Source File: FieldAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Is the instruction a write of a field?
 *
 * @param ins
 *            the Instruction to check
 * @param cpg
 *            ConstantPoolGen of the method containing the instruction
 * @return the Field if instruction is a write of a field, null otherwise
 */
public static FieldAnnotation isWrite(Instruction ins, ConstantPoolGen cpg) {
    if (ins instanceof PUTFIELD || ins instanceof PUTSTATIC) {
        FieldInstruction fins = (FieldInstruction) ins;
        String className = fins.getClassName(cpg);
        return new FieldAnnotation(className, fins.getName(cpg), fins.getSignature(cpg), fins instanceof PUTSTATIC);
    } else {
        return null;
    }
}
 
Example #4
Source File: PUTFIELDReference.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param aInstruction
 * @param aPoolGen
 */
public PUTFIELDReference(
    PUTFIELD aInstruction,
    ConstantPoolGen aPoolGen)
{
    super(aInstruction, aPoolGen);
}
 
Example #5
Source File: PUTFIELDReference.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param aInstruction
 * @param aPoolGen
 */
public PUTFIELDReference(
    PUTFIELD aInstruction,
    ConstantPoolGen aPoolGen)
{
    super(aInstruction, aPoolGen);
}
 
Example #6
Source File: ResourceValueFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visitPUTFIELD(PUTFIELD putfield) {
    handleFieldStore(putfield);
}
 
Example #7
Source File: UnconditionalValueDerefAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock, UnconditionalValueDerefSet fact)
        throws DataflowAnalysisException {

    Instruction instruction = handle.getInstruction();
    if (fact.isTop()) {
        return;
    }
    Location location = new Location(handle, basicBlock);

    // If this is a call to an assertion method,
    // change the dataflow value to be TOP.
    // We don't want to report future derefs that would
    // be guaranteed only if the assertion methods
    // returns normally.
    // TODO: at some point, evaluate whether we should revisit this
    if (isAssertion(handle) // || handle.getInstruction() instanceof ATHROW
    ) {
        if (DEBUG) {
            System.out.println("MAKING BOTTOM0 AT: " + location);
        }
        fact.clear();
        return;
    }

    // Get value number frame
    ValueNumberFrame vnaFrame = vnaDataflow.getFactAtLocation(location);
    if (!vnaFrame.isValid()) {
        if (DEBUG) {
            System.out.println("MAKING TOP1 AT: " + location);
        }
        // Probably dead code.
        // Assume this location can't be reached.
        makeFactTop(fact);
        return;
    }
    if (isNullCheck(handle, methodGen.getConstantPool())) {
        handleNullCheck(location, vnaFrame, fact);
    }

    // Check for calls to a method that unconditionally dereferences
    // a parameter. Mark any such arguments as derefs.
    if (CHECK_CALLS && instruction instanceof InvokeInstruction) {
        checkUnconditionalDerefDatabase(location, vnaFrame, fact);
    }

    // If this is a method call instruction,
    // check to see if any of the parameters are @NonNull,
    // and treat them as dereferences.
    if (CHECK_ANNOTATIONS && instruction instanceof InvokeInstruction) {
        checkNonNullParams(location, vnaFrame, fact);
    }

    if (CHECK_ANNOTATIONS && instruction instanceof ARETURN) {
        XMethod thisMethod = XFactory.createXMethod(methodGen);
        checkNonNullReturnValue(thisMethod, location, vnaFrame, fact);
    }

    if (CHECK_ANNOTATIONS && (instruction instanceof PUTFIELD || instruction instanceof PUTSTATIC)) {
        checkNonNullPutField(location, vnaFrame, fact);
    }

    // Check to see if an instance value is dereferenced here
    checkInstance(location, vnaFrame, fact);

    /*
    if (false) {
        fact.cleanDerefSet(location, vnaFrame);
    }*/

    if (DEBUG && fact.isTop()) {
        System.out.println("MAKING TOP2 At: " + location);
    }

}
 
Example #8
Source File: BetterCFGBuilder2.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Return whether or not the given instruction can throw exceptions.
 *
 * @param handle
 *            the instruction
 * @return true if the instruction can throw an exception, false otherwise
 * @throws CFGBuilderException
 */
private boolean isPEI(InstructionHandle handle) throws CFGBuilderException {
    Instruction ins = handle.getInstruction();

    if (!(ins instanceof ExceptionThrower)) {
        return false;
    }

    if (ins instanceof NEW) {
        return false;
    }
    // if (ins instanceof ATHROW) return false;
    if (ins instanceof GETSTATIC) {
        return false;
    }
    if (ins instanceof PUTSTATIC) {
        return false;
    }
    if (ins instanceof ReturnInstruction) {
        return false;
    }
    if (ins instanceof INSTANCEOF) {
        return false;
    }
    if (ins instanceof MONITOREXIT) {
        return false;
    }
    if (ins instanceof LDC) {
        return false;
    }
    if (ins instanceof GETFIELD && !methodGen.isStatic()) {
        // Assume that GETFIELD on this object is not PEI
        return !isSafeFieldSource(handle.getPrev());
    }
    if (ins instanceof PUTFIELD && !methodGen.isStatic()) {
        // Assume that PUTFIELD on this object is not PEI
        int depth = ins.consumeStack(cpg);
        for (InstructionHandle prev = handle.getPrev(); prev != null; prev = prev.getPrev()) {
            Instruction prevInst = prev.getInstruction();
            if (prevInst instanceof BranchInstruction) {
                if (prevInst instanceof GotoInstruction) {
                    // Currently we support only jumps to the PUTFIELD itself
                    // This will cover simple cases like this.a = flag ? foo : bar
                    if (((BranchInstruction) prevInst).getTarget() == handle) {
                        depth = ins.consumeStack(cpg);
                    } else {
                        return true;
                    }
                } else if (!(prevInst instanceof IfInstruction)) {
                    // As IF instructions may fall through then the stack depth remains unchanged
                    // Actually we should not go here for normal Java bytecode: switch or jsr should not appear in this context
                    return true;
                }
            }
            depth = depth - prevInst.produceStack(cpg) + prevInst.consumeStack(cpg);
            if (depth < 1) {
                throw new CFGBuilderException("Invalid stack at " + prev + " when checking " + handle);
            }
            if (depth == 1) {
                InstructionHandle prevPrev = prev.getPrev();
                if (prevPrev != null && prevPrev.getInstruction() instanceof BranchInstruction) {
                    continue;
                }
                return !isSafeFieldSource(prevPrev);
            }
        }
    }
    return true;
}
 
Example #9
Source File: ReferenceVisitor.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/** @see org.apache.bcel.generic.Visitor */
public void visitPUTFIELD(PUTFIELD aPUTFIELD)
{
    addFieldReference(
        new PUTFIELDReference(aPUTFIELD, mCurrentPoolGen));
}
 
Example #10
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 #11
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 #12
Source File: ReferenceVisitor.java    From contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/** @see org.apache.bcel.generic.Visitor */
public void visitPUTFIELD(PUTFIELD aPUTFIELD)
{
    addFieldReference(
        new PUTFIELDReference(aPUTFIELD, mCurrentPoolGen));
}