Details about the known misuse from the MUBench dataset.
Description: | In line 256, scanner.next() is called without checking if there are more
elements. Because the scanner is built from the JavaSource parameter,
which may be invalid (e.g., package keyword followed by an empty package
name), this may lead to a runtime exception without a useful error message. |
Fix Description: |
Add a check and report a meaningful error message. (see diff) |
Violation Types: |
- missing/condition/value_or_state
|
In File: | de/strullerbaumann/visualee/examiner/Examiner.java |
In Method: | findAndSetPackage(JavaSource) |
Code with Misuse: |
class Examiner {
protected static void findAndSetPackage(JavaSource javaSource) {
Scanner scanner = Examiner.getSourceCodeScanner(javaSource.getSourceCode());
while (scanner.hasNext()) {
String token = scanner.next();
if (javaSource.getPackagePath() == null && token.equals("package")) {
token = scanner.next();
if (token.endsWith(";")) {
String packagePath = token.substring(0, token.indexOf(';'));
javaSource.setPackagePath(packagePath);
}
}
}
}
}
|
Code with Pattern(s): |
class SetPackagePath {
void pattern(Scanner scanner, JavaSource javaSource) {
while (scanner.hasNext()) {
String token = scanner.next();
if (javaSource.getPackagePath() == null && token.equals("package")) {
if (!scanner.hasNext()) {
throw new IllegalArgumentException("Insufficient number of tokens to set package");
}
token = scanner.next();
if (token.endsWith(";")) {
String packagePath = token.substring(0, token.indexOf(';'));
javaSource.setPackagePath(packagePath);
}
}
}
}
}
|