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

The following examples show how to use org.apache.bcel.Const#PUTSTATIC . 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: Hierarchy.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Look up the field referenced by given FieldInstruction, returning it as
 * an {@link XField XField} object.
 *
 * @param fins
 *            the FieldInstruction
 * @param cpg
 *            the ConstantPoolGen used by the class containing the
 *            instruction
 * @return an XField object representing the field, or null if no such field
 *         could be found
 */
public static @CheckForNull XField findXField(FieldInstruction fins, @Nonnull ConstantPoolGen cpg) {

    String className = fins.getClassName(cpg);
    String fieldName = fins.getFieldName(cpg);
    String fieldSig = fins.getSignature(cpg);

    boolean isStatic = (fins.getOpcode() == Const.GETSTATIC || fins.getOpcode() == Const.PUTSTATIC);

    XField xfield = findXField(className, fieldName, fieldSig, isStatic);
    short opcode = fins.getOpcode();
    if (xfield != null && xfield.isResolved()
            && xfield.isStatic() == (opcode == Const.GETSTATIC || opcode == Const.PUTSTATIC)) {
        return xfield;
    } else {
        return null;
    }
}
 
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: 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 4
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 5
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 6
Source File: FieldSetAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void handleInstruction(InstructionHandle handle, BasicBlock basicBlock, FieldSet fact) {
    Instruction ins = handle.getInstruction();
    short opcode = ins.getOpcode();
    XField field;

    switch (opcode) {
    case Const.GETFIELD:
    case Const.GETSTATIC:
        field = lookupField(handle, (FieldInstruction) ins);
        if (field != null) {
            sawLoad(fact, field);
        }
        break;

    case Const.PUTFIELD:
    case Const.PUTSTATIC:
        field = lookupField(handle, (FieldInstruction) ins);
        if (field != null) {
            sawStore(fact, field);
        }
        break;

    case Const.INVOKEINTERFACE:
    case Const.INVOKESPECIAL:
    case Const.INVOKESTATIC:
    case Const.INVOKEVIRTUAL:
        // Assume that the called method assigns loads and stores all
        // possible fields
        fact.setBottom();
        break;
    default:
        break;
    }
}
 
Example 7
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 8
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 9
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 10
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 11
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 12
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 13
Source File: InitializationChain.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    InvocationInfo prev = lastInvocation;
    lastInvocation = null;
    if (Const.CONSTRUCTOR_NAME.equals(getMethodName())) {
        if (seen == Const.GETSTATIC && getClassConstantOperand().equals(getClassName())) {
            staticFieldsReadInAnyConstructor.add(getXFieldOperand());
            fieldsReadInThisConstructor.add(getXFieldOperand());
        }
        return;
    }

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

        XMethod m = getXMethodOperand();
        Set<XField> read = staticFieldsRead.get(m);
        if (constructorsInvokedInStaticInitializer.add(m) && read != null && !read.isEmpty()) {
            lastInvocation = new InvocationInfo(m, getPC());
            invocationInfo.add(lastInvocation);

        }

    }
    if (seen == Const.PUTSTATIC && getClassConstantOperand().equals(getClassName())) {
        XField f = getXFieldOperand();
        if (prev != null) {
            prev.field = f;
        }
        if (staticFieldsReadInAnyConstructor.contains(f) && !warningGiven.contains(f)) {
            for (InvocationInfo i : invocationInfo) {
                Set<XField> fields = staticFieldsRead.get(i.constructor);
                if (fields != null && fields.contains(f)) {
                    warningGiven.add(f);
                    BugInstance bug = new BugInstance(this, "SI_INSTANCE_BEFORE_FINALS_ASSIGNED", NORMAL_PRIORITY).addClassAndMethod(this);
                    if (i.field != null) {
                        bug.addField(i.field).describe(FieldAnnotation.STORED_ROLE);
                    }
                    bug.addMethod(i.constructor).describe(MethodAnnotation.METHOD_CONSTRUCTOR);
                    bug.addReferencedField(this).describe(FieldAnnotation.VALUE_OF_ROLE).addSourceLine(this, i.pc);
                    bugReporter.reportBug(bug);
                    break;

                }
            }
        }

    } else if (seen == Const.PUTSTATIC || seen == Const.GETSTATIC || seen == Const.INVOKESTATIC || seen == Const.NEW) {
        if (getPC() + 6 < codeBytes.length) {
            requires.add(getDottedClassConstantOperand());
        }
    }
}
 
Example 14
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 15
Source File: PUTSTATIC.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
public PUTSTATIC(final int index) {
    super(Const.PUTSTATIC, index);
}