Skip to content

Commit

Permalink
ISPN-9600 ReflectionUtil.invokeAccessibly should not be public
Browse files Browse the repository at this point in the history
(cherry picked from commit 7bdc282)

Conflicts:
	commons/src/main/java/org/infinispan/commons/util/ReflectionUtil.java
	commons/src/main/java/org/infinispan/commons/util/SecurityActions.java
	core/src/main/java/org/infinispan/distribution/group/impl/GroupManagerImpl.java
	core/src/main/java/org/infinispan/factories/impl/BasicComponentRegistryImpl.java
	core/src/test/java/org/infinispan/test/TestingUtil.java
  • Loading branch information
danberindei authored and tristantarrant committed Nov 14, 2019
1 parent 949ce5d commit 5dbb05c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
Expand Up @@ -6,6 +6,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -161,8 +162,19 @@ private static Field findFieldRecursively(Class<?> c, String fieldName) {
* @param method method to execute
* @param parameters parameters
*/
public static Object invokeAccessibly(Object instance, Method method, Object[] parameters) {
return SecurityActions.invokeAccessibly(instance, method, parameters);
public static Object invokeMethod(Object instance, Method method, Object[] parameters) {
try {
return method.invoke(instance, parameters);
} catch (InvocationTargetException e) {
Throwable cause = e.getCause() != null ? e.getCause() : e;
throw new CacheException("Unable to invoke method " + method + " on object of type " + (instance == null ? "null" : instance
.getClass().getSimpleName()) +
(parameters != null ? " with parameters " + Arrays.asList(parameters) : ""), cause);
} catch (Exception e) {
throw new CacheException("Unable to invoke method " + method + " on object of type " + (instance == null ? "null" : instance
.getClass().getSimpleName()) +
(parameters != null ? " with parameters " + Arrays.asList(parameters) : ""), e);
}
}

public static Method findGetterForField(Class<?> c, String fieldName) {
Expand Down Expand Up @@ -260,7 +272,6 @@ public static Object getValue(Object instance, String fieldName) {
* @param ann annotation to search for. Must be a class-level annotation.
* @return the annotation instance, or null
*/
@SuppressWarnings("unchecked")
public static <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> ann) {
while (true) {
// first check class
Expand Down
@@ -1,12 +1,7 @@
package org.infinispan.commons.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Arrays;

import org.infinispan.commons.CacheException;

/**
* Privileged actions for the package
Expand Down Expand Up @@ -84,22 +79,6 @@ private static <T> T doPrivileged(PrivilegedAction<T> action) {
}
}

static Object invokeAccessibly(Object instance, Method method, Object[] parameters) {
return doPrivileged((PrivilegedAction<Object>) () -> {
try {
method.setAccessible(true);
return method.invoke(instance, parameters);
} catch (InvocationTargetException e) {
Throwable cause = e.getCause() != null ? e.getCause() : e;
throw new CacheException("Unable to invoke method " + method + " on object of type " + (instance == null ? "null" : instance.getClass().getSimpleName()) +
(parameters != null ? " with parameters " + Arrays.asList(parameters) : ""), cause);
} catch (Exception e) {
throw new CacheException("Unable to invoke method " + method + " on object of type " + (instance == null ? "null" : instance.getClass().getSimpleName()) +
(parameters != null ? " with parameters " + Arrays.asList(parameters) : ""), e);
}
});
}

static ClassLoader[] getClassLoaders(ClassLoader appClassLoader) {
return doPrivileged((PrivilegedAction<ClassLoader[]>) () -> {
return new ClassLoader[] { appClassLoader, // User defined classes
Expand Down
@@ -1,6 +1,6 @@
package org.infinispan.distribution.group;

import static org.infinispan.commons.util.ReflectionUtil.invokeAccessibly;
import static org.infinispan.commons.util.ReflectionUtil.invokeMethod;

import org.infinispan.commons.util.CollectionFactory;
import org.infinispan.commons.util.ReflectionUtil;
Expand Down Expand Up @@ -50,13 +50,15 @@ public GroupMetadataImpl(Method method) {

@Override
public String getGroup(Object instance) {
Object object;
if (System.getSecurityManager() == null) {
object = invokeAccessibly(instance, method, Util.EMPTY_OBJECT_ARRAY);
method.setAccessible(true);
} else {
object = AccessController.doPrivileged((PrivilegedAction<Object>) () -> invokeAccessibly(instance, method, Util.EMPTY_OBJECT_ARRAY));
AccessController.doPrivileged((PrivilegedAction<List<Method>>) () -> {
method.setAccessible(true);
return null;
});
}
return String.class.cast(object);
return String.class.cast(invokeMethod(instance, method, Util.EMPTY_OBJECT_ARRAY));
}

}
Expand Down
Expand Up @@ -21,7 +21,6 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -35,8 +34,6 @@
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

import static org.infinispan.commons.util.ReflectionUtil.invokeAccessibly;

/**
* A registry where components which have been created are stored. Components are stored as singletons, registered
* under a specific name.
Expand Down Expand Up @@ -245,11 +242,7 @@ private void invokeInjectionMethod(Object o, ComponentMetadata.InjectMetadata in
boolean nameIsFQCN = !injectMetadata.isParameterNameSet(i);
params[i] = getOrCreateComponent(dependencies[i], name, nameIsFQCN);
}
if (System.getSecurityManager() == null) {
invokeAccessibly(o, injectMetadata.getMethod(), params);
} else {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> invokeAccessibly(o, injectMetadata.getMethod(), params));
}
invokeAccessibly(o, injectMetadata.getMethod(), params);
}
}

Expand Down Expand Up @@ -465,6 +458,13 @@ public void rewire() {
}
}

private static Object invokeAccessibly(Object instance, Method method, Object[] parameters) {
return SecurityActions.doPrivileged(() -> {
method.setAccessible(true);
return ReflectionUtil.invokeMethod(instance, method, parameters);
});
}

/**
* Scans each registered component for lifecycle methods, and adds them to the appropriate lists, and then sorts them
* by priority.
Expand Down
Expand Up @@ -21,6 +21,14 @@
final class SecurityActions {
private static final Log log = LogFactory.getLog(SecurityActions.class);

static <T> T doPrivileged(PrivilegedAction<T> action) {
if (System.getSecurityManager() != null) {
return AccessController.doPrivileged(action);
} else {
return action.run();
}
}

private static Field findFieldRecursively(Class<?> c, String fieldName) {
Field f = null;
try {
Expand Down

0 comments on commit 5dbb05c

Please sign in to comment.