倭マン's BLOG

くだらない日々の日記書いてます。 たまにプログラミング関連の記事書いてます。 書いてます。

いまさら!? Class クラス (3) : 文字列に関するメソッド

Java の Class クラスに定義されているメソッドを見ていくシリーズ(目次)。 さて、今回から本格的に Class クラスに定義されているメソッドを見ていきます。 今回は文字列に関するメソッド。 文字列からクラスを取得するメソッドは前回扱ったので(Class.forName() メソッド)、今回は文字列を返すメソッドを見ていきます。

String を返すメソッド

今回扱うメソッドは次の4つ。 返り値はどれも java.lang.String オブジェクト。

メソッド名 説明 Since
getName() この Class が表す型の名前を返す。 記法は JavaDoc などを参照。
getCanonicalName() Java 言語仕様に規定されたカノニカル名を返す。
カノニカル名がない場合は null を返す。
1.5
getSimpleName() ソースコードに記述された単純名(パッケージ名などを除外した名前)を返す。
単純名がない場合は空文字列を返す
1.5
toString() class/interface + " " + getName()

型別に分類

まずは型別に分類。 ここで扱っている型は、トップレベルクラス、前回扱った型、「java.lang.reflect.Proxy によって作ったプロキシクラス」です:

  • プリミティブ型
  • トップレベルクラス
  • メンバクラス
  • ローカルクラス
  • 無名クラス
  • インターフェース
  • アノテーション
  • 配列
  • 列挙型
  • プロキシクラス

プリミティブ型
まずはプリミティブ型。 ここでは int 型についての出力を載せています。

メソッド名 出力
getName() int
getCanonicalName() int
getSimpleName() int
toString() int

トップレベルクラス
次は パッケージ org.sample に定義されたトップレベルクラス MyClass。

package org.sample;

public class MyClass{...} 

出力結果は以下のようになります:

メソッド名 出力
getName() org.sample.MyClass
getCanonicalName() org.sample.MyClass
getSimpleName() MyClass
toString() class org.sample.MyClass

メンバクラス
上記の MyClass 内に定義されたメンバクラス MyMemberClass。

package org.sample;

public class MyClass{

    public static class MyMemberClass{...}
}

出力結果は以下のようになります:

メソッド名 出力
getName() org.sample.MyClass$MyMemberClass
getCanonicalName() org.sample.MyClass.MyMemberClass
getSimpleName() MyMemberClass
toString() class org.sample.MyClass$MyMemberClass

ローカルクラス
パッケージ org.sample 内の MyTest クラスのメソッド testClassNames() 内に定義されたローカルクラス MyLocalClass。

package org.sample;

public class MyTest{

    public void testClassNames(){
        class MyLocalClass{...}
    }
} 

出力結果は以下のようになります:

メソッド名 出力
getName() org.sample.MyTest$1MyLocalClass
getCanonicalName() null
getSimpleName() MyLocalClass
toString() class org.sample.MyTest$1MyLocalClass

無名クラス
パッケージ org.sample 内の MyTest クラスのメソッド testClassNames() 内に定義された無名クラス。

package org.sample;

public class MyTest{

    public void testClassNames(){
        MyInterface mine = new MyInterface(){};
        Class<?> type = mine.getClass();
    }
} 

出力結果は以下のようになります:

メソッド名 出力
getName() org.sample.MyTest$1
getCanonicalName() null
getSimpleName() (空文字列)
toString() class org.sample.MyTest$1

インターフェース
パッケージ org.sample 内に定義されたインターフェース MyInterface。

package org.sample;

public interface MyInterface{...}

出力結果は以下のようになります:

メソッド名 出力
getName() org.sample.MyInterface
getCanonicalName() org.sample.MyInterface
getSimpleName() MyInterface
toString() interface org.sample.MyInterface

アノテーション
パッケージ org.sample 内に定義されたアノテーション MyAnnotation。

package org.sample;

public @interface MyAnnotation{...}

出力結果は以下のようになります:

メソッド名 出力
getName() org.sample.MyAnnotation
getCanonicalName() org.sample.MyAnnotation
getSimpleName() MyAnnotation
toString() interface org.sample.MyAnnotation

配列
java.lang.String の配列 java.lang.String[]。 出力結果は以下のようになります:

メソッド名 出力
getName() [Ljava.lang.String;
getCanonicalName() java.lang.String[]
getSimpleName() String[]
toString() class [Ljava.lang.String;

列挙型
パッケージ org.sample 内に定義された列挙型 MyEnum。

package org.sample;

public enum MyEnum{...}

出力結果は以下のようになります:

メソッド名 出力
getName() org.sample.MyEnum
getCanonicalName() org.sample.MyEnum
getSimpleName() MyEnum
toString() class org.sample.MyEnum

プロキシクラス
org.sample.MyInterface に対して作られたプロキシクラス。

public class MyInvocationHandler implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {...}
}

InvocationHandler handler = new MyInvocationHandler();
MyInterface mine = (MyInterface)Proxy.newProxyInstance(
                                    MyInterface.class.getClassLoader(), 
                                    new Class[] { MyInterface.class }, 
                                    handler);
Class<?> type = mine.getClass();

出力結果は以下のようになります:

メソッド名 出力
getName() $Proxy0
getCanonicalName() $Proxy0
getSimpleName() $Proxy0
toString() class $Proxy0

メソッド別に分類

次は上記の結果をメソッド別に分類。 扱っている型は同じです(並べ替えただけ)。
getName() メソッド

出力
プリミティブ型 int
トップレベルクラス org.sample.MyClass
メンバクラス org.sample.MyClass$MyMemberClass
ローカルクラス org.sample.MyTest$1MyLocalClass
無名クラス org.sample.MyTest$1
インターフェース org.sample.MyInterface
アノテーション org.sample.MyAnnotation
配列 [Ljava.lang.String;
列挙型 org.sample.MyEnum
プロキシクラス $Proxy0

getCanonicalName() メソッド

出力
プリミティブ型 int
トップレベルクラス org.sample.MyClass
メンバクラス org.sample.MyClass.MyMemberClass
ローカルクラス (null)
無名クラス (null)
インターフェース org.sample.MyInterface
アノテーション org.sample.MyAnnotation
配列 java.lang.String[]
列挙型 org.sample.MyEnum
プロキシクラス $Proxy0

getSimpleName() メソッド

出力
プリミティブ型 int
トップレベルクラス MyClass
メンバクラス MyMemberClass
ローカルクラス MyLocalClass
無名クラス (空文字列)
インターフェース MyInterface
アノテーション MyAnnotation
配列 String[]
列挙型 MyEnum
プロキシクラス $Proxy0

toString() メソッド

出力
プリミティブ型 int
トップレベルクラス class org.sample.MyClass
メンバクラス class org.sample.MyClass$MyMemberClass
ローカルクラス class org.sample.MyTest$1MyLocalClass
無名クラス class org.sample.MyTest$1
インターフェース interface org.sample.MyInterface
アノテーション interface org.sample.MyAnnotation
配列 class [Ljava.lang.String;
列挙型 class org.sample.MyEnum
プロキシクラス class $Proxy0

Java言語仕様 第3版 (The Java Series)

Java言語仕様 第3版 (The Java Series)