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

How can I get the Java Class value for a generated param? #100

Open
djett41 opened this issue May 16, 2021 · 4 comments
Open

How can I get the Java Class value for a generated param? #100

djett41 opened this issue May 16, 2021 · 4 comments
Assignees

Comments

@djett41
Copy link

djett41 commented May 16, 2021

I am trying to figure out how to get the Java Class of a generated param. I have tried using getting the AbstractJType type() from the param and playing around with the API but cant seem to figure out how to get the Class value.

For example given the following params I would like to extract the type. I am also trying to determine if a param is a List or not, but I cant seem to figure that out either. I ave tried isArray on the type, as well a using a ref to a List.class and checking isAssignable but I think its checking the actual jcode type. I am trying to refrain from checking if the param.type().name().startsWith("List") or use regex to extract the type, but at this point thats all I can come up with

List - List
CustomObject - CustomObject
is List of type list? - true
is CustomObject of type list? - false

Thanks!

@glelouet
Copy link
Contributor

could you provide a minimal example of code ?

@glelouet
Copy link
Contributor

glelouet commented May 17, 2021

Hmm this does not work

  {
  	JMethod method = null;
        JVar firstParam = method.paramAtIndex(0);
  	if(firstParam.type() instanceof JReferencedClass ) {
  		var typeclass = ((JReferencedClass) firstParam.type()).getReferencedClass();
  	}
  }

I guess we should add the corresponding Class<?> JReferencedClass::getReferencedClass()
@phax ?

@phax phax self-assigned this May 17, 2021
@phax phax added the question label May 17, 2021
@glelouet
Copy link
Contributor

I think the method should be added in the abstractJType :

boolean AbstractJType::isInstanceOf(Class <?> clazz) {return owner().ref(clazz).isAssignableFrom(this);} . This would allow to call if(firstParam.type().isInstanceOf(List.class)){…}

@stale
Copy link

stale bot commented Aug 21, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Aug 21, 2021
@phax phax added pinned and removed wontfix labels Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants