JDKパッケージ1.8 java.langのClass

5793 ワード

Classクラスのインスタンスは、実行中のJavaアプリケーションのクラスとインタフェースを表します.各配列はクラスオブジェクトとしてマッピングされたクラスに属し、同じ要素タイプと緯度数を持つすべての配列がクラスオブジェクトを共有します.クラスオブジェクトは、クラスのロード時に仮想マシンによってクラスローダdefineClassメソッドによって自動的に構築されます.すべての位置決めメンテナンスのクラスとインタフェースは、Classのインスタンスです.Objectクラスメソッドは、返されるタイプを一意に導入します.基本データ型とvoidキーワードはクラスのインスタンスです.Final修飾は、不変クラスで継承できません.反射パケットjavaが実現する.lang.reflectの3つのインタフェース:AnnotatedElement、GenericDeclaration、Type.Typeはjava言語のすべてのタイプの共通の高度なインタフェースです.元のタイプ、パラメトリックタイプ、配列タイプ、タイプ変数、基本タイプ(1.5バージョンのみ).1.8リリースでは、このインタフェースにデフォルトのメソッドが追加されました.
package java.lang.reflect;

public interface Type {
    /**
     *           
     */
    default String getTypeName() {
        return toString();
    }
}
GenericDeclaration                 。                ,         ,       0。
    /**
     *   Class      ,       。        
     */
    @CallerSensitive
    public static Class> forName(String className)throws ClassNotFoundException {
        Class> caller = Reflection.getCallerClass();
        return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
    }


    /**
     *           Class        (    )
     *         
     */
    @CallerSensitive
    public static Class> forName(String name, boolean initialize,ClassLoaderloader)
throws ClassNotFoundException {
        Class> caller = null;
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            caller = Reflection.getCallerClass();
            if (sun.misc.VM.isSystemDomainLoader(loader)) {
                ClassLoader ccl = ClassLoader.getClassLoader(caller);
                if (!sun.misc.VM.isSystemDomainLoader(ccl)) {
                    sm.checkPermission(
                        SecurityConstants.GET_CLASSLOADER_PERMISSION);
                }
            }
        }
        return forName0(name, initialize, loader, caller);
    }


    private static native Class> forName0(String name, boolean initialize,
                                            ClassLoader loader,
                                            Class> caller)
        throws ClassNotFoundException;
    @CallerSensitive
    public T newInstance() throws InstantiationException, IllegalAccessException {
        if (System.getSecurityManager() != null) {
            checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false);
        }

        if (cachedConstructor == null) {
            if (this == Class.class) {
                throw new IllegalAccessException(
                    "Can not call newInstance() on the Class for java.lang.Class"
                );
            }
            try {
                Class>[] empty = {};
                final Constructor c = getConstructor0(empty, Member.DECLARED);
                java.security.AccessController.doPrivileged(
                    new java.security.PrivilegedAction() {
                        public Void run() {
                                c.setAccessible(true);
                                return null;
                            }
                        });
                cachedConstructor = c;
            } catch (NoSuchMethodException e) {
                throw (InstantiationException)
                    new InstantiationException(getName()).initCause(e);
            }
        }
        Constructor tmpConstructor = cachedConstructor;
        int modifiers = tmpConstructor.getModifiers();
        if (!Reflection.quickCheckMemberAccess(this, modifiers)) {
            Class> caller = Reflection.getCallerClass();
            if (newInstanceCallerCache != caller) {
                Reflection.ensureMemberAccess(caller, this, null, modifiers);
                newInstanceCallerCache = caller;
            }
        }
        // Run constructor
        try {
            return tmpConstructor.newInstance((Object[])null);
        } catch (InvocationTargetException e) {
            Unsafe.getUnsafe().throwException(e.getTargetException());
            // Not reached
            return null;
        }
    }

このClassオブジェクトが表すクラスの新しいインスタンスを作成します.クラスは、空のパラメータリストを持つnew式でインスタンス化されます.クラスがまだ初期化されていない場合は、このクラスを初期化します.
    public String getName() {
        String name = this.name;
        if (name == null)
            this.name = name = getName0();
        return name;
    }

    private transient String name;
    private native String getName0();

クラスオブジェクトインスタンスが示すクラスまたはインタフェース名(フルパス名)を返します.
    @CallerSensitive
    public ClassLoader getClassLoader() {
        ClassLoader cl = getClassLoader0();
        if (cl == null)
            return null;
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
        }
        return cl;
    }

    ClassLoader getClassLoader0() { return classLoader; }

    private final ClassLoader classLoader;

このクラスのローダを返し、ローダクラスがnullを返すように導く.基本タイプまたはvoidはNullを返します.
    public Package getPackage() {
        return Package.getPackage(this);
    }

このクラスのパッケージ名を返します.
    public Class>[] getInterfaces() {
        ReflectionData rd = reflectionData();
        if (rd == null) {
            return getInterfaces0();
        } else {
            Class>[] interfaces = rd.interfaces;
            if (interfaces == null) {
                interfaces = getInterfaces0();
                rd.interfaces = interfaces;
            }
            return interfaces.clone();
        }
    }
private native Class>[] getInterfaces0();

このインスタンスオブジェクトが表すクラスまたはインタフェースが実装するインタフェースを返します.