Top Description Fields Constructors Methods
com.google.escapevelocity

pack-priv Class MethodFinder

extends Object
Class Inheritance
Static Imports
com.google.common.reflect.Reflection.getPackageName, java.util.stream.Collectors.toSet

Finds public methods in a class. For each one, it determines the public class or interface in which it is declared. This avoids a problem with reflection, where we get an exception if we call a Method in a non-public class, even if the Method is public and if there is a public ancestor class or interface that declares it. We need to use the Method from the public ancestor.

Because looking for these methods is relatively expensive, an instance of this class will keep a cache of methods it previously discovered.

Field Summary

Modifier and TypeField and Description
private static final Method
private final Table<Class<?>, String, ImmutableSet<Method>>
methodCache

For a given class and name, returns all public methods of that name in the class, as previously determined by publicMethodsWithName.

private static final Method
private static final String

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private static boolean
private static boolean
classIsPublic(Class<?> c)

Returns whether the given class is public as seen from this class.

pack-priv synchronized ImmutableSet<Method>
publicMethodsWithName(Class<?> startClass, String name)

Returns the set of public methods with the given name in the given class.

private ImmutableSet<Method>
pack-priv static Method

Returns:

a public method in a public class or interface, or null if none was found.
visibleMethod
(Method method, Class<?> in)

Returns a Method with the same name and parameter types as the given one, but that is in a public class or interface.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CLASS_GET_MODULE_METHODback to summary
private static final Method CLASS_GET_MODULE_METHOD
methodCacheback to summary
private final Table<Class<?>, String, ImmutableSet<Method>> methodCache

For a given class and name, returns all public methods of that name in the class, as previously determined by publicMethodsWithName. The set of methods for a given class and name is saved the first time it is searched for, and returned directly thereafter. It may be empty.

Currently we add the entry for any given (class, name) pair on demand. An alternative would be to add all the methods for a given class at once. With the current scheme, we may end up calling Class#getMethods() several times for the same class, if methods of the different names are called at different times. With an all-at-once scheme, we might end up computing and storing information about a bunch of methods that will never be called. Because the profiling that led to the creation of this class revealed that visibleMethods in particular is quite expensive, it's probably best to avoid calling it unnecessarily.

MODULE_IS_EXPORTED_METHODback to summary
private static final Method MODULE_IS_EXPORTED_METHOD
THIS_PACKAGEback to summary
private static final String THIS_PACKAGE

Constructor Detail

MethodFinderback to summary
pack-priv MethodFinder()

Method Detail

classIsExportedback to summary
private static boolean classIsExported(Class<?> c)
classIsPublicback to summary
private static boolean classIsPublic(Class<?> c)

Returns whether the given class is public as seen from this class. Prior to Java 9, a class was either public or not public. But with the introduction of modules in Java 9, a class can be marked public and yet not be visible, if it is not exported from the module it appears in. So, on Java 9, we perform an additional check on class c, which is effectively c.getModule().isExported(c.getPackageName()). We use reflection so that the code can compile on earlier Java versions.

publicMethodsWithNameback to summary
pack-priv synchronized ImmutableSet<Method> publicMethodsWithName(Class<?> startClass, String name)

Returns the set of public methods with the given name in the given class. Here, "public methods" means public methods in public classes or interfaces. If startClass is not itself public, its methods are effectively not public either, but inherited methods may still appear in the returned set, with the Method objects belonging to public ancestors. More than one ancestor may define an appropriate method, but it doesn't matter because invoking any of those Method objects will have the same effect.

uncachedPublicMethodsWithNameback to summary
private ImmutableSet<Method> uncachedPublicMethodsWithName(Class<?> startClass, String name)
visibleMethodback to summary
pack-priv static Method visibleMethod(Method method, Class<?> in)

Returns a Method with the same name and parameter types as the given one, but that is in a public class or interface. This might be the given method, or it might be a method in a superclass or superinterface.

Returns:Method

a public method in a public class or interface, or null if none was found.