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

Filter out java.lang.Object from checked types and fallback to getMethods() if getDeclaredMethods() causes SecurityException. #3860

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Filter out java.lang.Object from checked types and fallback to getMet…
…hods() if getDeclaredMethods() causes SecurityException.
  • Loading branch information
vrozov committed Apr 10, 2020
commit 1e6ffa26f131466f1e7925104a78c1e00c9dd176
48 changes: 28 additions & 20 deletions guava/src/com/google/common/eventbus/SubscriberRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -175,27 +175,35 @@ private static ImmutableList<Method> getAnnotatedMethods(Class<?> clazz) {

private static ImmutableList<Method> getAnnotatedMethodsNotCached(Class<?> clazz) {
Set<? extends Class<?>> supertypes = TypeToken.of(clazz).getTypes().rawTypes();
Map<MethodIdentifier, Method> identifiers = Maps.newHashMap();
for (Class<?> supertype : supertypes) {
for (Method method : supertype.getDeclaredMethods()) {
if (method.isAnnotationPresent(Subscribe.class) && !method.isSynthetic()) {
// TODO(cgdecker): Should check for a generic parameter type and error out
Class<?>[] parameterTypes = method.getParameterTypes();
checkArgument(
parameterTypes.length == 1,
"Method %s has @Subscribe annotation but has %s parameters."
+ "Subscriber methods must have exactly 1 parameter.",
method,
parameterTypes.length);

MethodIdentifier ident = new MethodIdentifier(method);
if (!identifiers.containsKey(ident)) {
identifiers.put(ident, method);
Set<MethodIdentifier> identifiers = new HashSet<>();
return supertypes
.stream()
.filter(cls -> !Object.class.equals(cls))
.flatMap(supertype -> {
Method[] methods;
try {
methods = supertype.getDeclaredMethods();
} catch (SecurityException e) {
methods = supertype.getMethods();
}
return Arrays.stream(methods)
.filter(method -> method.isAnnotationPresent(Subscribe.class))
.filter(method -> !method.isSynthetic())
.filter(method -> {
Class<?>[] parameterTypes = method.getParameterTypes();
checkArgument(
parameterTypes.length == 1,
"Method %s has @Subscribe annotation but has %s parameters."
+ "Subscriber methods must have exactly 1 parameter.",
method,
parameterTypes.length);
return true;
}
);
}
}
}
return ImmutableList.copyOf(identifiers.values());
)
.filter(method -> identifiers.add(new MethodIdentifier(method)))
.collect(ImmutableList.toImmutableList());
}

/** Global cache of classes to their flattened hierarchy of supertypes. */
Expand Down