Comments (4)
pretty ping?
from geantyref.
Hey! I'll have a look at this today.
At first glance, I think this might be expected, because if the enclosing type is undecidable, so is the inner type (the inner type is the anonymous subclass in this case). But a partial resolution might be doable... I'll have to look deeper.
Btw, GeantyRef has TypeToken
for capturing generic types, you may find it useful.
from geantyref.
Ok, seems I was right. The issue is in the way TypeCapture
is implemented. The short of it is that you don't actually have to call GenericTypeReflector.getTypeParameter
in TypeCapture
, because the generic type you're after is already fully captured in its direct super type, so it's enough to just do this:
protected TypeCapture() {
ParameterizedType superType = (ParameterizedType) getClass().getGenericSuperclass();
this.type = superType.getActualTypeArguments()[0];
}
With that, the test will pass.
EDIT: I now see you've already discovered this 😄
The long of it is that GenericTypeReflector.getTypeParameter
tries to resolve the exact super type at the level where the variable is declared, e.g. if you have a type such as:
class StringList extends ArrayList<String> {}
and call GenericTypeReflector.getTypeParameter(StringList.class, List.class.getTypeParameters()[0])
, it first has to resolve StringList
as a List
(because it is the List
type that declares the variable), and that resolves to List<String>
.
In your case, the anonymous inner class is a subtype of Foo<T>.TypeCapture<List<String>>
, and because T
is unresolvable, the whole super type isn't resolvable exactly.
I think I could implement partial super type resolution, e.g. getSuperType
, as a complement to the existing getExactSuperType
(similar to the getParameterTypes
/getExactParameterTypes
pair), and then add an overload of getTypeParameter
based on that impl, if this is something you need? But as it stands, what you observed is the expected behavior.
from geantyref.
Thank you for the explanation. We don't specifically need a method to handle this use case, it was just surprising to get such a non-specific result. If you think it would be useful to have in the API, we certainly would love to use it, but are happy enough with the code as it is.
We ship our own TypeToken type in Jdbi because we want to allow our users to pass in type tokens without exposing geantyref as part of our public API.
from geantyref.
Related Issues (18)
- Add support for Java 8 AnnotatedType HOT 1
- No module name described in MANIFEST HOT 4
- Unexpected exception when erasing method return type from a wildcard generic class HOT 2
- GenericTypeReflector.transform visitor visitVariable cannot change type away from variable type HOT 3
- TypeVisitor has protected method that refers to package-private type
- Add a way to resolve bounded types directly HOT 2
- Calling hashCode on two equal `TypeToken`s returns different results HOT 2
- getExactParameterTypes() for constructors HOT 1
- reduceBounded StackOverflowError with self-recursing capture HOT 12
- compiling with JDK 11 leads to test failures
- ParameterizedTypeImpl `toString()` method returns wrong method name for nested classes HOT 1
- Current trunk does not build for JDK 11 and has test failures for JDK 8 HOT 9
- Implement getAnnotatedOwnerType() added in JDK9
- Provide a more general type resolution facility HOT 1
- GenericTypeReflector.annotate adds invalid annotations HOT 6
- Inconsistent results when using TypeToken to get AnnotatedType in static and instance methods HOT 6
- Mitigate Java's annotation parser bug where possible
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from geantyref.