Java Code Examples for org.apache.bcel.Const#PUTFIELD

The following examples show how to use org.apache.bcel.Const#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: MutableEnum.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (skip) {
        return;
    }
    if (isBranch(seen) || seen == Const.ATHROW || isReturn(seen)) {
        skip = true;
    }
    if (seen == Const.PUTFIELD) {
        XField xField = getXFieldOperand();
        if (xField != null && xField.getClassDescriptor().getClassName().equals(getClassName())) {
            Item val = getStack().getStackItem(0);
            if (val.isInitialParameter()) {
                reporter.reportBug(new BugInstance("ME_ENUM_FIELD_SETTER", NORMAL_PRIORITY).addClassAndMethod(this).addField(xField)
                        .addSourceLine(this));
            }
        }
    }
}
 
Example 2
Source File: BackwardTypeQualifierDataflowAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void registerInstructionSinks() throws DataflowAnalysisException {
    TypeQualifierAnnotation returnValueAnnotation = null;
    if (!xmethod.getSignature().endsWith(")V")) {
        returnValueAnnotation = TypeQualifierApplications.getEffectiveTypeQualifierAnnotation(xmethod, typeQualifierValue);
    }

    for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
        Location location = i.next();

        Instruction ins = location.getHandle().getInstruction();

        if (ins instanceof ReturnInstruction && !(ins instanceof RETURN)) {
            // Return instruction which returns a value
            modelReturn(returnValueAnnotation, location);
        } else {
            short opcode = ins.getOpcode();

            if (opcode == Const.PUTFIELD || opcode == Const.PUTSTATIC) {
                modelFieldStore(location);
            } else if (location.getHandle().getInstruction() instanceof InvokeInstruction) {
                modelArguments(location);
            }
        }
    }
}
 
Example 3
Source File: FinalizerNullsFields.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (state == 0 && seen == Const.ALOAD_0) {
        state++;
    } else if (state == 1 && seen == Const.ACONST_NULL) {
        state++;
    } else if (state == 2 && seen == Const.PUTFIELD) {
        bugAccumulator.accumulateBug(
                new BugInstance(this, "FI_FINALIZER_NULLS_FIELDS", NORMAL_PRIORITY).addClassAndMethod(this)
                        .addReferencedField(this), this);
        sawFieldNulling = true;
        state = 0;
    } else if (seen == Const.RETURN) {
        state = 0;
    } else {
        state = 0;
        sawAnythingElse = true;
    }
}
 
Example 4
Source File: InstructionFactory.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/** Create a field instruction.
 *
 * @param class_name name of the accessed class
 * @param name name of the referenced field
 * @param type  type of field
 * @param kind how to access, i.e., GETFIELD, PUTFIELD, GETSTATIC, PUTSTATIC
 * @see Const
 */
public FieldInstruction createFieldAccess( final String class_name, final String name, final Type type, final short kind ) {
    int index;
    final String signature = type.getSignature();
    index = cp.addFieldref(class_name, name, signature);
    switch (kind) {
        case Const.GETFIELD:
            return new GETFIELD(index);
        case Const.PUTFIELD:
            return new PUTFIELD(index);
        case Const.GETSTATIC:
            return new GETSTATIC(index);
        case Const.PUTSTATIC:
            return new PUTSTATIC(index);
        default:
            throw new IllegalArgumentException("Unknown getfield kind:" + kind);
    }
}
 
Example 5
Source File: RepeatedConditionals.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private boolean hasSideEffect(int seen) {
    if (seen == Const.INVOKEVIRTUAL || seen == Const.INVOKESPECIAL || seen == Const.INVOKEINTERFACE || seen == Const.INVOKESTATIC) {
        return noSideEffectMethods.is(getMethodDescriptorOperand(), MethodSideEffectStatus.SE, MethodSideEffectStatus.OBJ);
    }
    return isRegisterStore() || isReturn(seen) || isSwitch(seen) || seen == Const.INVOKEDYNAMIC || seen == Const.PUTFIELD
            || seen == Const.PUTSTATIC;
}
 
Example 6
Source File: NoteDirectlyRelevantTypeQualifiers.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    switch (seen) {
    case Const.INVOKEINTERFACE:
    case Const.INVOKEVIRTUAL:
    case Const.INVOKESTATIC:
    case Const.INVOKESPECIAL:
        // We don't need to look for method invocations
        // if Analysis.FIND_EFFECTIVE_RELEVANT_QUALIFIERS is enabled -
        // that will build an interprocedural call graph which
        // we'll use at a later point to find relevant qualifiers
        // stemming from called methods.

        if (!Analysis.FIND_EFFECTIVE_RELEVANT_QUALIFIERS) {
            XMethod m = getXMethodOperand();
            if (m != null) {
                updateApplicableAnnotations(m);
            }
        }
        break;

    case Const.GETSTATIC:
    case Const.PUTSTATIC:
    case Const.GETFIELD:
    case Const.PUTFIELD: {
        XField f = getXFieldOperand();
        if (f != null) {
            Collection<TypeQualifierAnnotation> annotations = TypeQualifierApplications.getApplicableApplications(f);
            Analysis.addKnownTypeQualifiers(applicableApplications, annotations);
        }

        break;
    }
    default:
        break;
    }
}
 
Example 7
Source File: MultithreadedInstanceAccess.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (seen == Const.MONITORENTER) {
        monitorCount++;
    } else if (seen == Const.MONITOREXIT) {
        monitorCount--;
    }

    writingField = ((seen == Const.PUTFIELD) || (seen == Const.PUTFIELD_QUICK) || (seen == Const.PUTFIELD_QUICK_W));
}
 
Example 8
Source File: MutableLock.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void sawOpcode(int seen) {

    switch (seen) {
    case Const.ALOAD_0:
        thisOnTOS = true;
        return;
    case Const.MONITOREXIT:
        setFields.clear();
        break;
    case Const.PUTFIELD:
        if (getClassConstantOperand().equals(getClassName())) {
            setFields.add(getNameConstantOperand());
        }
        break;
    case Const.GETFIELD:
        if (thisOnTOS && getClassConstantOperand().equals(getClassName()) && setFields.contains(getNameConstantOperand())
                && asUnsignedByte(codeBytes[getPC() + 3]) == Const.DUP && asUnsignedByte(codeBytes[getPC() + 5]) == Const.MONITORENTER

                && !finalFields.contains(getNameConstantOperand())) {
            bugReporter.reportBug(new BugInstance(this, "ML_SYNC_ON_UPDATED_FIELD", NORMAL_PRIORITY).addClassAndMethod(this)
                    .addReferencedField(this).addSourceLine(this, getPC() + 5));
        }
        break;
    default:
        break;
    }
    thisOnTOS = false;
}
 
Example 9
Source File: PutfieldScanner.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (seen != Const.PUTFIELD) {
        return;
    }
    XField xFieldOperand = getXFieldOperand();
    if (xFieldOperand != null && xFieldOperand.equals(targetField) && stack.getStackItem(1).getRegisterNumber() == 0) {
        putfields.put(getPC(), new OpcodeStack.Item(stack.getStackItem(0)));
    }

}
 
Example 10
Source File: InnerClassAccessMap.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void handleInstruction(int opcode, int index) {
    switch (opcode) {
    case Const.GETFIELD:
    case Const.PUTFIELD:
        setField(getIndex(instructionList, index), false, opcode == Const.GETFIELD);
        break;
    case Const.GETSTATIC:
    case Const.PUTSTATIC:
        setField(getIndex(instructionList, index), true, opcode == Const.GETSTATIC);
        break;
    default:
        break;
    }
}
 
