type information - The cast() Method

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/wangbingfengf98/article/details/101849943
    /**
     * Casts an object to the class or interface represented
     * by this {@code Class} object.
     *
     * @param obj the object to be cast
     * @return the object after casting, or null if obj is null
     *
     * @throws ClassCastException if the object is not
     * null and is not assignable to the type T.
     *
     * @since 1.5
     */
    @SuppressWarnings("unchecked")
    public T cast(Object obj) {
        if (obj != null && !isInstance(obj))
            throw new ClassCastException(cannotCastMsg(obj));
        return (T) obj;
    }
// typeinfo/ClassCasts.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

class Building {}

class House extends Building {}

public class ClassCasts {
  public static void main(String[] args) {
    Building b = new House();
    Class<House> houseType = House.class;
    House h = houseType.cast(b);
    h = (House) b; // ... or just do this.
  }
}

This turns out to be a rare thing—I(Bruce Eckel) found only one instance where cast() was used in the entire Java library (it was in com.sun.mirror.util.DeclarationFilter ). Another feature had no usage in the Java library: Class.asSubclass() . This casts the class object to a more specific type.

All components of this API have been superseded by the standardized annotation processing API. The replacement for the functionality of this class is ElementFilter.


    /**

     * Casts this {@code Class} object to represent a subclass of the class

     * represented by the specified class object.  Checks that the cast

     * is valid, and throws a {@code ClassCastException} if it is not.  If

     * this method succeeds, it always returns a reference to this class object.

     *

     * <p>This method is useful when a client needs to "narrow" the type of

     * a {@code Class} object to pass it to an API that restricts the

     * {@code Class} objects that it is willing to accept.  A cast would

     * generate a compile-time warning, as the correctness of the cast

     * could not be checked at runtime (because generic types are implemented

     * by erasure).

     *

     * @param <U> the type to cast this class object to

     * @param clazz the class of the type to cast this class object to

     * @return this {@code Class} object, cast to represent a subclass of

     *    the specified class object.

     * @throws ClassCastException if this {@code Class} object does not

     *    represent a subclass of the specified class (here "subclass" includes

     *    the class itself).

     * @since 1.5

     */

    @SuppressWarnings("unchecked")

    public <U> Class<? extends U> asSubclass(Class<U> clazz) {

        if (clazz.isAssignableFrom(this))

            return (Class<? extends U>) this;

        else

            throw new ClassCastException(this.toString());

    }

references:

1. On Java 8 - Bruce Eckel

2.https://github.com/wangbingfeng/OnJava8-Examples/blob/master/typeinfo/ClassCasts.java

3. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/Class.java

4. https://docs.oracle.com/javase/7/docs/jdk/api/apt/mirror/com/sun/mirror/util/DeclarationFilter.html

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/101849943