src/share/classes/java/beans/Introspector.java

Print this page




1367                             // commented out because of 6976577
1368                             // result[i] = null; // ignore inaccessible methods
1369                         }
1370                     }
1371                 }
1372                 declaredMethodCache.put(clz, result);
1373             }
1374             return result;
1375         }
1376     }
1377 
1378     //======================================================================
1379     // Package private support methods.
1380     //======================================================================
1381 
1382     /**
1383      * Internal support for finding a target methodName with a given
1384      * parameter list on a given class.
1385      */
1386     private static Method internalFindMethod(Class<?> start, String methodName,
1387                                                  int argCount, Class args[]) {
1388         // For overriden methods we need to find the most derived version.
1389         // So we start with the given class and walk up the superclass chain.
1390 
1391         Method method = null;
1392 
1393         for (Class<?> cl = start; cl != null; cl = cl.getSuperclass()) {
1394             Method methods[] = getPublicDeclaredMethods(cl);
1395             for (int i = 0; i < methods.length; i++) {
1396                 method = methods[i];
1397                 if (method == null) {
1398                     continue;
1399                 }
1400 
1401                 // make sure method signature matches.
1402                 if (method.getName().equals(methodName)) {
1403                     Type[] params = method.getGenericParameterTypes();
1404                     if (params.length == argCount) {
1405                         if (args != null) {
1406                             boolean different = false;
1407                             if (argCount > 0) {


1409                                     if (TypeResolver.erase(TypeResolver.resolveInClass(start, params[j])) != args[j]) {
1410                                         different = true;
1411                                         continue;
1412                                     }
1413                                 }
1414                                 if (different) {
1415                                     continue;
1416                                 }
1417                             }
1418                         }
1419                         return method;
1420                     }
1421                 }
1422             }
1423         }
1424         method = null;
1425 
1426         // Now check any inherited interfaces.  This is necessary both when
1427         // the argument class is itself an interface, and when the argument
1428         // class is an abstract class.
1429         Class ifcs[] = start.getInterfaces();
1430         for (int i = 0 ; i < ifcs.length; i++) {
1431             // Note: The original implementation had both methods calling
1432             // the 3 arg method. This is preserved but perhaps it should
1433             // pass the args array instead of null.
1434             method = internalFindMethod(ifcs[i], methodName, argCount, null);
1435             if (method != null) {
1436                 break;
1437             }
1438         }
1439         return method;
1440     }
1441 
1442     /**
1443      * Find a target methodName on a given class.
1444      */
1445     static Method findMethod(Class<?> cls, String methodName, int argCount) {
1446         return findMethod(cls, methodName, argCount, null);
1447     }
1448 
1449     /**
1450      * Find a target methodName with specific parameter list on a given class.
1451      * <p>
1452      * Used in the contructors of the EventSetDescriptor,
1453      * PropertyDescriptor and the IndexedPropertyDescriptor.
1454      * <p>
1455      * @param cls The Class object on which to retrieve the method.
1456      * @param methodName Name of the method.
1457      * @param argCount Number of arguments for the desired method.
1458      * @param args Array of argument types for the method.
1459      * @return the method or null if not found
1460      */
1461     static Method findMethod(Class<?> cls, String methodName, int argCount,
1462                              Class args[]) {
1463         if (methodName == null) {
1464             return null;
1465         }
1466         return internalFindMethod(cls, methodName, argCount, args);
1467     }
1468 
1469     /**
1470      * Return true if class a is either equivalent to class b, or
1471      * if class a is a subclass of class b, i.e. if a either "extends"
1472      * or "implements" b.
1473      * Note tht either or both "Class" objects may represent interfaces.
1474      */
1475     static  boolean isSubclass(Class<?> a, Class<?> b) {
1476         // We rely on the fact that for any given java class or
1477         // primtitive type there is a unqiue Class object, so
1478         // we can use object equivalence in the comparisons.
1479         if (a == b) {
1480             return true;
1481         }
1482         if (a == null || b == null) {


1485         for (Class<?> x = a; x != null; x = x.getSuperclass()) {
1486             if (x == b) {
1487                 return true;
1488             }
1489             if (b.isInterface()) {
1490                 Class<?>[] interfaces = x.getInterfaces();
1491                 for (int i = 0; i < interfaces.length; i++) {
1492                     if (isSubclass(interfaces[i], b)) {
1493                         return true;
1494                     }
1495                 }
1496             }
1497         }
1498         return false;
1499     }
1500 
1501     /**
1502      * Return true iff the given method throws the given exception.
1503      */
1504     private boolean throwsException(Method method, Class<?> exception) {
1505         Class exs[] = method.getExceptionTypes();
1506         for (int i = 0; i < exs.length; i++) {
1507             if (exs[i] == exception) {
1508                 return true;
1509             }
1510         }
1511         return false;
1512     }
1513 
1514     /**
1515      * Try to create an instance of a named class.
1516      * First try the classloader of "sibling", then try the system
1517      * classloader then the class loader of the current Thread.
1518      */
1519     static Object instantiate(Class<?> sibling, String className)
1520                  throws InstantiationException, IllegalAccessException,
1521                                                 ClassNotFoundException {
1522         // First check with sibling's classloader (if any).
1523         ClassLoader cl = sibling.getClassLoader();
1524         Class<?> cls = ClassFinder.findClass(className, cl);
1525         return cls.newInstance();




1367                             // commented out because of 6976577
1368                             // result[i] = null; // ignore inaccessible methods
1369                         }
1370                     }
1371                 }
1372                 declaredMethodCache.put(clz, result);
1373             }
1374             return result;
1375         }
1376     }
1377 
1378     //======================================================================
1379     // Package private support methods.
1380     //======================================================================
1381 
1382     /**
1383      * Internal support for finding a target methodName with a given
1384      * parameter list on a given class.
1385      */
1386     private static Method internalFindMethod(Class<?> start, String methodName,
1387                                                  int argCount, Class<?> args[]) {
1388         // For overriden methods we need to find the most derived version.
1389         // So we start with the given class and walk up the superclass chain.
1390 
1391         Method method = null;
1392 
1393         for (Class<?> cl = start; cl != null; cl = cl.getSuperclass()) {
1394             Method methods[] = getPublicDeclaredMethods(cl);
1395             for (int i = 0; i < methods.length; i++) {
1396                 method = methods[i];
1397                 if (method == null) {
1398                     continue;
1399                 }
1400 
1401                 // make sure method signature matches.
1402                 if (method.getName().equals(methodName)) {
1403                     Type[] params = method.getGenericParameterTypes();
1404                     if (params.length == argCount) {
1405                         if (args != null) {
1406                             boolean different = false;
1407                             if (argCount > 0) {


1409                                     if (TypeResolver.erase(TypeResolver.resolveInClass(start, params[j])) != args[j]) {
1410                                         different = true;
1411                                         continue;
1412                                     }
1413                                 }
1414                                 if (different) {
1415                                     continue;
1416                                 }
1417                             }
1418                         }
1419                         return method;
1420                     }
1421                 }
1422             }
1423         }
1424         method = null;
1425 
1426         // Now check any inherited interfaces.  This is necessary both when
1427         // the argument class is itself an interface, and when the argument
1428         // class is an abstract class.
1429         Class<?>[] ifcs = start.getInterfaces();
1430         for (int i = 0 ; i < ifcs.length; i++) {
1431             // Note: The original implementation had both methods calling
1432             // the 3 arg method. This is preserved but perhaps it should
1433             // pass the args array instead of null.
1434             method = internalFindMethod(ifcs[i], methodName, argCount, null);
1435             if (method != null) {
1436                 break;
1437             }
1438         }
1439         return method;
1440     }
1441 
1442     /**
1443      * Find a target methodName on a given class.
1444      */
1445     static Method findMethod(Class<?> cls, String methodName, int argCount) {
1446         return findMethod(cls, methodName, argCount, null);
1447     }
1448 
1449     /**
1450      * Find a target methodName with specific parameter list on a given class.
1451      * <p>
1452      * Used in the contructors of the EventSetDescriptor,
1453      * PropertyDescriptor and the IndexedPropertyDescriptor.
1454      * <p>
1455      * @param cls The Class object on which to retrieve the method.
1456      * @param methodName Name of the method.
1457      * @param argCount Number of arguments for the desired method.
1458      * @param args Array of argument types for the method.
1459      * @return the method or null if not found
1460      */
1461     static Method findMethod(Class<?> cls, String methodName, int argCount,
1462                              Class<?>[] args) {
1463         if (methodName == null) {
1464             return null;
1465         }
1466         return internalFindMethod(cls, methodName, argCount, args);
1467     }
1468 
1469     /**
1470      * Return true if class a is either equivalent to class b, or
1471      * if class a is a subclass of class b, i.e. if a either "extends"
1472      * or "implements" b.
1473      * Note tht either or both "Class" objects may represent interfaces.
1474      */
1475     static  boolean isSubclass(Class<?> a, Class<?> b) {
1476         // We rely on the fact that for any given java class or
1477         // primtitive type there is a unqiue Class object, so
1478         // we can use object equivalence in the comparisons.
1479         if (a == b) {
1480             return true;
1481         }
1482         if (a == null || b == null) {


1485         for (Class<?> x = a; x != null; x = x.getSuperclass()) {
1486             if (x == b) {
1487                 return true;
1488             }
1489             if (b.isInterface()) {
1490                 Class<?>[] interfaces = x.getInterfaces();
1491                 for (int i = 0; i < interfaces.length; i++) {
1492                     if (isSubclass(interfaces[i], b)) {
1493                         return true;
1494                     }
1495                 }
1496             }
1497         }
1498         return false;
1499     }
1500 
1501     /**
1502      * Return true iff the given method throws the given exception.
1503      */
1504     private boolean throwsException(Method method, Class<?> exception) {
1505         Class<?>[] exs = method.getExceptionTypes();
1506         for (int i = 0; i < exs.length; i++) {
1507             if (exs[i] == exception) {
1508                 return true;
1509             }
1510         }
1511         return false;
1512     }
1513 
1514     /**
1515      * Try to create an instance of a named class.
1516      * First try the classloader of "sibling", then try the system
1517      * classloader then the class loader of the current Thread.
1518      */
1519     static Object instantiate(Class<?> sibling, String className)
1520                  throws InstantiationException, IllegalAccessException,
1521                                                 ClassNotFoundException {
1522         // First check with sibling's classloader (if any).
1523         ClassLoader cl = sibling.getClassLoader();
1524         Class<?> cls = ClassFinder.findClass(className, cl);
1525         return cls.newInstance();