@@ -49,19 +49,11 @@ public class JsonRpcBasicServer {
49
49
public static final String EXCEPTION_TYPE_NAME = "exceptionTypeName" ;
50
50
public static final String VERSION = "2.0" ;
51
51
public static final int CODE_OK = 0 ;
52
- public static final String WEB_PARAM_ANNOTATION_CLASS_LOADER = "javax.jws.WebParam" ;
53
- public static final String WEB_PARAM_ANNOTATION_CLASS_LOADER_JAKARTA = "jakarta.jws.WebParam" ;
54
52
public static final String NAME = "name" ;
55
53
public static final String NULL = "null" ;
56
54
private static final Logger logger = LoggerFactory .getLogger (JsonRpcBasicServer .class );
57
55
private static final ErrorResolver DEFAULT_ERROR_RESOLVER = new MultipleErrorResolver (AnnotationsErrorResolver .INSTANCE , DefaultErrorResolver .INSTANCE );
58
56
private static final Pattern BASE64_PATTERN = Pattern .compile ("[A-Za-z0-9_=-]+" );
59
- private static Class <? extends Annotation > WEB_PARAM_ANNOTATION_CLASS ;
60
- private static Method WEB_PARAM_NAME_METHOD ;
61
-
62
- static {
63
- loadAnnotationSupportEngine ();
64
- }
65
57
66
58
private final ObjectMapper mapper ;
67
59
private final Class <?> remoteInterface ;
@@ -79,6 +71,7 @@ public class JsonRpcBasicServer {
79
71
private List <JsonRpcInterceptor > interceptorList = new ArrayList <>();
80
72
private ExecutorService batchExecutorService = null ;
81
73
private long parallelBatchProcessingTimeout = Long .MAX_VALUE ;
74
+ private final Set <Class <? extends Annotation >> webParamAnnotationClasses ;
82
75
83
76
/**
84
77
* Creates the server with the given {@link ObjectMapper} delegating
@@ -104,6 +97,7 @@ public JsonRpcBasicServer(final ObjectMapper mapper, final Object handler, final
104
97
this .mapper = mapper ;
105
98
this .handler = handler ;
106
99
this .remoteInterface = remoteInterface ;
100
+ this .webParamAnnotationClasses = loadWebParamAnnotationClasses ();
107
101
if (handler != null ) {
108
102
logger .debug ("created server for interface {} with handler {}" , remoteInterface , handler .getClass ());
109
103
}
@@ -131,22 +125,31 @@ public JsonRpcBasicServer(final Object handler) {
131
125
this (new ObjectMapper (), handler , null );
132
126
}
133
127
134
- private static void loadAnnotationSupportEngine () {
128
+ private Set < Class <? extends Annotation >> loadWebParamAnnotationClasses () {
135
129
final ClassLoader classLoader = JsonRpcBasicServer .class .getClassLoader ();
136
- try {
137
- WEB_PARAM_ANNOTATION_CLASS = classLoader .loadClass (WEB_PARAM_ANNOTATION_CLASS_LOADER ).asSubclass (Annotation .class );
138
- WEB_PARAM_NAME_METHOD = WEB_PARAM_ANNOTATION_CLASS .getMethod (NAME );
139
- } catch (ClassNotFoundException | NoSuchMethodException e ) {
140
- logger .debug ("Could not find {}.{}" , WEB_PARAM_ANNOTATION_CLASS_LOADER , NAME );
141
- logger .debug ("Try to load it from jakarta package" );
142
- try {
143
- WEB_PARAM_ANNOTATION_CLASS = classLoader .loadClass (WEB_PARAM_ANNOTATION_CLASS_LOADER_JAKARTA ).asSubclass (Annotation .class );
144
- WEB_PARAM_NAME_METHOD = WEB_PARAM_ANNOTATION_CLASS .getMethod (NAME );
145
- } catch (ClassNotFoundException | NoSuchMethodException ex ) {
146
- logger .debug ("Could not find {}.{}" , WEB_PARAM_ANNOTATION_CLASS_LOADER_JAKARTA , NAME );
147
- }
130
+ Set <Class <? extends Annotation >> webParamClasses = new HashSet <>(2 );
131
+ for (String className : Arrays .asList ("javax.jws.WebParam" , "jakarta.jws.WebParam" )) {
132
+ try {
133
+ Class <? extends Annotation > clazz =
134
+ classLoader
135
+ .loadClass (className )
136
+ .asSubclass (Annotation .class );
137
+ // check that method with name "name" is present
138
+ clazz .getMethod (NAME );
139
+ webParamClasses .add (clazz );
140
+ } catch (ClassNotFoundException | NoSuchMethodException e ) {
141
+ logger .debug ("Could not find {}.{}" , className , NAME );
142
+ }
143
+ }
148
144
145
+ if (webParamClasses .isEmpty ()) {
146
+ logger .debug (
147
+ "Could not find any @WebParam classes in classpath." +
148
+ " @WebParam support is disabled"
149
+ );
149
150
}
151
+
152
+ return Collections .unmodifiableSet (webParamClasses );
150
153
}
151
154
152
155
/**
@@ -1297,11 +1300,14 @@ private List<JsonRpcParam> getAnnotatedParameterNames(Method method) {
1297
1300
return parameterNames ;
1298
1301
}
1299
1302
1300
- private List <? extends List <? extends Annotation >> getWebParameterAnnotations (Method method ) {
1301
- if (WEB_PARAM_ANNOTATION_CLASS == null ) {
1302
- return new ArrayList <>();
1303
+ private List <List <? extends Annotation >> getWebParameterAnnotations (Method method ) {
1304
+ List <List <? extends Annotation >> annotations = new ArrayList <>();
1305
+ for (Class <? extends Annotation > clazz : JsonRpcBasicServer .this .webParamAnnotationClasses ) {
1306
+ annotations .addAll (
1307
+ ReflectionUtil .getParameterAnnotations (method , clazz )
1308
+ );
1303
1309
}
1304
- return ReflectionUtil . getParameterAnnotations ( method , WEB_PARAM_ANNOTATION_CLASS ) ;
1310
+ return annotations ;
1305
1311
}
1306
1312
1307
1313
private JsonRpcParam createNewJsonRcpParamType (final Annotation annotation ) {
@@ -1312,7 +1318,8 @@ public Class<? extends Annotation> annotationType() {
1312
1318
1313
1319
public String value () {
1314
1320
try {
1315
- return (String ) WEB_PARAM_NAME_METHOD .invoke (annotation );
1321
+ Method method = annotation .getClass ().getMethod (NAME );
1322
+ return (String ) method .invoke (annotation );
1316
1323
} catch (Exception e ) {
1317
1324
throw new RuntimeException (e );
1318
1325
}
0 commit comments