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

Retrieve inner class reference #60

Closed
fbaro opened this issue Apr 19, 2018 · 8 comments
Closed

Retrieve inner class reference #60

fbaro opened this issue Apr 19, 2018 · 8 comments
Assignees
Labels

Comments

@fbaro
Copy link
Contributor

fbaro commented Apr 19, 2018

I was looking for a way to get a reference to an inner class from another class, but I was not able to find it. I was looking for something like codeModel.ref("x.y.z.AClass").getInner("Inner"), which would give a reference to x.y.z.AClass$Inner.
Would it be possible to add it? Or it's already there and I did not find it?

@phax phax self-assigned this Apr 19, 2018
@phax phax added the question label Apr 19, 2018
@phax
Copy link
Owner

phax commented Apr 19, 2018

With codeModel.ref("x.y.z.AClass").classes() you could iterate the inner classes, but not very sexy. An improved API would be better I guess....

@fbaro
Copy link
Contributor Author

fbaro commented Apr 20, 2018

Yep I knew the classes() method, and I'm actually using it. Unfortunately it's available only on AbstractJClassContainer, while codeModel.ref() returns and AbstractJClass. I'm doing a cast, but depending on what codeModel.ref() actually returns, it might not work.

phax added a commit that referenced this issue Apr 23, 2018
@phax
Copy link
Owner

phax commented Apr 23, 2018

So I added a getInner(String) method to AbstractJClass and initial tests worked fine. Can you test the SNAPSHOT version or do you need the release?

@fbaro
Copy link
Contributor Author

fbaro commented Apr 23, 2018

I'll check the snapshot, thank you very much!

@fbaro
Copy link
Contributor Author

fbaro commented Apr 23, 2018

I checked it out: it works for my specific use case, but it breaks on something more general like this:

AbstractJClass c1 = codeModel.ref(java.util.Map.Entry.class);
AbstractJClass c2 = codeModel.ref(java.util.Map.class).getInnerClass("Entry");
assertEquals(c1.fullName(), c2.fullName());
assertEquals(c1, c2); // Maybe this too?

By the way I already have a workaround in place, so no hurry on my side.

@phax
Copy link
Owner

phax commented Apr 23, 2018

This is an issue because inner classes are not automatically referenced when you reference codeModel.ref(java.util.Map.class). Additionally not even super or outer classes are automatically added into the reference space of code model. So codeModel.ref(java.util.Map.class).getInnerClass("Entry") works as expected by delivering null!

@fbaro
Copy link
Contributor Author

fbaro commented Apr 24, 2018

I'm a bit unconvinced on this... after all, we are basically just trying to construct a sort of type-safe placeholder for the "java.util.Map.Entry" string, which will then be printed in the generated file. It appears to me that there should not be any substantial difference between the two ways of constructing such a string, or even with codeModel.ref("java.util.Map.Entry"). I thought the result should be the same, even in the case that the referenced classes are not visible in the classloader.

@phax
Copy link
Owner

phax commented Oct 20, 2018

Inner classes are a construct of the Java source files, but when compiled, they reside as 2 independent files on disk and only a "similiar" name links them together. They are - when compiled - indeed 2 separate independent classes.

@phax phax closed this as completed Oct 20, 2018
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

2 participants