-
Notifications
You must be signed in to change notification settings - Fork 16
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
GenericTypeReflector.annotate adds invalid annotations #7
Comments
Anyway, for the moment you can use Btw, out of curiosity, does GeAnTyRef have the features you're looking for and, if not, which ones do you lack? Asking because I find it a pretty complete solution for working with (annotated) types and advanced reflection, but I of course don't know everyone's use-case. |
I don't use it for the "find exact type based on generics" functionality, as that's what Mirror is actually for (Mirror is kinda like GeAnTyRef in that way, but on steroids). I use it primarily for creating Type/AnnotatedType objects. For example, when I call Currently I created a fork that kinda gets the job done, so there isn't much time pressure at the moment. Along with the Other than these few hitches though your library has been immensely useful. |
Ah, I'll see to fix the null varargs thing as well.
What was wrong there? The same varargs issue or something else?
I'm now curious :) What features are you making?
So I presume you're mainly making use of |
The existing methods didn’t support owner types. It was as simple as adding a Yeah, I’m primarily using those two classes. From what I can see GeAnTyRef allows you to directly query the resolved types of methods/fields based upon a subclass. In the readme it also only shows getting it from a non-generic type, so I’m not sure if you could, say, get the return value of What Mirror does is create a whole separate family of objects, mirrors, which closely match the Java Core Reflection classes. These objects can be “specialized”, which means that This allows stuff like this: class CoolObject<T> extends BoringObject<Set<T>> {}
class BoringObject<T> {
public HashMap<String, ArrayList<T>> thing;
}
ClassMirror coolNumber = Mirror.reflect(new TypeToken<CoolObject<Number>>() {});
// asClassMirror just casts to ClassMirror and is there to avoid the nested casts from hell
TypeMirror listValue =
coolNumber.getField("thing").getType().asClassMirror() // HashMap<String, ArrayList<Set<String>>>
.getTypeParameters()[1].asClassMirror() // ArrayList<Set<String>>
.getTypeParameters()[0].asClassMirror(); // Set<String
assert(listValue == Mirror.reflect(new TypeToken<Set<Number>>() {})); |
Aah, I see. Will fix! Thanks for reporting these!
Yes, of course! This is what GeAnTyRef is all about. Type listOfString = new TypeToken<ArrayList<String>>() {}.getType();
Method get = List.class.getMethod("get", Integer.TYPE);
Type returnType = GenericTypeReflector.getReturnType(get, listOfString);
assertEquals(String.class, returnType);
Since GeAnTyRef only exposes (and expects) the regular Java interfaces ( Type coolNumber = new TypeToken<CoolObject<Number>>(){}.getType();
Field thing = CoolObject.class.getField("thing");
Type list = ((ParameterizedType) GenericTypeReflector.getFieldType(thing, coolNumber)) // HashMap<String, ArrayList<Set<String>>>
.getActualTypeArguments()[1]; // ArrayList<Set<String>>
Type listValue = ((ParameterizedType) list).getActualTypeArguments()[0]; // Set<String
assertEquals(listValue, new TypeToken<Set<Number>>() {}.getType()); (Same thing also works for |
To check if one type extends another GeAnTyRef provides Type listOfIntegers = TypeFactory.parameterizedClass(List.class, Integer.class);
Type listOfStrings = TypeFactory.parameterizedClass(List.class, String.class);
Type arrayListOfIntegers = TypeFactory.parameterizedClass(ArrayList.class, Integer.class);
Type arrayListOfStrings = TypeFactory.parameterizedClass(ArrayList.class, String.class);
// ArrayList<X> extends List<X>
assertTrue(GenericTypeReflector.isSuperType(listOfIntegers, arrayListOfIntegers));
assertTrue(GenericTypeReflector.isSuperType(listOfStrings, arrayListOfStrings));
//ArrayList<X> does not extend List<Y>
assertFalse(GenericTypeReflector.isSuperType(listOfStrings, arrayListOfIntegers));
assertFalse(GenericTypeReflector.isSuperType(listOfIntegers, arrayListOfStrings)); Your approach got me thinking... I should maybe provide a nicer API to construct types than what //Concept only, does not exist
TypeBuilder.of(List.class)
.annotations(...)
.parameters(...)
.owner(...)
.build(); |
In its current state,
GenericTypeReflector.annotate
adds class annotations as type annotations. I'm not entirely sure where this would be useful, but it is a serious problem for almost anything that wants to create annotated classes. See, I'm making a library and I need to createAnnotatedTypes
given a Type and a set of given annotations, however I need those exact annotations, and don't want any of the random@SuppressWarnings
etc. annotations from the class being added to the type on my behalf.The text was updated successfully, but these errors were encountered: