Package edu.umd.cs.findbugs.ba

Examples of edu.umd.cs.findbugs.ba.XMethod


    public void visitSyntheticParameterAnnotation(int p, boolean runtimeVisible) {
        if (database == null) {
            return;
        }

        XMethod xmethod = XFactory.createXMethod(this);

        XMethodParameter xparameter = new XMethodParameter(xmethod, p);

        database.addDirectAnnotation(xparameter, NullnessAnnotation.UNKNOWN_NULLNESS);
View Full Code Here


        annotationClass = lastPortion(annotationClass);
        if (n == null) {
            return;
        }

        XMethod xmethod = XFactory.createXMethod(this);
        if (DEBUG) {
            System.out.println("Parameter " + p + " @" + annotationClass.substring(annotationClass.lastIndexOf('/') + 1) + " in "
                    + xmethod.toString());
        }
        XMethodParameter xparameter = new XMethodParameter(xmethod, p);

        database.addDirectAnnotation(xparameter, n);
View Full Code Here

            Instruction ins = handle.getInstruction();
            actionList = Collections.emptyList();
            if (ins instanceof InvokeInstruction) {

                InvokeInstruction inv = (InvokeInstruction) ins;
                XMethod invokedMethod = XFactory.createXMethod(inv, cpg);
                String signature = invokedMethod.getSignature();
                String methodName = invokedMethod.getName();

                if (DEBUG_LOOKUP) {
                    System.out.println("Looking up actions for call to " + invokedMethod);
                }


                if (invokedMethod.getAnnotationDescriptors().contains(WILL_CLOSE) && methodName.startsWith("close") && signature.endsWith(")V")) {
                    actionList = Collections.singletonList(ObligationPolicyDatabaseAction.CLEAR);
                } else if (signature.indexOf(';') >= -1) {
                    ReferenceType receiverType = inv.getReferenceType(cpg);

                    boolean isStatic = inv.getOpcode() == Constants.INVOKESTATIC;
                    actionList = new LinkedList<ObligationPolicyDatabaseAction>();

                    database.getActions(receiverType, methodName, signature, isStatic, actionList);

                    if (actionList.isEmpty()) {

                        try {
                            TypeFrame factAtLocation = null;
                            SignatureParser sigParser = new SignatureParser(signature);
                            //                        int startIndex = 0;
                            //                        if (!xmethod.isStatic())
                            //                            startIndex = 1;
                            Iterator<String> signatureIterator = sigParser.parameterSignatureIterator();
                            int parameters = sigParser.getNumParameters();
                            for (int i = 0; i < parameters; i++) {
                                String sig = signatureIterator.next();
                                Collection<ClassDescriptor> annotations = invokedMethod.getParameterAnnotationDescriptors(i);
                                if (annotations.contains(WILL_CLOSE) || sig.equals("Ljava/io/Closeable;") || methodName.startsWith("close")) {
                                    // closing this value
                                    if (factAtLocation == null) {
                                        factAtLocation = typeDataflow.getFactAtLocation( new Location(handle, block));
                                    }
View Full Code Here

    @Override
    public ObligationDataflow analyze(IAnalysisCache analysisCache, MethodDescriptor methodDescriptor)
            throws CheckedAnalysisException {
        CFG cfg = analysisCache.getMethodAnalysis(CFG.class, methodDescriptor);
        DepthFirstSearch dfs = analysisCache.getMethodAnalysis(DepthFirstSearch.class, methodDescriptor);
        XMethod xmethod = XFactory.createXMethod(methodDescriptor);
        ConstantPoolGen cpg = analysisCache.getClassAnalysis(ConstantPoolGen.class, methodDescriptor.getClassDescriptor());

        ObligationPolicyDatabase database = analysisCache.getDatabase(ObligationPolicyDatabase.class);

        TypeDataflow typeDataflow = analysisCache.getMethodAnalysis(TypeDataflow.class, methodDescriptor);
View Full Code Here

        Method method = getMethod(analysisCache, descriptor);

        if (method.getCode() == null) {
            return null;
        }
        XMethod xmethod =  XFactory.createXMethod(descriptor);
        if (xmethod.usesInvokeDynamic() && false) {
            AnalysisContext.currentAnalysisContext().analysisSkippedDueToInvokeDynamic(xmethod);
            return null;
        }

        try {
View Full Code Here

        String whereEqual = getDottedClassName();
        boolean inheritedHashCodeIsFinal = false;
        boolean inheritedEqualsIsFinal = false;
        boolean inheritedEqualsIsAbstract = false;
        boolean inheritedEqualsFromAbstractClass = false;
        XMethod inheritedEquals = null;
        if (!hasEqualsObject) {
            XClass we = Lookup.findImplementor(getXClass(), "equals", "(Ljava/lang/Object;)Z", false, bugReporter);
            if (we == null || we.equals(getXClass())) {
                whereEqual = "java.lang.Object";
            } else {
                inheritedEqualsFromAbstractClass = we.isAbstract();
                whereEqual = we.getClassDescriptor().getDottedClassName();
                inheritedEquals = we.findMethod("equals", "(Ljava/lang/Object;)Z", false);
                if (inheritedEquals != null) {
                    inheritedEqualsIsFinal = inheritedEquals.isFinal();
                    inheritedEqualsIsAbstract = inheritedEquals.isAbstract();
                }
            }
        }
        boolean usesDefaultEquals = whereEqual.equals("java.lang.Object");
        String whereHashCode = getDottedClassName();
        if (!hasHashCode) {
            XClass wh = Lookup.findSuperImplementor(getXClass(), "hashCode", "()I", false, bugReporter);
            if (wh == null) {
                whereHashCode = "java.lang.Object";
            } else {
                whereHashCode = wh.getClassDescriptor().getDottedClassName();
                XMethod m = wh.findMethod("hashCode", "()I", false);
                if (m != null && m.isFinal()) {
                    inheritedHashCodeIsFinal = true;
                }
            }
        }
        boolean usesDefaultHashCode = whereHashCode.equals("java.lang.Object");
View Full Code Here

        if (seen == INVOKESPECIAL && getNameConstantOperand().equals("<init>") && getSigConstantOperand().equals("(Ljava/util/Collection;)V")
                && getClassConstantOperand().contains("Set")
                || (seen == INVOKEVIRTUAL || seen == INVOKEINTERFACE) && getNameConstantOperand().equals("addAll") && getSigConstantOperand().equals("(Ljava/util/Collection;)Z")) {
            OpcodeStack.Item top = stack.getStackItem(0);
            XMethod returnValueOf = top.getReturnValueOf();
            if (returnValueOf != null && returnValueOf.getName().equals("entrySet")) {
                String name = returnValueOf.getClassName();
                int priority = Priorities.LOW_PRIORITY;
                if (name.equals("java.util.Map")) {
                    priority = Priorities.NORMAL_PRIORITY;
                } else if (name.equals(EnumMap.class.getName())
                        || name.equals(IdentityHashMap.class.getName())) {
                    priority = Priorities.HIGH_PRIORITY;
                }
                bugReporter.reportBug(new BugInstance(this, "DMI_ENTRY_SETS_MAY_REUSE_ENTRY_OBJECTS", priority)
                .addClassAndMethod(this).addCalledMethod(returnValueOf).addCalledMethod(this).addValueSource(top, this).addSourceLine(this));
            }


        }

        if (seen == INVOKEVIRTUAL && getNameConstantOperand().equals("hashCode") && getSigConstantOperand().equals("()I")
                && stack.getStackDepth() > 0) {
            OpcodeStack.Item item0 = stack.getStackItem(0);
            if (item0.getSignature().charAt(0) == '[') {
                bugReporter.reportBug(new BugInstance(this, "DMI_INVOKING_HASHCODE_ON_ARRAY", NORMAL_PRIORITY)
                .addClassAndMethod(this).addValueSource(item0, this).addSourceLine(this));
            }
        }
        if (seen != RETURN && isReturn(seen) && isRegisterStore(getPrevOpcode(1))) {

            int priority = Priorities.NORMAL_PRIORITY;
            if (getMethodSig().endsWith(")Z")) {
                priority = Priorities.HIGH_PRIORITY;
            } else {
                if (getMethodSig().endsWith(")Ljava/lang/String;")) {
                    priority = Priorities.LOW_PRIORITY;
                }
                if (getPC() == getCode().getCode().length - 1) {
                    priority++;
                }
            }
            bugReporter.reportBug(new BugInstance(this, "DLS_DEAD_LOCAL_STORE_IN_RETURN", priority).addClassAndMethod(this)
                    .addSourceLine(this));
        }
        // System.out.println(getPC() + " " + OPCODE_NAMES[seen] + " " +
        // ternaryConversionState);
        if (seen == IMUL) {
            if (imul_distance != 1) {
                resetIMulCastLong();
            }
            imul_distance = 0;
            if (stack.getStackDepth() > 1) {
                OpcodeStack.Item item0 = stack.getStackItem(0);
                OpcodeStack.Item item1 = stack.getStackItem(1);
                imul_constant = adjustMultiplier(item0.getConstant(), imul_constant);
                imul_constant = adjustMultiplier(item1.getConstant(), imul_constant);

                if (item0.isInitialParameter() || item1.isInitialParameter()) {
                    imul_operand_is_parameter = true;
                }
            }
        } else {
            imul_distance++;
        }

        if (prevOpCode == IMUL && seen == I2L) {
            int priority = adjustPriority(imul_constant, NORMAL_PRIORITY);
            if (priority >= LOW_PRIORITY && imul_constant != 1000 && imul_constant != 60 && imul_operand_is_parameter) {
                priority = NORMAL_PRIORITY;
            }
            if (priority <= best_priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG) {
                best_priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG = priority;
                bugAccumulator.accumulateBug(
                        new BugInstance(this, "ICAST_INTEGER_MULTIPLY_CAST_TO_LONG", priority).addClassAndMethod(this), this);
            }
        }

        if (getMethodName().equals("<clinit>") && (seen == PUTSTATIC || seen == GETSTATIC || seen == INVOKESTATIC)) {
            String clazz = getClassConstantOperand();
            if (!clazz.equals(getClassName())) {
                try {
                    JavaClass targetClass = Repository.lookupClass(clazz);
                    if (Repository.instanceOf(targetClass, getThisClass())) {
                        int priority = NORMAL_PRIORITY;
                        if (seen == GETSTATIC) {
                            priority--;
                        }
                        if (!targetClass.isPublic()) {
                            priority++;
                        }
                        bugAccumulator.accumulateBug(new BugInstance(this, "IC_SUPERCLASS_USES_SUBCLASS_DURING_INITIALIZATION",
                                priority).addClassAndMethod(this).addClass(getClassConstantOperand()), this);

                    }
                } catch (ClassNotFoundException e) {
                    // ignore it
                }

            }
        }
        /*
        if (false && (seen == INVOKEVIRTUAL) && getNameConstantOperand().equals("equals")
                && getSigConstantOperand().equals("(Ljava/lang/Object;)Z") && stack.getStackDepth() > 1) {
            OpcodeStack.Item item0 = stack.getStackItem(0);
            OpcodeStack.Item item1 = stack.getStackItem(1);

            if (item0.isArray() || item1.isArray()) {
                bugAccumulator.accumulateBug(
                        new BugInstance(this, "EC_BAD_ARRAY_COMPARE", NORMAL_PRIORITY).addClassAndMethod(this), this);
            }
        }
         */

        if (seen >= IALOAD && seen <= SALOAD || seen >= IASTORE && seen <= SASTORE) {
            Item index = stack.getStackItem(0);
            if (index.getSpecialKind() == Item.AVERAGE_COMPUTED_USING_DIVISION) {
                SourceLineAnnotation where;
                if (index.getPC() >= 0) {
                    where = SourceLineAnnotation.fromVisitedInstruction(this, index.getPC());
                } else {
                    where = SourceLineAnnotation.fromVisitedInstruction(this);
                }
                bugAccumulator.accumulateBug(
                        new BugInstance(this, "IM_AVERAGE_COMPUTATION_COULD_OVERFLOW", NORMAL_PRIORITY).addClassAndMethod(this),
                        where);
            }

        }

        if ((seen == IFEQ || seen == IFNE) && getPrevOpcode(1) == IMUL
                && (getPrevOpcode(2) == SIPUSH || getPrevOpcode(2) == BIPUSH) && getPrevOpcode(3) == IREM) {
            bugAccumulator.accumulateBug(
                    new BugInstance(this, "IM_MULTIPLYING_RESULT_OF_IREM", LOW_PRIORITY).addClassAndMethod(this), this);
        }

        if (seen == I2S && getPrevOpcode(1) == IUSHR && !shiftOfNonnegativeValue
                && (!constantArgumentToShift || valueOfConstantArgumentToShift % 16 != 0) || seen == I2B
                && getPrevOpcode(1) == IUSHR && !shiftOfNonnegativeValue
                && (!constantArgumentToShift || valueOfConstantArgumentToShift % 8 != 0)) {
            bugAccumulator.accumulateBug(
                    new BugInstance(this, "ICAST_QUESTIONABLE_UNSIGNED_RIGHT_SHIFT", NORMAL_PRIORITY).addClassAndMethod(this),
                    this);
        }

        constantArgumentToShift = false;
        shiftOfNonnegativeValue = false;
        if ((seen == IUSHR || seen == ISHR || seen == ISHL)) {
            if (stack.getStackDepth() <= 1) {
                // don't understand; lie so other detectors won't get concerned
                constantArgumentToShift = true;
                valueOfConstantArgumentToShift = 8;
            } else {
                Object rightHandSide = stack.getStackItem(0).getConstant();

                Object leftHandSide = stack.getStackItem(1).getConstant();
                shiftOfNonnegativeValue = stack.getStackItem(1).isNonNegative();
                if (rightHandSide instanceof Integer) {
                    constantArgumentToShift = true;
                    valueOfConstantArgumentToShift = ((Integer) rightHandSide);
                    if (valueOfConstantArgumentToShift < 0 || valueOfConstantArgumentToShift >= 32) {
                        bugAccumulator.accumulateBug(new BugInstance(this, "ICAST_BAD_SHIFT_AMOUNT",
                                valueOfConstantArgumentToShift < 0 ? LOW_PRIORITY : (valueOfConstantArgumentToShift == 32
                                && getMethodName().equals("hashCode") ? NORMAL_PRIORITY : HIGH_PRIORITY))
                        .addClassAndMethod(this).addInt(valueOfConstantArgumentToShift).describe(IntAnnotation.INT_SHIFT)
                        .addValueSource(stack.getStackItem(1), this), this);
                    }
                }
                if (leftHandSide != null && leftHandSide instanceof Integer && ((Integer) leftHandSide) > 0) {
                    // boring; lie so other detectors won't get concerned
                    constantArgumentToShift = true;
                    valueOfConstantArgumentToShift = 8;
                }

            }
        }

        if (seen == INVOKEVIRTUAL && stack.getStackDepth() > 0
                && (getClassConstantOperand().equals("java/util/Date") || getClassConstantOperand().equals("java/sql/Date"))
                && getNameConstantOperand().equals("setMonth") && getSigConstantOperand().equals("(I)V")) {
            OpcodeStack.Item item = stack.getStackItem(0);
            Object o = item.getConstant();
            if (o != null && o instanceof Integer) {
                int v = (Integer) o;
                if (v < 0 || v > 11) {
                    bugReporter.reportBug(new BugInstance(this, "DMI_BAD_MONTH", HIGH_PRIORITY).addClassAndMethod(this).addInt(v)
                            .describe(IntAnnotation.INT_VALUE).addCalledMethod(this).addSourceLine(this));
                }
            }
        }

        if (seen == INVOKEVIRTUAL && stack.getStackDepth() > 1 && getClassConstantOperand().equals("java/util/Calendar")
                && getNameConstantOperand().equals("set")

                || seen == INVOKESPECIAL && stack.getStackDepth() > 1
                && getClassConstantOperand().equals("java/util/GregorianCalendar") && getNameConstantOperand().equals("<init>")

                ) {
            String sig = getSigConstantOperand();
            if (sig.startsWith("(III")) {
                int pos = sig.length() - 5;
                OpcodeStack.Item item = stack.getStackItem(pos);
                Object o = item.getConstant();
                if (o != null && o instanceof Integer) {
                    int v = (Integer) o;
                    if (v < 0 || v > 11) {
                        bugReporter.reportBug(new BugInstance(this, "DMI_BAD_MONTH", NORMAL_PRIORITY).addClassAndMethod(this)
                                .addInt(v).describe(IntAnnotation.INT_VALUE).addCalledMethod(this).addSourceLine(this));
                    }
                }
            }
        }

        if (isRegisterStore() && (seen == ISTORE || seen == ISTORE_0 || seen == ISTORE_1 || seen == ISTORE_2 || seen == ISTORE_3)
                && getRegisterOperand() == prevOpcodeIncrementedRegister) {
            bugAccumulator.accumulateBug(
                    new BugInstance(this, "DLS_OVERWRITTEN_INCREMENT", HIGH_PRIORITY).addClassAndMethod(this), this);

        }
        if (seen == IINC) {
            prevOpcodeIncrementedRegister = getRegisterOperand();
        } else {
            prevOpcodeIncrementedRegister = -1;
        }

        // Java Puzzlers, Chapter 2, puzzle 1
        // Look for ICONST_2 IREM ICONST_1 IF_ICMPNE L1

        switch (badlyComputingOddState) {
        case 0:
            if (seen == ICONST_2) {
                badlyComputingOddState++;
            }
            break;
        case 1:
            if (seen == IREM) {
                OpcodeStack.Item item = stack.getStackItem(1);
                if (!item.isNonNegative() && item.getSpecialKind() != OpcodeStack.Item.MATH_ABS) {
                    badlyComputingOddState++;
                } else {
                    badlyComputingOddState = 0;
                }
            } else {
                badlyComputingOddState = 0;
            }
            break;
        case 2:
            if (seen == ICONST_1) {
                badlyComputingOddState++;
            } else {
                badlyComputingOddState = 0;
            }
            break;
        case 3:
            if (seen == IF_ICMPEQ || seen == IF_ICMPNE) {
                bugAccumulator.accumulateBug(
                        new BugInstance(this, "IM_BAD_CHECK_FOR_ODD", NORMAL_PRIORITY).addClassAndMethod(this), this);
            }
            badlyComputingOddState = 0;
            break;
        default:
            break;
        }

        // Java Puzzlers, chapter 3, puzzle 12
        if (seen == INVOKEVIRTUAL
                && stack.getStackDepth() > 0
                && (getNameConstantOperand().equals("toString") && getSigConstantOperand().equals("()Ljava/lang/String;")
                        || getNameConstantOperand().equals("append")
                        && getSigConstantOperand().equals("(Ljava/lang/Object;)Ljava/lang/StringBuilder;")
                        && getClassConstantOperand().equals("java/lang/StringBuilder")
                        || getNameConstantOperand().equals("append")
                        && getSigConstantOperand().equals("(Ljava/lang/Object;)Ljava/lang/StringBuffer;")
                        && getClassConstantOperand().equals("java/lang/StringBuffer") || (getNameConstantOperand()
                                .equals("print") || getNameConstantOperand().equals("println"))
                                && getSigConstantOperand().equals("(Ljava/lang/Object;)V"))) {
            OpcodeStack.Item item = stack.getStackItem(0);
            String signature = item.getSignature();
            if (signature != null && signature.startsWith("[")) {
                boolean debuggingContext = signature.equals("[Ljava/lang/StackTraceElement;");

                if (!debuggingContext) {
                    for (CodeException e : getCode().getExceptionTable()) {
                        if (e.getHandlerPC() <= getPC() && e.getHandlerPC() + 30 >= getPC()) {
                            debuggingContext = true;
                        }
                    }

                    for (int i = 1; !debuggingContext && i < stack.getStackDepth(); i++) {
                        OpcodeStack.Item e = stack.getStackItem(i);

                        if (e.getSignature().indexOf("Logger") >= 0 || e.getSignature().indexOf("Exception") >= 0) {
                            debuggingContext = true;
                        }

                        XField f = e.getXField();
                        if (f != null && (SYSTEM_ERR.equals(f.getFieldDescriptor()) || SYSTEM_OUT.equals(f.getFieldDescriptor()))) {
                            debuggingContext = true;
                        }
                    }
                }
                //                String name = null;
                int reg = item.getRegisterNumber();
                Collection<BugAnnotation> as = new ArrayList<BugAnnotation>();
                XField field = item.getXField();
                FieldAnnotation fieldAnnotation = null;
                if (field != null) {
                    fieldAnnotation = FieldAnnotation.fromXField(field);
                    fieldAnnotation.setDescription(FieldAnnotation.LOADED_FROM_ROLE);
                }

                if (reg != -1) {
                    LocalVariableAnnotation lva = LocalVariableAnnotation.getLocalVariableAnnotation(getMethod(), reg, getPC(),
                            getPC() - 1);
                    if (lva.isNamed()) {
                        as.add(lva);
                        if (fieldAnnotation != null) {
                            as.add(fieldAnnotation);
                        }
                    } else {
                        if (fieldAnnotation != null) {
                            as.add(fieldAnnotation);
                        }
                        as.add(lva);
                    }
                } else if (fieldAnnotation != null) {
                    as.add(fieldAnnotation);
                } else {
                    XMethod m = item.getReturnValueOf();
                    if (m != null) {
                        MethodAnnotation methodAnnotation = MethodAnnotation.fromXMethod(m);
                        methodAnnotation.setDescription(MethodAnnotation.METHOD_RETURN_VALUE_OF);
                        as.add(methodAnnotation);
                    }
                }
                int priority = debuggingContext ? NORMAL_PRIORITY : HIGH_PRIORITY;
                if (!as.isEmpty()) {
                    bugAccumulator.accumulateBug(new BugInstance(this, "DMI_INVOKING_TOSTRING_ON_ARRAY", priority)
                    .addClassAndMethod(this).addAnnotations(as), this);
                } else {
                    bugAccumulator.accumulateBug(
                            new BugInstance(this, "DMI_INVOKING_TOSTRING_ON_ANONYMOUS_ARRAY", priority).addClassAndMethod(this),
                            this);
                }
            }
        }

        if (isTigerOrHigher) {
            if (previousMethodInvocation != null && prevOpCode == INVOKEVIRTUAL && seen == INVOKESTATIC) {
                String classNameForPreviousMethod = previousMethodInvocation.getClassName();
                String classNameForThisMethod = getClassConstantOperand();
                if (classNameForPreviousMethod.startsWith("java.lang.")
                        && classNameForPreviousMethod.equals(classNameForThisMethod.replace('/', '.'))
                        && previousMethodInvocation.getName().endsWith("Value")
                        && previousMethodInvocation.getSignature().length() == 3
                        && getNameConstantOperand().equals("valueOf")
                        && getSigConstantOperand().charAt(1) == previousMethodInvocation.getSignature().charAt(2)) {
                    bugAccumulator.accumulateBug(
                            new BugInstance(this, "BX_UNBOXING_IMMEDIATELY_REBOXED", NORMAL_PRIORITY).addClassAndMethod(this)
                            .addCalledMethod(this),
                            this);
                }

            }

            if (previousMethodInvocation != null && prevOpCode == INVOKESPECIAL && seen == INVOKEVIRTUAL) {
                String classNameForPreviousMethod = previousMethodInvocation.getClassName();
                String classNameForThisMethod = getClassConstantOperand();
                if (classNameForPreviousMethod.startsWith("java.lang.")
                        && classNameForPreviousMethod.equals(classNameForThisMethod.replace('/', '.'))
                        && getNameConstantOperand().endsWith("Value") && getSigConstantOperand().length() == 3) {
                    if (getSigConstantOperand().charAt(2) == previousMethodInvocation.getSignature().charAt(1)) {
                        bugAccumulator.accumulateBug(
                                new BugInstance(this, "BX_BOXING_IMMEDIATELY_UNBOXED", NORMAL_PRIORITY).addClassAndMethod(this),
                                this);
                    } else {
                        bugAccumulator.accumulateBug(new BugInstance(this, "BX_BOXING_IMMEDIATELY_UNBOXED_TO_PERFORM_COERCION",
                                NORMAL_PRIORITY).addClassAndMethod(this), this);
                    }

                    ternaryConversionState = 1;
                } else {
                    ternaryConversionState = 0;
                }

            } else if (seen == INVOKEVIRTUAL) {
                if (getClassConstantOperand().startsWith("java/lang") && getNameConstantOperand().endsWith("Value")
                        && getSigConstantOperand().length() == 3) {
                    ternaryConversionState = 1;
                } else {
                    ternaryConversionState = 0;
                }
            } else if (ternaryConversionState == 1) {
                if (I2L < seen && seen <= I2S) {
                    ternaryConversionState = 2;
                } else {
                    ternaryConversionState = 0;
                }
            } else if (ternaryConversionState == 2) {
                ternaryConversionState = 0;
                if (seen == GOTO) {
                    bugReporter.reportBug(new BugInstance(this, "BX_UNBOXED_AND_COERCED_FOR_TERNARY_OPERATOR", NORMAL_PRIORITY)
                    .addClassAndMethod(this).addSourceLine(this));
                }
            } else {
                ternaryConversionState = 0;
            }
        }

        AssertInvokedFromRun: if (seen == INVOKESTATIC) {
            if ((getNameConstantOperand().startsWith("assert") || getNameConstantOperand().startsWith("fail"))
                    && getMethodName().equals("run") && implementsRunnable(getThisClass())) {
                int size1 = Util.getSizeOfSurroundingTryBlock(getConstantPool(), getMethod().getCode(), "java/lang/Throwable",
                        getPC());
                int size2 = Util.getSizeOfSurroundingTryBlock(getConstantPool(), getMethod().getCode(), "java/lang/Error",
                        getPC());
                int size3 = Util.getSizeOfSurroundingTryBlock(getConstantPool(), getMethod().getCode(),
                        "java/lang/AssertionFailureError", getPC());
                int size = Math.min(Math.min(size1, size2), size3);
                if (size == Integer.MAX_VALUE) {
                    String dottedClassName = getClassConstantOperand().replace('/', '.');
                    if (!dottedClassName.startsWith("junit")) {
                        try {
                            JavaClass targetClass = AnalysisContext.currentAnalysisContext().lookupClass(dottedClassName);
                            if (!targetClass.getSuperclassName().startsWith("junit")) {
                                break AssertInvokedFromRun;
                            }
                        } catch (ClassNotFoundException e) {
                            AnalysisContext.reportMissingClass(e);
                            break AssertInvokedFromRun;
                        }
                    }

                    bugAccumulator.accumulateBug(new BugInstance(this, "IJU_ASSERT_METHOD_INVOKED_FROM_RUN_METHOD",
                            extendsThread(getThisClass()) ? NORMAL_PRIORITY : LOW_PRIORITY).addClassAndMethod(this), this);

                }

            }
        }
        if (seen == INVOKESPECIAL && getClassConstantOperand().startsWith("java/lang/")
                && getNameConstantOperand().equals("<init>") && getSigConstantOperand().length() == 4) {
            previousMethodInvocation = XFactory.createReferencedXMethod(this);
        } else if (seen == INVOKESTATIC && getClassConstantOperand().startsWith("java/lang/")
                && getNameConstantOperand().equals("valueOf") && getSigConstantOperand().length() == 4) {
            previousMethodInvocation = XFactory.createReferencedXMethod(this);
        } else if (seen == INVOKEVIRTUAL && getClassConstantOperand().startsWith("java/lang/")
                && getNameConstantOperand().endsWith("Value")
                && getSigConstantOperand().length() == 3) {
            previousMethodInvocation = XFactory.createReferencedXMethod(this);
        } else {
            previousMethodInvocation = null;
        }

        if (testingEnabled && seen == IAND || seen == LAND) {
            OpcodeStack.Item rhs = stack.getStackItem(0);
            OpcodeStack.Item lhs = stack.getStackItem(1);
            Object constant = rhs.getConstant();
            OpcodeStack.Item value = lhs;
            if (constant == null) {
                constant = lhs.getConstant();
                value = rhs;
            }
            if (constant instanceof Number && (seen == LAND || value.getSpecialKind() == OpcodeStack.Item.RESULT_OF_L2I)) {
                long constantValue = ((Number) constant).longValue();
                if ((constantValue == 0xEFFFFFFFL || constantValue == 0xEFFFFFFFFFFFFFFFL || seen == IAND
                        && constantValue == 0xEFFFFFFF)) {
                    bugAccumulator.accumulateBug(new BugInstance(this, "TESTING", seen == LAND ? HIGH_PRIORITY : NORMAL_PRIORITY)
                    .addClassAndMethod(this).addString("Possible failed attempt to mask lower 31 bits of an int")
                    .addValueSource(value, this), this);
                }

            }
        }

        if (seen == INEG) {
            OpcodeStack.Item top = stack.getStackItem(0);
            XMethod m = top.getReturnValueOf();
            if (m != null) {
                if (m.getName().equals("compareTo") || m.getName().equals("compare")) {
                    bugAccumulator.accumulateBug(new BugInstance(this, "RV_NEGATING_RESULT_OF_COMPARETO", NORMAL_PRIORITY)
                    .addClassAndMethod(this)
                    .addCalledMethod(m).addValueSource(top, this), this);
                }
            }
View Full Code Here

            String className = getClassConstantOperand();
            if (!className.startsWith("[")) {
                try {
                    XClass c = Global.getAnalysisCache().getClassAnalysis(XClass.class,
                            DescriptorFactory.createClassDescriptor(className));
                    XMethod m = Hierarchy2.findInvocationLeastUpperBound(c, getNameConstantOperand(), getSigConstantOperand(),
                            seen == INVOKESTATIC, seen == INVOKEINTERFACE);
                    if (m == null) {
                        break;
                    }
                    String[] exceptions = m.getThrownExceptions();
                    if (exceptions != null) {
                        for (String name : exceptions) {
                            throwList.add(new ExceptionThrown(ClassName.toDottedClassName(name), getPC()));
                        }
                    }
View Full Code Here

    HashSet<TypeQualifierValue<?>> applicableApplications;

    @Override
    public void visit(Code m) {
        applicableApplications = new HashSet<TypeQualifierValue<?>>();
        XMethod xMethod = getXMethod();

        // Find the direct annotations on this method
        updateApplicableAnnotations(xMethod);

        // Find direct annotations on called methods and loaded fields
View Full Code Here

            // 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;
View Full Code Here

TOP

Related Classes of edu.umd.cs.findbugs.ba.XMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.