■リフレクションAPI -- メソッド、フィールドの処理■
戻る
::::::::::::::
Customer.java
::::::::::::::
/**
$Id: reflection.html,v 1.1 2009/06/22 16:12:22 kishi Exp kishi $
*/
public class Customer {
private String name;
private int age;
private Customer.Gender gender;
public enum Gender {MALE, FEMALE, UNKNOWN}
private double income;
public void setName( String name ) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge( int age ) {
this.age = age;
}
public int getAge() {
return age;
}
public void setGender( Customer.Gender gender ) {
this.gender = gender;
}
public Customer.Gender getGender() {
return gender;
}
public void setIncome( double income ) {
this.income = income;
}
public double getIncome() {
return income;
}
}
::::::::::::::
ReflectionTest.java
::::::::::::::
import java.lang.reflect.*;
/**
$Id: reflection.html,v 1.1 2009/06/22 16:12:22 kishi Exp kishi $
*/
public class ReflectionTest {
static public void main( String[] args ) {
Customer customer = new Customer();
customer.setName( "岸 康弘" );
customer.setAge( 39 ); // うそデース!
customer.setGender( Customer.Gender.MALE );
printReturnValueOfParticularMethod( customer, "getName" );
printReturnValueOfParticularMethod( customer, "getAge" );
printReturnValueOfParticularMethod( customer, "getGender" );
}
static public void printReturnValueOfParticularMethod( Object instance, String methodName ) {
Class clazz = instance.getClass();
System.out.print( "クラス名: " + clazz.getName() );
Method method = null;
try {
method = clazz.getMethod( methodName, new Class[ 0 ] );
} catch ( Exception e ) {
e.printStackTrace();
}
Object ret = null;
try {
ret = method.invoke( instance, new Object[ 0 ] );
} catch ( Exception e ) {
e.printStackTrace();
}
System.out.print( "\tメソッド名: " + methodName );
System.out.print( "\t戻り値: " + ret );
System.out.println();
}
}
::::::::::::::
ReflectionTest2.java
::::::::::::::
import java.lang.reflect.*;
/**
$Id: reflection.html,v 1.1 2009/06/22 16:12:22 kishi Exp kishi $
*/
public class ReflectionTest2 {
public static String toString( Object instance ) {
StringBuilder sb = new StringBuilder();
Class clazz = instance.getClass();
sb.append( "クラス名: " + clazz.getName() );
//privateメソッド、フィールドの取得にはgetDeclared...
//を使います。
Field[] fields = clazz.getDeclaredFields();
for ( int i = 0; i < fields.length;i++ ) {
Field field = fields[ i ];
//============================================================
//privateフィールドの取得には、accessibleフラグを
//trueにする必要があります。
//============================================================
field.setAccessible( true );
String name = field.getName();
Object value = null;
try {
value = field.get( instance );
} catch ( IllegalAccessException e ) {
value = "*";
}
sb.append( "\n" + name + ": " + value );
}
return sb.toString();
}
public static void main( String[] args ) {
Customer customer = new Customer();
customer.setName( "岸 康弘" );
customer.setAge( 39 ); // うそデース!
customer.setGender( Customer.Gender.MALE );
customer.setIncome( 20000000 );
String s = ReflectionTest2.toString( customer );
System.out.println( s );
}
}
::::::::::::::
result.txt
::::::::::::::
■実行結果
$ java ReflectionTest
クラス名: Customer メソッド名: getName 戻り値: 岸 康弘
クラス名: Customer メソッド名: getAge 戻り値: 39
クラス名: Customer メソッド名: getGender 戻り値: MALE
$ java ReflectionTest2
クラス名: Customer
name: 岸 康弘
age: 39
gender: MALE
income: 2.0E7
戻る