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

Preserve all method signature info #80

Open
lukehutch opened this issue Dec 23, 2024 · 2 comments
Open

Preserve all method signature info #80

lukehutch opened this issue Dec 23, 2024 · 2 comments

Comments

@lukehutch
Copy link

Currently it is not possible to print out the prototype for a method, including parameter names. There is no return type recorded in therapi-runtime-javadoc's data structures, and no function parameter types.

I would like to have a simple toString() method for a method's type signature, including return type, method name, parameter names and types, shown the same was as the method is declared in the code.

@dnault
Copy link
Owner

dnault commented Dec 24, 2024

Hi Luke!

It's possible to reconstruct a method declaration using the standard reflection API. Here's a document from Oracle that covers how to get the required information using reflection: https://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html

For your use case, perhaps you could start by using reflection to get a java.lang.reflect.Method. From there you could get the return type, method name, parameter types, and parameter names. Then if you want the Javadoc as well, pass the Method to RuntimeJavadoc.getMethod(Method).

(Strictly speaking, a Java method's signature consists of only the method name and the types of its parameters. This information is present in the JSON written by the scribe, and used by the runtime component to find the Javadoc associated with a given Method.)

@lukehutch
Copy link
Author

Yes, that's exactly what I am doing to manually print out the method prototype. But it's a lot of boilerplate, and your library would be more complete if it had support for this. Also, as you point out the parameter names are not by default compiled into the classfile, and since the javadoc @param order doesn't have to match thw actual para order, it's not possible to align the parameters by name, in order to figure out the type of each para (unless the classfile was compiled with the -parameters switch, which is off by default).

However an annotation processor does have access to the method param names, I believe. This is an opportunity to provide some info to your users that is not typically available at runtime (without messing with compiler switches) -- and to reduce boilerplate.

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