Skip to content

Commit 759a22f

Browse files
committed
HV-1628 Add a NewProxyInstance privileged action
1 parent 0880b8b commit 759a22f

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

engine/src/main/java/org/hibernate/validator/internal/util/annotation/AnnotationFactory.java

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@
77
package org.hibernate.validator.internal.util.annotation;
88

99
import java.lang.annotation.Annotation;
10-
import java.lang.reflect.Constructor;
11-
import java.lang.reflect.InvocationHandler;
12-
import java.lang.reflect.InvocationTargetException;
13-
import java.lang.reflect.Proxy;
1410
import java.security.AccessController;
1511
import java.security.PrivilegedAction;
1612

17-
import org.hibernate.validator.internal.util.privilegedactions.ConstructorInstance;
1813
import org.hibernate.validator.internal.util.privilegedactions.GetClassLoader;
19-
import org.hibernate.validator.internal.util.privilegedactions.GetDeclaredConstructor;
14+
import org.hibernate.validator.internal.util.privilegedactions.NewProxyInstance;
2015

2116
/**
2217
* Creates live annotations (actually {@link AnnotationProxy} instances) from {@code AnnotationDescriptor}s.
@@ -32,31 +27,11 @@ private AnnotationFactory() {
3227
}
3328

3429
public static <T extends Annotation> T create(AnnotationDescriptor<T> descriptor) {
35-
@SuppressWarnings("unchecked")
36-
Class<T> proxyClass = (Class<T>) Proxy.getProxyClass(
30+
return run( NewProxyInstance.action(
3731
run( GetClassLoader.fromClass( descriptor.getType() ) ),
38-
descriptor.getType()
39-
);
40-
InvocationHandler handler = new AnnotationProxy( descriptor );
41-
try {
42-
return getProxyInstance( proxyClass, handler );
43-
}
44-
catch (RuntimeException e) {
45-
throw e;
46-
}
47-
catch (Exception e) {
48-
throw new RuntimeException( e );
49-
}
50-
}
51-
52-
private static <T extends Annotation> T getProxyInstance(Class<T> proxyClass, InvocationHandler handler) throws
53-
SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException,
54-
IllegalAccessException, InvocationTargetException {
55-
final Constructor<T> constructor = run( GetDeclaredConstructor.action(
56-
proxyClass,
57-
InvocationHandler.class
32+
descriptor.getType(),
33+
new AnnotationProxy( descriptor )
5834
) );
59-
return run( ConstructorInstance.action( constructor, handler ) );
6035
}
6136

6237
/**
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.internal.util.privilegedactions;
8+
9+
import java.lang.reflect.InvocationHandler;
10+
import java.lang.reflect.Proxy;
11+
import java.security.PrivilegedAction;
12+
13+
/**
14+
* Execute proxy creation as privileged action.
15+
*
16+
* @author Guillaume Smet
17+
*/
18+
public final class NewProxyInstance<T> implements PrivilegedAction<T> {
19+
20+
private final ClassLoader classLoader;
21+
private final Class<?>[] interfaces;
22+
private final InvocationHandler invocationHandler;
23+
24+
public static <T> NewProxyInstance<T> action(ClassLoader classLoader, Class<T> interfaze, InvocationHandler invocationHandler) {
25+
return new NewProxyInstance<T>( classLoader, interfaze, invocationHandler );
26+
}
27+
28+
public static NewProxyInstance<Object> action(ClassLoader classLoader, Class<?>[] interfaces, InvocationHandler invocationHandler) {
29+
return new NewProxyInstance<Object>( classLoader, interfaces, invocationHandler );
30+
}
31+
32+
private NewProxyInstance(ClassLoader classLoader, Class<?>[] interfaces, InvocationHandler invocationHandler) {
33+
this.classLoader = classLoader;
34+
this.interfaces = interfaces;
35+
this.invocationHandler = invocationHandler;
36+
}
37+
38+
private NewProxyInstance(ClassLoader classLoader, Class<T> interfaze, InvocationHandler invocationHandler) {
39+
this.classLoader = classLoader;
40+
this.interfaces = new Class<?>[] { interfaze };
41+
this.invocationHandler = invocationHandler;
42+
}
43+
44+
@SuppressWarnings("unchecked")
45+
@Override
46+
public T run() {
47+
return (T) Proxy.newProxyInstance( classLoader, interfaces, invocationHandler );
48+
}
49+
}

0 commit comments

Comments
 (0)