Skip to content

Commit 2d2d18f

Browse files
authored
Merge pull request #18569 from JasonFengJ9/findmethodv2
JDK22+ update Access.findMethod(publicOnly, methodName, parameters)
2 parents 051b08e + 44bd7d5 commit 2d2d18f

File tree

2 files changed

+23
-60
lines changed

2 files changed

+23
-60
lines changed

Diff for: jcl/src/java.base/share/classes/java/lang/Access.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,6 @@ public Stream<Package> packages(ClassLoader classLoader) {
285285
return classLoader.createOrGetClassLoaderValueMap();
286286
}
287287

288-
public Method getMethodOrNull(Class<?> clz, String name, Class<?>... parameterTypes) {
289-
try {
290-
return clz.getMethodHelper(false, false, null, name, parameterTypes);
291-
} catch (NoSuchMethodException ex) {
292-
return null;
293-
}
294-
}
295-
296288
@SuppressWarnings("removal")
297289
public void invalidatePackageAccessCache() {
298290
/*[IF JAVA_SPEC_VERSION >= 10]*/
@@ -752,8 +744,8 @@ public void copyToSegmentRaw(String string, MemorySegment segment, long offset)
752744
}
753745

754746
// No @Override to avoid breaking valhalla builds which is behind latest OpenJDK head stream update.
755-
public Method findMethod(Class<?> clazz, boolean isPublic, String methodName, Class<?>... parameterTypes) {
756-
return clazz.findMethod(isPublic, methodName, parameterTypes);
747+
public Method findMethod(Class<?> clazz, boolean publicOnly, String methodName, Class<?>... parameterTypes) {
748+
return clazz.findMethod(publicOnly, methodName, parameterTypes);
757749
}
758750

759751
/*[ENDIF] JAVA_SPEC_VERSION >= 22 */

Diff for: jcl/src/java.base/share/classes/java/lang/Class.java

+21-50
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,6 @@ private static final class AnnotationCache {
255255
private static boolean reflectCacheDebug;
256256
private static boolean reflectCacheAppOnly = true;
257257

258-
/*
259-
* The target method types to be searched by getMethodHelper().
260-
*/
261-
enum SearchMethodType {
262-
// all method types, public or not
263-
ALL,
264-
// public method only
265-
PUBLIC,
266-
// not public method
267-
NOTPUBLIC,
268-
}
269-
270258
/*
271259
* This {@code ClassReflectNullPlaceHolder} class is created to indicate the cached class value is
272260
* initialized to null rather than the default value null ;e.g. {@code cachedDeclaringClass}
@@ -1274,7 +1262,7 @@ List<Method> getDeclaredPublicMethods(String name, Class<?>... parameterTypes) {
12741262
}
12751263
try {
12761264
methodList = new ArrayList<>();
1277-
getMethodHelper(false, true, methodList, name, parameterTypes);
1265+
getMethodHelper(false, true, true, methodList, name, parameterTypes);
12781266
} catch (NoSuchMethodException e) {
12791267
// no NoSuchMethodException expected
12801268
}
@@ -1374,7 +1362,7 @@ public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws
13741362
ClassLoader callerClassLoader = ClassLoader.getStackClassLoader(1);
13751363
checkMemberAccess(security, callerClassLoader, Member.DECLARED);
13761364
}
1377-
return getMethodHelper(true, true, null, name, parameterTypes);
1365+
return getMethodHelper(true, true, false, null, name, parameterTypes);
13781366
}
13791367

13801368
/**
@@ -1699,7 +1687,7 @@ public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMe
16991687
ClassLoader callerClassLoader = ClassLoader.getStackClassLoader(1);
17001688
checkMemberAccess(security, callerClassLoader, Member.PUBLIC);
17011689
}
1702-
return getMethodHelper(true, false, null, name, parameterTypes);
1690+
return getMethodHelper(true, false, true, null, name, parameterTypes);
17031691
}
17041692

17051693
/**
@@ -1713,21 +1701,12 @@ private Method throwExceptionOrReturnNull(boolean throwException, String name, C
17131701
}
17141702
}
17151703

1716-
/**
1717-
* A convenient method for getMethodHelper() with SearchMethodType.ALL.
1718-
*/
1719-
Method getMethodHelper(
1720-
boolean throwException, boolean forDeclaredMethod, List<Method> methodList, String name, Class<?>... parameterTypes)
1721-
throws NoSuchMethodException {
1722-
return getMethodHelper(throwException, forDeclaredMethod, methodList, SearchMethodType.ALL, name, parameterTypes);
1723-
}
1724-
17251704
/**
17261705
* Helper method for
17271706
* public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
17281707
* public Method getMethod(String name, Class<?>... parameterTypes)
17291708
* List<Method> getDeclaredPublicMethods(String name, Class<?>... parameterTypes)
1730-
* Method findMethod(boolean isPublic, String methodName, Class<?>... parameterTypes)
1709+
* Method findMethod(boolean publicOnly, String methodName, Class<?>... parameterTypes)
17311710
* without going thorough security checking
17321711
*
17331712
* @param throwException boolean
@@ -1737,28 +1716,25 @@ Method getMethodHelper(
17371716
* true - for getDeclaredMethod(String name, Class<?>... parameterTypes)
17381717
* & getDeclaredPublicMethods(String name, Class<?>... parameterTypes);
17391718
* false - for getMethod(String name, Class<?>... parameterTypes)
1740-
* & findMethod(boolean isPublic, String methodName, Class<?>... parameterTypes);
1719+
* & findMethod(boolean publicOnly, String methodName, Class<?>... parameterTypes);
17411720
* @param name String the name of the method
17421721
* @param parameterTypes Class<?>[] the types of the arguments
17431722
* @param methodList List<Method> a list to store the methods described by the arguments
17441723
* for getDeclaredPublicMethods()
17451724
* or null for getDeclaredMethod(), getMethod() & findMethod()
1746-
* @param methodScope SearchMethodType the method type to be searched
1747-
* SearchMethodType.PUBLIC/NOTPUBLIC for findMethod()
1748-
* or SearchMethodType.ALL for others
1725+
* @param publicOnly boolean true - only search public methods
1726+
* false - search all methods
17491727
* @return Method the method described by the arguments.
17501728
* @throws NoSuchMethodException if the method could not be found.
17511729
*/
17521730
@CallerSensitive
17531731
Method getMethodHelper(
1754-
boolean throwException, boolean forDeclaredMethod, List<Method> methodList, SearchMethodType searchMethodType, String name, Class<?>... parameterTypes)
1732+
boolean throwException, boolean forDeclaredMethod, boolean publicOnly, List<Method> methodList, String name, Class<?>... parameterTypes)
17551733
throws NoSuchMethodException {
17561734
Method result;
17571735
Method bestCandidate;
17581736
String strSig;
17591737
boolean candidateFromInterface = false;
1760-
boolean searchAllMethods = searchMethodType == SearchMethodType.ALL;
1761-
boolean searchPublicMethodOnly = searchMethodType == SearchMethodType.PUBLIC;
17621738

17631739
/*[PR CMVC 114820, CMVC 115873, CMVC 116166] add reflection cache */
17641740
if (parameterTypes == null) {
@@ -1768,15 +1744,12 @@ Method getMethodHelper(
17681744
// getDeclaredPublicMethods() has to go through all methods anyway
17691745
Method cachedMethod = lookupCachedMethod(name, parameterTypes);
17701746
if (cachedMethod != null) {
1771-
if (searchAllMethods && forDeclaredMethod && (cachedMethod.getDeclaringClass() == this)) {
1772-
return cachedMethod;
1747+
if (forDeclaredMethod) {
1748+
if (cachedMethod.getDeclaringClass() == this) {
1749+
return cachedMethod;
1750+
}
17731751
} else {
1774-
boolean isPublic = Modifier.isPublic(cachedMethod.getModifiers());
1775-
if (searchAllMethods) {
1776-
if (!forDeclaredMethod && isPublic) {
1777-
return cachedMethod;
1778-
}
1779-
} else if (searchPublicMethodOnly == isPublic) {
1752+
if (!publicOnly || Modifier.isPublic(cachedMethod.getModifiers())) {
17801753
return cachedMethod;
17811754
}
17821755
}
@@ -1863,7 +1836,7 @@ Method getMethodHelper(
18631836
* Otherwise, the result method is chosen arbitrarily from specific methods.
18641837
*/
18651838
bestCandidate = result;
1866-
boolean initialResultShouldBeReplaced = !searchAllMethods && (publicMethodInitialResult != searchPublicMethodOnly);
1839+
boolean initialResultShouldBeReplaced = !forDeclaredMethod && publicOnly && !publicMethodInitialResult;
18671840
if (!candidateFromInterface) {
18681841
Class<?> declaringClass = forDeclaredMethod ? this : result.getDeclaringClass();
18691842
while (true) {
@@ -1875,12 +1848,11 @@ Method getMethodHelper(
18751848
if ((methodList != null) && publicMethod) {
18761849
methodList.add(result);
18771850
}
1878-
boolean searchIsPublic = !searchAllMethods && (searchPublicMethodOnly == publicMethod);
1879-
if (searchIsPublic && initialResultShouldBeReplaced) {
1880-
// Current result is the method type to be found but the initial result wasn't.
1851+
if (publicMethod && initialResultShouldBeReplaced) {
1852+
// Current result is a public method to be searched but the initial result wasn't.
18811853
bestCandidate = result;
18821854
initialResultShouldBeReplaced = false;
1883-
} else if (forDeclaredMethod || publicMethod || searchIsPublic) {
1855+
} else if (forDeclaredMethod || publicMethod || !publicOnly) {
18841856
// bestCandidate and result have same declaringClass.
18851857
Class<?> candidateRetType = bestCandidate.getReturnType();
18861858
Class<?> resultRetType = result.getReturnType();
@@ -1890,8 +1862,8 @@ Method getMethodHelper(
18901862
}
18911863
}
18921864
}
1893-
if (!searchAllMethods && initialResultShouldBeReplaced) {
1894-
// The initial result doesn't match the searching method type.
1865+
if (initialResultShouldBeReplaced) {
1866+
// The initial result is not a public method to be searched, and no other public methods found.
18951867
return null;
18961868
} else {
18971869
return cacheMethod(bestCandidate);
@@ -5870,10 +5842,9 @@ public static Class<?> forPrimitiveName(String typeName) {
58705842
};
58715843
}
58725844

5873-
Method findMethod(boolean isPublic, String methodName, Class<?>... parameterTypes) {
5845+
Method findMethod(boolean publicOnly, String methodName, Class<?>... parameterTypes) {
58745846
try {
5875-
return getMethodHelper(true, false, null, isPublic ? SearchMethodType.PUBLIC : SearchMethodType.NOTPUBLIC,
5876-
methodName, parameterTypes);
5847+
return getMethodHelper(false, false, publicOnly, null, methodName, parameterTypes);
58775848
} catch (NoSuchMethodException nsme) {
58785849
return null;
58795850
}

0 commit comments

Comments
 (0)