src/share/classes/java/lang/Class.java

Print this page




 588      * <p>If this object
 589      * represents a primitive type or void, null is returned.
 590      *
 591      * @return  the class loader that loaded the class or interface
 592      *          represented by this object.
 593      * @throws SecurityException
 594      *    if a security manager exists and its
 595      *    {@code checkPermission} method denies
 596      *    access to the class loader for the class.
 597      * @see java.lang.ClassLoader
 598      * @see SecurityManager#checkPermission
 599      * @see java.lang.RuntimePermission
 600      */
 601     public ClassLoader getClassLoader() {
 602         ClassLoader cl = getClassLoader0();
 603         if (cl == null)
 604             return null;
 605         SecurityManager sm = System.getSecurityManager();
 606         if (sm != null) {
 607             ClassLoader ccl = ClassLoader.getCallerClassLoader();
 608             if (ccl != null && ccl != cl && !cl.isAncestor(ccl)) {
 609                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 610             }
 611         }
 612         return cl;
 613     }
 614 
 615     // Package-private to allow ClassLoader access
 616     native ClassLoader getClassLoader0();
 617 
 618 
 619     /**
 620      * Returns an array of {@code TypeVariable} objects that represent the
 621      * type variables declared by the generic declaration represented by this
 622      * {@code GenericDeclaration} object, in declaration order.  Returns an
 623      * array of length 0 if the underlying generic declaration declares no type
 624      * variables.
 625      *
 626      * @return an array of {@code TypeVariable} objects that represent
 627      *     the type variables declared by this generic declaration
 628      * @throws java.lang.reflect.GenericSignatureFormatError if the generic


2153      */
2154     static native Class<?> getPrimitiveClass(String name);
2155 
2156 
2157     /*
2158      * Check if client is allowed to access members.  If access is denied,
2159      * throw a SecurityException.
2160      *
2161      * Be very careful not to change the stack depth of this checkMemberAccess
2162      * call for security reasons.
2163      * See java.lang.SecurityManager.checkMemberAccess.
2164      *
2165      * <p> Default policy: allow all clients access with normal Java access
2166      * control.
2167      */
2168     private void checkMemberAccess(int which, ClassLoader ccl) {
2169         SecurityManager s = System.getSecurityManager();
2170         if (s != null) {
2171             s.checkMemberAccess(this, which);
2172             ClassLoader cl = getClassLoader0();
2173             if ((ccl != null) && (ccl != cl) &&
2174                   ((cl == null) || !cl.isAncestor(ccl))) {
2175                 String name = this.getName();
2176                 int i = name.lastIndexOf('.');
2177                 if (i != -1) {
2178                     s.checkPackageAccess(name.substring(0, i));
2179                 }
2180             }
2181         }
2182     }
2183 
2184     /**
2185      * Add a package name prefix if the name is not absolute Remove leading "/"
2186      * if name is absolute
2187      */
2188     private String resolveName(String name) {
2189         if (name == null) {
2190             return name;
2191         }
2192         if (!name.startsWith("/")) {
2193             Class<?> c = this;
2194             while (c.isArray()) {




 588      * <p>If this object
 589      * represents a primitive type or void, null is returned.
 590      *
 591      * @return  the class loader that loaded the class or interface
 592      *          represented by this object.
 593      * @throws SecurityException
 594      *    if a security manager exists and its
 595      *    {@code checkPermission} method denies
 596      *    access to the class loader for the class.
 597      * @see java.lang.ClassLoader
 598      * @see SecurityManager#checkPermission
 599      * @see java.lang.RuntimePermission
 600      */
 601     public ClassLoader getClassLoader() {
 602         ClassLoader cl = getClassLoader0();
 603         if (cl == null)
 604             return null;
 605         SecurityManager sm = System.getSecurityManager();
 606         if (sm != null) {
 607             ClassLoader ccl = ClassLoader.getCallerClassLoader();
 608             if (ClassLoader.needsClassLoaderPermissionCheck(ccl, cl)) {
 609                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 610             }
 611         }
 612         return cl;
 613     }
 614 
 615     // Package-private to allow ClassLoader access
 616     native ClassLoader getClassLoader0();
 617 
 618 
 619     /**
 620      * Returns an array of {@code TypeVariable} objects that represent the
 621      * type variables declared by the generic declaration represented by this
 622      * {@code GenericDeclaration} object, in declaration order.  Returns an
 623      * array of length 0 if the underlying generic declaration declares no type
 624      * variables.
 625      *
 626      * @return an array of {@code TypeVariable} objects that represent
 627      *     the type variables declared by this generic declaration
 628      * @throws java.lang.reflect.GenericSignatureFormatError if the generic


2153      */
2154     static native Class<?> getPrimitiveClass(String name);
2155 
2156 
2157     /*
2158      * Check if client is allowed to access members.  If access is denied,
2159      * throw a SecurityException.
2160      *
2161      * Be very careful not to change the stack depth of this checkMemberAccess
2162      * call for security reasons.
2163      * See java.lang.SecurityManager.checkMemberAccess.
2164      *
2165      * <p> Default policy: allow all clients access with normal Java access
2166      * control.
2167      */
2168     private void checkMemberAccess(int which, ClassLoader ccl) {
2169         SecurityManager s = System.getSecurityManager();
2170         if (s != null) {
2171             s.checkMemberAccess(this, which);
2172             ClassLoader cl = getClassLoader0();
2173             if (sun.reflect.misc.ReflectUtil.needsPackageAccessCheck(ccl, cl)) {

2174                 String name = this.getName();
2175                 int i = name.lastIndexOf('.');
2176                 if (i != -1) {
2177                     s.checkPackageAccess(name.substring(0, i));
2178                 }
2179             }
2180         }
2181     }
2182 
2183     /**
2184      * Add a package name prefix if the name is not absolute Remove leading "/"
2185      * if name is absolute
2186      */
2187     private String resolveName(String name) {
2188         if (name == null) {
2189             return name;
2190         }
2191         if (!name.startsWith("/")) {
2192             Class<?> c = this;
2193             while (c.isArray()) {