Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SecurityManager and AccessController dependencies in Grpc #3685

Merged
merged 1 commit into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,8 +19,6 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
Expand Down Expand Up @@ -153,16 +151,11 @@ protected Supplier<?> instanceSupplier() {
*/
protected List<Method> getAllDeclaredMethods(Class<?> clazz) {
List<Method> result = new LinkedList<>();

AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
Class current = clazz;
while (current != Object.class && current != null) {
result.addAll(Arrays.asList(current.getDeclaredMethods()));
current = current.getSuperclass();
}
return null;
});

Class current = clazz;
while (current != Object.class && current != null) {
result.addAll(Arrays.asList(current.getDeclaredMethods()));
current = current.getSuperclass();
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,6 @@
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.security.AccessController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -395,7 +394,7 @@ private static Method findAnnotatedMethod(Class<?> declaringClass, Method declar
return null;
}

declaredMethod = AccessController.doPrivileged(ModelHelper.findMethodOnClassPA(declaringClass, declaredMethod));
declaredMethod = ModelHelper.findMethodOnClass(declaringClass, declaredMethod);
if (declaredMethod == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -254,7 +253,7 @@ private static List<Method> methodList(Class<?> c) {
private static List<Method> allDeclaredMethods(Class<?> c) {
List<Method> l = new ArrayList<>();
while (c != null && c != Object.class) {
l.addAll(AccessController.doPrivileged(ModelHelper.getDeclaredMethodsPA(c)));
l.addAll(ModelHelper.getDeclaredMethods(c));
c = c.getSuperclass();
}
return l;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
Expand Down Expand Up @@ -90,56 +89,45 @@ public static Class<?> getAnnotatedResourceClass(Class<?> resourceClass, Class<?
}

/**
* Get privileged action to obtain methods declared on given class.
* If run using security manager, the returned privileged action
* must be invoked within a doPrivileged block.
* Get collection of methods declared on given class.
*
* @param clazz class for which to get the declared methods.
* @return privileged action to obtain methods declared on the {@code clazz} class.
* @see java.security.AccessController#doPrivileged(java.security.PrivilegedAction)
* @return methods declared on the {@code clazz} class.
*/
public static PrivilegedAction<Collection<? extends Method>> getDeclaredMethodsPA(final Class<?> clazz) {
return () -> Arrays.asList(clazz.getDeclaredMethods());
public static Collection<? extends Method> getDeclaredMethods(final Class<?> clazz) {
return Arrays.asList(clazz.getDeclaredMethods());
}

/**
* Get privileged action to find a method on a class given an existing method.
* If run using security manager, the returned privileged action
* must be invoked within a doPrivileged block.
* <p>
* If there exists a public method on the class that has the same name
* and parameters as the existing method then that public method is
* returned from the action.
* Find a method in a class. If there exists a public method on the class
* that has the same name and parameters then that public method is
* returned.
* <p>
* Otherwise, if there exists a public method on the class that has
* the same name and the same number of parameters as the existing method,
* the same name and the same number of parameters,
* and each generic parameter type, in order, of the public method is equal
* to the generic parameter type, in the same order, of the existing method
* or is an instance of {@link TypeVariable} then that public method is
* returned from the action.
* to the generic parameter type, in the same order or is an instance of
* {@link TypeVariable} then that public method is returned.
*
* @param cls the class to search for a public method
* @param m the method to find
* @return privileged action to return public method found.
* @see java.security.AccessController#doPrivileged(java.security.PrivilegedAction)
* @return public method found.
*/
public static PrivilegedAction<Method> findMethodOnClassPA(final Class<?> cls, final Method m) {
return () -> {
try {
return cls.getMethod(m.getName(), m.getParameterTypes());
} catch (final NoSuchMethodException e) {
for (final Method method : cls.getMethods()) {
if (method.getName().equals(m.getName())
&& method.getParameterTypes().length == m.getParameterTypes().length) {
if (compareParameterTypes(m.getGenericParameterTypes(),
method.getGenericParameterTypes())) {
return method;
}
public static Method findMethodOnClass(final Class<?> cls, final Method m) {
try {
return cls.getMethod(m.getName(), m.getParameterTypes());
} catch (final NoSuchMethodException e) {
for (final Method method : cls.getMethods()) {
if (method.getName().equals(m.getName())
&& method.getParameterTypes().length == m.getParameterTypes().length) {
if (compareParameterTypes(m.getGenericParameterTypes(),
method.getGenericParameterTypes())) {
return method;
}
}
return null;
}
};
return null;
}
}

/**
Expand Down