Java: Add compact source file tests

This commit is contained in:
idrissrio
2025-07-23 15:00:01 +02:00
parent 81b1e73e18
commit 9363bc318a
10 changed files with 96 additions and 0 deletions

View File

@@ -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' |

View File

@@ -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() + "'"

View File

@@ -0,0 +1 @@
| Test.java:0:0:0:0 | Test |

View File

@@ -0,0 +1,5 @@
import java
from CompilationUnit cu
where cu.isCompactSourceFile()
select cu

View File

@@ -0,0 +1,2 @@
| Test.java:1:1:1:1 | Test | implicit |
| Test.java:25:7:25:16 | NotCompact | not implicit |

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1 @@
semmle/code/java/PrintAst.ql

View File

@@ -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
}

View File

@@ -0,0 +1 @@
//semmle-extractor-options: --javac-args --release 25 --enable-preview