org.apache.bcel.generic.GETFIELD Java Examples

The following examples show how to use org.apache.bcel.generic.GETFIELD. 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 6 votes vote down vote up
@Override
public void visitGETFIELD(GETFIELD obj) {
    XField xfield = Hierarchy.findXField(obj, getCPG());
    if (xfield != null) {
        if (xfield.isVolatile()) {
            getFrame().killAllLoads();
        }

        if (doRedundantLoadElimination()) {
            loadInstanceField(xfield, obj);
            return;
        }
    }

    handleNormalInstruction(obj);
}
 
Example #2
Source File: BetterCFGBuilder2.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @param handle instruction handle which loads the object for further GETFIELD/PUTFIELD operation
 * @return true if this object is known to be non-null
 */
private boolean isSafeFieldSource(InstructionHandle handle) {
    while (handle != null && handle.getInstruction().getOpcode() == Const.DUP) {
        // Some compilers generate DUP for field increment code like
        // ALOAD_0 / DUP / GETFIELD x / ICONST_1 / IADD / PUTFIELD x
        handle = handle.getPrev();
    }
    if (handle == null) {
        return false;
    }
    Instruction inst = handle.getInstruction();
    if (inst.getOpcode() == Const.ALOAD_0) {
        return true;
    }
    return inst instanceof GETFIELD && ((GETFIELD) inst).getFieldName(cpg).startsWith("this$");
}
 
Example #3
Source File: TaintFrameModelingVisitor.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void visitGETFIELD(GETFIELD obj) {
    Taint.State state = taintConfig.getClassTaintState(obj.getSignature(cpg), Taint.State.UNKNOWN);
    Taint taint = new Taint(state);

    if (!state.equals(Taint.State.SAFE)){
        taint.addLocation(getTaintLocation(), false);
    }
    if (FindSecBugsGlobalConfig.getInstance().isDebugTaintState()) {
        taint.setDebugInfo("." + obj.getFieldName(cpg));
    }
    modelInstruction(obj, getNumWordsConsumed(obj), getNumWordsProduced(obj), taint);
}
 
Example #4
Source File: FindRefComparison.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitGETFIELD(GETFIELD obj) {
    Type type = obj.getType(getCPG());
    if (STRING_SIGNATURE.equals(type.getSignature())) {
        handleLoad(obj);
    } else {
        XField xf = XFactory.createXField(obj, cpg);
        if (xf.isFinal()) {
            FieldSummary fieldSummary = AnalysisContext.currentAnalysisContext().getFieldSummary();
            Item summary = fieldSummary.getSummary(xf);
            if (summary.isNull()) {
                consumeStack(obj);
                pushValue(TypeFrame.getNullType());
                return;
            }

            String slashedClassName = ClassName.fromFieldSignature(type.getSignature());
            if (slashedClassName != null) {
                String dottedClassName = ClassName.toDottedClassName(slashedClassName);
                if (DEFAULT_SUSPICIOUS_SET.contains(dottedClassName)) {
                    type = new FinalConstant(dottedClassName, xf);
                    consumeStack(obj);
                    pushValue(type);
                    return;
                }
            }
        }
        super.visitGETFIELD(obj);
    }
}
 
Example #5
Source File: IsNullValueFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitGETFIELD(GETFIELD obj) {
    if (getNumWordsProduced(obj) != 1) {
        super.visitGETFIELD(obj);
        return;
    }

    if (checkForKnownValue(obj)) {
        return;
    }

    XField field = XFactory.createXField(obj, cpg);

    NullnessAnnotation annotation = AnalysisContext.currentAnalysisContext().getNullnessAnnotationDatabase()
            .getResolvedAnnotation(field, false);
    if (annotation == NullnessAnnotation.NONNULL) {
        modelNormalInstruction(obj, getNumWordsConsumed(obj), 0);
        produce(IsNullValue.nonNullValue());
    } else if (annotation == NullnessAnnotation.CHECK_FOR_NULL) {
        modelNormalInstruction(obj, getNumWordsConsumed(obj), 0);
        produce(IsNullValue.nullOnSimplePathValue().markInformationAsComingFromFieldValue(field));
    } else {

        super.visitGETFIELD(obj);
    }

}
 
