4141
4242// simplified+dependency free version of apache johnzon JsonbAccessMode
4343public class JsonbParser extends ModelParser {
44- private static final Class <? extends Annotation > JOHNZON_ANY ;
45- static {
46- Class <? extends Annotation > johnzonAny = null ;
47- try {
48- johnzonAny = (Class <? extends Annotation >) Thread .currentThread ().getContextClassLoader ()
49- .loadClass ("org.apache.johnzon.mapper.JohnzonAny" );
50- } catch (ClassNotFoundException e ) {
51- // no-op
52- }
53- JOHNZON_ANY = johnzonAny ;
54- }
44+
45+ private final Class <? extends Annotation > johnzonAny ;
5546
5647 public static class Factory extends ModelParser .Factory {
5748
@@ -75,6 +66,17 @@ public JsonbParser(Settings settings, TypeProcessor commonTypeProcessor) {
7566 public JsonbParser (Settings settings , TypeProcessor commonTypeProcessor ,
7667 List <RestApplicationParser > restApplicationParsers ) {
7768 super (settings , commonTypeProcessor , restApplicationParsers );
69+ johnzonAny = loadJohnzonAnyClass ();
70+ }
71+
72+ @ SuppressWarnings ("unchecked" )
73+ private Class <? extends Annotation > loadJohnzonAnyClass () {
74+ try {
75+ return (Class <? extends Annotation >) settings .classLoader
76+ .loadClass ("org.apache.johnzon.mapper.JohnzonAny" );
77+ } catch (ClassNotFoundException e ) {
78+ return null ;
79+ }
7880 }
7981
8082 @ Override
@@ -110,19 +112,24 @@ private BeanModel parseBean(final SourceType<Class<?>> sourceClass) {
110112
111113 private JsonbPropertyExtractor createExtractor () {
112114 return new JsonbPropertyExtractor (
115+ johnzonAny ,
113116 new PropertyNamingStrategyFactory (Optional .ofNullable (settings .jsonbConfiguration ).map (c -> c .namingStrategy ).orElse ("IDENTITY" )).create (),
114- new DefaultPropertyVisibilityStrategy (),
115- new FieldAndMethodAccessMode ());
117+ new DefaultPropertyVisibilityStrategy (settings . classLoader ),
118+ new FieldAndMethodAccessMode (johnzonAny ));
116119 }
117120
118121 private static class JsonbPropertyExtractor {
122+ private final Class <? extends Annotation > johnzonAny ;
119123 private final PropertyNamingStrategy naming ;
120124 private final PropertyVisibilityStrategy visibility ;
121125 private final BaseAccessMode delegate ;
122126
123- public JsonbPropertyExtractor (final PropertyNamingStrategy propertyNamingStrategy ,
124- final PropertyVisibilityStrategy visibilityStrategy ,
125- final BaseAccessMode delegate ) {
127+ public JsonbPropertyExtractor (
128+ final Class <? extends Annotation > johnzonAny ,
129+ final PropertyNamingStrategy propertyNamingStrategy ,
130+ final PropertyVisibilityStrategy visibilityStrategy ,
131+ final BaseAccessMode delegate ) {
132+ this .johnzonAny = johnzonAny ;
126133 this .naming = propertyNamingStrategy ;
127134 this .visibility = visibilityStrategy ;
128135 this .delegate = delegate ;
@@ -131,7 +138,7 @@ public JsonbPropertyExtractor(final PropertyNamingStrategy propertyNamingStrateg
131138 public List <PropertyModel > visit (final Class <?> clazz ) {
132139 return delegate .find (clazz ).entrySet ().stream ()
133140 .filter (e -> !isTransient (e .getValue (), visibility ))
134- .filter (e -> JOHNZON_ANY == null || e .getValue ().getAnnotation (JOHNZON_ANY ) == null )
141+ .filter (e -> johnzonAny == null || e .getValue ().getAnnotation (johnzonAny ) == null )
135142 .map (e -> {
136143 final Type type ;
137144 final Type readerType = e .getValue ().getType ();
@@ -230,12 +237,18 @@ private interface BaseAccessMode {
230237 }
231238
232239 private static class FieldAccessMode implements BaseAccessMode {
240+ private final Class <? extends Annotation > johnzonAny ;
241+
242+ public FieldAccessMode (final Class <? extends Annotation > johnzonAny ) {
243+ this .johnzonAny = johnzonAny ;
244+ }
245+
233246 @ Override
234247 public Map <String , JsonbParser .DecoratedType > find (final Class <?> clazz ) {
235248 final Map <String , JsonbParser .DecoratedType > readers = new HashMap <>();
236249 for (final Map .Entry <String , Field > f : fields (clazz , true ).entrySet ()) {
237250 final String key = f .getKey ();
238- if (isIgnored (key ) || (JOHNZON_ANY != null && Meta .getAnnotation (f .getValue (), JOHNZON_ANY ) != null )) {
251+ if (isIgnored (key ) || (johnzonAny != null && Meta .getAnnotation (f .getValue (), johnzonAny ) != null )) {
239252 continue ;
240253 }
241254
@@ -313,14 +326,20 @@ public String toString() {
313326 }
314327
315328 private static class MethodAccessMode implements BaseAccessMode {
329+ private final Class <? extends Annotation > johnzonAny ;
330+
331+ public MethodAccessMode (final Class <? extends Annotation > johnzonAny ) {
332+ this .johnzonAny = johnzonAny ;
333+ }
334+
316335 @ Override
317336 public Map <String , DecoratedType > find (final Class <?> clazz ) {
318337 final Map <String , DecoratedType > readers = new HashMap <>();
319338 if (Records .isRecord (clazz )) {
320339 readers .putAll (Stream .of (clazz .getMethods ())
321340 .filter (it -> it .getDeclaringClass () != Object .class && it .getParameterCount () == 0 )
322341 .filter (it -> !"toString" .equals (it .getName ()) && !"hashCode" .equals (it .getName ()))
323- .filter (it -> !isIgnored (it .getName ()) && JOHNZON_ANY != null && Meta .getAnnotation (it , JOHNZON_ANY ) == null )
342+ .filter (it -> !isIgnored (it .getName ()) && johnzonAny != null && Meta .getAnnotation (it , johnzonAny ) == null )
324343 .collect (Collectors .toMap (Method ::getName , it -> new MethodDecoratedType (it , it .getGenericReturnType ()) {
325344 })));
326345 } else {
@@ -329,7 +348,7 @@ public Map<String, DecoratedType> find(final Class<?> clazz) {
329348 final Method readMethod = descriptor .getReadMethod ();
330349 final String name = descriptor .getName ();
331350 if (readMethod != null && readMethod .getDeclaringClass () != Object .class ) {
332- if (isIgnored (name ) || JOHNZON_ANY != null && Meta .getAnnotation (readMethod , JOHNZON_ANY ) != null ) {
351+ if (isIgnored (name ) || johnzonAny != null && Meta .getAnnotation (readMethod , johnzonAny ) != null ) {
333352 continue ;
334353 }
335354 readers .put (name , new MethodDecoratedType (readMethod , readMethod .getGenericReturnType ()));
@@ -409,9 +428,9 @@ private static class FieldAndMethodAccessMode implements BaseAccessMode {
409428 private final FieldAccessMode fields ;
410429 private final MethodAccessMode methods ;
411430
412- private FieldAndMethodAccessMode () {
413- this .fields = new FieldAccessMode ();
414- this .methods = new MethodAccessMode ();
431+ private FieldAndMethodAccessMode (final Class <? extends Annotation > johnzonAny ) {
432+ this .fields = new FieldAccessMode (johnzonAny );
433+ this .methods = new MethodAccessMode (johnzonAny );
415434 }
416435
417436 @ Override
@@ -512,9 +531,14 @@ public String toString() {
512531 }
513532
514533
515- private static class DefaultPropertyVisibilityStrategy implements javax .json .bind .config .PropertyVisibilityStrategy {
534+ private static class DefaultPropertyVisibilityStrategy implements PropertyVisibilityStrategy {
535+ private final ClassLoader classLoader ;
516536 private final ConcurrentMap <Class <?>, PropertyVisibilityStrategy > strategies = new ConcurrentHashMap <>();
517537
538+ public DefaultPropertyVisibilityStrategy (ClassLoader classLoader ) {
539+ this .classLoader = classLoader ;
540+ }
541+
518542 @ Override
519543 public boolean isVisible (final Field field ) {
520544 if (field .getAnnotation (JsonbProperty .class ) != null ) {
@@ -560,8 +584,7 @@ private PropertyVisibilityStrategy visibilityStrategy(final Class<?> type) {
560584 p = Package .getPackage (parentPack );
561585 if (p == null ) {
562586 try {
563- p = Optional .ofNullable (type .getClassLoader ()).orElseGet (ClassLoader ::getSystemClassLoader )
564- .loadClass (parentPack + ".package-info" ).getPackage ();
587+ p = classLoader .loadClass (parentPack + ".package-info" ).getPackage ();
565588 } catch (final ClassNotFoundException e ) {
566589 // no-op
567590 }
0 commit comments