Skip to content
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

[Bug]: When you import a static inner class, spoon converts it to a non-static import #5528

Open
honghao12 opened this issue Nov 2, 2023 · 4 comments
Labels

Comments

@honghao12
Copy link

honghao12 commented Nov 2, 2023

Describe the bug

When you import a static inner class, spoon converts it to a non-static import

Source code you are trying to analyze/transform

import static StaticTest3.StaticInnerTest1;

public class ImportsTest {
    void funT1(){      
        int i = StaticInnerTest1.destInnerTest1_static_var1;
    }
}
-----------------------------------------------------------
public class StaticTest3 {
    public static class StaticInnerTest1 {
        public static int destInnerTest1_static_var1 = 0;
    }
}

Source code for your Spoon processing

Launcher launcher = new Launcher();
        launcher.getEnvironment().setComplianceLevel(11);
        launcher.getEnvironment().setAutoImports(true);
        launcher.getEnvironment().setIgnoreDuplicateDeclarations(true);
        launcher.addInputResource("");
        model = launcher.buildModel();

        Iterator<CtType<?>> iterator = model.getAllTypes().iterator();
        while(iterator.hasNext()){
            CtType<?> type = iterator.next();
            for (CtImport anImport : type.getPosition().getCompilationUnit().getImports()) {
                if("import StaticTest3.StaticInnerTest1;".equals(anImport.toString())){
                    System.out.println("exists error");
                }
            }
        }

Actual output

import StaticTest3.StaticInnerTest1;

Expected output

import static StaticTest3.StaticInnerTest1;

Spoon Version

10.4.3-beta-2

JVM Version

11

What operating system are you using?

windows

@honghao12 honghao12 added the bug label Nov 2, 2023
@algomaster99
Copy link
Contributor

algomaster99 commented Nov 3, 2023

Hi @honghao12 ! I tried reproducing it, but anImport.toString does evaluate to import static StaticTest3.StaticInnerTest1. This functionality was last touched in #5213 and it was also released in 10.4.0 so it should work on any release after that. However, if it is still not working for you, could you please reproduce the output as a pull request (ideally as a new testcase)?

@xzel23
Copy link
Contributor

xzel23 commented Nov 4, 2023

In what version of Java is import static used to import classes as opposed to fields and methods? Does that even compile with javac? import static StaticTest3.StaticInnerTest1 looks like it should be a compile time error anyway. import StaticTest3.StaticInnerTest1 looks correct.

@SirYwell
Copy link
Collaborator

SirYwell commented Nov 4, 2023

The relevant part of the JLS regarding that is § 7.5.3. It mainly mentions static members, which includes static nested classes. This becomes clear from the following sentence (emphasis mine):

It is permissible for one single-static-import declaration to import several fields, classes, or interfaces with the same name, or several methods with the same name and signature.

I don't know how well we have test coverage for that, but we should make sure that we get imports right for both static nested classes and non-static nested classes.

@xzel23
Copy link
Contributor

xzel23 commented Nov 5, 2023

Today I learned. From what I understand, for a static inner class it makes no real difference anyway, and some coding standards (like Google's) explicitly forbid using a single static import for static one classes ("use normal import instead").

Of course SPOON should handle this correctly. But the result (the missing "static" keyword) should have no negative effect on the resulting code from a practical point of view in (nearly?) all cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants