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

The following examples show how to use org.apache.bcel.Const#ALOAD_0 . 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: 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 2
Source File: InvalidJUnitTest.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    switch (state) {
    case SEEN_NOTHING:
        if (seen == Const.ALOAD_0) {
            state = SEEN_ALOAD_0;
        }
        break;

    case SEEN_ALOAD_0:
        if ((seen == Const.INVOKESPECIAL) && (getNameConstantOperand().equals(getMethodName()))
                && (getSigConstantOperand().equals("()V"))) {
            sawSuperCall = true;
        }
        state = SEEN_NOTHING;
        break;
    default:
        state = SEEN_NOTHING;
    }
}
 
Example 3
Source File: UnconditionalValueDerefAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static boolean check(InstructionHandle h, int[] opcodes) {
    for (int opcode : opcodes) {
        if (h == null) {
            return false;
        }
        short opcode2 = h.getInstruction().getOpcode();
        if (opcode == Const.LDC) {
            switch (opcode2) {
            case Const.LDC:
            case Const.ALOAD:
            case Const.ALOAD_0:
            case Const.ALOAD_1:
            case Const.ALOAD_2:
            case Const.ALOAD_3:
                break;
            default:
                return false;
            }
        } else if (opcode2 != opcode) {
            return false;
        }
        h = h.getNext();
    }
    return true;
}
 
Example 4
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 5
Source File: PublicSemaphores.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (alreadyReported) {
        return;
    }

    switch (state) {
    case SEEN_NOTHING:
        if (seen == Const.ALOAD_0) {
            state = SEEN_ALOAD_0;
        }
        break;

    case SEEN_ALOAD_0:
        if ((seen == Const.INVOKEVIRTUAL) && "java/lang/Object".equals(getClassConstantOperand())) {
            String methodName = getNameConstantOperand();
            if ("wait".equals(methodName) || "notify".equals(methodName) || "notifyAll".equals(methodName)) {
                bugReporter.reportBug(new BugInstance(this, "PS_PUBLIC_SEMAPHORES", NORMAL_PRIORITY).addClassAndMethod(this)
                        .addSourceLine(this));
                alreadyReported = true;
            }
        }
        state = SEEN_NOTHING;
        break;
    default:
        break;
    }

}
 
Example 6
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 7
Source File: FindSpinLoop.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("PC: " + PC + ", stage: " + stage1);
    switch (seen) {
    case Const.ALOAD_0:
    case Const.ALOAD_1:
    case Const.ALOAD_2:
    case Const.ALOAD_3:
    case Const.ALOAD:
        if (DEBUG) {
            System.out.println("   ALOAD at PC " + getPC());
        }
        start = getPC();
        stage = 1;
        break;
    case Const.GETSTATIC:
        if (DEBUG) {
            System.out.println("   getfield in stage " + stage);
        }
        lastFieldSeen = FieldAnnotation.fromReferencedField(this);
        start = getPC();
        stage = 2;
        break;
    case Const.GETFIELD:
        if (DEBUG) {
            System.out.println("   getfield in stage " + stage);
        }
        lastFieldSeen = FieldAnnotation.fromReferencedField(this);
        if (stage == 1 || stage == 2) {
            stage = 2;
        } else {
            stage = 0;
        }
        break;
    case Const.GOTO:
    case Const.IFNE:
    case Const.IFEQ:
    case Const.IFNULL:
    case Const.IFNONNULL:
        if (DEBUG) {
            System.out.println("   conditional branch in stage " + stage + " to " + getBranchTarget());
        }
        if (stage == 2 && getBranchTarget() == start) {
            bugReporter.reportBug(new BugInstance(this, "SP_SPIN_ON_FIELD", NORMAL_PRIORITY).addClassAndMethod(this)
                    .addReferencedField(lastFieldSeen).addSourceLine(this, start));
            stage = 0;
        } else if (getBranchTarget() < getPC()) {
            stage = 0;
        }
        break;
    default:
        stage = 0;
        break;
    }

}
 
Example 8
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;
}
 
