coekie / gentyref Goto Github PK
View Code? Open in Web Editor NEWGeneric type reflection library for Java
License: Apache License 2.0
Generic type reflection library for Java
License: Apache License 2.0
Context
Assume we have an outer class or interface A<X>
with type parameters, and a static class B
nested in there, which implements this or another generic class/interface C<Y>
with a concrete type parameter (e.g. B implements C<String>
).
class A<X> {
static class B implements C<String> {
}
}
interface C<Y> {}
Wrong behavior
Then GenericTypeReflector.getExactSuperType(B.class, C.class)
returns the raw type C
Expected behavior
GenericTypeReflector.getExactSuperType(B.class, C.class)
should return the parameterized Type C<String>
.
Analysis
In GenericTypeReflector.isMissingTypeParameters
(which tries to find out whether we have a (partially) raw type at hand), the loop goes through enclosing classes even if the class itself is static, and thus the type parameters of the enclosing class have no effect on the nested class. It should stop for static nested classes.
Workaround
Put your static nested class into an outer class without type parameters, or make it a top-level class itself.
Hi,
I encountered the following problem:
Given
public class Parent <A> {
public <T extends Annotation> T getAnnotation(Class<T> clazz) {
return null
}
}
public class Child extends Parent<Child> {
}
this call will fail
Method m = Child.getMethod("getAnnotation", Class.class);
GenericTypeReflector.getExactReturnType(m, Child.class);
UnresolvedTypeVariableException: An exact type is requested, but the type contains a type variable that cannot be resolved.
Variable: T from public java.lang.annotation.Annotation Parent.getAnnotation(java.lang.Class)
Hint: This is usually caused by trying to get an exact type when a generic method who's type parameters are not given is involved.
The same call does not fail if the containing type is not parameterized.
Method m = Child.getMethod("getAnnotation", Class.class);
GenericTypeReflector.getExactReturnType(m, Class.class);
Furthermore, would it be possible to add a resolveReturnType(Method m, Object[] args)
that could return the actual type for such signatures <T extends Annotation> T getAnnotation(Class<T> clazz)
when the arguments are given?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.