INRIA / INRIA/spoon

[Bug]: Implicit class from JEP 512 compact source files not registered in model (allTypes, rootPackage, declaredTypes all empty)

Open
#6,709 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
2k
Forks
392
Avg merge
11h 24m
Merged PRs (30d)
36

Description

### Describe the bug

When parsing a JEP 512 compact source file (no class declaration, methods at top level) at
compliance level 25, Spoon creates the implicit CtClass wrapper internally but never registers
it in any of the model's lookup structures.

model.getAllTypes(), rootPackage.getPackage("test").getTypes(), and
compilationUnit.getDeclaredTypes() all return empty for the compact source file. The implicit
class is effectively invisible to any consumer iterating the model.

### Source code you are trying to analyze/transform

```Java
package test;

void main() {
System.out.println("hello from compact source");
}

void helper(int x) {
System.out.println(x);
}

int add(int a, int b) {
return a + b;
}
```

### Source code for your Spoon processing

```Java
import spoon.Launcher;
import spoon.reflect.CtModel;
import spoon.reflect.declaration.CtCompilationUnit;

Launcher launcher = new Launcher();
launcher.addInputResource("src/test/resources/CompactSource.java");
launcher.getEnvironment().setComplianceLevel(25);
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();

CtModel model = launcher.getModel();

// Expected: [CompactSource(implicit=true)]
// Actual: []
System.out.println("getAllTypes(): " + model.getAllTypes().stream()
.map(t -> t.getSimpleName() + "(implicit=" + t.isImplicit() + ")")
.toList());

// Expected: [CompactSource(implicit=true)]
// Actual: []
System.out.println("package 'test' types: " +
model.getRootPackage().getPackage("test").getTypes().stream()
.map(t -> t.getSimpleName() + "(implicit=" + t.isImplicit() + ")")
.toList());

// Expected: CompactSource.java -> declaredTypes=[CompactSource(implicit=true)]
// Actual: CompactSource.java -> declaredTypes=[]
for (CtCompilationUnit cu : launcher.getFactory().CompilationUnit().getMap().values()) {
System.out.println(cu.getFile().getName() + " -> declaredTypes=" +
cu.getDeclaredTypes().stream()
.map(t -> t.getSimpleName() + "(implicit=" + t.isImplicit() + ")")
.toList());
}
```

### Actual output

```Java
getAllTypes(): []
package 'test' types: []
CompactSource.java -> declaredTypes=[]
```

### Expected output

```Java
getAllTypes(): [CompactSource(implicit=true)]
package 'test' types: [CompactSource(implicit=true)]
CompactSource.java -> declaredTypes=[CompactSource(implicit=true)]
```

### Spoon Version

11.3.1-beta-8

### JVM Version

JVM Version: 21.0.6

### What operating system are you using?

Alpine Linux 3.19.9

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.