Example #6
Source File: FieldAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Is the given instruction a read of a field?
 *
 * @param ins
 *            the Instruction to check
 * @param cpg
 *            ConstantPoolGen of the method containing the instruction
 * @return the Field if the instruction is a read of a field, null otherwise
 */
public static FieldAnnotation isRead(Instruction ins, ConstantPoolGen cpg) {
    if (ins instanceof GETFIELD || ins instanceof GETSTATIC) {
        FieldInstruction fins = (FieldInstruction) ins;
        String className = fins.getClassName(cpg);
        return new FieldAnnotation(className, fins.getName(cpg), fins.getSignature(cpg), fins instanceof GETSTATIC);
    } else {
        return null;
    }
}
 
Example #7
Source File: GETFIELDReference.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param aInstruction
 * @param aPoolGen
 */
public GETFIELDReference(
    GETFIELD aInstruction,
    ConstantPoolGen aPoolGen)
{
    super(aInstruction, aPoolGen);
}
 
Example #8
Source File: GETFIELDReference.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param aInstruction
 * @param aPoolGen
 */
public GETFIELDReference(
    GETFIELD aInstruction,
    ConstantPoolGen aPoolGen)
{
    super(aInstruction, aPoolGen);
}
 
Example #9
Source File: FindUnreleasedLock.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public boolean ignoreExceptionEdge(Edge edge, Lock resource, ConstantPoolGen cpg) {

    try {
        Location location = cfg.getExceptionThrowerLocation(edge);
        if (DEBUG) {
            System.out.println("Exception thrower location: " + location);
        }
        Instruction ins = location.getHandle().getInstruction();

        if (ins instanceof GETFIELD) {
            GETFIELD insGetfield = (GETFIELD) ins;
            String fieldName = insGetfield.getFieldName(cpg);
            if (DEBUG) {
                System.out.println("Inspecting GETFIELD of " + fieldName + " at " + location);
            }
            // Ignore exceptions from getfield instructions where the
            // object reference is known not to be null
            if ("lock".equals(fieldName)) {
                return true;
            }
            IsNullValueFrame frame = isNullDataflow.getFactAtLocation(location);
            if (!frame.isValid()) {
                return false;
            }
            IsNullValue receiver = frame.getInstance(ins, cpg);
            boolean notNull = receiver.isDefinitelyNotNull();
            if (DEBUG && notNull) {
                System.out.println("Ignoring exception from non-null GETFIELD");
            }
            return notNull;
        } else if (ins instanceof InvokeInstruction) {
            InvokeInstruction iins = (InvokeInstruction) ins;
            String methodName = iins.getMethodName(cpg);
            // System.out.println("Method " + methodName);
            if (methodName.startsWith("access$")) {
                return true;
            }
            if ("readLock".equals(methodName) || "writeLock".equals(methodName)) {
                return true;
            }
            if ("lock".equals(methodName) || "unlock".equals(methodName)) {
                return true;
            }
        }
        if (DEBUG) {
            System.out.println("FOUND Exception thrower at: " + location);
        }
    } catch (DataflowAnalysisException e) {
        AnalysisContext.logError("Error while looking for exception edge", e);
    }

    return false;
}
 
Example #10
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 #11
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 visitGETFIELD(GETFIELD aGETFIELD)
{
    addFieldReference(
        new GETFIELDReference(aGETFIELD, mCurrentPoolGen));
}
 
Example #12
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 #13
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 #14
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 visitGETFIELD(GETFIELD aGETFIELD)
{
    addFieldReference(
        new GETFIELDReference(aGETFIELD, mCurrentPoolGen));
}