-
-
Notifications
You must be signed in to change notification settings - Fork 352
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
feat: read parameter names from bytecode if possible #4972
feat: read parameter names from bytecode if possible #4972
Conversation
Hi, |
I'm not sure how a test for this would look like. We either need .class files as resources (which is bad because we can't properly read them here) or compile a java file and read it then. For that, a proper test infrastructure that can be reused would be needed, otherwise it'll get ugly really quick. |
I could potentially add a mvn sub-module to the project, which compiles a test class with the appropriate compiler settings for the sake of testing.
I feels like quite a bit of overhead for this relatively small and straightforward addition.
Please let me know how to proceed.
Best regards
Daniel
Am 26.10.2022 um 15:22 schrieb Hannes Greule ***@***.***>:
This email originated from outside of CGM. Please do not click links or open attachments unless you know the sender and know the content is safe.
I'm not sure how a test for this would look like. We either need .class files as resources (which is bad because we can't properly read them here) or compile a java file and read it then. For that, a proper test infrastructure that can be reused would be needed, otherwise it'll get ugly really quick.
Edit[MartinWitt]: I removed the auth token from the post.
|
I agree, that sounds a bit too heavy handed. What about an API like @Test
void parameterNamesAreParsedWhenCompilingWithParametersFlag() throws ClassNotFoundException {
ClassLoader loader = JavacFacade.compileFiles(
Map.of(
"Test.java",
"class Test {\n"
+ " public void foo(String bar) {}\n" +
"}\n"
),
List.of("-parameters")
);
CtType<?> test = new JavaReflectionTreeBuilder(createFactory()).scan(loader.loadClass("Test"));
CtMethod<?> method = test.getMethodsByName("foo").get(0);
CtParameter<?> parameter = method.getParameters().get(0);
assertThat(parameter.getSimpleName(), is("bar"));
} Depending on what @MartinWitt and @SirYwell think, I could PR such a facade and then you can use this test (or one you come up with)? Alternatively we could leverage JDT to compile this for us with the parameter flag instead of javac. I'd assume javac is a bit more common though, but this shouldn't really matter. EDIT: It could look something like this |
@danielbobbert The facade was merged in master. If you rebase or merge this PR onto master, you can adapt the test case it brought with it and/or write an additional one :) |
Hey @danielbobbert, is there anything we can help you with to finish this PR? :) |
4b14eee
to
80cd1c0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, will merge.
This feature allows Spoon to read the parameter name from Java 8 and later byte code that has been built with the "-parameters" flag such that CtParameter objects representing code from the class path contain the "real" parameter name instead of "argX".