File tree Expand file tree Collapse file tree
java-checks-test-sources/default/src/main/java/checks
main/java/org/sonar/java/checks
test/java/org/sonar/java/checks Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ void main () {
2+ System .out .println ("Hello from compact source!" );
3+ }
4+
5+ int value = 1 ;
6+
7+ void helper () {}
Original file line number Diff line number Diff line change 2020import org .sonar .plugins .java .api .JavaFileScanner ;
2121import org .sonar .plugins .java .api .JavaFileScannerContext ;
2222import org .sonar .plugins .java .api .tree .CompilationUnitTree ;
23+ import org .sonar .plugins .java .api .tree .Tree ;
24+
25+ import java .util .List ;
2326
2427@ Rule (key = "S1220" )
2528public class DefaultPackageCheck implements JavaFileScanner {
@@ -28,10 +31,14 @@ public class DefaultPackageCheck implements JavaFileScanner {
2831 public void scanFile (JavaFileScannerContext context ) {
2932 if (context .fileParsed ()) {
3033 CompilationUnitTree cut = context .getTree ();
31- if (cut .moduleDeclaration () == null && cut .packageDeclaration () == null ) {
34+ if (cut .moduleDeclaration () == null && cut .packageDeclaration () == null && ! isCompactSource ( cut ) ) {
3235 context .addIssueOnFile (this , "Move this file to a named package." );
3336 }
3437 }
3538 }
3639
40+ private static boolean isCompactSource (CompilationUnitTree cu ) {
41+ List <Tree > types = cu .types ();
42+ return types .size () == 1 && types .get (0 ).is (Tree .Kind .IMPLICIT_CLASS );
43+ }
3744}
Original file line number Diff line number Diff line change @@ -47,4 +47,11 @@ void with_module() {
4747 .verifyNoIssues ();
4848 }
4949
50+ @ Test
51+ void compact_source () {
52+ CheckVerifier .newVerifier ()
53+ .onFile (mainCodeSourcesPath ("checks/DefaultPackageCheckCompactSample.java" ))
54+ .withCheck (new DefaultPackageCheck ())
55+ .verifyNoIssues ();
56+ }
5057}
You can’t perform that action at this time.
0 commit comments