Skip to content

Commit

Permalink
WELD-2789 Replace all PrivilegedAction class implementations in core
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed May 31, 2024
1 parent 2fba6c7 commit 3d076ed
Show file tree
Hide file tree
Showing 66 changed files with 190 additions and 1,606 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.security.AccessController;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -44,7 +43,6 @@
import org.jboss.weld.config.ConfigurationKey;
import org.jboss.weld.environment.deployment.AbstractWeldDeployment;
import org.jboss.weld.environment.logging.CommonLogger;
import org.jboss.weld.security.GetSystemPropertyAction;
import org.jboss.weld.util.collections.ImmutableList;
import org.jboss.weld.util.collections.ImmutableSet;

Expand Down Expand Up @@ -82,7 +80,7 @@ public ClassPathBeanArchiveScanner(Bootstrap bootstrap, BeanDiscoveryMode emptyB

@Override
public List<ScanResult> scan() {
String javaClassPath = AccessController.doPrivileged(new GetSystemPropertyAction(JAVA_CLASS_PATH_SYSTEM_PROPERTY));
String javaClassPath = System.getProperty(JAVA_CLASS_PATH_SYSTEM_PROPERTY);
if (javaClassPath == null) {
throw CommonLogger.LOG.cannotReadJavaClassPathSystemProperty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.AccessController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -95,8 +94,6 @@
import org.jboss.weld.resources.ClassLoaderResourceLoader;
import org.jboss.weld.resources.spi.ClassFileServices;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.jboss.weld.security.GetClassLoaderAction;
import org.jboss.weld.security.GetSystemPropertyAction;
import org.jboss.weld.util.Preconditions;
import org.jboss.weld.util.ServiceLoader;
import org.jboss.weld.util.Services;
Expand Down Expand Up @@ -1327,7 +1324,7 @@ protected boolean isEnabled(String key, boolean defaultValue) {
if (value != null) {
return Boolean.TRUE.equals(value);
}
String system = AccessController.doPrivileged(new GetSystemPropertyAction(key));
String system = System.getProperty(key);
if (system != null) {
return Boolean.valueOf(system);
}
Expand Down Expand Up @@ -1365,8 +1362,7 @@ private void parseAdditionalBeanDefiningAnnotations() {
}

// parse from system properties
String stringValue = AccessController
.doPrivileged(new GetSystemPropertyAction(ADDITIONAL_BEAN_DEFINING_ANNOTATIONS_PROPERTY));
String stringValue = System.getProperty(ADDITIONAL_BEAN_DEFINING_ANNOTATIONS_PROPERTY);
if (stringValue != null) {
for (String className : stringValue.split(",")) {
if (!className.isEmpty()) {
Expand Down Expand Up @@ -1400,8 +1396,7 @@ private static class PackInfo {
this.packName = packClass.getPackage().getName();
this.packClassName = packClass.getName();
this.scanRecursively = recursiveScan;
this.classLoaderRef = new WeakReference<ClassLoader>(
AccessController.doPrivileged(new GetClassLoaderAction(packClass)));
this.classLoaderRef = new WeakReference<ClassLoader>(packClass.getClassLoader());
}

PackInfo(Package pack, boolean recursiveScan) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import org.jboss.weld.environment.servlet.logging.TomcatLogger;
import org.jboss.weld.environment.util.Reflections;
import org.jboss.weld.manager.api.WeldManager;
import org.jboss.weld.security.FieldLookupAction;
import org.jboss.weld.security.MethodLookupAction;
import org.jboss.weld.util.collections.Arrays2;

/**
Expand Down Expand Up @@ -113,15 +111,15 @@ private static StandardContext getStandardContext(ServletContext context) {
}

private static <E> Object getContextFieldValue(E obj, Class<E> clazz) throws NoSuchFieldException, IllegalAccessException {
Field field = FieldLookupAction.lookupField(clazz, CONTEXT_FIELD_NAME);
Field field = org.jboss.weld.util.reflection.Reflections.lookupField(clazz, CONTEXT_FIELD_NAME);
org.jboss.weld.util.reflection.Reflections.ensureAccessible(field, obj);
return field.get(obj);
}

private static InstanceManager getInstanceManager(StandardContext stdContext) {
try {
Method method = MethodLookupAction.lookupMethod(stdContext.getClass(), INSTANCE_MANAGER_GETTER_NAME,
Arrays2.EMPTY_CLASS_ARRAY);
Method method = org.jboss.weld.util.reflection.Reflections.lookupMethod(stdContext.getClass(),
INSTANCE_MANAGER_GETTER_NAME, Arrays2.EMPTY_CLASS_ARRAY);
org.jboss.weld.util.reflection.Reflections.ensureAccessible(method, stdContext);
try {
return Reflections.cast(method.invoke(stdContext));
Expand All @@ -132,7 +130,8 @@ private static InstanceManager getInstanceManager(StandardContext stdContext) {
// Getter/setter not found
}
try {
Field field = FieldLookupAction.lookupField(stdContext.getClass(), INSTANCE_MANAGER_FIELD_NAME);
Field field = org.jboss.weld.util.reflection.Reflections.lookupField(stdContext.getClass(),
INSTANCE_MANAGER_FIELD_NAME);
org.jboss.weld.util.reflection.Reflections.ensureAccessible(field, stdContext);
try {
return Reflections.cast(field.get(stdContext));
Expand All @@ -147,7 +146,8 @@ private static InstanceManager getInstanceManager(StandardContext stdContext) {

private static void setInstanceManager(StandardContext stdContext, InstanceManager instanceManager) {
try {
Method method = MethodLookupAction.lookupMethod(stdContext.getClass(), INSTANCE_MANAGER_SETTER_NAME,
Method method = org.jboss.weld.util.reflection.Reflections.lookupMethod(stdContext.getClass(),
INSTANCE_MANAGER_SETTER_NAME,
new Class[] { InstanceManager.class });
org.jboss.weld.util.reflection.Reflections.ensureAccessible(method, stdContext);
try {
Expand All @@ -160,7 +160,8 @@ private static void setInstanceManager(StandardContext stdContext, InstanceManag
// Getter/setter not found
}
try {
Field field = FieldLookupAction.lookupField(stdContext.getClass(), INSTANCE_MANAGER_FIELD_NAME);
Field field = org.jboss.weld.util.reflection.Reflections.lookupField(stdContext.getClass(),
INSTANCE_MANAGER_FIELD_NAME);
org.jboss.weld.util.reflection.Reflections.ensureAccessible(field, stdContext);
try {
field.set(stdContext, instanceManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

import jakarta.enterprise.inject.spi.AnnotatedMethod;

import org.jboss.weld.security.MethodLookupAction;
import org.jboss.weld.util.annotated.ForwardingAnnotatedMethod;
import org.jboss.weld.util.collections.WeldCollections;
import org.jboss.weld.util.reflection.Reflections;

/**
* An implementation of {@link AnnotatedMethod} used at runtime for invoking Java methods.
Expand Down Expand Up @@ -81,7 +81,7 @@ public <X> X invokeOnInstance(Object instance, Object... parameters) throws Ille
// the same method may be written to the map twice, but that is ok
// lookupMethod is very slow
Method delegate = annotatedMethod.getJavaMember();
method = MethodLookupAction.lookupMethod(instance.getClass(), delegate.getName(), delegate.getParameterTypes());
method = Reflections.lookupMethod(instance.getClass(), delegate.getName(), delegate.getParameterTypes());
if (method != null && !method.canAccess(instance)) {
method.setAccessible(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.jboss.weld.util.reflection.Reflections.cast;

import java.lang.reflect.Field;
import java.security.AccessController;

import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.BeanAttributes;
Expand All @@ -35,7 +34,6 @@
import org.jboss.weld.injection.ResourceInjection;
import org.jboss.weld.injection.ResourceInjectionFactory;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.security.GetAccessibleCopyOfMember;
import org.jboss.weld.util.reflection.Reflections;

/**
Expand Down Expand Up @@ -63,7 +61,7 @@ protected StaticEEResourceProducerField(BeanAttributes<T> attributes, EnhancedAn
ServiceRegistry services) {
super(attributes, field, declaringBean, disposalMethod, manager, services);
this.resourceInjection = getResourceInjection(field, declaringBean, manager);
this.accessibleField = AccessController.doPrivileged(new GetAccessibleCopyOfMember<Field>(field.getJavaMember()));
this.accessibleField = Reflections.getAccessibleCopyOfMember(field.getJavaMember());
this.injectionContext = new InjectionContextImpl<X>(manager, declaringBean.getInjectionTarget(),
declaringBean.getAnnotated(), null) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
Expand All @@ -40,12 +39,11 @@
import org.jboss.weld.injection.attributes.WeldInjectionPointAttributes;
import org.jboss.weld.interceptor.util.proxy.TargetInstanceProxy;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.security.GetDeclaredMethodAction;
import org.jboss.weld.security.GetDeclaredMethodsAction;
import org.jboss.weld.util.bytecode.BytecodeUtils;
import org.jboss.weld.util.bytecode.MethodInformation;
import org.jboss.weld.util.bytecode.RuntimeMethodInformation;
import org.jboss.weld.util.bytecode.StaticMethodInformation;
import org.jboss.weld.util.reflection.Reflections;

/**
* This special proxy factory is mostly used for abstract decorators. When a
Expand Down Expand Up @@ -141,7 +139,7 @@ private void decoratorMethods(Class<?> cls, Set<Method> all) {
if (cls == null) {
return;
}
all.addAll(Arrays.asList(AccessController.doPrivileged(new GetDeclaredMethodsAction(cls))));
all.addAll(Arrays.asList(cls.getDeclaredMethods()));

decoratorMethods(cls.getSuperclass(), all);

Expand Down Expand Up @@ -292,10 +290,8 @@ public void getDeclaredMethod(ClassMethod classMethod, String declaringClass, St
// and store it in the array
code.aastore();
}
code.invokestatic(GetDeclaredMethodAction.class.getName(), "wrapException",
"(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/security/PrivilegedAction;");
code.invokestatic(AccessController.class.getName(), "doPrivileged",
"(Ljava/security/PrivilegedAction;)Ljava/lang/Object;");
code.invokestatic(Reflections.class.getName(), "wrapException",
"(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");
code.checkcast(Method.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
package org.jboss.weld.bean.proxy;

import java.lang.reflect.Method;
import java.security.AccessController;
import java.util.concurrent.atomic.AtomicLong;

import org.jboss.classfilewriter.AccessFlag;
import org.jboss.classfilewriter.ClassFile;
import org.jboss.classfilewriter.ClassMethod;
import org.jboss.classfilewriter.code.CodeAttribute;
import org.jboss.weld.security.GetDeclaredMethodAction;
import org.jboss.weld.logging.ReflectionLogger;
import org.jboss.weld.util.bytecode.BytecodeUtils;

/**
Expand Down Expand Up @@ -88,7 +87,11 @@ private void addInitMethod(final String declaringClass, final String methodName,
}

public static Method getMethod(Class<?> javaClass, String methodName, Class<?>... parameterTypes) {
return AccessController.doPrivileged(GetDeclaredMethodAction.wrapException(javaClass, methodName, parameterTypes));
try {
return javaClass.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
throw ReflectionLogger.LOG.noSuchMethodWrapper(e, e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
Expand All @@ -39,7 +38,6 @@
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.injection.InterceptionFactoryImpl;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.security.GetDeclaredMethodsAction;
import org.jboss.weld.util.bytecode.BytecodeUtils;
import org.jboss.weld.util.bytecode.MethodInformation;
import org.jboss.weld.util.bytecode.RuntimeMethodInformation;
Expand Down Expand Up @@ -100,7 +98,7 @@ protected void addMethodsFromClass(ClassFile proxyClassType, ClassMethod staticC

for (Class<?> cls : classes) {
Set<MethodSignature> declaredBridgeMethods = new HashSet<MethodSignature>();
for (Method method : AccessController.doPrivileged(new GetDeclaredMethodsAction(cls))) {
for (Method method : cls.getDeclaredMethods()) {

final MethodSignatureImpl methodSignature = new MethodSignatureImpl(method);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.security.AccessController;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
Expand All @@ -49,7 +48,6 @@
import org.jboss.weld.interceptor.proxy.LifecycleMixin;
import org.jboss.weld.interceptor.util.proxy.TargetInstanceProxy;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.security.GetDeclaredMethodsAction;
import org.jboss.weld.util.bytecode.BytecodeUtils;
import org.jboss.weld.util.bytecode.MethodInformation;
import org.jboss.weld.util.bytecode.RuntimeMethodInformation;
Expand Down Expand Up @@ -210,7 +208,7 @@ protected void addMethodsFromClass(ClassFile proxyClassType, ClassMethod staticC
while (cls != null) {
Set<BridgeMethod> declaredBridgeMethods = new HashSet<BridgeMethod>();
Collection<Method> classDeclaredMethods = Arrays
.asList(AccessController.doPrivileged(new GetDeclaredMethodsAction(cls)).clone());
.asList(cls.getDeclaredMethods().clone());
for (Method method : classDeclaredMethods) {

final MethodSignatureImpl methodSignature = new MethodSignatureImpl(method);
Expand Down Expand Up @@ -732,4 +730,4 @@ public String toString() {

}

}
}
11 changes: 3 additions & 8 deletions impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.security.AccessController;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -63,9 +62,6 @@
import org.jboss.weld.interceptor.util.proxy.TargetInstanceProxy;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.proxy.WeldConstruct;
import org.jboss.weld.security.GetDeclaredConstructorsAction;
import org.jboss.weld.security.GetDeclaredMethodsAction;
import org.jboss.weld.security.GetProtectionDomainAction;
import org.jboss.weld.serialization.spi.BeanIdentifier;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.serialization.spi.ProxyServices;
Expand Down Expand Up @@ -504,7 +500,7 @@ private Class<T> createProxyClass(Class<?> originalClass, String proxyClassName)
// Dump proxy type bytecode if necessary
dumpToFile(proxyClassName, proxyClassType.toBytecode());

ProtectionDomain domain = AccessController.doPrivileged(new GetProtectionDomainAction(proxiedBeanType));
ProtectionDomain domain = proxiedBeanType.getProtectionDomain();

if (proxiedBeanType.getPackage() == null || proxiedBeanType.getPackage().getName().isEmpty()
|| proxiedBeanType.equals(Object.class)) {
Expand Down Expand Up @@ -553,8 +549,7 @@ protected void addConstructors(ClassFile proxyClassType, List<DeferredBytecode>
ConstructorUtils.addDefaultConstructor(proxyClassType, initialValueBytecode, !useConstructedFlag());
} else {
boolean constructorFound = false;
for (Constructor<?> constructor : AccessController
.doPrivileged(new GetDeclaredConstructorsAction(getBeanType()))) {
for (Constructor<?> constructor : getBeanType().getDeclaredConstructors()) {
if ((constructor.getModifiers() & Modifier.PRIVATE) == 0) {
constructorFound = true;
String[] exceptions = new String[constructor.getExceptionTypes().length];
Expand Down Expand Up @@ -663,7 +658,7 @@ protected void addMethodsFromClass(ClassFile proxyClassType, ClassMethod staticC

private void addMethods(Class<?> cls, ClassFile proxyClassType, ClassMethod staticConstructor,
Set<MethodSignature> foundFinalmethods) {
for (Method method : AccessController.doPrivileged(new GetDeclaredMethodsAction(cls))) {
for (Method method : cls.getDeclaredMethods()) {
MethodSignature methodSignature = new MethodSignatureImpl(method);
if (Modifier.isFinal(method.getModifiers())) {
foundFinalmethods.add(methodSignature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.jboss.weld.bootstrap;

import java.lang.reflect.Method;
import java.security.AccessController;
import java.util.Collections;
import java.util.Set;

Expand All @@ -36,7 +35,6 @@
import org.jboss.weld.resources.spi.ClassFileInfoException;
import org.jboss.weld.resources.spi.ClassFileServices;
import org.jboss.weld.resources.spi.ResourceLoadingException;
import org.jboss.weld.security.GetDeclaredMethodAction;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.bytecode.BytecodeUtils;
import org.jboss.weld.util.reflection.Reflections;
Expand Down Expand Up @@ -140,8 +138,7 @@ private boolean initCheckTypeModifiers() {
new ClassLoaderResourceLoader(classFileServices.getClass().getClassLoader()));
if (classInfoclass != null) {
try {
Method setFlags = AccessController
.doPrivileged(GetDeclaredMethodAction.of(classInfoclass, "setFlags", short.class));
Method setFlags = classInfoclass.getDeclaredMethod("setFlags", short.class);
return setFlags != null;
} catch (Exception exceptionIgnored) {
BootstrapLogger.LOG.usingOldJandexVersion();
Expand Down
Loading

0 comments on commit 3d076ed

Please sign in to comment.