barry-m / checker-framework

Automatically exported from code.google.com/p/checker-framework
Other
0 stars 0 forks source link

Warning message when using -AresolveReflection #430

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Here's a small example:

class Test {

    boolean test(@Nullable String str) {
        if (str == null) {
            return false;
        }
        int index = str.indexOf(' ');
        String className = index == -1 ? str : str.substring(0, index);
        return className.equals("X");
    }
}

Generates this warning message:

Test.java:10: warning: [method.evaluation.exception] Failed to evaluate method 
public java.lang.String java.lang.String.substring(int,int) because it threw an 
exception: java.lang.StringIndexOutOfBoundsException: String index out of 
range: -1. Treating result as @UnknownVal
        String className = index == -1 ? str : str.substring(0, index);
                                                            ^

It seems harmless as far I can tell so far.

Original issue reported on code.google.com by trask.st...@gmail.com on 19 Apr 2015 at 7:22

GoogleCodeExporter commented 9 years ago
You are correct, this is harmless (but the test case still indicates a bug).

The warning message is related to the constant value analysis (which is invoked 
by reflection resolution).
It says that the Checker Framework tried to determine, at compile time, the 
run-time value of an expression; it was unable to do so, so it proceeded 
normally.

I see a few issues here that could be improved:
 * the method.evaluation.exception error message should default to off rather than on, since it is primarily of interest to people who are trying to understand the behavior of the constant value analysis.
 * if the message is issued, it should be issued only once rather than multiple times (I saw it 4 times in the test case).
 * str is not a constant expression, so I'm not sure why str.substring(0, index) is being evaluated by the constant value analysis at all.

The command line

  $CHECKERFRAMEWORK/bin/javac -g -processor org.checkerframework.checker.nullness.NullnessChecker TestResolveReflection.java -AresolveReflection

reproduces the error, when used with the attached file.

Original comment by michael.ernst@gmail.com on 19 Apr 2015 at 8:12

Attachments: