Skip to content

[Bug]: Confusion between simple access to class fields (or enum) and fully qualified name #5151

Closed

Description

@salmonb

Describe the bug

Having imported an external class C (source not provided) with import c.C;, then when parsing expressions like C.XXX, the parser should understand that C is referring to the class C, and not to a fully qualified class XXX in package C.

For example, the 2 following classes A and B should have the same result, but the parser is making that confusion with B.

Source code you are trying to analyze/transform

// First file A.java
package a;

import c.C; // external source not provided

public class A {

    public static void a() {
        C c = C.XXX; // C.XXX may be an enum, or a field
        System.out.println(c);
    }

}

// Second file B.java
package b;

import c.C;

public class B {

    public static void b() {
        System.out.println(C.XXX);
    }

}

Source code for your Spoon processing

Launcher launcher = new Launcher();
launcher.addInputResource(inputSource);
CtModel model = launcher.buildModel();

// Find all the packages used in the code
for (CtElement ctClass : model.getElements(e -> e instanceof CtClass || e instanceof CtInterface)) {
    CtType<?> ctType = (CtType<?>) ctClass;
    System.out.println(ctType.getQualifiedName() + " is using:");
    for (CtTypeReference<?> type : ctType.getUsedTypes(true)) {
        System.out.println(" - " + type.getQualifiedName() + " (package " + type.getPackage() + ")");
    }
}

Actual output

a.A is using:
 - a.A (package a)
 - java.io.PrintStream (package java.io)
 - c.C (package c)
b.B is using:
 - C.XXX (package C)
 - java.io.PrintStream (package java.io)

Expected output

a.A is using:
 - a.A (package a)
 - java.io.PrintStream (package java.io)
 - c.C (package c)
b.B is using:
 - c.C (package c)
 - java.io.PrintStream (package java.io)

Spoon Version

10.30.0

JVM Version

11

What operating system are you using?

macOS 11.7.4 (BigSur) - IntelliJ IDEA 2023.1

Activity

  1. added a commit that references this issue on Apr 10, 2023
    46928a1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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