Example 11
Source File: SynchronizingOnContentsOfFieldToProtectField.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    // System.out.println(state + " " + getPC() + " " + Const.getOpcodeName(seen));
    if (countDown == 2 && seen == Const.GOTO) {
        CodeException tryBlock = getSurroundingTryBlock(getPC());
        if (tryBlock != null && tryBlock.getEndPC() == getPC()) {
            pendingBug.lowerPriority();
        }
    }
    if (countDown > 0) {
        countDown--;
        if (countDown == 0) {
            if (seen == Const.MONITOREXIT) {
                pendingBug.lowerPriority();
            }

            bugReporter.reportBug(pendingBug);
            pendingBug = null;
        }
    }
    if (seen == Const.PUTFIELD) {

        if (syncField != null && getPrevOpcode(1) != Const.ALOAD_0 && syncField.equals(getXFieldOperand())) {
            OpcodeStack.Item value = stack.getStackItem(0);
            int priority = Priorities.HIGH_PRIORITY;
            if (value.isNull()) {
                priority = Priorities.NORMAL_PRIORITY;
            }
            pendingBug = new BugInstance(this, "ML_SYNC_ON_FIELD_TO_GUARD_CHANGING_THAT_FIELD", priority)
                    .addClassAndMethod(this).addField(syncField).addSourceLine(this);
            countDown = 2;

        }

    }
    if (seen == Const.MONITOREXIT) {
        pendingBug = null;
        countDown = 0;
    }

    if (seen == Const.MONITORENTER) {
        syncField = null;
    }

    switch (state) {
    case 0:
        if (seen == Const.ALOAD_0) {
            state = 1;
        }
        break;
    case 1:
        if (seen == Const.GETFIELD) {
            state = 2;
            field = getXFieldOperand();
        } else {
            state = 0;
        }
        break;
    case 2:
        if (seen == Const.DUP) {
            state = 3;
        } else {
            state = 0;
        }
        break;
    case 3:
        if (isRegisterStore()) {
            state = 4;
        } else {
            state = 0;
        }
        break;
    case 4:
        if (seen == Const.MONITORENTER) {
            state = 0;
            syncField = field;
        } else {
            state = 0;
        }
        break;
    default:
        break;
    }

}
 
Example 12
Source File: Noise.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    int priority;
    switch (seen) {
    case Const.INVOKEINTERFACE:
    case Const.INVOKEVIRTUAL:
    case Const.INVOKESPECIAL:
    case Const.INVOKESTATIC:
        hq.pushHash(getClassConstantOperand());
        if (getNameConstantOperand().indexOf('$') == -1) {
            hq.pushHash(getNameConstantOperand());
        }
        hq.pushHash(getSigConstantOperand());

        priority = hq.getPriority();
        if (priority <= Priorities.LOW_PRIORITY) {
            accumulator.accumulateBug(new BugInstance(this, "NOISE_METHOD_CALL", priority).addClassAndMethod(this)
                    .addCalledMethod(this), this);
        }
        break;
    case Const.GETFIELD:
    case Const.PUTFIELD:
    case Const.GETSTATIC:
    case Const.PUTSTATIC:
        hq.pushHash(getClassConstantOperand());
        if (getNameConstantOperand().indexOf('$') == -1) {
            hq.pushHash(getNameConstantOperand());
        }
        hq.pushHash(getSigConstantOperand());
        priority = hq.getPriority();
        if (priority <= Priorities.LOW_PRIORITY) {
            accumulator.accumulateBug(new BugInstance(this, "NOISE_FIELD_REFERENCE", priority).addClassAndMethod(this)
                    .addReferencedField(this), this);
        }
        break;
    case Const.CHECKCAST:
    case Const.INSTANCEOF:
    case Const.NEW:
        hq.pushHash(getClassConstantOperand());
        break;
    case Const.IFEQ:
    case Const.IFNE:
    case Const.IFNONNULL:
    case Const.IFNULL:
    case Const.IF_ICMPEQ:
    case Const.IF_ICMPNE:
    case Const.IF_ICMPLE:
    case Const.IF_ICMPGE:
    case Const.IF_ICMPGT:
    case Const.IF_ICMPLT:
    case Const.IF_ACMPEQ:
    case Const.IF_ACMPNE:
    case Const.RETURN:
    case Const.ARETURN:
    case Const.IRETURN:
    case Const.MONITORENTER:
    case Const.MONITOREXIT:
    case Const.IINC:
    case Const.NEWARRAY:
    case Const.TABLESWITCH:
    case Const.LOOKUPSWITCH:
    case Const.LCMP:
    case Const.INEG:
    case Const.IADD:
    case Const.IMUL:
    case Const.ISUB:
    case Const.IDIV:
    case Const.IREM:
    case Const.IXOR:
    case Const.ISHL:
    case Const.ISHR:
    case Const.IUSHR:
    case Const.IAND:
    case Const.IOR:
    case Const.LAND:
    case Const.LOR:
    case Const.LADD:
    case Const.LMUL:
    case Const.LSUB:
    case Const.LDIV:
    case Const.LSHL:
    case Const.LSHR:
    case Const.LUSHR:
    case Const.AALOAD:
    case Const.AASTORE:
    case Const.IALOAD:
    case Const.IASTORE:
    case Const.BALOAD:
    case Const.BASTORE:
        hq.push(seen);
        priority = hq.getPriority();
        if (priority <= Priorities.LOW_PRIORITY) {
            accumulator.accumulateBug(
                    new BugInstance(this, "NOISE_OPERATION", priority).addClassAndMethod(this).addString(Const.getOpcodeName(seen)),
                    this);
        }
        break;
    default:
        break;
    }
}
 
Example 13
Source File: SerializableIdiom.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (seen == Const.PUTFIELD) {
        XField xField = getXFieldOperand();
        if (xField != null && xField.getClassDescriptor().equals(getClassDescriptor())) {
            Item first = stack.getStackItem(0);

            boolean isPutOfDefaultValue = first.isNull(); // huh?? ||
            // first.isInitialParameter();
            if (!isPutOfDefaultValue && first.getConstant() != null && !first.isArray()) {
                Object constant = first.getConstant();
                if (constant instanceof Number && ((Number) constant).intValue() == 0 || constant.equals(Boolean.FALSE)) {
                    isPutOfDefaultValue = true;
                }
            }

            if (isPutOfDefaultValue) {
                if (Const.CONSTRUCTOR_NAME.equals(getMethodName())) {
                    transientFieldsSetToDefaultValueInConstructor.add(xField);
                }
            } else {
                String nameOfField = getNameConstantOperand();

                if (transientFieldsUpdates.containsKey(xField)) {
                    if (Const.CONSTRUCTOR_NAME.equals(getMethodName())) {
                        transientFieldsSetInConstructor.add(xField);
                    } else {
                        transientFieldsUpdates.put(xField, transientFieldsUpdates.get(xField) + 1);
                    }
                } else if (fieldsThatMightBeAProblem.containsKey(nameOfField)) {
                    try {

                        JavaClass classStored = first.getJavaClass();
                        if (classStored == null) {
                            return;
                        }
                        double isSerializable = DeepSubtypeAnalysis.isDeepSerializable(classStored);
                        if (isSerializable <= 0.2) {
                            XField f = fieldsThatMightBeAProblem.get(nameOfField);

                            String sig = f.getSignature();
                            // System.out.println("Field signature: " +
                            // sig);
                            // System.out.println("Class stored: " +
                            // classStored.getClassName());
                            String genSig = "L" + classStored.getClassName().replace('.', '/') + ";";
                            if (!sig.equals(genSig)) {
                                double bias = 0.0;
                                if (!Const.CONSTRUCTOR_NAME.equals(getMethodName())) {
                                    bias = 1.0;
                                }
                                int priority = computePriority(isSerializable, bias);

                                fieldWarningList.add(new BugInstance(this, "SE_BAD_FIELD_STORE", priority)
                                        .addClass(getThisClass().getClassName()).addField(f).addType(genSig)
                                        .describe("TYPE_FOUND").addSourceLine(this));
                            }
                        }
                    } catch (ClassNotFoundException e) {
                        // ignore it
                    }
                }
            }
        }

    }

}
 
Example 14
Source File: TrainFieldStoreTypes.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void analyzeMethod(ClassContext classContext, Method method) throws CFGBuilderException, DataflowAnalysisException {
    CFG cfg = classContext.getCFG(method);
    TypeDataflow typeDataflow = classContext.getTypeDataflow(method);
    ConstantPoolGen cpg = classContext.getConstantPoolGen();

    for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
        Location location = i.next();
        Instruction ins = location.getHandle().getInstruction();
        short opcode = ins.getOpcode();

        // Field store instruction?
        if (opcode != Const.PUTFIELD && opcode != Const.PUTSTATIC) {
            continue;
        }

        // Check if field type is a reference type
        FieldInstruction fins = (FieldInstruction) ins;
        Type fieldType = fins.getType(cpg);
        if (!(fieldType instanceof ReferenceType)) {
            continue;
        }

        // Find the exact field being stored into
        XField xfield = Hierarchy.findXField(fins, cpg);
        if (xfield == null) {
            continue;
        }

        // Skip public and protected fields, since it is reasonable to
        // assume
        // we won't see every store to those fields
        if (xfield.isPublic() || xfield.isProtected()) {
            continue;
        }

        // The top value on the stack is the one which will be stored
        // into the field
        TypeFrame frame = typeDataflow.getFactAtLocation(location);
        if (!frame.isValid()) {
            continue;
        }
        Type storeType = frame.getTopValue();
        if (!(storeType instanceof ReferenceType)) {
            continue;
        }

        // Get or create the field store type set
        FieldStoreType property = database.getProperty(xfield.getFieldDescriptor());
        if (property == null) {
            property = new FieldStoreType();
            database.setProperty(xfield.getFieldDescriptor(), property);
        }

        // Add the store type to the set
        property.addTypeSignature(storeType.getSignature());
    }
}
 
Example 15
Source File: FieldItemSummary.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (Const.CONSTRUCTOR_NAME.equals(getMethodName()) && seen == Const.INVOKEVIRTUAL) {
        XMethod m = getXMethodOperand();
        if (m != null && !m.isPrivate() && !m.isFinal()) {
            int args = PreorderVisitor.getNumberArguments(m.getSignature());
            OpcodeStack.Item item = stack.getStackItem(args);
            if (item.getRegisterNumber() == 0) {
                try {
                    Set<XMethod> targets = Hierarchy2.resolveVirtualMethodCallTargets(m, false, false);
                    Subtypes2 subtypes2 = AnalysisContext.currentAnalysisContext().getSubtypes2();

                    for (XMethod called : targets) {
                        if (!called.isAbstract() && !called.equals(m)
                                && subtypes2.isSubtype(called.getClassDescriptor(), getClassDescriptor())) {
                            fieldSummary.setCalledFromSuperConstructor(new ProgramPoint(this), called);
                        }
                    }
                } catch (ClassNotFoundException e) {
                    AnalysisContext.reportMissingClass(e);
                }

            }

        }

    }

    if (seen == Const.INVOKESPECIAL && Const.CONSTRUCTOR_NAME.equals(getMethodName()) && Const.CONSTRUCTOR_NAME.equals(
            getNameConstantOperand())) {

        String classOperand = getClassConstantOperand();
        OpcodeStack.Item invokedOn = stack.getItemMethodInvokedOn(this);
        if (invokedOn.getRegisterNumber() == 0 && !classOperand.equals(getClassName())) {
            sawInitializeSuper = true;
            XMethod invoked = getXMethodOperand();
            if (invoked != null) {
                fieldSummary.sawSuperCall(getXMethod(), invoked);
            }
        }

    }

    if (seen == Const.PUTFIELD || seen == Const.PUTSTATIC) {
        XField fieldOperand = getXFieldOperand();
        if (fieldOperand == null) {
            return;
        }
        touched.add(fieldOperand);
        if (!fieldOperand.getClassDescriptor().getClassName().equals(getClassName())) {
            fieldSummary.addWrittenOutsideOfConstructor(fieldOperand);
        } else if (seen == Const.PUTFIELD) {
            OpcodeStack.Item addr = stack.getStackItem(1);
            {
                if (addr.getRegisterNumber() != 0 || !Const.CONSTRUCTOR_NAME.equals(getMethodName())) {
                    fieldSummary.addWrittenOutsideOfConstructor(fieldOperand);
                }
            }
        } else if (seen == Const.PUTSTATIC && !Const.STATIC_INITIALIZER_NAME.equals(getMethodName())) {
            fieldSummary.addWrittenOutsideOfConstructor(fieldOperand);
        }
        OpcodeStack.Item top = stack.getStackItem(0);
        fieldSummary.mergeSummary(fieldOperand, top);
    }

}
 
Example 16
Source File: FindNoSideEffectMethods.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (!allowedFields.isEmpty() && seen == Const.PUTFIELD) {
        Item objItem = getStack().getStackItem(1);
        if (objItem.getRegisterNumber() == 0) {
            if (allowedFields.contains(getFieldDescriptorOperand())) {
                Item valueItem = getStack().getStackItem(0);
                if (!isNew(valueItem) && !valueItem.isNull()) {
                    allowedFields.remove(getFieldDescriptorOperand());
                }
            }
        }
    }
    if (status == SideEffectStatus.SIDE_EFFECT && allowedFields.isEmpty()) {
        // Nothing to do: skip the rest of the method
        throw new EarlyExitException();
    }
    if (status == SideEffectStatus.SIDE_EFFECT) {
        return;
    }
    switch (seen) {
    case Const.ASTORE:
    case Const.ASTORE_0:
    case Const.ASTORE_1:
    case Const.ASTORE_2:
    case Const.ASTORE_3:
        if (finallyTargets.contains(getPC())) {
            finallyExceptionRegisters.add(getRegisterOperand());
        }
        break;
    case Const.ATHROW: {
        Item exceptionItem = getStack().getStackItem(0);
        if (!finallyExceptionRegisters.remove(exceptionItem.getRegisterNumber())) {
            uselessVoidCandidate = false;
            try {
                JavaClass javaClass = exceptionItem.getJavaClass();
                if (javaClass != null && ALLOWED_EXCEPTIONS.contains(javaClass.getClassName())) {
                    break;
                }
            } catch (ClassNotFoundException e) {
            }
            status = SideEffectStatus.SIDE_EFFECT;
        }
        break;
    }
    case Const.PUTSTATIC:
        if (classInit) {
            if (getClassConstantOperand().equals(getClassName())) {
                break;
            }
        }
        status = SideEffectStatus.SIDE_EFFECT;
        break;
    case Const.INVOKEDYNAMIC:
        status = SideEffectStatus.SIDE_EFFECT;
        break;
    case Const.PUTFIELD:
        sawCall(getMethodCall(FIELD_STORE_STUB_METHOD), false);
        break;
    case Const.AASTORE:
    case Const.DASTORE:
    case Const.CASTORE:
    case Const.BASTORE:
    case Const.IASTORE:
    case Const.LASTORE:
    case Const.FASTORE:
    case Const.SASTORE:
        sawCall(getMethodCall(ARRAY_STORE_STUB_METHOD), false);
        break;
    case Const.INVOKESTATIC:
        if (changesOnlyNewObjects(getMethodDescriptorOperand())) {
            break;
        }
        sawCall(new MethodCall(getMethodDescriptorOperand(), TARGET_OTHER), false);
        break;
    case Const.INVOKESPECIAL:
    case Const.INVOKEINTERFACE:
    case Const.INVOKEVIRTUAL: {
        XMethod xMethodOperand = getXMethodOperand();
        MethodDescriptor methodDescriptorOperand = xMethodOperand == null ? getMethodDescriptorOperand()
                : xMethodOperand
                        .getMethodDescriptor();
        if (changesOnlyNewObjects(getMethodDescriptorOperand())) {
            break;
        }
        MethodCall methodCall = getMethodCall(methodDescriptorOperand);
        sawCall(methodCall, false);
        break;
    }
    default:
        break;
    }
}
 
Example 17
Source File: FindInconsistentSync2.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Determine whether or not the the given method is a getter method. I.e.,
 * if it just returns the value of an instance field.
 *
 * @param classContext
 *            the ClassContext for the class containing the method
 * @param method
 *            the method
 */
public static boolean isGetterMethod(ClassContext classContext, Method method) {
    MethodGen methodGen = classContext.getMethodGen(method);
    if (methodGen == null) {
        return false;
    }
    InstructionList il = methodGen.getInstructionList();
    // System.out.println("Checking getter method: " + method.getName());
    if (il.getLength() > 60) {
        return false;
    }

    int count = 0;
    for (InstructionHandle ih : il) {
        switch (ih.getInstruction().getOpcode()) {
        case Const.GETFIELD:
            count++;
            if (count > 1) {
                return false;
            }
            break;
        case Const.PUTFIELD:
        case Const.BALOAD:
        case Const.CALOAD:
        case Const.DALOAD:
        case Const.FALOAD:
        case Const.IALOAD:
        case Const.LALOAD:
        case Const.SALOAD:
        case Const.AALOAD:
        case Const.BASTORE:
        case Const.CASTORE:
        case Const.DASTORE:
        case Const.FASTORE:
        case Const.IASTORE:
        case Const.LASTORE:
        case Const.SASTORE:
        case Const.AASTORE:
        case Const.PUTSTATIC:
            return false;
        case Const.INVOKESTATIC:
        case Const.INVOKEVIRTUAL:
        case Const.INVOKEINTERFACE:
        case Const.INVOKESPECIAL:
        case Const.GETSTATIC:
            // no-op

        }
    }
    // System.out.println("Found getter method: " + method.getName());
    return true;
}
 
Example 18
Source File: FindNonShortCircuit.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void scanForShortCircuit(int seen) {
    switch (seen) {
    case Const.IAND:
    case Const.IOR:

        // System.out.println("Saw IOR or IAND at distance " + distance);
        OpcodeStack.Item item0 = stack.getStackItem(0);
        OpcodeStack.Item item1 = stack.getStackItem(1);
        if (item0.getConstant() == null && item1.getConstant() == null && distance < 4) {
            //                if (item0.getRegisterNumber() >= 0 && item1.getRegisterNumber() >= 0) {
            //                    if (false) {
            //                        clearAll();
            //                    }
            //                }
            operator = seen;
            stage2 = 1;
        } else {
            stage2 = 0;
        }
        break;
    case Const.IFEQ:
    case Const.IFNE:
        if (stage2 == 1) {
            // System.out.println("Found nsc");
            reportBug();
        }
        stage2 = 0;
        break;
    case Const.PUTFIELD:
    case Const.PUTSTATIC:
    case Const.IRETURN:
        if (operator == Const.IAND && stage2 == 1) {
            reportBug();
        }
        stage2 = 0;
        break;
    default:
        stage2 = 0;
        break;
    }
}
 
Example 19
Source File: PUTFIELD.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
public PUTFIELD(final int index) {
    super(Const.PUTFIELD, index);
}
 
Example 20
Source File: FindUninitializedGet.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (!inConstructor) {
        return;
    }
    if (uninitializedFieldReadAndCheckedForNonnull != null) {
        if (seen == Const.NEW && getClassConstantOperand().endsWith("Exception")) {
            uninitializedFieldReadAndCheckedForNonnull.raisePriority();
        }
        uninitializedFieldReadAndCheckedForNonnull = null;
    }

    if (seen == Const.ALOAD_0) {
        thisOnTOS = true;
        return;
    }

    if (seen == Const.PUTFIELD && getClassConstantOperand().equals(getClassName())) {
        initializedFields.add(FieldAnnotation.fromReferencedField(this));
    } else if (thisOnTOS && seen == Const.GETFIELD && getClassConstantOperand().equals(getClassName())) {
        UnreadFieldsData unreadFields = AnalysisContext.currentAnalysisContext().getUnreadFieldsData();
        XField xField = XFactory.createReferencedXField(this);
        FieldAnnotation f = FieldAnnotation.fromReferencedField(this);
        int nextOpcode = 0xff & codeBytes[getPC() + 3];
        if (nextOpcode != Const.POP && !initializedFields.contains(f) && declaredFields.contains(f) && !containerFields.contains(f)
                && !unreadFields.isContainerField(xField)) {
            // System.out.println("Next opcode: " +
            // Const.getOpcodeName(nextOpcode));
            LocalVariableAnnotation possibleTarget = LocalVariableAnnotation.findMatchingIgnoredParameter(getClassContext(),
                    getMethod(), getNameConstantOperand(), xField.getSignature());
            if (possibleTarget == null) {
                possibleTarget = LocalVariableAnnotation.findUniqueBestMatchingParameter(getClassContext(), getMethod(),
                        getNameConstantOperand(), getSigConstantOperand());
            }
            int priority = unreadFields.getReadFields().contains(xField) ? NORMAL_PRIORITY : LOW_PRIORITY;
            boolean priorityLoweredBecauseOfIfNonnullTest = false;
            if (possibleTarget != null) {
                priority--;
            } else {
                FieldSummary fieldSummary = AnalysisContext.currentAnalysisContext().getFieldSummary();
                if (fieldSummary.callsOverriddenMethodsFromSuperConstructor(getClassDescriptor())) {
                    priority++;
                } else if (nextOpcode == Const.IFNONNULL) {
                    priority++;
                    priorityLoweredBecauseOfIfNonnullTest = true;
                }
            }

            BugInstance bug = new BugInstance(this, "UR_UNINIT_READ", priority).addClassAndMethod(this).addField(f)
                    .addOptionalAnnotation(possibleTarget).addSourceLine(this);
            pendingBugs.add(bug);
            if (priorityLoweredBecauseOfIfNonnullTest) {
                uninitializedFieldReadAndCheckedForNonnull = bug;
            }
            initializedFields.add(f);
        }
    } else if ((seen == Const.INVOKESPECIAL && !(Const.CONSTRUCTOR_NAME.equals(getNameConstantOperand()) && !getClassConstantOperand().equals(
            getClassName())))
            || (seen == Const.INVOKESTATIC && "doPrivileged".equals(getNameConstantOperand()) && "java/security/AccessController".equals(
                    getClassConstantOperand()))
            || (seen == Const.INVOKEVIRTUAL && getClassConstantOperand().equals(getClassName()))
            || (seen == Const.INVOKEVIRTUAL && "start".equals(getNameConstantOperand()))) {

        inConstructor = false;
    }

    thisOnTOS = false;
}