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

Static field access sometimes get target as CtThisAccess instead of CtTypeAccess (noclasspath) #3329

Closed
slarse opened this issue Apr 16, 2020 · 3 comments · Fixed by #3331
Closed

Comments

@slarse
Copy link
Collaborator

slarse commented Apr 16, 2020

Hello!

I've encountered some bugs with statically accessed variables. This was the most easily reproducible one, I'll try to reproduce the other ones later.

Anyway, my problem is that sometimes when a static field is accessed from a class for which the source is not available, the target of the field read is a CtThisAccess instead of a CtTypeAccess. This results in some pretty gnarly problems for me. I found this old issue discussing something similar: #346

Here's an example:

import spoon.Launcher;

public class Main {
    public static void main(String[] args) {
        Launcher.logger.error("Hello :)");
    }
}

Here, I expect the target of the logger field read to be a type access to the Launcher type. But it's a CtThisAccess. So if I just parse this and immediately print it, this is what I get.

import spoon.Launcher;

public class Main {
    public static void main(String[] args) {
        logger.error("Hello :)");
    }
}

A this access in a static context doesn't make sense to begin with, and what makes even less sense is that the target of the CtThisAccess is the Launcher type. So it's a strange cross-type this access in a static context. The problem persists in a non-static methods as well.

If I add a Launcher class in the same file, as shown below, then it correctly resolves a type access instead of a this access.

public class Main {

    private static class Launcher {
        static Logger logger;
    }

    public static void main(String[] args) {
        Launcher.logger.error("Hello :)");
    }
}

I'll have a PR with this test case up in a few minutes.

@slarse slarse changed the title Statically accessed variables sometimes get target as CtThisAccess instead of CtTypeAccess (noclasspath) Static field access sometimes get target as CtThisAccess instead of CtTypeAccess (noclasspath) Apr 16, 2020
Egor18 added a commit to Egor18/spoon that referenced this issue Apr 16, 2020
Egor18 added a commit to Egor18/spoon that referenced this issue Apr 16, 2020
@Egor18
Copy link
Contributor

Egor18 commented Apr 16, 2020

After some research, I figured out that despite being in noclasspath mode spoon still loads spoon.Launcher class from classloader:

try {
Class zeClass = this.getClass().getClassLoader().loadClass(className);
klass = this.factory.Type().get(zeClass);
return klass;
} catch (NoClassDefFoundError | ClassNotFoundException e) {

The thing is there is no logger field in spoon.Launcher, there is LOGGER, so this drives it a bit crazy.
You can try renaming it to LOGGER or replacing import spoon.Launcher with import abc.Launcher or something to see it yourself.
Anyway, I provided a PR with a possible fix for this corner case.

@monperrus
Copy link
Collaborator

Thanks a lot to @slarse for the detailed bug report and to @Egor18 for the super fast analysis & fix!

@slarse
Copy link
Collaborator Author

slarse commented Apr 18, 2020

Great, thanks @Egor18 @monperrus

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

Successfully merging a pull request may close this issue.

3 participants