Skip to content

Commit 13606da

Browse files
committed
SONARJAVA-1382 Add flag for failing parsing in context
1 parent f5539ef commit 13606da

5 files changed

Lines changed: 48 additions & 10 deletions

File tree

java-squid/src/main/java/org/sonar/java/model/DefaultJavaFileScannerContext.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,20 @@ public class DefaultJavaFileScannerContext implements JavaFileScannerContext {
5454
private final ComplexityVisitor complexityVisitor;
5555
private final File file;
5656
private final Integer javaVersion;
57+
private final boolean fileParsed;
5758
private final Map<Class<? extends SECheck>, SetMultimap<Tree, String>> seIssues = new HashMap<>();
5859

5960
public DefaultJavaFileScannerContext(
6061
CompilationUnitTree tree, SourceFile sourceFile, File file, SemanticModel semanticModel, boolean analyseAccessors, @Nullable SonarComponents sonarComponents,
61-
@Nullable Integer javaVersion) {
62+
@Nullable Integer javaVersion, boolean fileParsed) {
6263
this.tree = tree;
6364
this.sourceFile = sourceFile;
6465
this.file = file;
6566
this.semanticModel = semanticModel;
6667
this.sonarComponents = sonarComponents;
6768
this.complexityVisitor = new ComplexityVisitor(analyseAccessors);
6869
this.javaVersion = javaVersion;
70+
this.fileParsed = fileParsed;
6971
}
7072

7173
@Override
@@ -110,6 +112,11 @@ public Integer getJavaVersion() {
110112
return this.javaVersion;
111113
}
112114

115+
@Override
116+
public boolean fileParsed() {
117+
return fileParsed;
118+
}
119+
113120
@Override
114121
public String getFileKey() {
115122
return sourceFile.getKey();

java-squid/src/main/java/org/sonar/java/model/InternalVisitorsBridge.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ public Integer getJavaVersion() {
9999
public void visitFile(@Nullable Tree parsedTree) {
100100
semanticModel = null;
101101
CompilationUnitTree tree = new JavaTree.CompilationUnitTreeImpl(null, Lists.<ImportClauseTree>newArrayList(), Lists.<Tree>newArrayList(), null);
102-
if ((parsedTree != null) && parsedTree.is(Tree.Kind.COMPILATION_UNIT)) {
102+
boolean fileParsed = parsedTree != null;
103+
if (fileParsed && parsedTree.is(Tree.Kind.COMPILATION_UNIT)) {
103104
tree = (CompilationUnitTree) parsedTree;
104105
if (isNotJavaLangOrSerializable(PackageUtils.packageName(tree.packageDeclaration(), "/"))) {
105106
try {
@@ -113,7 +114,7 @@ public void visitFile(@Nullable Tree parsedTree) {
113114
SemanticModel.handleMissingTypes(tree);
114115
}
115116
}
116-
JavaFileScannerContext javaFileScannerContext = createScannerContext(tree, semanticModel, analyseAccessors, sonarComponents);
117+
JavaFileScannerContext javaFileScannerContext = createScannerContext(tree, semanticModel, analyseAccessors, sonarComponents, fileParsed);
117118
// Symbolic execution checks
118119
if (symbolicExecutionEnabled && isNotJavaLangOrSerializable(PackageUtils.packageName(tree.packageDeclaration(), "/"))) {
119120
new SymbolicExecutionVisitor().scanFile(javaFileScannerContext);
@@ -144,15 +145,17 @@ private boolean shouldBeExecuted(JavaFileScanner scanner) {
144145
return true;
145146
}
146147

147-
protected JavaFileScannerContext createScannerContext(CompilationUnitTree tree, SemanticModel semanticModel, boolean analyseAccessors, SonarComponents sonarComponents) {
148+
protected JavaFileScannerContext createScannerContext(
149+
CompilationUnitTree tree, SemanticModel semanticModel, boolean analyseAccessors, SonarComponents sonarComponents, boolean fileParsed) {
148150
return new DefaultJavaFileScannerContext(
149151
tree,
150152
(SourceFile) getContext().peekSourceCode(),
151153
getContext().getFile(),
152154
semanticModel,
153155
analyseAccessors,
154156
sonarComponents,
155-
javaVersion);
157+
javaVersion,
158+
fileParsed);
156159
}
157160

158161
private boolean isNotJavaLangOrSerializable(String packageName) {

java-squid/src/main/java/org/sonar/java/model/VisitorsBridge.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ public VisitorsBridge(Iterable visitors, List<File> projectClasspath, @Nullable
7272
}
7373

7474
@Override
75-
protected JavaFileScannerContext createScannerContext(CompilationUnitTree tree, SemanticModel semanticModel, boolean analyseAccessors, SonarComponents sonarComponents) {
75+
protected JavaFileScannerContext createScannerContext(CompilationUnitTree tree, SemanticModel semanticModel, boolean analyseAccessors, SonarComponents sonarComponents,
76+
boolean failedParsing) {
7677
testContext = new TestJavaFileScannerContext(tree, (SourceFile) getContext().peekSourceCode(), getContext().getFile(), semanticModel, analyseAccessors, sonarComponents,
77-
getJavaVersion());
78+
getJavaVersion(), failedParsing);
7879
return testContext;
7980
}
8081

@@ -88,8 +89,8 @@ public static class TestJavaFileScannerContext extends DefaultJavaFileScannerCon
8889

8990
public TestJavaFileScannerContext(
9091
CompilationUnitTree tree, SourceFile sourceFile, File file, SemanticModel semanticModel, boolean analyseAccessors, @Nullable SonarComponents sonarComponents,
91-
@Nullable Integer javaVersion) {
92-
super(tree, sourceFile, file, semanticModel, analyseAccessors, sonarComponents, javaVersion);
92+
@Nullable Integer javaVersion, boolean failedParsing) {
93+
super(tree, sourceFile, file, semanticModel, analyseAccessors, sonarComponents, javaVersion, failedParsing);
9394
}
9495

9596
public Set<AnalyzerMessage> getIssues() {

java-squid/src/main/java/org/sonar/plugins/java/api/JavaFileScannerContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public interface JavaFileScannerContext {
5757
@Nullable
5858
Integer getJavaVersion();
5959

60+
boolean fileParsed();
61+
6062
/**
6163
* @deprecated As of release 3.6, replaced by {@link #getComplexityNodes(Tree)}
6264
*/

java-squid/src/test/java/org/sonar/java/model/InternalVisitorsBridgeTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.base.Charsets;
2323
import com.google.common.collect.ImmutableList;
2424
import com.google.common.collect.Lists;
25+
import com.sonar.sslr.api.RecognitionException;
2526
import org.junit.Test;
2627
import org.sonar.java.ast.parser.JavaParser;
2728
import org.sonar.java.ast.visitors.VisitorContext;
@@ -32,8 +33,11 @@
3233
import org.sonar.plugins.java.api.tree.CompilationUnitTree;
3334
import org.sonar.plugins.java.api.tree.MethodTree;
3435
import org.sonar.plugins.java.api.tree.Tree;
36+
import org.sonar.plugins.java.api.tree.Tree.Kind;
3537
import org.sonar.squidbridge.api.SourceProject;
3638

39+
import javax.annotation.Nullable;
40+
3741
import java.io.File;
3842
import java.util.Collections;
3943
import java.util.List;
@@ -50,6 +54,7 @@ public void test_semantic_exclusions() {
5054
@Override
5155
public void scanFile(JavaFileScannerContext context) {
5256
assertThat(context.getSemanticModel() == null).isTrue();
57+
assertThat(context.fileParsed()).isTrue();
5358
}
5459
}), Lists.<File>newArrayList(), null);
5560
visitorsBridgeWithoutSemantic.setContext(context);
@@ -69,6 +74,7 @@ public List<Tree.Kind> nodesToVisit() {
6974
@Override
7075
public void scanFile(JavaFileScannerContext context) {
7176
assertThat(context.getSemanticModel()).isNotNull();
77+
assertThat(context.fileParsed()).isTrue();
7278
super.scanFile(context);
7379
}
7480

@@ -86,6 +92,20 @@ public void visitNode(Tree tree) {
8692
checkFile(contstructFileName("java", "lang", "annotation", "Foo.java"), "package java.lang.annotation; class Annotation {}", visitorsBridgeWithSemantic);
8793
checkFile(contstructFileName("java", "io", "File.java"), "package java.io; class A {}", visitorsBridgeWithSemantic);
8894
checkFile(contstructFileName("src", "foo", "bar", "java", "lang", "someFile.java"), "package foo.bar.java.lang; class A { void method() { ; } }", visitorsBridgeWithSemantic);
95+
96+
InternalVisitorsBridge visitorsBridgeWithParsingIssue = new InternalVisitorsBridge(Collections.singletonList(new IssuableSubscriptionVisitor() {
97+
@Override
98+
public void scanFile(JavaFileScannerContext context) {
99+
assertThat(context.fileParsed()).isFalse();
100+
}
101+
102+
@Override
103+
public List<Kind> nodesToVisit() {
104+
return ImmutableList.of(Tree.Kind.METHOD);
105+
}
106+
}), Lists.<File>newArrayList(), null);
107+
visitorsBridgeWithParsingIssue.setContext(context);
108+
checkFile(contstructFileName("org", "foo", "bar", "Foo.java"), "class Foo { arrrrrrgh", visitorsBridgeWithParsingIssue);
89109
}
90110

91111
private void checkFile(String filename, String code, InternalVisitorsBridge visitorsBridge) {
@@ -101,8 +121,13 @@ private static String contstructFileName(String... path) {
101121
return result.substring(0, result.length() - 1);
102122
}
103123

124+
@Nullable
104125
private static CompilationUnitTree parse(String code) {
105-
return (CompilationUnitTree) JavaParser.createParser(Charsets.UTF_8).parse(code);
126+
try {
127+
return (CompilationUnitTree) JavaParser.createParser(Charsets.UTF_8).parse(code);
128+
} catch (RecognitionException e) {
129+
return null;
130+
}
106131
}
107132

108133
}

0 commit comments

Comments
 (0)