304 * {@code Class.forName(className, true, currentLoader)}
305 * </blockquote>
306 *
307 * where {@code currentLoader} denotes the defining class loader of
308 * the current class.
309 *
310 * <p> For example, the following code fragment returns the
311 * runtime {@code Class} descriptor for the class named
312 * {@code java.lang.Thread}:
313 *
314 * <blockquote>
315 * {@code Class t = Class.forName("java.lang.Thread")}
316 * </blockquote>
317 * <p>
318 * A call to {@code forName("X")} causes the class named
319 * {@code X} to be initialized.
320 *
321 * @param className the fully qualified name of the desired class.
322 * @return the {@code Class} object for the class with the
323 * specified name.
324 * @exception LinkageError if the linkage fails
325 * @exception ExceptionInInitializerError if the initialization provoked
326 * by this method fails
327 * @exception ClassNotFoundException if the class cannot be located
328 */
329 @CallerSensitive
330 public static Class<?> forName(String className)
331 throws ClassNotFoundException {
332 Class<?> caller = Reflection.getCallerClass();
333 return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
334 }
335
336
337 /**
338 * Returns the {@code Class} object associated with the class or
339 * interface with the given string name, using the given class loader.
340 * Given the fully qualified name for a class or interface (in the same
341 * format returned by {@code getName}) this method attempts to
342 * locate, load, and link the class or interface. The specified class
343 * loader is used to load the class or interface. If the parameter
344 * {@code loader} is null, the class is loaded through the bootstrap
345 * class loader. The class is initialized only if the
346 * {@code initialize} parameter is {@code true} and if it has
347 * not been initialized earlier.
362 * </blockquote>
363 *
364 * is equivalent to:
365 *
366 * <blockquote>
367 * {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
368 * </blockquote>
369 *
370 * Note that this method throws errors related to loading, linking or
371 * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
372 * Java Language Specification</em>.
373 * Note that this method does not check whether the requested class
374 * is accessible to its caller.
375 *
376 * @param name fully qualified name of the desired class
377 * @param initialize if {@code true} the class will be initialized.
378 * See Section 12.4 of <em>The Java Language Specification</em>.
379 * @param loader class loader from which the class must be loaded
380 * @return class object representing the desired class
381 *
382 * @exception LinkageError if the linkage fails
383 * @exception ExceptionInInitializerError if the initialization provoked
384 * by this method fails
385 * @exception ClassNotFoundException if the class cannot be located by
386 * the specified class loader
387 * @exception SecurityException
388 * if a security manager is present, and the {@code loader} is
389 * {@code null}, and the caller's class loader is not
390 * {@code null}, and the caller does not have the
391 * {@link RuntimePermission}{@code ("getClassLoader")}
392 *
393 * @see java.lang.Class#forName(String)
394 * @see java.lang.ClassLoader
395 *
396 * @jls 12.2 Loading of Classes and Interfaces
397 * @jls 12.3 Linking of Classes and Interfaces
398 * @jls 12.4 Initialization of Classes and Interfaces
399 * @since 1.2
400 */
401 @CallerSensitive
402 public static Class<?> forName(String name, boolean initialize,
403 ClassLoader loader)
404 throws ClassNotFoundException
405 {
406 Class<?> caller = null;
407 SecurityManager sm = System.getSecurityManager();
653 /**
654 * Determines if the class or interface represented by this
655 * {@code Class} object is either the same as, or is a superclass or
656 * superinterface of, the class or interface represented by the specified
657 * {@code Class} parameter. It returns {@code true} if so;
658 * otherwise it returns {@code false}. If this {@code Class}
659 * object represents a primitive type, this method returns
660 * {@code true} if the specified {@code Class} parameter is
661 * exactly this {@code Class} object; otherwise it returns
662 * {@code false}.
663 *
664 * <p> Specifically, this method tests whether the type represented by the
665 * specified {@code Class} parameter can be converted to the type
666 * represented by this {@code Class} object via an identity conversion
667 * or via a widening reference conversion. See <em>The Java Language
668 * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
669 *
670 * @param cls the {@code Class} object to be checked
671 * @return the {@code boolean} value indicating whether objects of the
672 * type {@code cls} can be assigned to objects of this class
673 * @exception NullPointerException if the specified Class parameter is
674 * null.
675 * @since 1.1
676 */
677 @HotSpotIntrinsicCandidate
678 public native boolean isAssignableFrom(Class<?> cls);
679
680
681 /**
682 * Determines if the specified {@code Class} object represents an
683 * interface type.
684 *
685 * @return {@code true} if this object represents an interface;
686 * {@code false} otherwise.
687 */
688 @HotSpotIntrinsicCandidate
689 public native boolean isInterface();
690
691
692 /**
693 * Determines if this {@code Class} object represents an array class.
1495 final Class<?> candidate = getDeclaringClass0();
1496
1497 if (candidate != null) {
1498 SecurityManager sm = System.getSecurityManager();
1499 if (sm != null) {
1500 candidate.checkPackageAccess(sm,
1501 ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
1502 }
1503 }
1504 return candidate;
1505 }
1506
1507 private native Class<?> getDeclaringClass0();
1508
1509
1510 /**
1511 * Returns the immediately enclosing class of the underlying
1512 * class. If the underlying class is a top level class this
1513 * method returns {@code null}.
1514 * @return the immediately enclosing class of the underlying class
1515 * @exception SecurityException
1516 * If a security manager, <i>s</i>, is present and the caller's
1517 * class loader is not the same as or an ancestor of the class
1518 * loader for the enclosing class and invocation of {@link
1519 * SecurityManager#checkPackageAccess s.checkPackageAccess()}
1520 * denies access to the package of the enclosing class
1521 * @since 1.5
1522 */
1523 @CallerSensitive
1524 public Class<?> getEnclosingClass() throws SecurityException {
1525 // There are five kinds of classes (or interfaces):
1526 // a) Top level classes
1527 // b) Nested classes (static member classes)
1528 // c) Inner classes (non-static member classes)
1529 // d) Local classes (named classes declared within a method)
1530 // e) Anonymous classes
1531
1532
1533 // JVM Spec 4.7.7: A class must have an EnclosingMethod
1534 // attribute if and only if it is a local class or an
1535 // anonymous class.
|
304 * {@code Class.forName(className, true, currentLoader)}
305 * </blockquote>
306 *
307 * where {@code currentLoader} denotes the defining class loader of
308 * the current class.
309 *
310 * <p> For example, the following code fragment returns the
311 * runtime {@code Class} descriptor for the class named
312 * {@code java.lang.Thread}:
313 *
314 * <blockquote>
315 * {@code Class t = Class.forName("java.lang.Thread")}
316 * </blockquote>
317 * <p>
318 * A call to {@code forName("X")} causes the class named
319 * {@code X} to be initialized.
320 *
321 * @param className the fully qualified name of the desired class.
322 * @return the {@code Class} object for the class with the
323 * specified name.
324 * @throws LinkageError if the linkage fails
325 * @throws ExceptionInInitializerError if the initialization provoked
326 * by this method fails
327 * @throws ClassNotFoundException if the class cannot be located
328 */
329 @CallerSensitive
330 public static Class<?> forName(String className)
331 throws ClassNotFoundException {
332 Class<?> caller = Reflection.getCallerClass();
333 return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
334 }
335
336
337 /**
338 * Returns the {@code Class} object associated with the class or
339 * interface with the given string name, using the given class loader.
340 * Given the fully qualified name for a class or interface (in the same
341 * format returned by {@code getName}) this method attempts to
342 * locate, load, and link the class or interface. The specified class
343 * loader is used to load the class or interface. If the parameter
344 * {@code loader} is null, the class is loaded through the bootstrap
345 * class loader. The class is initialized only if the
346 * {@code initialize} parameter is {@code true} and if it has
347 * not been initialized earlier.
362 * </blockquote>
363 *
364 * is equivalent to:
365 *
366 * <blockquote>
367 * {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
368 * </blockquote>
369 *
370 * Note that this method throws errors related to loading, linking or
371 * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
372 * Java Language Specification</em>.
373 * Note that this method does not check whether the requested class
374 * is accessible to its caller.
375 *
376 * @param name fully qualified name of the desired class
377 * @param initialize if {@code true} the class will be initialized.
378 * See Section 12.4 of <em>The Java Language Specification</em>.
379 * @param loader class loader from which the class must be loaded
380 * @return class object representing the desired class
381 *
382 * @throws LinkageError if the linkage fails
383 * @throws ExceptionInInitializerError if the initialization provoked
384 * by this method fails
385 * @throws ClassNotFoundException if the class cannot be located by
386 * the specified class loader
387 * @throws SecurityException
388 * if a security manager is present, and the {@code loader} is
389 * {@code null}, and the caller's class loader is not
390 * {@code null}, and the caller does not have the
391 * {@link RuntimePermission}{@code ("getClassLoader")}
392 *
393 * @see java.lang.Class#forName(String)
394 * @see java.lang.ClassLoader
395 *
396 * @jls 12.2 Loading of Classes and Interfaces
397 * @jls 12.3 Linking of Classes and Interfaces
398 * @jls 12.4 Initialization of Classes and Interfaces
399 * @since 1.2
400 */
401 @CallerSensitive
402 public static Class<?> forName(String name, boolean initialize,
403 ClassLoader loader)
404 throws ClassNotFoundException
405 {
406 Class<?> caller = null;
407 SecurityManager sm = System.getSecurityManager();
653 /**
654 * Determines if the class or interface represented by this
655 * {@code Class} object is either the same as, or is a superclass or
656 * superinterface of, the class or interface represented by the specified
657 * {@code Class} parameter. It returns {@code true} if so;
658 * otherwise it returns {@code false}. If this {@code Class}
659 * object represents a primitive type, this method returns
660 * {@code true} if the specified {@code Class} parameter is
661 * exactly this {@code Class} object; otherwise it returns
662 * {@code false}.
663 *
664 * <p> Specifically, this method tests whether the type represented by the
665 * specified {@code Class} parameter can be converted to the type
666 * represented by this {@code Class} object via an identity conversion
667 * or via a widening reference conversion. See <em>The Java Language
668 * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
669 *
670 * @param cls the {@code Class} object to be checked
671 * @return the {@code boolean} value indicating whether objects of the
672 * type {@code cls} can be assigned to objects of this class
673 * @throws NullPointerException if the specified Class parameter is
674 * null.
675 * @since 1.1
676 */
677 @HotSpotIntrinsicCandidate
678 public native boolean isAssignableFrom(Class<?> cls);
679
680
681 /**
682 * Determines if the specified {@code Class} object represents an
683 * interface type.
684 *
685 * @return {@code true} if this object represents an interface;
686 * {@code false} otherwise.
687 */
688 @HotSpotIntrinsicCandidate
689 public native boolean isInterface();
690
691
692 /**
693 * Determines if this {@code Class} object represents an array class.
1495 final Class<?> candidate = getDeclaringClass0();
1496
1497 if (candidate != null) {
1498 SecurityManager sm = System.getSecurityManager();
1499 if (sm != null) {
1500 candidate.checkPackageAccess(sm,
1501 ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
1502 }
1503 }
1504 return candidate;
1505 }
1506
1507 private native Class<?> getDeclaringClass0();
1508
1509
1510 /**
1511 * Returns the immediately enclosing class of the underlying
1512 * class. If the underlying class is a top level class this
1513 * method returns {@code null}.
1514 * @return the immediately enclosing class of the underlying class
1515 * @throws SecurityException
1516 * If a security manager, <i>s</i>, is present and the caller's
1517 * class loader is not the same as or an ancestor of the class
1518 * loader for the enclosing class and invocation of {@link
1519 * SecurityManager#checkPackageAccess s.checkPackageAccess()}
1520 * denies access to the package of the enclosing class
1521 * @since 1.5
1522 */
1523 @CallerSensitive
1524 public Class<?> getEnclosingClass() throws SecurityException {
1525 // There are five kinds of classes (or interfaces):
1526 // a) Top level classes
1527 // b) Nested classes (static member classes)
1528 // c) Inner classes (non-static member classes)
1529 // d) Local classes (named classes declared within a method)
1530 // e) Anonymous classes
1531
1532
1533 // JVM Spec 4.7.7: A class must have an EnclosingMethod
1534 // attribute if and only if it is a local class or an
1535 // anonymous class.
|