下面列出了怎么用javax.ws.rs.ConstrainedTo的API类实例代码及写法,或者点击链接到github查看源代码。
private Set<Class<?>> filterClasses(Collection<Class<?>> classes) {
Set<Class<?>> result = new HashSet<>();
if (classes != null) {
for (Class<?> aClass : classes) {
ConstrainedTo annotation = aClass.getAnnotation(ConstrainedTo.class);
if (annotation == null || annotation.value() == RuntimeType.SERVER) {
result.add(aClass);
}
}
}
return result;
}
private boolean checkConstraints(Object provider) {
Class<?> providerClass = provider.getClass();
ConstrainedTo providerConstraint = providerClass.getAnnotation(ConstrainedTo.class);
if (providerConstraint != null) {
RuntimeType currentRuntime = config.getRuntimeType();
RuntimeType providerRuntime = providerConstraint.value();
// need to check (1) whether the registration is occurring in the specified runtime type
// and (2) does the provider implement an invalid interface based on the constrained runtime type
if (!providerRuntime.equals(currentRuntime)) {
LOG.warning("Provider " + provider + " cannot be registered in this " + currentRuntime
+ " runtime because it is constrained to " + providerRuntime + " runtimes.");
return false;
}
Class<?>[] restrictedInterfaces = RuntimeType.CLIENT.equals(providerRuntime) ? RESTRICTED_CLASSES_IN_CLIENT
: RESTRICTED_CLASSES_IN_SERVER;
for (Class<?> restrictedContract : restrictedInterfaces) {
if (restrictedContract.isAssignableFrom(providerClass)) {
RuntimeType opposite = RuntimeType.CLIENT.equals(providerRuntime) ? RuntimeType.SERVER
: RuntimeType.CLIENT;
LOG.warning("Provider " + providerClass.getName() + " is invalid - it is constrained to "
+ providerRuntime + " runtimes but implements a " + opposite + " interface ");
return false;
}
}
}
return true;
}
private boolean isNotServerProvider(Class<?> clazz) {
final ConstrainedTo ct = clazz.getAnnotation(ConstrainedTo.class);
if (ct != null && ct.value() != RuntimeType.SERVER) {
if (!FAIL_ON_CONSTRAINED_TO) {
LOGGER.warning(clazz + " is not a SERVER provider, ignoring");
return true;
}
throw new IllegalArgumentException(clazz + " is not a SERVER provider");
}
return false;
}