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

Parsing class with name containing $ and with a nested classe fails #1889

Closed
nharrand opened this issue Mar 2, 2018 · 2 comments
Closed

Comments

@nharrand
Copy link
Collaborator

nharrand commented Mar 2, 2018

Launcher launcher = new Launcher();
launcher.addInputResource("./$Poor$Name.java");
launcher.buildModel();
CtModel model = launcher.getModel();
System.out.println(model.getRootPackage().getType("$Poor$Name"));

with file $Poor$Name.java containing:

public class $Poor$Name {
    public Choice lookingForTroubles() {
        return new Choice();
    }
    class Choice {
        public Choice() {}
    }
}

produces the following exceptions:

Exception in thread "main" spoon.support.SpoonClassNotFoundException: cannot load class: $Poor$Name
	at spoon.support.reflect.reference.CtTypeReferenceImpl.findClass(CtTypeReferenceImpl.java:164)
	at spoon.support.reflect.reference.CtTypeReferenceImpl.getActualClass(CtTypeReferenceImpl.java:146)
	at spoon.support.reflect.reference.CtTypeReferenceImpl.getModifiers(CtTypeReferenceImpl.java:491)
	at spoon.support.reflect.reference.CtTypeReferenceImpl.canAccess(CtTypeReferenceImpl.java:621)
	at spoon.support.reflect.reference.CtTypeReferenceImpl.getAccessType(CtTypeReferenceImpl.java:691)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.visitCtTypeReference(DefaultJavaPrettyPrinter.java:1824)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.visitCtTypeReference(DefaultJavaPrettyPrinter.java:1794)
	at spoon.support.reflect.reference.CtTypeReferenceImpl.accept(CtTypeReferenceImpl.java:83)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.scan(DefaultJavaPrettyPrinter.java:345)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.visitCtMethod(DefaultJavaPrettyPrinter.java:1369)
	at spoon.support.reflect.declaration.CtMethodImpl.accept(CtMethodImpl.java:73)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.scan(DefaultJavaPrettyPrinter.java:345)
	at spoon.reflect.visitor.ElementPrinterHelper.writeElementList(ElementPrinterHelper.java:196)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.visitCtClass(DefaultJavaPrettyPrinter.java:622)
	at spoon.support.reflect.declaration.CtClassImpl.accept(CtClassImpl.java:68)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.scan(DefaultJavaPrettyPrinter.java:345)
	at spoon.support.reflect.declaration.CtElementImpl.toString(CtElementImpl.java:283)
	at java.lang.String.valueOf(String.java:2994)
	at java.io.PrintStream.println(PrintStream.java:821)
	at se.kth.App.main(App.java:25)
Caused by: java.lang.ClassNotFoundException: $Poor$Name
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at spoon.support.reflect.reference.CtTypeReferenceImpl.findClass(CtTypeReferenceImpl.java:162)
	... 19 more

(This issue is the minimalistic version of what I encountered working on https://github.com/google/gson/blob/master/gson/src/main/java/com/google/gson/internal/%24Gson%24Types.java )

I'm working on a fix, but if anyone has any ideas, I'm all ears!

@nharrand nharrand changed the title Parsing class containing $ with a nested classe fails Parsing class with name containing $ with a nested classe fails Mar 2, 2018
@nharrand nharrand changed the title Parsing class with name containing $ with a nested classe fails Parsing class with name containing $ and with a nested classe fails Mar 2, 2018
@nharrand
Copy link
Collaborator Author

nharrand commented Mar 2, 2018

Ok, I found an ugly fix.

The current behavior when trying to resolve a referenced type is to assume that $ is only used for nested (and anonymous) types. So when trying to resolve the class $Poor$Name$Choice it try to resolve $Poor which does not exist.

What we can do is to change TypeFactory.get(final String qualifiedName) to check if the type is already known at the beginning of the method instead of just doing it at the end. It is more a workaround than a fix though...

I will submit a pull request.

@monperrus
Copy link
Collaborator

closed per #1891

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

No branches or pull requests

2 participants