「Java」Field.getType()とField.getModifiers()メソッドでフィールドの宣言タイプと修飾子を取得
説明
1.getModifiers()
Memberが識別したメンバーやコンストラクタのためのJava言語修飾子を整数型で返します。
2.getType()
Fieldオブジェクトで表されるフィールドの宣言タイプを識別するClassオブジェクトを返します。
■ クラスの定義
package com.arkgame.study.java;
import java.lang.reflect.Field;
public class FieldDemo {
// parameter definition
public String username = "UserA001";
protected Integer age = 32;
public Boolean canAcess= false;
// constructor definition
public FieldDemo(String username, Integer age, Boolean canAccess) {
this.username = username;
this.age = age;
this.canAcess = canAccess;
}
// method definition
public String getFieldName() throws IllegalArgumentException, IllegalAccessException {
StringBuilder sb = new StringBuilder();
// Fieldオブジェクトの配列
Field[] cftFd = this.getClass().getDeclaredFields();
for (Field fd : cftFd) {
// フィールドの名前 フィールドの値
sb.append("type: " + fd.getType() + " modifiers: " + fd.getModifiers() + " フィールド名: " + fd.getName() + " 値: "
+ fd.get(this) + "\n");
}
return sb.toString();
}
}
package com.arkgame.study.java;
import java.lang.reflect.Field;
public class FieldDemo {
// parameter definition
public String username = "UserA001";
protected Integer age = 32;
public Boolean canAcess= false;
// constructor definition
public FieldDemo(String username, Integer age, Boolean canAccess) {
this.username = username;
this.age = age;
this.canAcess = canAccess;
}
// method definition
public String getFieldName() throws IllegalArgumentException, IllegalAccessException {
StringBuilder sb = new StringBuilder();
// Fieldオブジェクトの配列
Field[] cftFd = this.getClass().getDeclaredFields();
for (Field fd : cftFd) {
// フィールドの名前 フィールドの値
sb.append("type: " + fd.getType() + " modifiers: " + fd.getModifiers() + " フィールド名: " + fd.getName() + " 値: "
+ fd.get(this) + "\n");
}
return sb.toString();
}
}
package com.arkgame.study.java; import java.lang.reflect.Field; public class FieldDemo { // parameter definition public String username = "UserA001"; protected Integer age = 32; public Boolean canAcess= false; // constructor definition public FieldDemo(String username, Integer age, Boolean canAccess) { this.username = username; this.age = age; this.canAcess = canAccess; } // method definition public String getFieldName() throws IllegalArgumentException, IllegalAccessException { StringBuilder sb = new StringBuilder(); // Fieldオブジェクトの配列 Field[] cftFd = this.getClass().getDeclaredFields(); for (Field fd : cftFd) { // フィールドの名前 フィールドの値 sb.append("type: " + fd.getType() + " modifiers: " + fd.getModifiers() + " フィールド名: " + fd.getName() + " 値: " + fd.get(this) + "\n"); } return sb.toString(); } }
■実行確認クラス
package com.arkgame.study.java;
public class FieldGet {
public static void main(String[] args)
throws IllegalArgumentException, IllegalAccessException {
// FieldDemo オブジェクト宣言
FieldDemo fieldDemoObj = new FieldDemo("user001", 30, true);
String fieldName;
// getFieldNameメソッドを呼び出す
fieldName = fieldDemoObj.getFieldName();
System.out.println("クラスの全てフィールドの宣言タイプ、修飾子、名前、値:\n" + fieldName);
}
}
package com.arkgame.study.java;
public class FieldGet {
public static void main(String[] args)
throws IllegalArgumentException, IllegalAccessException {
// FieldDemo オブジェクト宣言
FieldDemo fieldDemoObj = new FieldDemo("user001", 30, true);
String fieldName;
// getFieldNameメソッドを呼び出す
fieldName = fieldDemoObj.getFieldName();
System.out.println("クラスの全てフィールドの宣言タイプ、修飾子、名前、値:\n" + fieldName);
}
}
package com.arkgame.study.java; public class FieldGet { public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException { // FieldDemo オブジェクト宣言 FieldDemo fieldDemoObj = new FieldDemo("user001", 30, true); String fieldName; // getFieldNameメソッドを呼び出す fieldName = fieldDemoObj.getFieldName(); System.out.println("クラスの全てフィールドの宣言タイプ、修飾子、名前、値:\n" + fieldName); } }
■実行結果
クラスの全てフィールドの宣言タイプ、修飾子、名前、値:
type: class java.lang.String modifiers: 1 フィールド名: username 値: user001
type: class java.lang.Integer modifiers: 4 フィールド名: age 値: 30
type: class java.lang.Boolean modifiers: 1 フィールド名: canAcess 値: true