< prev index next >

src/java.base/share/classes/java/io/ObjectStreamClass.java

Print this page
rev 15365 : imported patch 8163517-Various-cleanup-in-java-io-code


1492         } catch (NoSuchMethodException ex) {
1493             return null;
1494         }
1495     }
1496 
1497     /**
1498      * Returns true if classes are defined in the same runtime package, false
1499      * otherwise.
1500      */
1501     private static boolean packageEquals(Class<?> cl1, Class<?> cl2) {
1502         return (cl1.getClassLoader() == cl2.getClassLoader() &&
1503                 getPackageName(cl1).equals(getPackageName(cl2)));
1504     }
1505 
1506     /**
1507      * Returns package name of given class.
1508      */
1509     private static String getPackageName(Class<?> cl) {
1510         String s = cl.getName();
1511         int i = s.lastIndexOf('[');
1512         if (i >= 0) {
1513             s = s.substring(i + 2);
1514         }
1515         i = s.lastIndexOf('.');
1516         return (i >= 0) ? s.substring(0, i) : "";
1517     }
1518 
1519     /**
1520      * Compares class names for equality, ignoring package names.  Returns true
1521      * if class names equal, false otherwise.
1522      */
1523     private static boolean classNamesEqual(String name1, String name2) {
1524         int idx1 = name1.lastIndexOf('.') + 1;
1525         int idx2 = name2.lastIndexOf('.') + 1;
1526         int len1 = name1.length() - idx1;
1527         int len2 = name2.length() - idx2;
1528         return len1 == len2 &&
1529                 name1.regionMatches(idx1, name2, idx2, len1);
1530     }
1531 
1532     /**
1533      * Returns JVM type signature for given list of parameters and return type.
1534      */
1535     private static String getMethodSignature(Class<?>[] paramTypes,
1536                                              Class<?> retType)
1537     {
1538         StringBuilder sbuf = new StringBuilder();
1539         sbuf.append('(');
1540         for (int i = 0; i < paramTypes.length; i++) {
1541             appendClassSignature(sbuf, paramTypes[i]);
1542         }
1543         sbuf.append(')');
1544         appendClassSignature(sbuf, retType);
1545         return sbuf.toString();
1546     }
1547 
1548     /**
1549      * Convenience method for throwing an exception that is either a
1550      * RuntimeException, Error, or of some unexpected type (in which case it is
1551      * wrapped inside an IOException).
1552      */
1553     private static void throwMiscException(Throwable th) throws IOException {
1554         if (th instanceof RuntimeException) {
1555             throw (RuntimeException) th;
1556         } else if (th instanceof Error) {
1557             throw (Error) th;
1558         } else {
1559             IOException ex = new IOException("unexpected exception type");
1560             ex.initCause(th);
1561             throw ex;
1562         }
1563     }
1564 
1565     /**




1492         } catch (NoSuchMethodException ex) {
1493             return null;
1494         }
1495     }
1496 
1497     /**
1498      * Returns true if classes are defined in the same runtime package, false
1499      * otherwise.
1500      */
1501     private static boolean packageEquals(Class<?> cl1, Class<?> cl2) {
1502         return (cl1.getClassLoader() == cl2.getClassLoader() &&
1503                 getPackageName(cl1).equals(getPackageName(cl2)));
1504     }
1505 
1506     /**
1507      * Returns package name of given class.
1508      */
1509     private static String getPackageName(Class<?> cl) {
1510         String s = cl.getName();
1511         int i = s.lastIndexOf('[');
1512         i = (i < 0) ? 0 : i + 2;
1513         int j = s.lastIndexOf('.');
1514         return (i < j) ? s.substring(i, j) : "";


1515     }
1516 
1517     /**
1518      * Compares class names for equality, ignoring package names.  Returns true
1519      * if class names equal, false otherwise.
1520      */
1521     private static boolean classNamesEqual(String name1, String name2) {
1522         int idx1 = name1.lastIndexOf('.') + 1;
1523         int idx2 = name2.lastIndexOf('.') + 1;
1524         int len1 = name1.length() - idx1;
1525         int len2 = name2.length() - idx2;
1526         return len1 == len2 &&
1527                 name1.regionMatches(idx1, name2, idx2, len1);
1528     }
1529 
1530     /**
1531      * Returns JVM type signature for given list of parameters and return type.
1532      */
1533     private static String getMethodSignature(Class<?>[] paramTypes,
1534                                              Class<?> retType)
1535     {
1536         StringBuilder sb = new StringBuilder();
1537         sb.append('(');
1538         for (int i = 0; i < paramTypes.length; i++) {
1539             appendClassSignature(sb, paramTypes[i]);
1540         }
1541         sb.append(')');
1542         appendClassSignature(sb, retType);
1543         return sb.toString();
1544     }
1545 
1546     /**
1547      * Convenience method for throwing an exception that is either a
1548      * RuntimeException, Error, or of some unexpected type (in which case it is
1549      * wrapped inside an IOException).
1550      */
1551     private static void throwMiscException(Throwable th) throws IOException {
1552         if (th instanceof RuntimeException) {
1553             throw (RuntimeException) th;
1554         } else if (th instanceof Error) {
1555             throw (Error) th;
1556         } else {
1557             IOException ex = new IOException("unexpected exception type");
1558             ex.initCause(th);
1559             throw ex;
1560         }
1561     }
1562 
1563     /**


< prev index next >