Skip to content

Java: Add support to Compact Source Files #20116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Java: Add compact source file tests
  • Loading branch information
IdrissRio committed Jul 29, 2025
commit ab2cf131185bf0cdd86ff67ce7a29e9fe1f5fc50
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| Test.java:0:0:0:0 | Test | Test.java:1:1:1:1 | Test | Compact source file 'Test' contains implicit class 'Test' |
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import java

from CompilationUnit cu, Class c
where
cu.isCompactSourceFile() and
c.getCompilationUnit() = cu and
c.isImplicit()
select cu, c,
"Compact source file '" + cu.getName() + "' contains implicit class '" + c.getName() + "'"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| Test.java:0:0:0:0 | Test |
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java

from CompilationUnit cu
where cu.isCompactSourceFile()
select cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| Test.java:1:1:1:1 | Test | implicit |
| Test.java:25:7:25:16 | NotCompact | not implicit |
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java

from Class c, string res
where
exists(c.getCompilationUnit().getRelativePath()) and
if c.isImplicit() then res = "implicit" else res = "not implicit"
select c, res
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Test.java:
# 0| [CompilationUnit] Test
# 1| 1: [Class] Test
# 1| 4: [FieldDeclaration] int instanceField;
# 1| -1: [TypeAccess] int
# 1| 0: [IntegerLiteral] 10
# 2| 5: [FieldDeclaration] int STATIC_CONSTANT;
# 2| -1: [TypeAccess] int
# 2| 0: [IntegerLiteral] 42
# 3| 6: [FieldDeclaration] String privateField;
# 3| -1: [TypeAccess] String
# 3| 0: [StringLiteral] "data"
# 5| 7: [Method] main
# 5| 3: [TypeAccess] void
# 5| 5: [BlockStmt] { ... }
# 6| 0: [ExprStmt] <Expr>;
# 6| 0: [MethodCall] processData(...)
# 7| 1: [ExprStmt] <Expr>;
# 7| 0: [MethodCall] testStaticAccess(...)
# 11| 8: [Method] processData
# 11| 3: [TypeAccess] void
# 11| 5: [BlockStmt] { ... }
# 12| 0: [ExprStmt] <Expr>;
# 12| 0: [PostIncExpr] ...++
# 12| 0: [VarAccess] instanceField
# 13| 1: [ExprStmt] <Expr>;
# 13| 0: [MethodCall] updatePrivateField(...)
# 16| 9: [Method] updatePrivateField
# 16| 3: [TypeAccess] void
# 16| 5: [BlockStmt] { ... }
# 17| 0: [ExprStmt] <Expr>;
# 17| 0: [AssignExpr] ...=...
# 17| 0: [VarAccess] privateField
# 17| 1: [StringLiteral] "updated"
# 21| 10: [Method] testStaticAccess
# 21| 3: [TypeAccess] void
# 21| 5: [BlockStmt] { ... }
# 22| 0: [ExprStmt] <Expr>;
# 22| 0: [MethodCall] println(...)
# 22| -1: [TypeAccess] IO
# 22| 0: [StringLiteral] "Static access test"
# 25| 11: [Class] NotCompact
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
semmle/code/java/PrintAst.ql
27 changes: 27 additions & 0 deletions java/ql/test/library-tests/compact-source-files/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
int instanceField = 10;
static final int STATIC_CONSTANT = 42;
private String privateField = "data";

void main() {
processData();
testStaticAccess();
}

// Test instance methods
void processData() {
instanceField++;
updatePrivateField();
}

private void updatePrivateField() {
privateField = "updated";
}

// Test static method access
static void testStaticAccess() {
IO.println("Static access test");
}

class NotCompact {
//Test explict class
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a spelling error in the comment: 'explict' should be 'explicit'.

Suggested change
//Test explict class
//Test explicit class

Copilot uses AI. Check for mistakes.

}
1 change: 1 addition & 0 deletions java/ql/test/library-tests/compact-source-files/options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//semmle-extractor-options: --javac-args --release 25 --enable-preview
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy