Skip to content

Commit

Permalink
Preparing for WELD-32, centralizing all SecurityManager-aware reflect…
Browse files Browse the repository at this point in the history
…ion access. Privileged execution disabled for now, uncomment the true block in SecureReflectionAccess to play around with it.
  • Loading branch information
nickarls committed Jan 7, 2010
1 parent d036c81 commit 336d0ca
Show file tree
Hide file tree
Showing 29 changed files with 1,064 additions and 327 deletions.
7 changes: 4 additions & 3 deletions impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java
Expand Up @@ -75,6 +75,7 @@
import org.jboss.weld.util.Strings;
import org.jboss.weld.util.Proxies.TypeInfo;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;
import org.slf4j.cal10n.LocLogger;

/**
Expand Down Expand Up @@ -224,7 +225,7 @@ protected T applyDecorators(T instance, CreationalContext<T> creationalContext,
}
try
{
T proxy = proxyClassForDecorators.newInstance();
T proxy = SecureReflections.newInstance(proxyClassForDecorators);
// temporary fix for decorators - make sure that the instance wrapped by the decorators
// is the contextual instance
// TODO - correct the decoration algorithm to avoid the creation of new target class instances
Expand Down Expand Up @@ -583,7 +584,7 @@ protected void initDirectlyDefinedInterceptors()
if (getAnnotatedItem().isAnnotationPresent(InterceptionUtils.getInterceptorsAnnotationClass()))
{
Annotation interceptorsAnnotation = getType().getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass());
classDeclaredInterceptors = Reflections.extractValues(interceptorsAnnotation);
classDeclaredInterceptors = SecureReflections.extractValues(interceptorsAnnotation);
}

if (classDeclaredInterceptors != null)
Expand All @@ -598,7 +599,7 @@ protected void initDirectlyDefinedInterceptors()
Class<?>[] methodDeclaredInterceptors = null;
if (method.isAnnotationPresent(InterceptionUtils.getInterceptorsAnnotationClass()))
{
methodDeclaredInterceptors = Reflections.extractValues(method.getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass()));
methodDeclaredInterceptors = SecureReflections.extractValues(method.getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass()));
}
if (excludeClassInterceptors)
{
Expand Down
9 changes: 3 additions & 6 deletions impl/src/main/java/org/jboss/weld/bean/DisposalMethod.java
Expand Up @@ -41,6 +41,7 @@
import org.jboss.weld.introspector.WeldMethod;
import org.jboss.weld.introspector.WeldParameter;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.reflection.SecureReflections;

public class DisposalMethod<X, T> extends AbstractReceiverBean<X, T, Method>
{
Expand Down Expand Up @@ -219,14 +220,10 @@ private void checkDisposalMethod()
if (type instanceof Class<?>)
{
Class<?> clazz = (Class<?>) type;
try
if (SecureReflections.methodExists(clazz, disposalMethodInjectionPoint.getName(), disposalMethodInjectionPoint.getParameterTypesAsArray()))
{
clazz.getDeclaredMethod(disposalMethodInjectionPoint.getName(), disposalMethodInjectionPoint.getParameterTypesAsArray());
methodDeclaredOnTypes = true;
}
catch (NoSuchMethodException nsme)
{
// No - op
continue;
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java
Expand Up @@ -43,6 +43,7 @@
import org.jboss.weld.introspector.WeldMethod;
import org.jboss.weld.introspector.WeldParameter;
import org.jboss.weld.util.Names;
import org.jboss.weld.util.reflection.SecureReflections;

/**
* Represents a producer method bean
Expand Down Expand Up @@ -166,15 +167,10 @@ else if (getDeclaringBean() instanceof SessionBean<?>)
{
if (type instanceof Class)
{
Class<?> clazz = (Class<?>) type;
try
if (SecureReflections.methodExists((Class<?>) type, getAnnotatedItem().getName(), getAnnotatedItem().getParameterTypesAsArray()))
{
clazz.getDeclaredMethod(getAnnotatedItem().getName(), getAnnotatedItem().getParameterTypesAsArray());
methodDeclaredOnTypes = true;
}
catch (NoSuchMethodException nsme)
{
// No - op
continue;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions impl/src/main/java/org/jboss/weld/bean/SessionBean.java
Expand Up @@ -74,6 +74,7 @@
import org.jboss.weld.util.Proxies;
import org.jboss.weld.util.Proxies.TypeInfo;
import org.jboss.weld.util.reflection.HierarchyDiscovery;
import org.jboss.weld.util.reflection.SecureReflections;

/**
* An enterprise bean representation
Expand Down Expand Up @@ -181,7 +182,7 @@ public T produce(CreationalContext<T> ctx)
{
try
{
T instance = proxyClass.newInstance();
T instance = SecureReflections.newInstance(proxyClass);
ctx.push(instance);
return Proxies.attachMethodHandler(instance, new EnterpriseBeanProxyMethodHandler<T>(SessionBean.this, ctx));
}
Expand Down Expand Up @@ -409,7 +410,7 @@ public boolean isMethodExistsOnTypes(WeldMethod<?, ?> method)
{
if (type instanceof Class)
{
for (Method m : ((Class<?>) type).getMethods())
for (Method m : SecureReflections.getMethods((Class<?>) type))
{
if (method.getName().equals(m.getName()) && Arrays.equals(method.getParameterTypesAsArray(), m.getParameterTypes()))
{
Expand Down
Expand Up @@ -19,6 +19,7 @@
import org.jboss.weld.NullInstanceException;
import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;
import org.slf4j.cal10n.LocLogger;

public class CallableMethodHandler implements MethodHandler, Serializable
Expand Down Expand Up @@ -77,7 +78,7 @@ public Object invoke(Object self, Method proxiedMethod, Method proceed, Object[]
}
try
{
Object returnValue = Reflections.invoke(proxiedMethod, instance, args);
Object returnValue = SecureReflections.invoke(instance, proxiedMethod, args);
log.trace(CALL_PROXIED_METHOD, proxiedMethod, instance, args, returnValue == null ? null : returnValue);
return returnValue;
}
Expand Down
Expand Up @@ -21,12 +21,12 @@

import javax.enterprise.context.spi.CreationalContext;

import org.jboss.interceptor.proxy.InterceptionHandlerFactory;
import org.jboss.interceptor.proxy.InterceptionHandler;
import org.jboss.interceptor.proxy.DirectClassInterceptionHandler;
import org.jboss.interceptor.proxy.InterceptionHandler;
import org.jboss.interceptor.proxy.InterceptionHandlerFactory;
import org.jboss.weld.BeanManagerImpl;
import org.jboss.weld.DeploymentException;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;

/**
* @author Marius Bogoevici
Expand All @@ -47,8 +47,7 @@ public InterceptionHandler createFor(Class clazz)
try
{
// this is not a managed instance - assume no-argument constructor exists
Constructor constructor = clazz.getDeclaredConstructor();
Reflections.ensureAccessible(constructor);
Constructor constructor = SecureReflections.getDeclaredConstructor(clazz);
Object interceptorInstance = constructor.newInstance();
// inject
manager.createInjectionTarget(manager.createAnnotatedType(clazz)).inject(interceptorInstance, creationalContext);
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.introspector.jlr.MethodSignatureImpl;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;

/**
* {@link MethodHandler} for Abstract decorators.
Expand Down Expand Up @@ -74,7 +75,7 @@ public Object invoke(Object self, Method thisMethod, Method proceed, Object[] ar
if (Reflections.isAbstract(thisMethod))
{
Method method = ((AnnotatedMethod<?>) delegateClass.getWeldMethod(new MethodSignatureImpl(thisMethod))).getJavaMember();
return Reflections.invoke(method, delegate, args);
return SecureReflections.invoke(delegate, method, args);
}

return proceed.invoke(self, args);
Expand Down
Expand Up @@ -34,6 +34,7 @@
import org.jboss.weld.context.WeldCreationalContext;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;
import org.slf4j.cal10n.LocLogger;

/**
Expand Down Expand Up @@ -110,8 +111,8 @@ public Object invoke(Object self, Method proxiedMethod, Method proceed, Object[]
}
try
{
Method method = Reflections.lookupMethod(proxiedMethod, proxiedInstance);
Object returnValue = Reflections.invoke(method, proxiedInstance, args);
Method method = SecureReflections.lookupMethod(proxiedInstance, proxiedMethod);
Object returnValue = SecureReflections.invoke(proxiedInstance, method, args);
log.trace(CALL_PROXIED_METHOD, proxiedMethod, proxiedInstance, args, returnValue == null ? null : returnValue);
return returnValue;
}
Expand Down
Expand Up @@ -30,7 +30,7 @@
import org.jboss.weld.introspector.WeldMethod;
import org.jboss.weld.introspector.jlr.MethodSignatureImpl;
import org.jboss.weld.serialization.spi.helpers.SerializableContextualInstance;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;

/**
* Method handler for decorated beans
Expand Down Expand Up @@ -97,6 +97,6 @@ protected Object doInvoke(Object self, Method method, Method proceed, Object[] a
throw new ForbiddenStateException(UNEXPECTED_UNWRAPPED_CUSTOM_DECORATOR, beanInstance.getContextual().get());
}
}
return Reflections.invoke(method, getTargetInstance(), args);
return SecureReflections.invoke(getTargetInstance(), method, args);
}
}
Expand Up @@ -37,6 +37,7 @@
import org.jboss.weld.introspector.MethodSignature;
import org.jboss.weld.introspector.jlr.MethodSignatureImpl;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;
import org.slf4j.cal10n.LocLogger;

/**
Expand Down Expand Up @@ -120,10 +121,10 @@ public Object invoke(Object self, Method method, Method proceed, Object[] args)
}
Class<?> businessInterface = getBusinessInterface(method);
Object proxiedInstance = reference.getBusinessObject(businessInterface);
Method proxiedMethod = Reflections.lookupMethod(method, proxiedInstance);
Method proxiedMethod = SecureReflections.lookupMethod(proxiedInstance, method);
try
{
Object returnValue = Reflections.invoke(proxiedMethod, proxiedInstance, args);
Object returnValue = SecureReflections.invoke(proxiedInstance, proxiedMethod, args);
log.trace(CALL_PROXIED_METHOD, method, proxiedInstance, args, returnValue);
return returnValue;
}
Expand Down
3 changes: 2 additions & 1 deletion impl/src/main/java/org/jboss/weld/injection/Exceptions.java
Expand Up @@ -21,6 +21,7 @@
import javax.enterprise.inject.CreationException;

import org.jboss.weld.WeldException;
import org.jboss.weld.util.reflection.SecureReflections;

class Exceptions
{
Expand All @@ -36,7 +37,7 @@ private static void rethrowException(Throwable t, Class<? extends RuntimeExcepti
RuntimeException e;
try
{
e = exceptionToThrow.newInstance();
e = SecureReflections.newInstance(exceptionToThrow);
}
catch (InstantiationException e1)
{
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.Names;
import org.jboss.weld.util.reflection.HierarchyDiscovery;
import org.jboss.weld.util.reflection.SecureReflections;

import com.google.common.base.Supplier;
import com.google.common.collect.Multimaps;
Expand Down Expand Up @@ -89,7 +90,7 @@ protected WeldAnnotationImpl(Class<T> annotationType, ClassTransformer classTran

});
this.namedMembers = new HashMap<String, WeldMethod<?, ?>>();
for (Method member : clazz.getDeclaredMethods())
for (Method member : SecureReflections.getDeclaredMethods(clazz))
{
WeldMethod<?, ?> annotatedMethod = WeldMethodImpl.of(member, this, classTransformer);
members.add(annotatedMethod);
Expand Down
Expand Up @@ -48,6 +48,7 @@
import org.jboss.weld.util.Names;
import org.jboss.weld.util.reflection.HierarchyDiscovery;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;

import com.google.common.base.Supplier;
import com.google.common.collect.Multimaps;
Expand Down Expand Up @@ -225,12 +226,8 @@ public Set<WeldField<?, T>> get()

for (Class<?> c = rawType; c != Object.class && c != null; c = c.getSuperclass())
{
for (Field field : c.getDeclaredFields())
for (Field field : SecureReflections.getDeclaredFields(c))
{
if (!field.isAccessible())
{
field.setAccessible(true);
}
WeldField<?, T> annotatedField = null;
if (annotatedTypeFields.containsKey(field))
{
Expand Down Expand Up @@ -299,7 +296,7 @@ public Set<WeldConstructor<?>> get()
}

this.declaredConstructorsBySignature = new HashMap<ConstructorSignature, WeldConstructor<?>>();
for (Constructor<?> constructor : rawType.getDeclaredConstructors())
for (Constructor<?> constructor : SecureReflections.getDeclaredConstructors(rawType))
{
WeldConstructor<T> annotatedConstructor = null;
if (annotatedTypeConstructors.containsKey(constructor))
Expand All @@ -314,10 +311,6 @@ public Set<WeldConstructor<?>> get()
annotatedConstructor = WeldConstructorImpl.of(c, this.<T>getDeclaringWBClass(c, classTransformer), classTransformer);
}

if (!constructor.isAccessible())
{
constructor.setAccessible(true);
}
this.constructors.add(annotatedConstructor);
this.constructorsByArgumentMap.put(Arrays.asList(constructor.getParameterTypes()), annotatedConstructor);

Expand Down Expand Up @@ -394,13 +387,8 @@ public Set<WeldMethod<?, T>> get()

for (Class<?> c = rawType; c != Object.class && c != null; c = c.getSuperclass())
{
for (Method method : c.getDeclaredMethods())
for (Method method : SecureReflections.getDeclaredMethods(c))
{
if (!method.isAccessible())
{
method.setAccessible(true);
}

WeldMethod<?, T> annotatedMethod = null;
if (annotatedTypeMethods.containsKey(method))
{
Expand Down
Expand Up @@ -16,8 +16,6 @@
*/
package org.jboss.weld.introspector.jlr;