Example 9
Source File: SuspiciousThreadInterrupted.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {

    if (state == SEEN_POSSIBLE_THREAD) {
        if (seen == Const.POP) {
            state = SEEN_UNKNOWNCONTEXT_POP;
            return;
        } else {
            state = SEEN_NOTHING;
        }
    }
    switch (state) {
    case SEEN_NOTHING:
        if ((seen == Const.INVOKESTATIC) && "java/lang/Thread".equals(getClassConstantOperand())
                && "currentThread".equals(getNameConstantOperand()) && "()Ljava/lang/Thread;".equals(getSigConstantOperand())) {
            state = SEEN_CURRENTTHREAD;
        } else if ((seen == Const.INVOKESTATIC || seen == Const.INVOKEINTERFACE || seen == Const.INVOKEVIRTUAL || seen == Const.INVOKESPECIAL)
                && getSigConstantOperand().endsWith("Ljava/lang/Thread;")) {
            state = SEEN_POSSIBLE_THREAD;
        } else if (seen == Const.ALOAD) {
            if (localsWithCurrentThreadValue.get(getRegisterOperand())) {
                state = SEEN_CURRENTTHREAD;
            } else {
                state = SEEN_POSSIBLE_THREAD;
            }
        } else if ((seen >= Const.ALOAD_0) && (seen <= Const.ALOAD_3)) {
            if (localsWithCurrentThreadValue.get(seen - Const.ALOAD_0)) {
                state = SEEN_CURRENTTHREAD;
            } else {
                state = SEEN_POSSIBLE_THREAD;
            }
        } else if ((seen == Const.GETFIELD || seen == Const.GETSTATIC) && "Ljava/lang/Thread;".equals(getSigConstantOperand())) {
            state = SEEN_POSSIBLE_THREAD;
        }
        break;

    case SEEN_CURRENTTHREAD:
        if (seen == Const.POP) {
            state = SEEN_POP_AFTER_CURRENTTHREAD;
        } else if (seen == Const.ASTORE) {
            localsWithCurrentThreadValue.set(getRegisterOperand());
            state = SEEN_NOTHING;
        } else if ((seen >= Const.ASTORE_0) && (seen <= Const.ASTORE_3)) {
            localsWithCurrentThreadValue.set(seen - Const.ASTORE_0);
            state = SEEN_NOTHING;
        } else {
            state = SEEN_NOTHING;
        }
        break;

    default:
        if ((seen == Const.INVOKESTATIC) && "java/lang/Thread".equals(getClassConstantOperand())
                && "interrupted".equals(getNameConstantOperand()) && "()Z".equals(getSigConstantOperand())) {
            if (state == SEEN_POP_AFTER_CURRENTTHREAD) {
                bugReporter.reportBug(new BugInstance(this, "STI_INTERRUPTED_ON_CURRENTTHREAD", LOW_PRIORITY)
                        .addClassAndMethod(this).addSourceLine(this));
            } else if (state == SEEN_UNKNOWNCONTEXT_POP) {
                bugReporter.reportBug(new BugInstance(this, "STI_INTERRUPTED_ON_UNKNOWNTHREAD", NORMAL_PRIORITY)
                        .addClassAndMethod(this).addSourceLine(this));
            }
        }
        state = SEEN_NOTHING;
        break;
    }
}
 
Example 10
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 11
Source File: SuperfluousInstanceOf.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    switch (state) {
    case SEEN_NOTHING:
        if (seen == Const.ALOAD) {
            register = getRegisterOperand();
        } else if ((seen >= Const.ALOAD_0) && (seen <= Const.ALOAD_3)) {
            register = seen - Const.ALOAD_0;
        } else {
            return;
        }
        state = SEEN_ALOAD;
        break;

    case SEEN_ALOAD:
        try {
            if (seen == Const.INSTANCEOF) {
                LocalVariable lv = LVTHelper.getLocalVariableAtPC(varTable, register, getPC());
                if (lv != null) {
                    String objSignature = lv.getSignature();
                    if (objSignature.charAt(0) == 'L') {
                        objSignature = objSignature.substring(1, objSignature.length() - 1).replace('/', '.');
                        String clsSignature = getDottedClassConstantOperand();

                        if (clsSignature.charAt(0) != '[') {
                            if (org.apache.bcel.Repository.instanceOf(objSignature, clsSignature)) {
                                bugReporter.reportBug(new BugInstance(this, "SIO_SUPERFLUOUS_INSTANCEOF", LOW_PRIORITY)
                                        .addClassAndMethod(this).addSourceLine(this));
                            }
                        }
                    }
                }
            }
        } catch (ClassNotFoundException cnfe) {
            bugReporter.reportMissingClass(cnfe);
        }

        state = SEEN_NOTHING;
        break;
    default:
        break;
    }

}
 
Example 12
Source File: UselessSubclassMethod.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visitCode(Code obj) {
    try {
        String methodName = getMethodName();

        if (!Const.CONSTRUCTOR_NAME.equals(methodName) && !"clone".equals(methodName)
                && ((getMethod().getAccessFlags() & (Const.ACC_STATIC | Const.ACC_SYNTHETIC)) == 0)) {

            /*
             * for some reason, access flags doesn't return Synthetic, so do
             * this hocus pocus
             */
            Attribute[] atts = getMethod().getAttributes();
            for (Attribute att : atts) {
                if (att.getClass().equals(Synthetic.class)) {
                    return;
                }
            }

            byte[] codeBytes = obj.getCode();
            if ((codeBytes.length == 0) || (codeBytes[0] != Const.ALOAD_0)) {
                return;
            }

            state = State.SEEN_NOTHING;
            invokePC = 0;
            super.visitCode(obj);
            if ((state == State.SEEN_RETURN) && (invokePC != 0)) {
                // Do this check late, as it is potentially expensive
                Method superMethod = findSuperclassMethod(superclassName, getMethod());
                if ((superMethod == null) || differentAttributes(getMethod(), superMethod)
                        || getMethod().isProtected()
                                && !samePackage(getDottedClassName(), superclassName)) {
                    return;
                }

                bugReporter.reportBug(new BugInstance(this, "USM_USELESS_SUBCLASS_METHOD", LOW_PRIORITY).addClassAndMethod(
                        this).addSourceLine(this, invokePC));
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
 
Example 13
Source File: UselessSubclassMethod.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    switch (state) {
    case SEEN_NOTHING:
        if (seen == Const.ALOAD_0) {
            argTypes = Type.getArgumentTypes(this.getMethodSig());
            curParm = 0;
            curParmOffset = 1;
            if (argTypes.length > 0) {
                state = State.SEEN_PARM;
            } else {
                state = State.SEEN_LAST_PARM;
            }
        } else {
            state = State.SEEN_INVALID;
        }
        break;

    case SEEN_PARM:
        if (curParm >= argTypes.length) {
            state = State.SEEN_INVALID;
        } else {
            String signature = argTypes[curParm++].getSignature();
            char typeChar0 = signature.charAt(0);
            if ((typeChar0 == 'L') || (typeChar0 == '[')) {
                checkParm(seen, Const.ALOAD_0, Const.ALOAD, 1);
            } else if (typeChar0 == 'D') {
                checkParm(seen, Const.DLOAD_0, Const.DLOAD, 2);
            } else if (typeChar0 == 'F') {
                checkParm(seen, Const.FLOAD_0, Const.FLOAD, 1);
            } else if (typeChar0 == 'I' || typeChar0 == 'Z' || typeChar0 == 'S' || typeChar0 == 'C') {
                checkParm(seen, Const.ILOAD_0, Const.ILOAD, 1);
            } else if (typeChar0 == 'J') {
                checkParm(seen, Const.LLOAD_0, Const.LLOAD, 2);
            } else {
                state = State.SEEN_INVALID;
            }

            if ((state != State.SEEN_INVALID) && (curParm >= argTypes.length)) {
                state = State.SEEN_LAST_PARM;
            }

        }
        break;

    case SEEN_LAST_PARM:
        if ((seen == Const.INVOKENONVIRTUAL) && getMethodName().equals(getNameConstantOperand())
                && getMethodSig().equals(getSigConstantOperand())) {
            invokePC = getPC();
            state = State.SEEN_INVOKE;
        } else {
            state = State.SEEN_INVALID;
        }
        break;

    case SEEN_INVOKE:
        Type returnType = getMethod().getReturnType();
        char retSigChar0 = returnType.getSignature().charAt(0);
        if ((retSigChar0 == 'V') && (seen == Const.RETURN)) {
            state = State.SEEN_RETURN;
        } else if (((retSigChar0 == 'L') || (retSigChar0 == '[')) && (seen == Const.ARETURN)) {
            state = State.SEEN_RETURN;
        } else if ((retSigChar0 == 'D') && (seen == Const.DRETURN)) {
            state = State.SEEN_RETURN;
        } else if ((retSigChar0 == 'F') && (seen == Const.FRETURN)) {
            state = State.SEEN_RETURN;
        } else if ((retSigChar0 == 'I' || retSigChar0 == 'S' || retSigChar0 == 'C' || retSigChar0 == 'B' || retSigChar0 == 'Z')
                && (seen == Const.IRETURN)) {
            state = State.SEEN_RETURN;
        } else if ((retSigChar0 == 'J') && (seen == Const.LRETURN)) {
            state = State.SEEN_RETURN;
        } else {
            state = State.SEEN_INVALID;
        }
        break;

    case SEEN_RETURN:
        state = State.SEEN_INVALID;
        break;
    default:
        break;
    }
}
 
Example 14
Source File: VarArgsProblems.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:" + state);
    if (seen == Const.GOTO && getBranchOffset() == 4) {
        state = SEEN_GOTO;
    } else {
        switch (state) {
        case SEEN_NOTHING:
            if ((seen == Const.ICONST_1)) {
                state = SEEN_ICONST_1;
            }
            break;

        case SEEN_ICONST_1:
            if (seen == Const.ANEWARRAY && primitiveArray.matcher(getClassConstantOperand()).matches()) {
                // System.out.println("Allocation of array of type " +
                // getClassConstantOperand());
                primitiveArraySig = getClassConstantOperand();
                state = SEEN_ANEWARRAY;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_ANEWARRAY:
            if (seen == Const.DUP) {
                state = SEEN_DUP;
            } else {
                state = SEEN_NOTHING;
            }
            break;
        case SEEN_DUP:
            if (seen == Const.ICONST_0) {
                state = SEEN_ICONST_0;
            } else {
                state = SEEN_NOTHING;
            }
            break;
        case SEEN_ICONST_0:
            if (((seen >= Const.ALOAD_0) && (seen < Const.ALOAD_3)) || (seen == Const.ALOAD)) {
                state = SEEN_ALOAD;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_ALOAD:
            if (seen == Const.AASTORE) {
                state = SEEN_AASTORE;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_AASTORE:
            if (seen == Const.INVOKESTATIC || seen == Const.INVOKEINTERFACE || seen == Const.INVOKESPECIAL || seen == Const.INVOKEVIRTUAL) {
                // System.out.println(getClassConstantOperand());
                // System.out.println(getNameConstantOperand());
                // System.out.println(getSigConstantOperand());
                if (getSigConstantOperand().indexOf("Ljava/lang/Object;)") == -1) {
                    break;
                }
                int priority = NORMAL_PRIORITY;
                if ("asList".equals(getNameConstantOperand()) && "java/util/Arrays".equals(getClassConstantOperand())) {
                    priority = HIGH_PRIORITY;
                }
                bugReporter.reportBug(new BugInstance(this, "VA_PRIMITIVE_ARRAY_PASSED_TO_OBJECT_VARARG", priority)
                        .addClassAndMethod(this).addType(primitiveArraySig).describe(TypeAnnotation.FOUND_ROLE)
                        .addCalledMethod(this).addSourceLine(this));
            }
            state = SEEN_NOTHING;
            break;

        case SEEN_GOTO:
            state = SEEN_NOTHING;
            break;
        default:
            throw new IllegalStateException("State " + state + " not expected");

        }
    }
}
 
Example 15
Source File: InheritanceUnsafeGetResource.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (reportedForThisClass) {
        return;
    }

    switch (seen) {
    case Const.LDC:
        Constant constantValue = getConstantRefOperand();
        if (constantValue instanceof ConstantClass) {
            sawGetClass = -100;
        } else if (constantValue instanceof ConstantString) {
            stringConstant = ((ConstantString) constantValue).getBytes(getConstantPool());
        }
        break;

    case Const.ALOAD_0:
        state = 1;
        break;
    case Const.INVOKEVIRTUAL:
        if ("java/lang/Class".equals(getClassConstantOperand())
                && ("getResource".equals(getNameConstantOperand()) || "getResourceAsStream".equals(getNameConstantOperand()))
                && sawGetClass + 10 >= getPC()) {
            int priority = NORMAL_PRIORITY;
            if (prevOpcode == Const.LDC && stringConstant != null && stringConstant.length() > 0 && stringConstant.charAt(0) == '/') {
                priority = LOW_PRIORITY;
            } else {
                priority = adjustPriority(priority);
            }
            bugReporter.reportBug(new BugInstance(this, "UI_INHERITANCE_UNSAFE_GETRESOURCE", priority)
                    .addClassAndMethod(this).addSourceLine(this));
            reportedForThisClass = true;

        } else if (state == 1 && !methodIsStatic && !classIsFinal && classIsVisibleToOtherPackages
                && "getClass".equals(getNameConstantOperand()) && "()Ljava/lang/Class;".equals(getSigConstantOperand())) {
            sawGetClass = getPC();
        }
        state = 0;
        break;
    default:
        state = 0;
        break;
    }
    if (seen != Const.LDC) {
        stringConstant = null;
    }
    prevOpcode = seen;

}