Skip to content

Commit fdf8f97

Browse files
authored
Merge pull request #314 from cyb3r4nt/jsonrpc-fix-web-param-annotation-class-loading
Improve @WebParam class loading in JsonRpcBasicServer
2 parents 43566a0 + b0c7605 commit fdf8f97

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

src/main/java/com/googlecode/jsonrpc4j/JsonRpcBasicServer.java

+33-26
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,11 @@ public class JsonRpcBasicServer {
4949
public static final String EXCEPTION_TYPE_NAME = "exceptionTypeName";
5050
public static final String VERSION = "2.0";
5151
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";
5452
public static final String NAME = "name";
5553
public static final String NULL = "null";
5654
private static final Logger logger = LoggerFactory.getLogger(JsonRpcBasicServer.class);
5755
private static final ErrorResolver DEFAULT_ERROR_RESOLVER = new MultipleErrorResolver(AnnotationsErrorResolver.INSTANCE, DefaultErrorResolver.INSTANCE);
5856
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-
}
6557

6658
private final ObjectMapper mapper;
6759
private final Class<?> remoteInterface;
@@ -79,6 +71,7 @@ public class JsonRpcBasicServer {
7971
private List<JsonRpcInterceptor> interceptorList = new ArrayList<>();
8072
private ExecutorService batchExecutorService = null;
8173
private long parallelBatchProcessingTimeout = Long.MAX_VALUE;
74+
private final Set<Class<? extends Annotation>> webParamAnnotationClasses;
8275

8376
/**
8477
* Creates the server with the given {@link ObjectMapper} delegating
@@ -104,6 +97,7 @@ public JsonRpcBasicServer(final ObjectMapper mapper, final Object handler, final
10497
this.mapper = mapper;
10598
this.handler = handler;
10699
this.remoteInterface = remoteInterface;
100+
this.webParamAnnotationClasses = loadWebParamAnnotationClasses();
107101
if (handler != null) {
108102
logger.debug("created server for interface {} with handler {}", remoteInterface, handler.getClass());
109103
}
@@ -131,22 +125,31 @@ public JsonRpcBasicServer(final Object handler) {
131125
this(new ObjectMapper(), handler, null);
132126
}
133127

134-
private static void loadAnnotationSupportEngine() {
128+
private Set<Class<? extends Annotation>> loadWebParamAnnotationClasses() {
135129
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+
}
148144

145+
if (webParamClasses.isEmpty()) {
146+
logger.debug(
147+
"Could not find any @WebParam classes in classpath." +
148+
" @WebParam support is disabled"
149+
);
149150
}
151+
152+
return Collections.unmodifiableSet(webParamClasses);
150153
}
151154

152155
/**
@@ -1297,11 +1300,14 @@ private List<JsonRpcParam> getAnnotatedParameterNames(Method method) {
12971300
return parameterNames;
12981301
}
12991302

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+
);
13031309
}
1304-
return ReflectionUtil.getParameterAnnotations(method, WEB_PARAM_ANNOTATION_CLASS);
1310+
return annotations;
13051311
}
13061312

13071313
private JsonRpcParam createNewJsonRcpParamType(final Annotation annotation) {
@@ -1312,7 +1318,8 @@ public Class<? extends Annotation> annotationType() {
13121318

13131319
public String value() {
13141320
try {
1315-
return (String) WEB_PARAM_NAME_METHOD.invoke(annotation);
1321+
Method method = annotation.getClass().getMethod(NAME);
1322+
return (String) method.invoke(annotation);
13161323
} catch (Exception e) {
13171324
throw new RuntimeException(e);
13181325
}

0 commit comments

Comments
 (0)