@@ -255,18 +255,6 @@ private static final class AnnotationCache {
255
255
private static boolean reflectCacheDebug ;
256
256
private static boolean reflectCacheAppOnly = true ;
257
257
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
-
270
258
/*
271
259
* This {@code ClassReflectNullPlaceHolder} class is created to indicate the cached class value is
272
260
* initialized to null rather than the default value null ;e.g. {@code cachedDeclaringClass}
@@ -1274,7 +1262,7 @@ List<Method> getDeclaredPublicMethods(String name, Class<?>... parameterTypes) {
1274
1262
}
1275
1263
try {
1276
1264
methodList = new ArrayList <>();
1277
- getMethodHelper (false , true , methodList , name , parameterTypes );
1265
+ getMethodHelper (false , true , true , methodList , name , parameterTypes );
1278
1266
} catch (NoSuchMethodException e ) {
1279
1267
// no NoSuchMethodException expected
1280
1268
}
@@ -1374,7 +1362,7 @@ public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws
1374
1362
ClassLoader callerClassLoader = ClassLoader .getStackClassLoader (1 );
1375
1363
checkMemberAccess (security , callerClassLoader , Member .DECLARED );
1376
1364
}
1377
- return getMethodHelper (true , true , null , name , parameterTypes );
1365
+ return getMethodHelper (true , true , false , null , name , parameterTypes );
1378
1366
}
1379
1367
1380
1368
/**
@@ -1699,7 +1687,7 @@ public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMe
1699
1687
ClassLoader callerClassLoader = ClassLoader .getStackClassLoader (1 );
1700
1688
checkMemberAccess (security , callerClassLoader , Member .PUBLIC );
1701
1689
}
1702
- return getMethodHelper (true , false , null , name , parameterTypes );
1690
+ return getMethodHelper (true , false , true , null , name , parameterTypes );
1703
1691
}
1704
1692
1705
1693
/**
@@ -1713,21 +1701,12 @@ private Method throwExceptionOrReturnNull(boolean throwException, String name, C
1713
1701
}
1714
1702
}
1715
1703
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
-
1725
1704
/**
1726
1705
* Helper method for
1727
1706
* public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
1728
1707
* public Method getMethod(String name, Class<?>... parameterTypes)
1729
1708
* 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)
1731
1710
* without going thorough security checking
1732
1711
*
1733
1712
* @param throwException boolean
@@ -1737,28 +1716,25 @@ Method getMethodHelper(
1737
1716
* true - for getDeclaredMethod(String name, Class<?>... parameterTypes)
1738
1717
* & getDeclaredPublicMethods(String name, Class<?>... parameterTypes);
1739
1718
* false - for getMethod(String name, Class<?>... parameterTypes)
1740
- * & findMethod(boolean isPublic , String methodName, Class<?>... parameterTypes);
1719
+ * & findMethod(boolean publicOnly , String methodName, Class<?>... parameterTypes);
1741
1720
* @param name String the name of the method
1742
1721
* @param parameterTypes Class<?>[] the types of the arguments
1743
1722
* @param methodList List<Method> a list to store the methods described by the arguments
1744
1723
* for getDeclaredPublicMethods()
1745
1724
* 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
1749
1727
* @return Method the method described by the arguments.
1750
1728
* @throws NoSuchMethodException if the method could not be found.
1751
1729
*/
1752
1730
@ CallerSensitive
1753
1731
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 )
1755
1733
throws NoSuchMethodException {
1756
1734
Method result ;
1757
1735
Method bestCandidate ;
1758
1736
String strSig ;
1759
1737
boolean candidateFromInterface = false ;
1760
- boolean searchAllMethods = searchMethodType == SearchMethodType .ALL ;
1761
- boolean searchPublicMethodOnly = searchMethodType == SearchMethodType .PUBLIC ;
1762
1738
1763
1739
/*[PR CMVC 114820, CMVC 115873, CMVC 116166] add reflection cache */
1764
1740
if (parameterTypes == null ) {
@@ -1768,15 +1744,12 @@ Method getMethodHelper(
1768
1744
// getDeclaredPublicMethods() has to go through all methods anyway
1769
1745
Method cachedMethod = lookupCachedMethod (name , parameterTypes );
1770
1746
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
+ }
1773
1751
} 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 ())) {
1780
1753
return cachedMethod ;
1781
1754
}
1782
1755
}
@@ -1863,7 +1836,7 @@ Method getMethodHelper(
1863
1836
* Otherwise, the result method is chosen arbitrarily from specific methods.
1864
1837
*/
1865
1838
bestCandidate = result ;
1866
- boolean initialResultShouldBeReplaced = !searchAllMethods && ( publicMethodInitialResult != searchPublicMethodOnly ) ;
1839
+ boolean initialResultShouldBeReplaced = !forDeclaredMethod && publicOnly && ! publicMethodInitialResult ;
1867
1840
if (!candidateFromInterface ) {
1868
1841
Class <?> declaringClass = forDeclaredMethod ? this : result .getDeclaringClass ();
1869
1842
while (true ) {
@@ -1875,12 +1848,11 @@ Method getMethodHelper(
1875
1848
if ((methodList != null ) && publicMethod ) {
1876
1849
methodList .add (result );
1877
1850
}
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.
1881
1853
bestCandidate = result ;
1882
1854
initialResultShouldBeReplaced = false ;
1883
- } else if (forDeclaredMethod || publicMethod || searchIsPublic ) {
1855
+ } else if (forDeclaredMethod || publicMethod || ! publicOnly ) {
1884
1856
// bestCandidate and result have same declaringClass.
1885
1857
Class <?> candidateRetType = bestCandidate .getReturnType ();
1886
1858
Class <?> resultRetType = result .getReturnType ();
@@ -1890,8 +1862,8 @@ Method getMethodHelper(
1890
1862
}
1891
1863
}
1892
1864
}
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 .
1895
1867
return null ;
1896
1868
} else {
1897
1869
return cacheMethod (bestCandidate );
@@ -5870,10 +5842,9 @@ public static Class<?> forPrimitiveName(String typeName) {
5870
5842
};
5871
5843
}
5872
5844
5873
- Method findMethod (boolean isPublic , String methodName , Class <?>... parameterTypes ) {
5845
+ Method findMethod (boolean publicOnly , String methodName , Class <?>... parameterTypes ) {
5874
5846
try {
5875
- return getMethodHelper (true , false , null , isPublic ? SearchMethodType .PUBLIC : SearchMethodType .NOTPUBLIC ,
5876
- methodName , parameterTypes );
5847
+ return getMethodHelper (false , false , publicOnly , null , methodName , parameterTypes );
5877
5848
} catch (NoSuchMethodException nsme ) {
5878
5849
return null ;
5879
5850
}
0 commit comments