| 1 |
/* |
| 2 |
* Copyright (C) 2019 Google, Inc. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
package com.google.escapevelocity; |
| 17 |
|
| 18 |
import static com.google.common.reflect.Reflection.getPackageName; |
| 19 |
import static java.util.stream.Collectors.toSet; |
| 20 |
|
| 21 |
import com.google.common.collect.HashBasedTable; |
| 22 |
import com.google.common.collect.ImmutableSet; |
| 23 |
import com.google.common.collect.Table; |
| 24 |
import java.lang.reflect.Method; |
| 25 |
import java.lang.reflect.Modifier; |
| 26 |
import java.util.Arrays; |
| 27 |
import java.util.Objects; |
| 28 |
import java.util.Set; |
| 29 |
|
| 30 |
/** |
| 31 |
* Finds public methods in a class. For each one, it determines the public class or interface in |
| 32 |
* which it is declared. This avoids a problem with reflection, where we get an exception if we call |
| 33 |
* a {@code Method} in a non-public class, even if the {@code Method} is public and if there is a |
| 34 |
* public ancestor class or interface that declares it. We need to use the {@code Method} from the |
| 35 |
* public ancestor. |
| 36 |
* |
| 37 |
* <p>Because looking for these methods is relatively expensive, an instance of this class will keep |
| 38 |
* a cache of methods it previously discovered. |
| 39 |
*/ |
| 40 |
class MethodFinder { |
| 41 |
|
| 42 |
/** |
| 43 |
* For a given class and name, returns all public methods of that name in the class, as previously |
| 44 |
* determined by {@link #publicMethodsWithName}. The set of methods for a given class and name is |
| 45 |
* saved the first time it is searched for, and returned directly thereafter. It may be empty. |
| 46 |
* |
| 47 |
* <p>Currently we add the entry for any given (class, name) pair on demand. An alternative would |
| 48 |
* be to add all the methods for a given class at once. With the current scheme, we may end up |
| 49 |
* calling {@link Class#getMethods()} several times for the same class, if methods of the |
| 50 |
* different names are called at different times. With an all-at-once scheme, we might end up |
| 51 |
* computing and storing information about a bunch of methods that will never be called. Because |
| 52 |
* the profiling that led to the creation of this class revealed that {@link #visibleMethods} in |
| 53 |
* particular is quite expensive, it's probably best to avoid calling it unnecessarily. |
| 54 |
*/ |
| 55 |
private final Table<Class<?>, String, ImmutableSet<Method>> methodCache = HashBasedTable.create(); |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the set of public methods with the given name in the given class. Here, "public |
| 59 |
* methods" means public methods in public classes or interfaces. If {@code startClass} is not |
| 60 |
* itself public, its methods are effectively not public either, but inherited methods may still |
| 61 |
* appear in the returned set, with the {@code Method} objects belonging to public ancestors. More |
| 62 |
* than one ancestor may define an appropriate method, but it doesn't matter because invoking any |
| 63 |
* of those {@code Method} objects will have the same effect. |
| 64 |
*/ |
| 65 |
synchronized ImmutableSet<Method> publicMethodsWithName(Class<?> startClass, String name) { |
| 66 |
ImmutableSet<Method> cachedMethods = methodCache.get(startClass, name); |
| 67 |
if (cachedMethods == null) { |
| 68 |
cachedMethods = uncachedPublicMethodsWithName(startClass, name); |
| 69 |
methodCache.put(startClass, name, cachedMethods); |
| 70 |
} |
| 71 |
return cachedMethods; |
| 72 |
} |
| 73 |
|
| 74 |
private ImmutableSet<Method> uncachedPublicMethodsWithName(Class<?> startClass, String name) { |
| 75 |
// Class.getMethods() only returns public methods, so no need to filter explicitly for public. |
| 76 |
Set<Method> methods = |
| 77 |
Arrays.stream(startClass.getMethods()) |
| 78 |
.filter(m -> m.getName().equals(name)) |
| 79 |
.collect(toSet()); |
| 80 |
if (!classIsPublic(startClass)) { |
| 81 |
methods = |
| 82 |
methods.stream() |
| 83 |
.map(m -> visibleMethod(m, startClass)) |
| 84 |
.filter(Objects::nonNull) |
| 85 |
.collect(toSet()); |
| 86 |
// It would be a bit simpler to use ImmutableSet.toImmutableSet() here, but there've been |
| 87 |
// problems in the past with versions of Guava that don't have that method. |
| 88 |
} |
| 89 |
return ImmutableSet.copyOf(methods); |
| 90 |
} |
| 91 |
|
| 92 |
private static final String THIS_PACKAGE = getPackageName(Node.class) + "."; |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns a Method with the same name and parameter types as the given one, but that is in a |
| 96 |
* public class or interface. This might be the given method, or it might be a method in a |
| 97 |
* superclass or superinterface. |
| 98 |
* |
| 99 |
* @return a public method in a public class or interface, or null if none was found. |
| 100 |
*/ |
| 101 |
static Method visibleMethod(Method method, Class<?> in) { |
| 102 |
if (in == null) { |
| 103 |
return null; |
| 104 |
} |
| 105 |
Method methodInClass; |
| 106 |
try { |
| 107 |
methodInClass = in.getMethod(method.getName(), method.getParameterTypes()); |
| 108 |
} catch (NoSuchMethodException e) { |
| 109 |
return null; |
| 110 |
} |
| 111 |
if (classIsPublic(in) || in.getName().startsWith(THIS_PACKAGE)) { |
| 112 |
// The second disjunct is a hack to allow us to use the public methods of $foreach without |
| 113 |
// having to make the ForEachVar class public. We can invoke those methods from the same |
| 114 |
// package since ForEachVar is package-protected. |
| 115 |
return methodInClass; |
| 116 |
} |
| 117 |
Method methodInSuperclass = visibleMethod(method, in.getSuperclass()); |
| 118 |
if (methodInSuperclass != null) { |
| 119 |
return methodInSuperclass; |
| 120 |
} |
| 121 |
for (Class<?> superinterface : in.getInterfaces()) { |
| 122 |
Method methodInSuperinterface = visibleMethod(method, superinterface); |
| 123 |
if (methodInSuperinterface != null) { |
| 124 |
return methodInSuperinterface; |
| 125 |
} |
| 126 |
} |
| 127 |
return null; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Returns whether the given class is public as seen from this class. Prior to Java 9, a class was |
| 132 |
* either public or not public. But with the introduction of modules in Java 9, a class can be |
| 133 |
* marked public and yet not be visible, if it is not exported from the module it appears in. So, |
| 134 |
* on Java 9, we perform an additional check on class {@code c}, which is effectively {@code |
| 135 |
* c.getModule().isExported(c.getPackageName())}. We use reflection so that the code can compile |
| 136 |
* on earlier Java versions. |
| 137 |
*/ |
| 138 |
private static boolean classIsPublic(Class<?> c) { |
| 139 |
return Modifier.isPublic(c.getModifiers()) && classIsExported(c); |
| 140 |
} |
| 141 |
|
| 142 |
private static boolean classIsExported(Class<?> c) { |
| 143 |
if (CLASS_GET_MODULE_METHOD == null) { |
| 144 |
return true; // There are no modules, so all classes are exported. |
| 145 |
} |
| 146 |
try { |
| 147 |
String pkg = getPackageName(c); |
| 148 |
Object module = CLASS_GET_MODULE_METHOD.invoke(c); |
| 149 |
return (Boolean) MODULE_IS_EXPORTED_METHOD.invoke(module, pkg); |
| 150 |
} catch (Exception e) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
private static final Method CLASS_GET_MODULE_METHOD; |
| 156 |
private static final Method MODULE_IS_EXPORTED_METHOD; |
| 157 |
|
| 158 |
static { |
| 159 |
Method classGetModuleMethod; |
| 160 |
Method moduleIsExportedMethod; |
| 161 |
try { |
| 162 |
classGetModuleMethod = Class.class.getMethod("getModule"); |
| 163 |
Class<?> moduleClass = classGetModuleMethod.getReturnType(); |
| 164 |
moduleIsExportedMethod = moduleClass.getMethod("isExported", String.class); |
| 165 |
} catch (Exception e) { |
| 166 |
classGetModuleMethod = null; |
| 167 |
moduleIsExportedMethod = null; |
| 168 |
} |
| 169 |
CLASS_GET_MODULE_METHOD = classGetModuleMethod; |
| 170 |
MODULE_IS_EXPORTED_METHOD = moduleIsExportedMethod; |
| 171 |
} |
| 172 |
} |