Skip to content

Commit

Permalink
Deprecation: replace Proxy.getProxyClass
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangHG committed Nov 14, 2024
1 parent bcae3d6 commit 2f75bb0
Showing 1 changed file with 3 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;
import java.util.Map;

Expand Down Expand Up @@ -67,31 +65,15 @@ public static <T extends Annotation> T get(Class<T> annotationType, Map<String,
throw new IllegalArgumentException("Must specify an annotation");
}

Class<?> clazz = Proxy.getProxyClass(annotationType.getClassLoader(), annotationType, Serializable.class);
AnnotationInvocationHandler handler = new AnnotationInvocationHandler(values, annotationType);
// create a new instance by obtaining the constructor via relection
try {
return annotationType.cast(clazz.getConstructor(new Class[] {InvocationHandler.class}).newInstance(
new Object[] {handler}));
} catch (IllegalArgumentException e) {
throw new IllegalStateException(
"Error instantiating proxy for annotation. Annotation type: " + annotationType, e);
} catch (InstantiationException e) {
throw new IllegalStateException(
"Error instantiating proxy for annotation. Annotation type: " + annotationType, e);
} catch (IllegalAccessException e) {
throw new IllegalStateException(
"Error instantiating proxy for annotation. Annotation type: " + annotationType, e);
} catch (InvocationTargetException e) {
throw new IllegalStateException(
"Error instantiating proxy for annotation. Annotation type: " + annotationType,
e.getCause());
Object proxyInstance = Proxy.newProxyInstance(annotationType.getClassLoader(), new Class<?>[] {
annotationType, Serializable.class}, handler);
return annotationType.cast(proxyInstance);
} catch (SecurityException e) {
throw new IllegalStateException("Error accessing proxy constructor for annotation. Annotation type: "
+ annotationType, e);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("Error accessing proxy constructor for annotation. Annotation type: "
+ annotationType, e);
}
}
}

0 comments on commit 2f75bb0

Please sign in to comment.