< prev index next >

src/java.base/share/classes/java/lang/ClassLoader.java

Print this page




 966      * @throws  IndexOutOfBoundsException
 967      *          If either {@code off} or {@code len} is negative, or if
 968      *          {@code off+len} is greater than {@code b.length}.
 969      *
 970      * @throws  SecurityException
 971      *          If an attempt is made to add this class to a package that
 972      *          contains classes that were signed by a different set of
 973      *          certificates than this class, or if {@code name} begins with
 974      *          "{@code java.}" and this class loader is not the platform
 975      *          class loader or its ancestor.
 976      *
 977      * @revised 9
 978      * @spec JPMS
 979      */
 980     protected final Class<?> defineClass(String name, byte[] b, int off, int len,
 981                                          ProtectionDomain protectionDomain)
 982         throws ClassFormatError
 983     {
 984         protectionDomain = preDefineClass(name, protectionDomain);
 985         String source = defineClassSourceLocation(protectionDomain);
 986         Class<?> c = defineClass1(name, b, off, len, protectionDomain, source);
 987         postDefineClass(c, protectionDomain);
 988         return c;
 989     }
 990 
 991     /**
 992      * Converts a {@link java.nio.ByteBuffer ByteBuffer} into an instance
 993      * of class {@code Class}, with the given {@code ProtectionDomain}.
 994      * If the given {@code ProtectionDomain} is {@code null}, then a default
 995      * protection domain will be assigned to the class as
 996      * specified in the documentation for {@link #defineClass(String, byte[],
 997      * int, int)}.  Before the class can be used it must be resolved.
 998      *
 999      * <p>The rules about the first class defined in a package determining the
1000      * set of certificates for the package, the restrictions on class names,
1001      * and the defined package of the class
1002      * are identical to those specified in the documentation for {@link
1003      * #defineClass(String, byte[], int, int, ProtectionDomain)}.
1004      *
1005      * <p> An invocation of this method of the form
1006      * <i>cl</i>{@code .defineClass(}<i>name</i>{@code ,}


1058         throws ClassFormatError
1059     {
1060         int len = b.remaining();
1061 
1062         // Use byte[] if not a direct ByteBuffer:
1063         if (!b.isDirect()) {
1064             if (b.hasArray()) {
1065                 return defineClass(name, b.array(),
1066                                    b.position() + b.arrayOffset(), len,
1067                                    protectionDomain);
1068             } else {
1069                 // no array, or read-only array
1070                 byte[] tb = new byte[len];
1071                 b.get(tb);  // get bytes out of byte buffer.
1072                 return defineClass(name, tb, 0, len, protectionDomain);
1073             }
1074         }
1075 
1076         protectionDomain = preDefineClass(name, protectionDomain);
1077         String source = defineClassSourceLocation(protectionDomain);
1078         Class<?> c = defineClass2(name, b, b.position(), len, protectionDomain, source);
1079         postDefineClass(c, protectionDomain);
1080         return c;
1081     }
1082 
1083     private native Class<?> defineClass1(String name, byte[] b, int off, int len,
1084                                          ProtectionDomain pd, String source);
1085 
1086     private native Class<?> defineClass2(String name, java.nio.ByteBuffer b,
1087                                          int off, int len, ProtectionDomain pd,
1088                                          String source);
1089 
1090     // true if the name is null or has the potential to be a valid binary name
1091     private boolean checkName(String name) {
1092         if ((name == null) || (name.length() == 0))
1093             return true;
1094         if ((name.indexOf('/') != -1) || (name.charAt(0) == '['))
1095             return false;
1096         return true;
1097     }
1098 
1099     private void checkCerts(String name, CodeSource cs) {
1100         int i = name.lastIndexOf('.');
1101         String pname = (i == -1) ? "" : name.substring(0, i);
1102 
1103         Certificate[] certs = null;
1104         if (cs != null) {
1105             certs = cs.getCertificates();
1106         }




 966      * @throws  IndexOutOfBoundsException
 967      *          If either {@code off} or {@code len} is negative, or if
 968      *          {@code off+len} is greater than {@code b.length}.
 969      *
 970      * @throws  SecurityException
 971      *          If an attempt is made to add this class to a package that
 972      *          contains classes that were signed by a different set of
 973      *          certificates than this class, or if {@code name} begins with
 974      *          "{@code java.}" and this class loader is not the platform
 975      *          class loader or its ancestor.
 976      *
 977      * @revised 9
 978      * @spec JPMS
 979      */
 980     protected final Class<?> defineClass(String name, byte[] b, int off, int len,
 981                                          ProtectionDomain protectionDomain)
 982         throws ClassFormatError
 983     {
 984         protectionDomain = preDefineClass(name, protectionDomain);
 985         String source = defineClassSourceLocation(protectionDomain);
 986         Class<?> c = defineClass1(this, name, b, off, len, protectionDomain, source);
 987         postDefineClass(c, protectionDomain);
 988         return c;
 989     }
 990 
 991     /**
 992      * Converts a {@link java.nio.ByteBuffer ByteBuffer} into an instance
 993      * of class {@code Class}, with the given {@code ProtectionDomain}.
 994      * If the given {@code ProtectionDomain} is {@code null}, then a default
 995      * protection domain will be assigned to the class as
 996      * specified in the documentation for {@link #defineClass(String, byte[],
 997      * int, int)}.  Before the class can be used it must be resolved.
 998      *
 999      * <p>The rules about the first class defined in a package determining the
1000      * set of certificates for the package, the restrictions on class names,
1001      * and the defined package of the class
1002      * are identical to those specified in the documentation for {@link
1003      * #defineClass(String, byte[], int, int, ProtectionDomain)}.
1004      *
1005      * <p> An invocation of this method of the form
1006      * <i>cl</i>{@code .defineClass(}<i>name</i>{@code ,}


1058         throws ClassFormatError
1059     {
1060         int len = b.remaining();
1061 
1062         // Use byte[] if not a direct ByteBuffer:
1063         if (!b.isDirect()) {
1064             if (b.hasArray()) {
1065                 return defineClass(name, b.array(),
1066                                    b.position() + b.arrayOffset(), len,
1067                                    protectionDomain);
1068             } else {
1069                 // no array, or read-only array
1070                 byte[] tb = new byte[len];
1071                 b.get(tb);  // get bytes out of byte buffer.
1072                 return defineClass(name, tb, 0, len, protectionDomain);
1073             }
1074         }
1075 
1076         protectionDomain = preDefineClass(name, protectionDomain);
1077         String source = defineClassSourceLocation(protectionDomain);
1078         Class<?> c = defineClass2(this, name, b, b.position(), len, protectionDomain, source);
1079         postDefineClass(c, protectionDomain);
1080         return c;
1081     }
1082 
1083     static native Class<?> defineClass1(ClassLoader loader, String name, byte[] b, int off, int len,
1084                                         ProtectionDomain pd, String source);
1085 
1086     static native Class<?> defineClass2(ClassLoader loader, String name, java.nio.ByteBuffer b,
1087                                         int off, int len, ProtectionDomain pd,
1088                                         String source);
1089 
1090     // true if the name is null or has the potential to be a valid binary name
1091     private boolean checkName(String name) {
1092         if ((name == null) || (name.length() == 0))
1093             return true;
1094         if ((name.indexOf('/') != -1) || (name.charAt(0) == '['))
1095             return false;
1096         return true;
1097     }
1098 
1099     private void checkCerts(String name, CodeSource cs) {
1100         int i = name.lastIndexOf('.');
1101         String pname = (i == -1) ? "" : name.substring(0, i);
1102 
1103         Certificate[] certs = null;
1104         if (cs != null) {
1105             certs = cs.getCertificates();
1106         }


< prev index next >