FasterXML / FasterXML/java-classmate

AnnotatedType support

Offen
#25 7 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Java
Sterne
266
Forks
47
Ø Merge
1 Std. 42 Min.
Gemergte PRs (30 T.)
1

Beschreibung

New to Java 8, the AnnotatedType class carries annotations bind to types in Generic expressions.
So if I write

```
@MaxLength(3) List<@NotNull Integer> myField;
```

I can get the `@NotNull` annotation via something like:

```
Field f = MyClass.class.getDeclaredField("myField");
AnnotatedParameterizedType annType = (AnnotatedParameterizedType) field.getAnnotatedType();
AnnotatedType[] childAnnotatedTypes = annType.getAnnotatedActualTypeArguments();
```

But then I have to assume the parameter I want is the first in the returned array. But maybe I have something like

```
class MyFunnyList implements List<@NotNull Integer> {}
// ...
private MyFunnyList<@Null String> myFunnyField;
```

Using `ResolvedType`, I can call `type.typeParametersFor(List.class).get(0)` to get the correct parameter type (`Integer` rather than `String`). It'd be nice to have also a way to retrieve the annotations associated with it (`@NotNull` rather than `@Null`). Note that this feature must not break support for Java 7, so some type of switch will probably need to be used.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.