@634

リフレクションの利用(java.lang.reflect)

Advertisement

リフレクションとは

Javaのクラスより、メソッドやフィールドなどの情報を取得するための機能。リフレクションAPIが、標準APIの一部として提供されている(java.lang.reflect)。

基本となるクラス

java.lang.reflectパッケージの以下のクラスが基本。

クラス名用途
Constructorコンストラクタ
Fieldフィールド
Methodメソッド
Modifier修飾子
これらの情報をクラスから取得する。

サンプル

リフレクションの対象とするクラス
Target.java
public class Target {
	public Target(){}
	public Target(String param){}
	public Target(String param, int age){}

	public String name = null;
	public int age = 0;
	protected String sex = null;
	private String blood = null;

	public void setName(String name){
	}
	public static void getId(){
	}
	protected void print(){
		System.out.println(this.name);
	}
	private String getRank(){
		return "";
	}
}
コンストラクタ、メソッド、フィールドを適当に定義。

コンストラクタの取得

import java.lang.reflect.Constructor;

public class ClientConstructor {

    public static void main(String[] args) {
        Constructor[] constructor = Target.class.getConstructors();
        for(int i = 0; i < constructor.length; i++){
            System.out.print(constructor[i].getName());
            System.out.print("(");
            Class[] classes = constructor[i].getParameterTypes();
            for(int j = 0; j < classes.length; j++){
                System.out.print(classes[j].getName());
                if(j != classes.length - 1){
                    System.out.print(", ");
                }
            }
            System.out.println(")");
        }
    }
}
ClassクラスのメソッドgetConstructors()を利用して、Constructorクラスのインスタンスを取得する。

結果
bean.Target(java.lang.String)
bean.Target(java.lang.String, int)
bean.Target()

メソッドの取得

import java.lang.reflect.Method;

public class ClientMethod {
    public static void main(String[] args) {
        Method[] methods = Target.class.getMethods();
        for(int i = 0; i < methods.length; i++){
            System.out.println(methods[i].getName());
        }
    }
}
Methodクラスを利用する。

結果
getId
setName
hashCode
getClass
wait
wait
wait
equals
notify
notifyAll
toString

フィールドの取得

import java.lang.reflect.Field;

public class ClientField {
    public static void main(String[] args) {
        Field[] fields = Target.class.getFields();
        for(int i = 0; i < fields.length; i++){
            System.out.println(fields[i].getName());
        }
    }
}
Fieldクラスを利用。

結果
name
age

修飾子の取得

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ClientModifier {

    public static void main(String[] args) {
        Method[] methods = Target.class.getMethods();
        for(int i = 0; i < methods.length; i++){
            System.out.println(methods[i].getName());
            System.out.println("\tpublic:"  + Modifier.isPublic(methods[i].getModifiers()));
            System.out.println("\tfinal :"  + Modifier.isFinal(methods[i].getModifiers()));
            System.out.println("\tstatic:"  + Modifier.isStatic(methods[i].getModifiers()));
        }
    }
}
Modifierクラスのメソッドを利用することで、対象のアクセス修飾子を取得することができる。

結果
getId
    public:true
    final :false
    static:true
setName
    public:true
    final :false
    static:false
hashCode
    public:true
    final :false
    static:false
getClass
    public:true
    final :true
    static:false
wait
    public:true
    final :true
    static:false
wait
    public:true
    final :true
    static:false
wait
    public:true
    final :true
    static:false
equals
    public:true
    final :false
    static:false
notify
    public:true
    final :true
    static:false
notifyAll
    public:true
    final :true
    static:false
toString
    public:true
    final :false
    static:false

Advertisement

ショートカット

634
634ブログ
このカテゴリのトップページに戻る
Incubator(Pukiwiki)
634ラボ
   UIコレクションギャラリー
   ZO-3ジェネレーター

サイト検索


Y!ログール