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
Add API for resolving return type, field type, parameter type with replacement of the actual type arguments #735
Comments
This API already exists in ClassGraph but it is probable that it doesn't work in some cases, because I never looked closely at how type parameters are resolved by the Java compiler. Off the top of my head (I can't write some test code right now), just get a type signature for the generic class in question, then get the type arguments from the type signature, the call |
I'm afraid it looks like ClassGraph does not expose reflection API. Just in case, I tried "scanning" the class by its name, and I could not find APIs to resolve that |
I tried this out --
gives
while
gives
Please confirm if the latter form already gives you what you want. |
Well, do you expect that everybody reimplements type parameter substitution rules? I know What I ask is a higher-level API: "give me the actual type with all type parameters substituted with the corresponding actual type arguments". |
Just in case, I'm not like "implement the feature for me" (the feature can be borrowed from mybatis or spring-framework). |
Did you not see the second code example, which does exactly what you are asking for? There's one way to get the type signature that uses type variables, and there's another way to get the type signature that includes the full concrete type. Take another look at what I sent:
gives
However, like I said earlier, there is already support for doing the substitution you are asking for: To use this though, you need a As I said, the support for resolving type variables is probably incomplete. So what would be needed to ensure you could get the concrete type of the interface signature using either of the two APIs that I showed you, you would need to copy this logic from Code contributions are welcome. Since there is already a way to get exactly the answer you need, adding a second mechanism to do the same thing is not a high priority, and I don't have a lot of time to work on this. |
Would you please clarify what you mean by "second code example"? |
I literally quoted the code again that I was talking about. And then I showed again how it returns the actual concrete type you are looking for. |
I'm afraid you quoted |
Ah! I couldn't understand why we weren't understanding each other. I'm not at a computer, but getting the type signature of the method is the place to start. |
prints
|
Could you share the full code for public interface Base<T> {
T getT();
}
public abstract class Derived implements Base<String> {
} Test code: @Test
fun abc() {
ClassGraph().enableAllInfo().acceptClasses(Derived::class.java.name).scan().use { scanResult ->
val klass = scanResult.getClassInfo(Derived::class.java.name)
val methodInfo = klass.methodInfo.first()
println("methodInfo: ${methodInfo.typeSignatureOrTypeDescriptor.resultType}")
}
} The output is |
I used exactly the code you first gave, except that I had to define the I think you're using the Kotlin compiler? That has had bugs in code generation before that have affected ClassGraph scanning. Can you please put together a complete test project so that I can reproduce the problem? |
I am inclined that implementing Please try with the abstract interface Base<T> {
T get();
}
class Derived1 implements Base<String> {
public String get() {
return null;
}
}
abstract class Derived2 implements Base<String> {
} If I compile it with Java bytecode 1.8, then I get the following:
|
OK, the reason why ClassGraph returns |
That is a very different question from what I asked in the very first comment of the issue. What I want is an API that would resolve method return type taking into account all the generics. |
I agree that a I have extremely limited time to work on ClassGraph at this point, so I would appreciate it if you could please take a shot at implementing this, since I don't know when I could afford the time to work on it. You are the first person to request this feature. |
Here's a sample case:
I want a runtime API to resolve the type of
getT
inDerived
class asString
.In other words, something like
TypeParameterResolver#resolveReturnType
from mybatis https://github.com/mybatis/mybatis-3/blob/4b72ae4ffbf1029162233d3eb77c2f0b891c24fa/src/main/java/org/apache/ibatis/reflection/TypeParameterResolver.java#L50-L59, however, mybatis does not considerTypeParameterResolver
its API.WDYT?
The text was updated successfully, but these errors were encountered: