Skip to content

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

@faisal-990

Description

@faisal-990

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

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

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

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

Expected output

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions