Java Code Examples for javax.annotation.meta.When#UNKNOWN

The following examples show how to use javax.annotation.meta.When#UNKNOWN . 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: ForwardTypeQualifierDataflowAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void registerParameterSources() {
    ValueNumberFrame vnaFrameAtEntry = vnaDataflow.getStartFact(cfg.getEntry());

    SignatureParser sigParser = new SignatureParser(xmethod.getSignature());
    int firstParamSlot = xmethod.isStatic() ? 0 : 1;

    int param = 0;
    int slotOffset = 0;

    for (String paramSig : sigParser.parameterSignatures()) {

        // Get the TypeQualifierAnnotation for this parameter
        SourceSinkInfo info;
        TypeQualifierAnnotation tqa = TypeQualifierApplications.getEffectiveTypeQualifierAnnotation(xmethod, param,
                typeQualifierValue);
        When when = (tqa != null) ? tqa.when : When.UNKNOWN;
        ValueNumber vn = vnaFrameAtEntry.getValue(slotOffset + firstParamSlot);
        info = new SourceSinkInfo(SourceSinkType.PARAMETER, cfg.getLocationAtEntry(), vn, when);
        info.setParameterAndLocal(param, slotOffset + firstParamSlot);
        registerSourceSink(info);

        param++;
        slotOffset += SignatureParser.getNumSlotsForType(paramSig);
    }
}
 
Example 2
Source File: BackwardTypeQualifierDataflowAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void modelFieldStore(Location location) throws DataflowAnalysisException {
    // Model field stores
    XField writtenField = XFactory.createXField((FieldInstruction) location.getHandle().getInstruction(), cpg);
    TypeQualifierAnnotation tqa = TypeQualifierApplications.getEffectiveTypeQualifierAnnotation(writtenField,
            typeQualifierValue);
    When when = (tqa != null) ? tqa.when : When.UNKNOWN;

    // The ValueNumberFrame *before* the FieldInstruction should
    // have the ValueNumber of the stored value on the top of the stack.
    ValueNumberFrame vnaFrameAtStore = vnaDataflow.getFactAtLocation(location);
    if (vnaFrameAtStore.isValid()) {
        ValueNumber vn = vnaFrameAtStore.getTopValue();
        SourceSinkInfo sink = new SourceSinkInfo(SourceSinkType.FIELD_STORE, location, vn, when);
        registerSourceSink(sink);
    }
}
 
Example 3
Source File: TypeQualifierValue.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public When validate(@CheckForNull Object constantValue) {
    if (validator == null) {
        throw new IllegalStateException("No validator");
    }
    IAnalysisCache analysisCache = Global.getAnalysisCache();
    Profiler profiler = analysisCache.getProfiler();
    profiler.start(validator.getClass());
    try {
        return ValidationSecurityManager.sandboxedValidation(proxy, validator, constantValue);
    } catch (Exception e) {
        AnalysisContext.logError("Error executing custom validator for " + typeQualifier + " " + constantValue, e);
        return When.UNKNOWN;
    } finally {
        profiler.end(validator.getClass());
    }
}
 
Example 4
Source File: InconsistentAnnotations.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitClassContext(ClassContext classContext) {

    JavaClass jclass = classContext.getJavaClass();

    for (Method method : jclass.getMethods()) {
        XMethod xmethod = XFactory.createXMethod(classContext.getJavaClass(), method);
        ParameterProperty nonnullParameters = AnalysisContext.currentAnalysisContext().getUnconditionalDerefParamDatabase()
                .getProperty(xmethod.getMethodDescriptor());
        if (nonnullParameters != null) {
            for (int p : nonnullParameters.iterable()) {
                TypeQualifierAnnotation directTypeQualifierAnnotation = TypeQualifierApplications
                        .getDirectTypeQualifierAnnotation(xmethod, p, nonnullTypeQualifierValue);
                if (directTypeQualifierAnnotation != null && directTypeQualifierAnnotation.when == When.UNKNOWN) {
                    //
                    // The LocalVariableAnnotation is constructed using the
                    // local variable
                    // number of the parameter, not the parameter number.
                    //
                    int paramLocal = xmethod.isStatic() ? p : p + 1;

                    reporter.reportBug(new BugInstance(this, "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE",
                            NORMAL_PRIORITY).addClassAndMethod(jclass, method).add(
                                    LocalVariableAnnotation.getParameterLocalVariableAnnotation(method, paramLocal)));

                }

            }
        }
    }

}
 
Example 5
Source File: ForwardTypeQualifierDataflowAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void registerConstantSource(Location location, @CheckForNull Object constantValue) throws DataflowAnalysisException {

        When w;
        if (typeQualifierValue.canValidate(constantValue)) {
            w = typeQualifierValue.validate(constantValue);
        } else if (typeQualifierValue.isStrictQualifier()) {
            return;
        } else {
            w = When.UNKNOWN;
        }

        registerTopOfStackSource(SourceSinkType.CONSTANT_VALUE, location, w, false, constantValue);
    }
 
Example 6
Source File: ForwardTypeQualifierDataflowAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void registerReturnValueSource(Location location) throws DataflowAnalysisException {
    // Nothing to do if called method does not return a value
    InvokeInstruction inv = (InvokeInstruction) location.getHandle().getInstruction();
    if (inv instanceof INVOKEDYNAMIC) {
        return;
    }
    String calledMethodSig = inv.getSignature(cpg);
    if (calledMethodSig.endsWith(")V")) {
        return;
    }

    XMethod calledXMethod = XFactory.createXMethod(inv, cpg);
    if (TypeQualifierDataflowAnalysis.isIdentifyFunctionForTypeQualifiers(calledXMethod)) {
        return;
    }

    if (calledXMethod.isResolved()) {
        TypeQualifierAnnotation tqa = TypeQualifierApplications.getEffectiveTypeQualifierAnnotation(calledXMethod,
                typeQualifierValue);

        boolean interproc = false;
        if (TypeQualifierDatabase.USE_DATABASE && tqa == null) {
            // See if there's an entry in the interprocedural
            // type qualifier database.
            TypeQualifierDatabase tqdb = Global.getAnalysisCache().getDatabase(TypeQualifierDatabase.class);
            tqa = tqdb.getReturnValue(calledXMethod.getMethodDescriptor(), typeQualifierValue);
            if (tqa != null) {
                interproc = true;
            }
        }

        When when = (tqa != null) ? tqa.when : When.UNKNOWN;
        registerTopOfStackSource(SourceSinkType.RETURN_VALUE_OF_CALLED_METHOD, location, when, interproc, null);
    }
}
 
Example 7
Source File: ForwardTypeQualifierDataflowAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void registerFieldLoadSource(Location location) throws DataflowAnalysisException {
    XField loadedField = XFactory.createXField((FieldInstruction) location.getHandle().getInstruction(), cpg);
    if (loadedField.isResolved()) {
        TypeQualifierAnnotation tqa = TypeQualifierApplications.getEffectiveTypeQualifierAnnotation(loadedField,
                typeQualifierValue);
        When when = (tqa != null) ? tqa.when : When.UNKNOWN;
        registerTopOfStackSource(SourceSinkType.FIELD_LOAD, location, when, false, null);
    }

}
 
Example 8
Source File: ForwardTypeQualifierDataflowAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void registerTopOfStackSource(SourceSinkType sourceSinkType, Location location, When when, boolean interproc,
        @CheckForNull Object constantValue) throws DataflowAnalysisException {
    if (when == When.UNKNOWN && !typeQualifierValue.isStrictQualifier()) {
        return;
    }
    ValueNumberFrame vnaFrameAfterInstruction = vnaDataflow.getFactAfterLocation(location);
    if (vnaFrameAfterInstruction.isValid()) {
        ValueNumber tosValue = vnaFrameAfterInstruction.getTopValue();
        SourceSinkInfo sourceSinkInfo = new SourceSinkInfo(sourceSinkType, location, tosValue, when);
        sourceSinkInfo.setInterproc(interproc);
        sourceSinkInfo.setConstantValue(constantValue);
        registerSourceSink(sourceSinkInfo);
    }
}
 
Example 9
Source File: BackwardTypeQualifierDataflowAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void modelReturn(TypeQualifierAnnotation returnValueAnnotation, Location location) throws DataflowAnalysisException {
    When when = (returnValueAnnotation != null) ? returnValueAnnotation.when : When.UNKNOWN;

    // Model return statement
    ValueNumberFrame vnaFrameAtReturn = vnaDataflow.getFactAtLocation(location);
    if (vnaFrameAtReturn.isValid()) {
        ValueNumber topValue = vnaFrameAtReturn.getTopValue();
        SourceSinkInfo sink = new SourceSinkInfo(SourceSinkType.RETURN_VALUE, location, topValue, when);
        registerSourceSink(sink);
    }
}
 
Example 10
Source File: Analysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param result
 * @param applicableApplications
 */
public static void addKnownTypeQualifiers(HashSet<? super TypeQualifierValue<?>> result,
        Collection<TypeQualifierAnnotation> applicableApplications) {
    for (TypeQualifierAnnotation t : applicableApplications) {
        if (t.when != When.UNKNOWN) {
            result.add(t.typeQualifier);
        }
    }
}
 
Example 11
Source File: ClassName.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Convert class name to slashed format. If the class name is already in
 * slashed format, it is returned unmodified.
 *
 * @param className
 *            a class name
 * @return the same class name in slashed format
 */
@SlashedClassName
@SuppressFBWarnings("TQ_EXPLICIT_UNKNOWN_SOURCE_VALUE_REACHES_ALWAYS_SINK")
public static String toSlashedClassName(@SlashedClassName(when = When.UNKNOWN) String className) {
    if (className.indexOf('.') >= 0) {
        return className.replace('.', '/');
    }
    return className;
}
 
Example 12
Source File: ClassName.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Convert class name to dotted format. If the class name is already in
 * dotted format, it is returned unmodified.
 *
 * @param className
 *            a class name
 * @return the same class name in dotted format
 */
@DottedClassName
@SuppressFBWarnings("TQ_EXPLICIT_UNKNOWN_SOURCE_VALUE_REACHES_NEVER_SINK")
public static String toDottedClassName(@SlashedClassName(when = When.UNKNOWN) String className) {
    if (className.indexOf('/') >= 0) {
        return className.replace('/', '.');
    }
    return className;
}
 
Example 13
Source File: MockNullableTestCorrect.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public void paramNonnullUnknown (@Nonnull (when = When.UNKNOWN) final String s)
{}
 
Example 14
Source File: TaintedTest.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Untainted
Object sanitize(@Untainted(when = When.UNKNOWN) Object o) {
    return o;
}
 
Example 15
Source File: TestFooUnknownSource.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@ExpectWarning("TQ")
int unknownSourceToNeverSourceFalsePositive(@Foo(when = When.UNKNOWN) String c) {
    return g(c); // should generate a warning here
}
 
Example 16
Source File: TestFooUnknownSource.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@ExpectWarning("TQ")
int unknownSourceToNeverSinkFalsePositive(@Foo(when = When.UNKNOWN) String c) {
    return f(c); // should generate a warning here
}
 
Example 17
Source File: BackwardTypeQualifierDataflowAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void modelArguments(Location location) throws DataflowAnalysisException {
    // Model arguments to called method
    InvokeInstruction inv = (InvokeInstruction) location.getHandle().getInstruction();
    if (inv instanceof INVOKEDYNAMIC) {
        return;
    }
    XMethod calledMethod = XFactory.createXMethod(inv, cpg);

    SignatureParser sigParser = new SignatureParser(calledMethod.getSignature());
    if (sigParser.getNumParameters() == 0) {
        return;
    }
    ValueNumberFrame vnaFrame = vnaDataflow.getFactAtLocation(location);

    if (!vnaFrame.isValid()) {
        // AnalysisContext.logError("bad vna frame  in " + xmethod +
        // " at location " + location.getHandle().getPosition() +
        // " calling " + calledMethod);
        return;
    }

    if (TypeQualifierDataflowAnalysis.isIdentifyFunctionForTypeQualifiers(calledMethod)) {
        return;
    }

    for (int param = 0; param < calledMethod.getNumParams(); param++) {
        TypeQualifierAnnotation tqa = TypeQualifierApplications.getEffectiveTypeQualifierAnnotation(calledMethod, param,
                typeQualifierValue);

        boolean interproc = false;
        if (TypeQualifierDatabase.USE_DATABASE && tqa == null) {
            // See if there's an entry for this parameter
            // in the interprocedural type qualifier database.
            TypeQualifierDatabase tqdb = Global.getAnalysisCache().getDatabase(TypeQualifierDatabase.class);
            tqa = tqdb.getParameter(calledMethod.getMethodDescriptor(), param, typeQualifierValue);
            if (tqa != null) {
                interproc = true;
            }
        }

        When when = (tqa != null) ? tqa.when : When.UNKNOWN;

        ValueNumber vn = vnaFrame.getArgument(inv, cpg, param, sigParser);

        SourceSinkInfo info = new SourceSinkInfo(SourceSinkType.ARGUMENT_TO_CALLED_METHOD, location, vn, when);
        info.setParameter(param);
        info.setInterproc(interproc);

        registerSourceSink(info);

    }
}
 
Example 18
Source File: BugInstance.java    From spotbugs with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Add a class annotation. If this is the first class annotation added, it
 * becomes the primary class annotation.
 *
 * @param className
 *            the name of the class
 * @return this object
 */
@Nonnull
public BugInstance addClass(@SlashedClassName(when = When.UNKNOWN) String className) {
    ClassAnnotation classAnnotation = new ClassAnnotation(ClassName.toDottedClassName(className));
    add(classAnnotation);
    return this;
}
 
Example 19
Source File: IMockNullableTest.java    From ph-commons with Apache License 2.0 votes vote down vote up
void paramNonnullUnknown (@Nonnull (when = When.UNKNOWN) String s);