@@ -32,6 +32,19 @@ public class JacksonAnnotationIntrospector
32
32
{
33
33
private static final long serialVersionUID = 1L ;
34
34
35
+ private static final Java7Support _jdk7Helper ;
36
+ static {
37
+ Java7Support x = null ;
38
+ try {
39
+ x = Java7Support .class .newInstance ();
40
+ } catch (Throwable t ) {
41
+ // 24-Nov-2015, tatu: Should we log or not?
42
+ java .util .logging .Logger .getLogger (JacksonAnnotationIntrospector .class .getName ())
43
+ .warning ("Unable to load JDK7 annotation types; will have to skip" );
44
+ }
45
+ _jdk7Helper = x ;
46
+ }
47
+
35
48
/**
36
49
* Since introspection of annotation types is a performance issue in some
37
50
* use cases (rare, but do exist), let's try a simple cache to reduce
@@ -999,11 +1012,11 @@ public boolean hasCreatorAnnotation(Annotated a)
999
1012
return (ann .mode () != JsonCreator .Mode .DISABLED );
1000
1013
}
1001
1014
if (a instanceof AnnotatedConstructor ) {
1002
- ConstructorProperties props = _findAnnotation ( a , ConstructorProperties . class );
1003
- // 08-Nov-2015, tatu: One possible check would be to ensure there is at least
1004
- // one name iff constructor has arguments. But seems unnecessary for now.
1005
- if ( props != null ) {
1006
- return true ;
1015
+ if ( _jdk7Helper != null ) {
1016
+ Boolean b = _jdk7Helper . hasCreatorAnnotation ( a );
1017
+ if ( b != null ) {
1018
+ return b . booleanValue ();
1019
+ }
1007
1020
}
1008
1021
}
1009
1022
return false ;
@@ -1027,9 +1040,11 @@ protected boolean _isIgnorable(Annotated a)
1027
1040
if (ann != null ) {
1028
1041
return ann .value ();
1029
1042
}
1030
- Transient t = _findAnnotation (a , Transient .class );
1031
- if (t != null ) {
1032
- return t .value ();
1043
+ if (_jdk7Helper != null ) {
1044
+ Boolean b = _jdk7Helper .findTransient (a );
1045
+ if (b != null ) {
1046
+ return b .booleanValue ();
1047
+ }
1033
1048
}
1034
1049
return false ;
1035
1050
}
@@ -1063,12 +1078,10 @@ protected PropertyName _findConstructorName(Annotated a)
1063
1078
AnnotatedWithParams ctor = p .getOwner ();
1064
1079
1065
1080
if (ctor != null ) {
1066
- ConstructorProperties props = _findAnnotation (ctor , ConstructorProperties .class );
1067
- if (props != null ) {
1068
- String [] names = props .value ();
1069
- int ix = p .getIndex ();
1070
- if (ix < names .length ) {
1071
- return PropertyName .construct (names [ix ]);
1081
+ if (_jdk7Helper != null ) {
1082
+ PropertyName name = _jdk7Helper .findConstructorName (p );
1083
+ if (name != null ) {
1084
+ return name ;
1072
1085
}
1073
1086
}
1074
1087
}
@@ -1154,4 +1167,63 @@ protected StdTypeResolverBuilder _constructStdTypeResolverBuilder() {
1154
1167
protected StdTypeResolverBuilder _constructNoTypeResolverBuilder () {
1155
1168
return StdTypeResolverBuilder .noTypeInfoBuilder ();
1156
1169
}
1170
+
1171
+ /*
1172
+ /**********************************************************
1173
+ /* Helper classes
1174
+ /**********************************************************
1175
+ */
1176
+
1177
+ /**
1178
+ * To support Java7-incomplete platforms, we will offer support for JDK 7
1179
+ * annotations through this class, loaded dynamically; if loading fails,
1180
+ * support will be missing.
1181
+ */
1182
+ private static class Java7Support
1183
+ {
1184
+ @ SuppressWarnings ("unused" ) // compiler warns, just needed side-effects
1185
+ private final Class <?> _bogus ;
1186
+
1187
+ @ SuppressWarnings ("unused" ) // compiler warns; called via Reflection
1188
+ public Java7Support () {
1189
+ // Trigger loading of annotations that only JDK 7 has...
1190
+ Class <?> cls = Transient .class ;
1191
+ cls = ConstructorProperties .class ;
1192
+ _bogus = cls ;
1193
+ }
1194
+
1195
+ public Boolean findTransient (Annotated a ) {
1196
+ Transient t = a .getAnnotation (Transient .class );
1197
+ if (t != null ) {
1198
+ return t .value ();
1199
+ }
1200
+ return null ;
1201
+ }
1202
+
1203
+ public Boolean hasCreatorAnnotation (Annotated a ) {
1204
+ ConstructorProperties props = a .getAnnotation (ConstructorProperties .class );
1205
+ // 08-Nov-2015, tatu: One possible check would be to ensure there is at least
1206
+ // one name iff constructor has arguments. But seems unnecessary for now.
1207
+ if (props != null ) {
1208
+ return Boolean .TRUE ;
1209
+ }
1210
+ return null ;
1211
+ }
1212
+
1213
+ public PropertyName findConstructorName (AnnotatedParameter p )
1214
+ {
1215
+ AnnotatedWithParams ctor = p .getOwner ();
1216
+ if (ctor != null ) {
1217
+ ConstructorProperties props = ctor .getAnnotation (ConstructorProperties .class );
1218
+ if (props != null ) {
1219
+ String [] names = props .value ();
1220
+ int ix = p .getIndex ();
1221
+ if (ix < names .length ) {
1222
+ return PropertyName .construct (names [ix ]);
1223
+ }
1224
+ }
1225
+ }
1226
+ return null ;
1227
+ }
1228
+ }
1157
1229
}
0 commit comments