import static org.jboss.weld.util.reflection.Reflections.ensureAccessible;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -75,13 +73,13 @@ public class WeldConstructorImpl<T> extends AbstractWeldCallable<T, T, Construct
public static <T> WeldConstructor<T> of(Constructor<T> constructor, WeldClass<T> declaringClass, ClassTransformer classTransformer)
{
AnnotationStore annotationStore = AnnotationStore.of(constructor, classTransformer.getTypeStore());
return new WeldConstructorImpl<T>(ensureAccessible(constructor), constructor.getDeclaringClass(), constructor.getDeclaringClass(), null, new HierarchyDiscovery(constructor.getDeclaringClass()).getTypeClosure(), annotationStore, declaringClass, classTransformer);
return new WeldConstructorImpl<T>(constructor, constructor.getDeclaringClass(), constructor.getDeclaringClass(), null, new HierarchyDiscovery(constructor.getDeclaringClass()).getTypeClosure(), annotationStore, declaringClass, classTransformer);
}

public static <T> WeldConstructor<T> of(AnnotatedConstructor<T> annotatedConstructor, WeldClass<T> declaringClass, ClassTransformer classTransformer)
{
AnnotationStore annotationStore = AnnotationStore.of(annotatedConstructor.getAnnotations(), annotatedConstructor.getAnnotations(), classTransformer.getTypeStore());
return new WeldConstructorImpl<T>(ensureAccessible(annotatedConstructor.getJavaMember()), annotatedConstructor.getJavaMember().getDeclaringClass(), annotatedConstructor.getBaseType(), annotatedConstructor, annotatedConstructor.getTypeClosure(), annotationStore, declaringClass, classTransformer);
return new WeldConstructorImpl<T>(annotatedConstructor.getJavaMember(), annotatedConstructor.getJavaMember().getDeclaringClass(), annotatedConstructor.getBaseType(), annotatedConstructor, annotatedConstructor.getTypeClosure(), annotationStore, declaringClass, classTransformer);
}

/**
Expand Down

0 comments on commit 336d0ca

Please sign in to comment.