Java�ł́A�N���X������\�b�h�Ăяo�����\�[�X��ɒ��ڏ����ăR���p�C�����Ɍ��肳��邾���łȂ��A������i�N���X���j���g���ăN���X��������A ���\�b�h���̕�������g���ă��\�b�h���Ăяo�����肷�邱�Ƃ��o����B
|
|
Java�ł́A�N���X�������ׂ�java.lang.Class�Ƃ����N���X���p�ӂ���Ă���B
�ijava.lang�p�b�P�[�W�Ȃ̂��Aimport�͕s�v�j
�ǂ�ȃN���X�ł��A�u�N���X��.class�v�Ƃ�������Class�C���X�^���X���擾���邱�Ƃ��o����B
| JDK1.4�ȑO |
Class clazz = �N���X��.class;
|
Class strClass = String.class; Class intClass = int.class; //�v���~�e�B�u�^��OK! Class arrClass = String[].class; //�z����OK Class voidClass= void.class; //void�������� |
| JDK1.5�ȍ~ |
Class<�N���X��> clazz = �N���X��.class; |
Class<String> strClass = String.class;
Class<Integer> intClass = int.class;
Class<String[]> arrClass = String[].class;
Class<
|
//���̂悤�ȏ��������� Class<? extends �N���X��> clazz = �N���X��.class; |
Class<? extends String> strClass = String.class;
Class<? extends Integer> intClass = int.class;
Class<? extends String[]> arrClass = String[].class;
Class<? extends
|
�܂��A�ǂ�ȃC���X�^���X�ł��AObject#getClass()���g����Class�C���X�^���X���擾�ł���B
| JDK1.4�ȑO |
�N���X obj = �`;
Class clazz = obj.getClass();
|
�@ |
| JDK1.5�ȍ~ |
�N���X obj = �`; Class<? extends �N���X> clazz = obj.getClass(); |
getClass()�̖߂�^�́A�����W�F�l���N�X�̎g�����Ƃ��Ă�������Ɠ����B |
�����āA�C�ӂ��N���X���̕���������Class�C���X�^���X���擾����ɂ́A�ȉ��̂悤�ɂ����B
�i�N���X����FQCN�i�p�b�P�[�W��.�N���X���j�Ŏw�肷��B
�f�t�H���g�p�b�P�[�W�̃N���X�́A�p�b�P�[�W�������ł��̂܂܃N���X�����L�q����悢�B[2010-01-15]�j
| JDK1.4�ȑO |
Class clazz = Class.forName("�N���X��"); |
Class clazz;
try {
clazz = Class.forName("java.lang.String");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
|
| JDK1.5�ȍ~ [/2015-04-26] |
Class<?> clazz = Class.forName("�N���X��"); |
Class<?> clazz;
try {
clazz = Class.forName("java.lang.String");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
|
@SuppressWarnings("unchecked")
Class<�N���X> clazz = (Class<�N���X>)Class.forName("�N���X��");
|
Class<String> clazz = getClassForName("java.lang.String"); @SuppressWarnings("unchecked")
public static <T> Class<T> getClassForName(String className) {
try {
return (Class<T>) Class.forName(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} |
|
Class<? extends �N���X> clazz = Class.forName("�N���X��") .asSubclass(�N���X.class); |
Class<? extends String> clazz; try { clazz = Class.forName("java.lang.String").asSubclass(String.class); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } |
�N���X���[�_�[���g���ĔC�ӂ��N���X���̕���������Class�C���X�^���X���擾�����B[2010-01-15]
// �N���X���[�_�[�̎擾��
ClassLoader loader = Thread.currentThread().getContextClassLoader();
ClassLoader loader = ClassLoader.getSystemClassLoader()
ClassLoader loader = URLClassLoader.newInstance(new URL[]{ new File("jar�t�@�C��").toURI().toURL() });
| JDK1.4�ȑO |
Class clazz = loader.loadClass("�N���X��"); |
Class clazz;
try {
clazz = loader.loadClass("java.lang.String");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
|
| JDK1.5�ȍ~ |
Class<?> clazz = loader.loadClass("�N���X��"); |
Class<?> clazz;
try {
clazz = loader.loadClass("java.lang.String");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
|
���߂āuClass�C���X�^���X���擾�v�����ۂɂ́A�u�N���X�����[�h�v�����B[2010-01-15]
���Ȃ킿�A���̃N���X��static�t�B�[���h���������i������ÓI�������q�̎��s�j���s����B
java.lang.Class�ɂ́A�N���X�̏����������\�b�h���F�X����B
| ���\�b�h�� | �߂�^ | ���� | �X�V�� |
|---|---|---|---|
getPackage() |
Package | �p�b�P�[�W��Ԃ��B | �@ |
getPackageName() |
String | �p�b�P�[�W����Ԃ��BJava9�ȍ~ | 2020-09-29 |
getName() |
String | �N���X���i�p�b�P�[�W�t���B�܂����薼�iFQN�j�j��Ԃ��B | 2007-03-02 |
getSimpleName() |
String | �N���X���i�p�b�P�[�W�����B�܂��P�����j��Ԃ��BJDK1.5�ȍ~ | 2007-03-02 |
getCanonicalName() |
String | �N���X���i�������j��Ԃ��BJDK1.5�ȍ~ | 2008-09-13 |
getTypeName() |
String | �N���X����Ԃ��B�z��̂Ƃ��ȊO��getName()�Ɠ����BJDK1.8�ȍ~ | 2014-03-19 |
asSubclass(Class<U>
clazz) |
Class<? extends U> | U�̃N���X�iClass<? extends U>�j��Ԃ��BJDK1.5�ȍ~�ێ����Ă���N���X��U�ɃL���X�g�ł��Ȃ�����ClassCastException����������B |
2015-04-26 |
getSuperclass() |
Class | �e�N���X��Ԃ��B | 2014-03-19 |
getAnnotatedSuperclass() |
AnnotatedType | JDK1.8�ȍ~ | 2014-03-19 |
getInterfaces() |
Class[] | ���������ڎ������Ă���C���^�[�t�F�[�X��Ԃ��B �i�X�[�p�[�N���X�Ŏ������Ă���C���^�[�t�F�[�X�͑ΏۊO�j |
�@ |
getAnnotatedInterfaces() |
AnnotatedType[] | JDK1.8�ȍ~ | 2014-03-19 |
getConstructors() |
Constructor[] | public�ȃR���X�g���N�^�[��Ԃ��B | �@ |
getConstructor(�����̌^�̔z��) |
Constructor | ��������R���X�g���N�^�[��Ԃ��B�i�����̎�ނ����v����public�Ȃ��́j | �@ |
getDeclaredFields() |
Field[] | ���������ڐ錾���Ă���S�Ă̕ϐ���Ԃ��B | �@ |
getFields() |
Field[] | ������������public�ȕϐ���Ԃ��B�i�X�[�p�[�N���X�̕����܂ށj | �@ |
getField("�t�B�[���h��") |
Field | �t�B�[���h��Ԃ��B | �@ |
getDeclaredField("�t�B�[���h��") |
Field | �t�B�[���h��Ԃ��B�iprivate�ȃt�B�[���h�����A�N�Z�X���@�j | 2007-09-10 |
getRecordComponents() |
RecordComponent[] | ���R�[�h�R���|�[�l���g�̈ꗗ��Ԃ��B | 2021-03-21 |
getDeclaredMethods() |
Method[] | ���������ڐ錾���Ă���S�Ẵ��\�b�h��Ԃ��B | �@ |
getMethods() |
Method[] | ������������public�ȃ��\�b�h��Ԃ��B�i�X�[�p�[�N���X�̕����܂ށj | 2011-09-22 |
getMethod("���\�b�h��", �����̌^�̔z��) |
Method | ���\�b�h��Ԃ��B�i�����̎�ނ����v����public�Ȃ��́j | �@ |
getDeclaredMethod("���\�b�h��",
�����̌^�̔z��) |
Method | ���\�b�h��Ԃ��B�iprivate�ȃ��\�b�h�����Ăяo�����@�j | 2007-09-10 |
getAnnotationsByType(�N���X) |
A[] | �w�肳�ꂽ�N���X���A�m�e�[�V������Ԃ��BJDK1.8�ȍ~ �����w�肳�ꂽ�A�m�e�[�V�����ł����ڎ擾�ł���B |
2014-03-20 |
getAnnotations() |
Annotation[] | �A�m�e�[�V������Ԃ��BJDK1.5�ȍ~ | 2007-11-10 |
getDeclaredAnnotations() |
Annotation[] | �A�m�e�[�V�����i���ڎw�肵�Ă�����́j��Ԃ��BJDK1.5�ȍ~ | 2007-11-10 |
getDeclaredAnnotation(�N���X) |
A | �w�肳�ꂽ�N���X���A�m�e�[�V������Ԃ��BJDK1.8�ȍ~ | 2014-03-19 |
getDeclaredAnnotationsByType(�N���X) |
A[] | �w�肳�ꂽ�N���X���A�m�e�[�V������Ԃ��BJDK1.8�ȍ~ �����w�肳�ꂽ�A�m�e�[�V�����ł����ڎ擾�ł���B |
2014-03-20 |
getEnumConstants() |
T[] | �^�̏ꍇ�A�q�ꗗ��Ԃ��Bvalues���\�b�h�����BJDK1.5�ȍ~ �i�^�łȂ��ꍇ��null���Ԃ�j |
2015-04-16 |
componentType() |
Class<?> | �z��̏ꍇ�A�v�f�̌^�i�N���X�j��Ԃ��BJava12�ȍ~ �i�z��łȂ��ꍇ��null���Ԃ�j |
2020-09-29 |
arrayType() |
Class<?> | ������v�f�Ƃ���z��̌^�i�N���X�j��Ԃ��BJava12�ȍ~ | 2020-09-29 |
isHidden() |
boolean | hidden�N���X���ǂ�����Ԃ��BJava15�ȍ~ ��nowokay�����Java 15�V�@�\�܂Ƃ� ��cco�����Java 14��Java 15�̐V�@�\��� |
2020-09-29 |
equals(obj) |
boolean | Class#equals()�͓��ɃI�[�o�[���C�h����Ă��Ȃ��̂ŁA Object#equals()���̂��́B ���Ȃ킿�uthis==obj�v�Ȃ̂ŁAequals()���g�킸�� ���ځu==���Z�q�v��p���Ĕ�r���Ă悢�B |
2010-01-25 |
toGenericString() |
String | toString()�ɏC�����i�������j���������������Ԃ��BJDK1.8�ȍ~ | 2014-03-19 |
descriptorString() |
String | �^�L�q�q�������Ԃ��BJava12�ȍ~ �iString���ƁuLjava/lang/String;�v���Ԃ�j |
2020-09-29 |
����I�u�W�F�N�g������N���X���T�u�N���X�ł��邩�ǂ����i�p�����Ă��邩�ǂ����j�́A�ȉ��̂悤�ɂ��Ĕ��肷��B[2007-02-16]
if (obj instanceof �N���X) �`; if (�N���X.class.isInstance(obj)) �`; if (�N���X.class.isAssignableFrom(obj.getClass()) �`;
���t���N�V�������g���Ĉ��������R���X�g���N�^�[����C���X�^���X������ׂ�Class#newInstance()�Ƃ������\�b�h������B�i�������AJDK1.9�Ŕ��ƂȂ���[2018-04-30]�j
| JDK1.4�ȑO |
Class clazz = �`;
Object obj = clazz.newInstance(); // obj = new �N���X(); �Ɠ��� |
Class clazz = String.class; String obj; try { obj = (String)clazz.newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } |
newInstance()�̖߂�^��Object�Ȃ̂ŁA���̃I�u�W�F�N�g���g���ۂɂ� �K�v�Ȍ^���L���X�g����K�v������B |
| JDK1.5�ȍ~ |
Class<�N���X> clazz = �`; Class<? extends �N���X> clazz = �`; �N���X obj = clazz.newInstance(); // obj = new �N���X(); �Ɠ��� |
Class<String> clazz = String.class; String obj; try { obj = clazz.newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } |
Class���^�������w�肳��Ă���ꍇ�AnewInstance()�͂��̌^��Ԃ��悤�ɃR���p�C�������̂ŁA�L���X�g�͕s�v�B[2010-01-15] |
| JDK1.7�ȍ~ |
Class<String> clazz = String.class; String obj; try { obj = clazz.newInstance(); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } |
JDK1.7�ŁA���t���N�V�����n�̗�O�̋��ʃN���X����`���ꂽ�B[2013-08-06] |
����������Class#newInstance()��JDK1.9�Ŕ��ɂȂ����̂́A
�R���X�g���N�^�[�Ŕ��������O�𐳂��������ł��Ȃ��ׂ炵���B[2018-04-30]
JDK1.9�ȑO�ł����͔���������B
�R���X�g���N�^�[��throws�錾����Ă���iRuntimeException�ȊO�́j��O������ꍇ�AClass#newInstance()�͂��̗�O��catch���������Ƃ��o���Ȃ��B
public ClassExample {
// ��O����������R���X�g���N�^�[
public ClassExample() throws IOException {
throw new IOException("example");
}
}
try {
ClassExample.class.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
} catch(IOException e) { // ����catch���������Ƃ͏o���Ȃ����AIOException�͔�������
e.printStackTrace();
}
�R���X�g���N�^�[���擾������@�Ȃ���v�B�iConstructor#newInstance()�ł́A�R���X�g���N�^�[�Ŕ���������O��InvocationTargetException�Ń��b�v�����j
���t���N�V�����ŃC���X�^���X������ɂ́A��U�R���X�g���N�^�[���擾���A����newInstance()���g���B
�i���z��̃C���X�^���X������Array�N���X��newInstance()���g���j
getConstructor()�ɂ�Class�̔z���n���B���ꂪ�A�R���X�g���N�^�[�̈����̌^�̎�ނ�\���B
newInstance()�ɂ�Object�̔z���n���B���ꂪ�A�R���X�g���N�^�[�̈����̒l��\���B
�������v���~�e�B�u�^�iint���j�̏ꍇ�A�Y���������b�p�[�N���X�iInteger���j���g���B
| �@ |
import java.lang.reflect.Constructor; |
�@ | |
| JDK1.4�ȑO |
Class clazz = �`;
Class[] types = �����̌^���z��; Constructor constructor = clazz.getConstructor(types); Object[] args = �����̒l���z��; Object obj = constructor.newInstance(args); // obj = new �N���X(����,�c); �Ɠ��� |
Class clazz = Integer.class; Class[] types = { int.class }; Constructor constructor; try { constructor = clazz.getConstructor(types); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } Object[] args = { new Integer(123) }; Object obj; try { obj = constructor.newInstance(args); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } |
�@ |
| JDK1.5�ȍ~ |
Class<�N���X> clazz = �`; Class<? extends �N���X> clazz = �`; Class<?>[] types = �����̌^���z��; Constructor<�N���X> constructor = clazz.getConstructor(types); Object[] args = �����̒l���z��; �N���X obj = constructor.newInstance(args); // obj = new �N���X(����,�c); �Ɠ��� |
Class<Integer> clazz = Integer.class; Class<?>[] types = { int.class }; Constructor<Integer> constructor; try { constructor = clazz.getConstructor(types); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } Object[] args = { Integer.valueOf(123) }; Integer obj; try { obj = constructor.newInstance(args); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } |
Constructor�ɂ�Class�Ɠ����^���w�肷��B[2010-01-15] |
| JDK1.7�ȍ~ |
Class<Integer> clazz = Integer.class; Class<?>[] types = { int.class }; Constructor<Integer> constructor; try { constructor = clazz.getConstructor(types); } catch (SecurityException | NoSuchMethodException e) { throw new RuntimeException(e); } Object[] args = { Integer.valueOf(123) }; Integer obj; try { obj = constructor.newInstance(args); } catch (IllegalArgumentException | ReflectiveOperationException e) { throw new RuntimeException(e); } |
JDK1.7�ŁA���t���N�V�����n�̗�O�̋��ʃN���X����`���ꂽ�B[2013-08-06] | |
JDK1.5����̓R���X�g���N�^�[�擾��C���X�^���X�������\�b�h�̈������ϒ������ɉ��߂�ꂽ�̂ŁA�����ƊȒP�Ɏw��ł���悤�ɂȂ����B[2007-11-13]
�ϒ������Ȃ̂��A������܂Œʂ�̔z��̕��@���g����B
// �R���X�g���N�^�[�̈����������ꍇ [2018-04-30]
Constructor<?> ct = clazz.getConstructor();
Object obj = ct.newInstance();
// �R���X�g���N�^�[�̈�����int�Q�̏ꍇ Constructor<?> ct = clazz.getConstructor(int.class, int.class); Object obj = ct.newInstance(123, 456); //�����{�N�V���O���g�p
�������A�u�����Ȃ��v���Ӗ�����null���w�肵�Ă����ꍇ�������B[2008-07-03]
Class��Constructor�͂ǂ̃N���X�������킯�����AJDK1.5�������̌^����������A����̃N���X���w���������Ƃ��o����悤�ɂȂ����B[2007-05-02]
JDK1.4�܂ł́AClass���g���ăC���X�^���X������ꍇ�A�ړI�̃N���X�ւ̓L���X�g���Ă��K�v���������B
�u����̃N���X�ł���v�ƌ��肵�Ă���ꍇ�́A���̌^���g���Ė������邱�Ƃ��o����B
public static ArrayList newArrayList(Class c) {
try {
return (ArrayList)c.newInstance(); //JDK1.4�܂ł̓L���X�g���K�{
} catch (Exception e) {
throw new RuntimeException(e);
}
}
�����̌^��ArrayList�N���X�݂̂��t����
public static ArrayList newArrayList(Class<ArrayList> c) {
try {
return c.newInstance(); //�L���X�g���s�v
} catch (Exception e) {
throw new RuntimeException(e);
}
}
List l = newArrayList(ArrayList.class); //ArrayList�ȊO�̓R���p�C���G���[�ƂȂ� //�~ newArrayList(l.getClass()); //�~ newArrayList(new ArrayList().getClass());
Object#getClass()�͓����Class��Ԃ��悤�ɂ͏o���Ă��Ȃ��悤���B������O���B
����̔h���N���X�݂̂��t����w����@������B
public static List newList(Class<? extends List> c) { //List�̔h���N���X��Class�̂ݎ����
try {
return c.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
List al = newList(ArrayList.class);
List gl = newList(new ArrayList().getClass());
List nl = newList(List.class); //�R���p�C���͒ʂ�iList�͒��ڃC���X�^���X���ł��Ȃ��̂�newList()�̒��Ŏ��s���G���[�ɂȂ�j
���t���N�V�����Łistatic�����́j�����N���X�̃C���X�^���X��������@�B[2007-02-09]
�����N���X�̒ʏ�̃C���X�^���X�����̌`�����N���XX�������N���XA�ɑ��āux.new A()�v�����A����unew A(x)�v�Ƃł������ׂ��Öق̈��������݂��Ă���悤���B
���������R���X�g���N�^�[�����݂��Ă��鎖�́A�N���X�̃R���p�C����̒�`�����Ă݂�ƕ�����B
�i�t�Ɂu�����Ȃ��R���X�g���N�^�[�v�͑��݂��Ă��Ȃ��c�\�[�X��͈��������ł����Ă��I�j
�Ƃ�����ŁA�����N���X�����t���N�V�������g���ăC���X�^���X������ɂ́A��������R���X�g���N�^�[�Ɠ������@��p����B
| JDK1.4�ȑO |
X x = new X(); Class cl = X.A.class; Constructor con = cl.getConstructor(new Class[]{ X.class }); X.A a = (X.A)con.newInstance(new Object[]{ x }); |
| JDK1.5�ȍ~ |
X x = new X(); Class<X.A> cl = X.A.class; Constructor<X.A> con = cl.getConstructor(X.class); X.A a = con.newInstance(x); |
�Ȃ��A�����N���X���ǂ�����Class#isMemberClass()�Ŕ��f�ł���B[2008-02-10]
Java�ł́A���\�b�h��ێ����邽�߂�Method�Ƃ����N���X���p�ӂ���Ă���B
import java.lang.reflect.Method;
Method�́AClass#getMethod()��getDeclaredMethod()���g���Ď擾�ł���B
���\�b�h�Ăяo���ɂ�Method#invoke()���g�p����B
�߂�l��Object�^�Ƃ��ĕԂ邪�A���e�����R���̃��\�b�h���Ԃ��^�ƂȂ�B�v���~�e�B�u�^�iint���j�̏ꍇ�A���b�p�[�N���X�iInteger���j�Ƃ��ĕԂ�B�܂�void�̏ꍇ��null���Ԃ�B
�i���̌^���Ԃ�̂���Method#getReturnType()�Œ��ׂ�j
| JDK1.4�ȑO |
Method method; try { method = clazz.getMethod("���\�b�h��", null); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } Object ret; //�߂�l try { ret = method.invoke(null, null); // = �N���X��.���\�b�h��(); �Ɠ��� } catch (IllegalArgumentException e) { throw e; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } |
�Ăяo�����\�b�h�������������ꍇ�́A getMethod�����invoke�̑�2�����i�z��j��null�i�܂��͋�̔z��j���w�肷��B static���\�b�h���Ăяo���ꍇ�Ainvoke�̑�1�����ɂ�null���w�肷��B |
| JDK1.5�ȍ~ |
Method method; try { method = clazz.getMethod("���\�b�h��"); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } Object ret; //�߂�l try { ret = method.invoke(null); // = �N���X��.���\�b�h��(); �Ɠ��� } catch (IllegalArgumentException e) { throw e; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } |
�ϒ�������
���p����ƁA�Ăяo�����\�b�h�Ɉ����������ꍇ�́A getMethod�����invoke�̑�2�����ȍ~���w�肷��K�v�������B[2017-07-23] |
| JDK1.7�ȍ~ |
Method method; try { method = clazz.getMethod("���\�b�h��"); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } Object ret; //�߂�l try { ret = method.invoke(null); // = �N���X��.���\�b�h��(); �Ɠ��� } catch (IllegalArgumentException e) { throw e; } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } |
JDK1.7�ŁA���t���N�V�����n�̗�O�̋��ʃN���X����`���ꂽ�B[2017-07-23] |
| JDK1.4�ȑO |
Method method; try { method = object.getClass().getMethod("���\�b�h��", null); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } Object ret; //�߂�l try { ret = method.invoke(object, null); // = object.���\�b�h��(); �Ɠ��� } catch (IllegalArgumentException e) { throw e; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } |
�Ăяo�����\�b�h�������������ꍇ�́A getMethod�����invoke�̑�2�����i�z��j��null�i�܂��͋�̔z��j���w�肷��B �C���X�^���X���\�b�h���Ăяo���ꍇ�Ainvoke�̑�1�����ɂ͑ΏۃC���X�^���X���w�肷��B |
| JDK1.5�ȍ~ |
Method method; try { method = object.getClass().getMethod("���\�b�h��"); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } Object ret; //�߂�l try { ret = method.invoke(object); // = object.���\�b�h��(); �Ɠ��� } catch (IllegalArgumentException e) { throw e; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } |
�ϒ������𗘗p����ƁA�Ăяo�����\�b�h�������������ꍇ�́A getMethod�����invoke�̑�2�����ȍ~���w�肷��K�v�������B[2017-07-23] |
| JDK1.7�ȍ~ |
Method method; try { method = object.getClass().getMethod("���\�b�h��"); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } Object ret; //�߂�l try { ret = method.invoke(object); // = object.���\�b�h��(); �Ɠ��� } catch (IllegalArgumentException e) { throw e; } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } |
JDK1.7�ŁA���t���N�V�����n�̗�O�̋��ʃN���X����`���ꂽ�B[2017-07-23] |
| JDK1.4�ȑO |
Method method; try { method = object.getClass().getMethod("���\�b�h��", new Class[]{ int.class }); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } Object ret; //�߂�l try { ret = method.invoke(object, new Object[]{ new Integer(1) }); // = object.���\�b�h((int)1); �Ɠ��� } catch (IllegalArgumentException e) { throw e; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } |
�Ăяo�����\�b�h������������ꍇ�́A getMethod�̑�2�����ɂ������̌^�̔z�� invoke�̑�2�����ɂ��l�̔z�� ���w�肷��B �����̌^��int�����v���~�e�B�u�^�̏ꍇ�Ainvoke�Ɏw�肷���l�ɂ�Integer���̃��b�p�[�N���X���g�p����B |
| JDK1.5�ȍ~ |
Method method; try { method = object.getClass().getMethod("���\�b�h��", int.class); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } Object ret; //�߂�l try { ret = method.invoke(object, 1); // = object.���\�b�h(1); �Ɠ��� } catch (IllegalArgumentException e) { throw e; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } |
�Ăяo�����\�b�h������������ꍇ�́A�ϒ������𗘗p���� getMethod�̑�2�����ȍ~�������̌^ invoke�̑�2�����ȍ~���l ����ׂ邾���ŗǂ��B[2017-07-23] �����̌^��int�����v���~�e�B�u�^�̏ꍇ�Ainvoke�Ɏw�肷���l�ɂ�int�̒l�����̂܂g�p�ł���B |
| JDK1.7�ȍ~ |
Method method; try { method = object.getClass().getMethod("���\�b�h��", int.class); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } Object ret; //�߂�l try { ret = method.invoke(object, 1); // = object.���\�b�h(1); �Ɠ��� } catch (IllegalArgumentException e) { throw e; } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } |
JDK1.7�ŁA���t���N�V�����n�̗�O�̋��ʃN���X����`���ꂽ�B[2017-07-23] |
���Ȃ킿�Ainvoke()�̑�1�������L����̃C���X�^���X�ɑ��Ď��s�A������inull�Ȃ�jstatic���\�b�h�Ƃ��Ď��s����B
��2�����Ń��\�b�h���g�̈������w�肷��B�����������ꍇ�A��̔z��null��n���B�����̈������ɂ��ẮA�R���X�g���N�^�[�̏ꍇ�Ɠ��l�B
�Ăяo�������\�b�h���Ԃ���O�́Ainvoke()�̒���InvocationTargetException�ɓ������B
JDK1.5�ł́A�C���X�^���X�����Ɠ��l�ɁA���\�b�h�̌Ăяo�����ϒ������ɂȂ��ĕ֗��ɂȂ����B[2007-11-13]
public Object ���\�b�h��(int arg1, int arg2) { �` } public Object �����������\�b�h() { �` }
Method m = clazz.getMethod("���\�b�h��", int.class, int.class);
Object ret = m.invoke(object, 123, 456); //�I�[�g�{�N�V���O���g�p
Method m2 = clazz.getMethod("�����������\�b�h");
Object ret2 = m2.invoke(object);
�������A�u�����Ȃ��v���Ӗ�����null���w�肵�Ă����ꍇ�������B[2008-07-03]
�ϒ������������\�b�h��getMethod()�Ŏ擾�������ꍇ�A�����̌^�ɂ͔z����w�肷��B[2008-02-10]
// ���t���N�V�����Ŏ擾�������ϒ��������\�b�h public void ���\�b�h��(�N���X... args) { �` }
Method m = clazz.getMethod("���\�b�h��", �N���X[].class);
m.invoke(�I�u�W�F�N�g, new Object[]{ new �N���X[]{ �l,�l,�c }});
//�~ m.invoke(�I�u�W�F�N�g, �l,�l,�c);
invoke()�̈����Ɂi�z����g�킸�Ɂj����������ׂ�悤�ȏ������͏o���Ȃ��B
�v��������ʂɔz��������̂Ɠ��l�ɃR�[�f�B���O����B
| �@ | �ϒ������ȊO�̈��������� | �ϒ������ȊO�̈������L�� |
|---|---|---|
| �擾���������\�b�h | public String example1(String...
ss) { |
public String example2(String s, String...
ss) { |
| �擾���@ | Method m = clazz.getDeclaredMethod("example1", String[].class); |
Method m = clazz.getDeclaredMethod("example2", String.class, String[].class); |
| �Ăяo�����@ �iinvoke�ɔz���n���j |
Object[] args = { new String[]{ "abc", "def" } }; |
Object[] args = { "123", new String[]{ "abc", "def" } }; |
| �Ăяo�����@ �iinvoke�̉ϒ������𗘗p�j |
�ȉ��̏������ł͎��s���ɗ�O�����BObject r = m.invoke(object, "abc", "def"); |
�ȉ��̏������ł͎��s���ɗ�O�����BObject r = m.invoke(object, "123", "abc", "def"); |
�ȉ��̏������ł̓R���p�C�����Ɍx���A���s���ɗ�O�����BObject r = m.invoke(object, new String[]{ "abc", "def" }); |
Object r = m.invoke(object, "123", new String[]{ "abc", "def" }); |
|
�ȉ��̏������Ȃ�OK�BObject r = m.invoke(object, (Object)new String[]{ "abc", "def" }); |
Object r = m.invoke(object, (Object)"123",
(Object)new String[]{ "abc", "def" }); |
�u�ϒ��������������Ȃ����\�b�h�v���Ăяo���ۂ�invoke�̉ϒ������𗘗p�������������o���Ȃ����R�́A�ȉ��̂悤�ɉ��߂���邩��B[2015-04-26]
| �� | m.invoke(object, "abc", "def") |
m.invoke(object, new String[]{ "abc", "def" }) |
m.invoke(object, (Object)new String[]{ "abc", "def" }) |
|---|---|---|---|
| ���� | 1�ڂ̈����̌^��String�A�l��"abc" 2�ڂ̈����̌^��String�A�l��"def" �������Ăяo���������\�b�h�̈�����1�݂̂ŁA�^��String[] �Ȃ̂ň�v���Ȃ��B |
invoke�̈����̌^��Object...�ł���A���̂�Object[]�B Java�̔z��͋����Ȃ̂ŁAString[]��Object[]�ɑ���\�B ���������� m.invoke(object, new Object[]{ "abc", "def" })�Ɖ��߂����B�i�������x�����o��j����͍��L�� m.invoke(object, "abc", "def")�Ɠ����B |
String[]��Object�ɃL���X�g���Ă���̂ŁA invoke�����猩��ƁA1�̈����Ƃ��������B |
�v����ɁAinvoke�ɔz���n���ƁA���̔z�uinvoke�̈���Object[]���̂��́v�Ȃ̂��uObject[0]�ɔz�����ꂽ�v�̂���ʂ��t���Ȃ��i�Ƃ������O�҂Ƃ��ĉ��߂����j�̂����B
�Ȃ̂ŁA�Ăяo���������\�b�h�ɉϒ������ȊO�̈�����1�ł�����A�uinvoke��Object[]�v�Ɓu�Ăяo���������\�b�h�̈����v�Ƃ̋�ʂ��t���B
���Ȃ݂ɁA���郁�\�b�h���ψ����������ǂ�����Method#isVarArgs()�Ŕ��f�ł���B[2007-11-13]
Method�N���X���烁�\�b�h�̈����̏����擾���邱�Ƃ��o����B[2014-03-20]
�i���l�ɁAConstructor�N���X����R���X�g���N�^�[�̈����̏����擾�ł���j
| ���\�b�h�� | �߂�^ | ���� | |
|---|---|---|---|
getParameterCount() |
1.8 | int | �����̌���Ԃ��B |
getParameterTypes() |
Class<?>[] | �����̌^�i�����̌����̔z��j��Ԃ��B | |
getParameterAnnotations() |
1.5 | Annotation[][] | �����ɕt����ꂽ�A�m�e�[�V�����i�����̌����̔z��j��Ԃ��B |
getParameters() |
1.8 | Parameter[] | �����̈ꗗ��Ԃ��B |
Parameter�N���X��JDK1.8�Œlj����ꂽ�N���X�ŁA����1���̏������B[2014-03-20]
import java.lang.reflect.Parameter;
| ���\�b�h�� | �߂�^ | ���� |
|---|---|---|
isNamePresent() |
boolean | �R���p�C�����ꂽclass�t�@�C�����Ɉ�������ێ����Ă��邩�ǂ�����Ԃ��B true�̏ꍇ�A�\�[�X��ɏ�����Ă�����������getName()�Ŏ擾���邱�Ƃ��o����B class�t�@�C�����Ɉ�������ێ�������ׂɂ́Ajavac��-parameters�I�v�V������t���ăR���p�C������K�v������B �iEclipse�̏ꍇ�̓v���W�F�N�g�̃v���p�e�B�[����uJava Compiler�v��I�сA�uStore method parameter names (usable via reflection)�v�Ƀ`�F�b�N��t����j �Q�l�F irof�����Java8�Ŏ��s���Ƀ��\�b�h�̈����̖��O���Ƃ��ۂ� |
getName() |
String | ��������Ԃ��B class�t�@�C�����ň�������ێ����Ă��Ȃ��ꍇ �i�f�t�H���g�̏ꍇ�j�A�u arg�����ԍ��v�Ƃ��������Ԃ�B |
getDeclaringExecutable() |
Executable | ���̈����������Ă���Method���邢��Constructor��Ԃ��B |
getModifiers() |
int | ���������Ԃ��B |
getParameterizedType() |
Type | �����̌^��Ԃ��B |
getType() |
Class<?> | �����̃N���X��Ԃ��B |
getAnnotatedType() |
AnnotatedType | �@ |
isImplicit() |
boolean | �Öقɍ��ꂽ�����̏ꍇ��true��Ԃ��H ���ʂ̃��\�b�h��false��Ԃ��B �����N���X�̃R���X�g���N�^�[�̑�1�����i�ÖقɊO���N���X�̃I�u�W�F�N�g��n���j�ł�false��Ԃ��悤���B |
isSynthetic() |
boolean | �������ꂽ�ꍇ��true��Ԃ��B |
isVarArgs() |
boolean | �ϒ������̏ꍇ��true��Ԃ��B |
getAnnotation(�N���X) |
A | �����ɕt�����Ă���A�m�e�[�V������Ԃ��B |
getAnnotationsByType(�N���X) |
A[] | �����ɕt�����Ă���A�m�e�[�V������Ԃ��B |
getDeclaredAnnotations() |
Annotation[] | �����ɕt�����Ă���A�m�e�[�V������Ԃ��B |
getDeclaredAnnotation(�N���X) |
A | getAnnotation()�Ɠ����B |
getDeclaredAnnotationsByType(�N���X) |
A[] | getAnnotationsByType()�Ɠ����B |
getAnnotations() |
Annotation[] | getDeclaredAnnotations()�Ɠ����B |
�t�B�[���h�i�����o�[�ϐ��j�����t���N�V�����ň����̂́A���\�b�h�����ȒP�B[2007-02-14/2007-11-15]
import java.lang.reflect.Field;
Class c = obj.getClass();
Field f = c.getField("field"); //�t�B�[���h�����w��
f.set(obj, new Integer(123)); //�v���~�e�B�u�^�����b�p�[�N���X���g�p���邪
f.setInt(obj, 123); //��p�̃��\�b�h������
f.set(obj, 123); //JDK1.5�i�����{�N�V���O���g�p�j
Integer n = (Integer)f.get(obj);
int n = f.getInt(obj);
int n = (Integer)f.get(obj); //JDK1.5�i�����{�N�V���O���g�p�j
static�t�B�[���h�ւ̃A�N�Z�X�́A���\�b�h�Ɠ������A�ΏۃI�u�W�F�N�g��null���w�肷��B
Object value = f.get(null); f.set(null, value);
���t���N�V�����ł͔����������O������������ׁAcatch�߂̌��������Ȃ�B[2012-04-08]
��������catch�������̂��ʓ|�Ȃ̂ŁA���uException�v�ŃL���b�`���Ă��܂������Ȃ邪�A����͖]�܂����Ȃ��B
JDK1.7�ł���O�̃}���`�L���b�`���o����悤�ɂȂ����̂ŁA�ʂɗ�O�������Ă�catch�߂�1�ł悭�Ȃ����B
Object ret;
try {
Method m = clazz.getMethod("���\�b�h��", int.class, int.class);
ret = m.invoke(null, 123, 456);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new RuntimeException(e);
}
�܂��AJDK1.7�ł̓��t���N�V�����n�̗�O�͋��ʂ̐e�N���X��1��`���ꂽ�̂ŁA������g���Ƃ����ƃV���v���ɂȂ�B
Object ret;
try {
Method m = clazz.getMethod("���\�b�h��", int.class, int.class);
ret = m.invoke(null, 123, 456);
} catch (IllegalArgumentException | SecurityException | ReflectiveOperationException e) {
throw new RuntimeException(e);
}
�z�������t���N�V�����ň����ɂ́A��p��Array�N���X���g�p����B[2007-02-13]
import java.lang.reflect.Array;
�I�u�W�F�N�g���z��Ȃ̂��ǂ�����Class#isArray()���g�p���Ĕ��肷��B
if (obj.getClass().isArray()) {
// obj�͔z��
}
�z��̌^�i�N���X�j��Class#getComponentType()�Ŏ擾����B[2010-02-01]
�i�z��łȂ��ꍇ��null���Ԃ�j
Class<?> type = obj.getClass().getComponentType();
�z��̒����i���j�iarr.length�j���擾����ɂ́A�ȉ��̂悤�ɂ���B
int length = Array.getLength(arr);
�z��̗v�f���擾����iType a = arr[index];�j�ɂ́A�ȉ��̂悤�ɂ���B
Type a = (Type)Array.get(arr, index);
�v���~�e�B�u�^�Ō^�̎�ނ��������Ă���ꍇ�́A����ɉ��������\�b�h���g���̂��֗��B
int n = Array.getInt(int_arr, index);
�z��ɒl���Z�b�g����iarr[index] = val;�j�ɂ́A�ȉ��̂悤�ɂ���B
Array.set(arr, index, val); Array.setInt(int_arr, index, n);
����N���X�̔z������ꍇ�iarr = new �v�f�N���X[�z��];�j�́A�ȉ��̂悤�ɂ���B
Object arr = Array.newInstance(�v�f�N���X.class, �z��);
�������z��쐬�p�iarr = new long[2][3];�j�̃��\�b�h������B[2007-02-14]
Object arr = Array.newInstance(long.class, new int[]{ 2, 3 });
JDK1.6�ł́A������ϒ������ɉ��߂�ꂽ�B[2008-02-10]
Object arr = Array.newInstance(long.class, 2, 3);
��newInstance()�̖߂�l�̌^���W�F�l���N�X�ɂȂ��Ă��Ȃ��̂́A�v���~�e�B�u�^�̔z�����肭�\���ł��Ȃ��ׂ炵���B[2010-02-13]
�@��IBM��Java�̗��_�Ǝ��H: Generics�A�����Ipublic static <T> T[] newArray(Class<T> c) { return (T[]) Array.newInstance(c, 10); } int[] i = newArray(int.class); //�R���p�C���G���[�uInteger[]����int[]�ɕϊ��ł��Ȃ��v //�^�����ɂ̓v���~�e�B�u�^�͎w��ł����A���b�p�[�N���X�ɂȂ邩��B
���Ȃ݂��z��̃_���v�iArrays.deepToString()�j�̈����͖����I�Ȕz��łȂ��ƃR���p�C���G���[�ɂȂ�̂ŁA�P���ɃL���X�g���Ă��B
System.out.println(Arrays.deepToString((Object[]) arr));
���t���N�V�����̔��e�ɓ��邩�ǂ����͉��������AJavaBeans��setter/getter���\�b�h������PropertyDescriptor�Ƃ����N���X������B[2007-12-03]
JavaBeanSample.java�F
public class JavaBeanSample {
private String str;
public void setData(String s) {
this.str = s;
}
public String getData() {
return str;
}
}
JavaBean�̃T���v���Ȃ̂ŁApublic�N���X�Apublic�ȃf�t�H���g�R���X�g���N�^�[�i�����Ȃ��̃R���X�g���N�^�[�B��L�̗�ł͈Öق̒�`�j�A�v���p�e�B�[���i��L�̗�ł́udata�v�j��set/get�̐ړ�����t����public���\�b�h��p�ӂ����B
import java.beans.PropertyDescriptor;
JavaBeanSample bean = new JavaBeanSample();
PropertyDescriptor pd = new PropertyDescriptor("data", JavaBeanSample.class);
//�Z�b�^�[���\�b�h���擾�E���s
Method w = pd.getWriteMethod();
w.invoke(bean, new Object[] { "abc" });
//�Q�b�^�[���\�b�h���擾�E���s
Method r = pd.getReadMethod();
String s = (String) r.invoke(bean, (Object[])null);
System.out.println(s);
PropertyDescriptor�̃R���X�g���N�^�[�Ƀv���p�e�B�[���ƃN���X��n���ƁA���̃v���p�e�B�[�Ɋւ������������B
���̍ہA���̃v���p�e�B�[��setter/getter���\�b�h�������Ƃ�������Ȃ��ƃG���[�ɂȂ�i��O����������j�i�Е������ł͑ʖځj�B
JDK1.4.2_07��Struts�̓��삪�ς�����̂́A���ꂪ�W���Ă����̂��Ȃ��c�H
�N���X����o�[�i���\�b�h�E�t�B�[���h�j�̑����i�C���q�j�́AModifier�N���X�Ŕ��f����B[2007-02-14]
�����Ƃ����̂́Astatic�ł���Ƃ�final�ł���Ƃ�public�ł���Ƃ��B
import java.lang.reflect.Modifier;
int mod = clazz .getModifiers(); //�N���X�̑����擾
// mod = method.getModifiers(); //���\�b�h�̑����擾
// mod = field .getModifiers(); //�t�B�[���h�̑����擾
// mod = param .getModifiers(); //�����̑����擾
if (Modifier.isStatic(mod)) {
//static����
}
//�����̕�����\�L System.out.println(Modifier.toString(mod));
| ���f���e | ���f���@ | Class | Accessible Object |
Member | Const ructor |
Method | Field | �X�V�� | |
|---|---|---|---|---|---|---|---|---|---|
| �v���~�e�B�u�^���ǂ��� | isPrimitive() |
�� | �@ | �@ | �@ | �@ | �@ | 2008-02-10 | |
| �C���^�[�t�F�[�X���ǂ��� | isInterface() |
�� | �@ | �@ | �@ | �@ | �@ | 2008-02-10 | |
| �z�����ǂ��� | isArray() |
�� | �@ | �@ | �@ | �@ | �@ | 2008-02-10 | |
| �����N���X���ǂ��� | isAnonymousClass() |
1.5 | �� | �@ | �@ | �@ | �@ | �@ | 2010-02-01 |
| �Ǐ��N���X���ǂ��� | isLocalClass() |
1.5 | �� | �@ | �@ | �@ | �@ | �@ | 2010-02-01 |
| �����o�[�N���X�i�����N���X�j���ǂ��� | isMemberClass() |
1.5 | �� | �@ | �@ | �@ | �@ | �@ | 2008-02-10 |
| �^���ǂ��� | isEnum() |
1.5 | �� | �@ | �@ | �@ | �@ | �@ | 2008-02-10 |
| ���R�[�h���ǂ��� | isRecord() |
16 | �� | �@ | �@ | �@ | �@ | �@ | 2021-03-21 |
| �A�m�e�[�V�������ǂ��� | isAnnotation() |
1.5 | �� | �@ | �@ | �@ | �@ | �@ | 2010-02-01 |
| �A�m�e�[�V�����̗L�� | isAnnotationPresent(�A�m�e�[�V�����̃N���X) |
1.5 | �� | �� | �@ | �� | �� | �� | 2008-02-10 |
| �A�N�Z�X�\���ǂ��� | isAccessible() |
�@ | �� | �@ | �� | �� | �� | 2008-02-10 | |
| �������ꂽ�̂��ǂ��� | isSynthetic() |
1.5 | �� | �@ | �� | �� | �� | �� | 2008-02-10 |
| �f�t�H���g���\�b�h���ǂ��� | isDefault() |
1.8 | �@ | �@ | �@ | �@ | �� | �@ | 2014-03-20 |
| public���ǂ��� | Modifier.isPublic(mod) |
�� | �@ | �� | �� | �� | �� | 2008-02-10 | |
| private���ǂ��� | Modifier.isPrivate(mod) |
2008-02-10 | |||||||
| protected���ǂ��� | Modifier.isProtected(mod) |
2008-02-10 | |||||||
| package private���ǂ��� | (mod & (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE)) == 0 |
2008-02-10 | |||||||
| static���ǂ��� | Modifier.isStatic(mod) |
2007-02-14 | |||||||
| final���ǂ��� | Modifier.isFinal(mod) |
2008-02-10 | |||||||
| synchronized����Ă��邩 | Modifier.isSynchronized(mod) |
2008-02-10 | |||||||
| volatile���ǂ��� | Modifier.isVolatile(mod) |
2008-02-10 | |||||||
| transient���ǂ��� | Modifier.isTransient(mod) |
2008-02-10 | |||||||
| native���ǂ��� | Modifier.isNative(mod) |
2008-02-10 | |||||||
| �C���^�[�t�F�[�X���ǂ��� | Modifier.isInterface(mod) |
2008-02-10 | |||||||
| �������ǂ��� | Modifier.isAbstract(mod) |
2008-02-10 | |||||||
| strictfp���ǂ��� | Modifier.isStrict(mod) |
2010-02-01 | |||||||
| �z��̌^ | getComponentType() |
�� | �@ | �@ | �@ | �@ | �@ | 2010-02-01 | |
| ���\�b�h�̖߂�l�̌^ | getReturnType() |
�@ | �@ | �@ | �@ | �� | �@ | 2007-06-20 | |
| throws�Ő錾����Ă����O | getExceptionTypes() |
�@ | �@ | �@ | �� | �� | �@ | 2008-02-10 | |
| �ϐ��̈����������ǂ��� | isVarArgs() |
1.5 | �@ | �@ | �@ | �� | �� | �@ | 2008-02-10 |
| ���n�����\�b�h���ǂ��� | isBridge() |
1.5 | �@ | �@ | �@ | �@ | �� | �@ | 2008-02-10 |
| �t�B�[���h�̌^ | getType() |
�@ | �@ | �@ | �@ | �@ | �� | 2008-02-10 | |
| �^�萔���ǂ��� | isEnumConstant() |
1.5 | �@ | �@ | �@ | �@ | �@ | �� | 2008-02-10 |
| hidden�N���X���ǂ��� | isHidden() |
15 | �� | �@ | �@ | �@ | �@ | �@ | 2021-03-21 |
���\�b�h�̖߂�l�̌^��Method#getReturnType()�Ŏ擾�ł���B[2010-01-25]
���̒l�̌^��Class�B
�܂�AString��Ԃ����\�b�h�̏ꍇ�AgetReturnType()�̖߂�l�́uString.class�v�Ɠ������B
�߂�l�̌^���v���~�e�B�u�^�̏ꍇ�A���b�p�[�N���X�ɒ�`����Ă���v���~�e�B�u�p�̃N���X���Ԃ�B
�Ⴆ��int�̏ꍇ�AInteger.TYPE�Ɠ������B
�������́uint.class�v�ł������B
void�̏ꍇ��Void.TYPE�ijava.lang.Void�j���Ԃ�B
�������́uvoid.class�v�ł������B
�Ȃ��AClass#equals(other)�́uthis==other�v�Ɠ����Ȃ̂ŁAequals()���\�b�h�łȂ��u==���Z�q�v�Ŕ�r���Ă悢�B
���t���N�V�����ł́Aprivate�ȃ��\�b�h��t�B�[���h���i�����t���Łj�A�N�Z�X���邱�Ƃ��o����B[2007-09-10]
���t���N�V�����ł��A�ʏ�� �v���C�x�[�g�ȃ��\�b�h���ĂԂ��Ƃ͏o���Ȃ����A�v���C�x�[�g�ȃt�B�[���h�̒l��ǂݏ������邱�Ƃ͏o���Ȃ��B���s���悤�Ƃ����java.lang.IllegalAccessException����������B
�������������g�A�N�Z�X�\�h�ɕς��Ă��ƁA�A�N�Z�X�ł���悤�ɂȂ�B
Target target = new Target();
Class<Target> c = Target.class;
Method m = c.getDeclaredMethod("setValue", int.class);
m.setAccessible(true);
m.invoke(target, 123); // target.setValue(123);
Field f = c.getDeclaredField("value");
f.setAccessible(true);
int n = f.getInt(target); // int n = target.value;
�������A�Z�L�����e�B�[�}�l�[�W���[�ɂ���ăA�N�Z�X�������������Ă���ꍇ�́A������ύX���邱�Ƃ͏o���Ȃ��B
setAccessible()�Ăяo������java.security.AccessControlException�iaccess
denied/suppressAccessChecks�j����������B
�iJava�A�v���P�[�V�����̃f�t�H���g�ł̓A�N�Z�X�������������Ă��Ȃ��̂ŁAJUnit��private���\�b�h���e�X�g�������Ƃ����Ƀv���C�x�[�g�A�N�Z�X���g�����Ƃ��o����j
���Ȃ݂�JNI�Ȃ�A�����������A�N�Z�X����̕ύX�������Ƀv���C�x�[�g�ȃ����o�[�ɃA�N�Z�X�ł���B[2008-02-10]
���t���N�V�����ł́Afinal�t���̃t�B�[���h�ւ̒l�̐ݒ���i�����t���Łj�s�����Ƃ��o����B[2010-01-15/2013-08-06]
public class Target {
public final int VALUE = 999;
}
Field f = clazz.getDeclaredField("VALUE"); f.setInt(target, 123); System.out.println(target.VALUE); |
�l���Z�b�g���郁�\�b�h��java.lang.IllegalAccessException����������B |
Field f = clazz.getDeclaredField("VALUE"); f.setAccessible(true); f.setInt(target, 123); System.out.println(target.VALUE); //�ς���Ă��Ȃ� System.out.println(f.getInt(target)); //�ς���Ă��� |
�������g�A�N�Z�X�\�h�ɕς��Ă��ƁA��O�͔�����������ɏI������B �������œK���i�C�����C���W�J�j����Ă���ӏ��ł͒l�͕ς��Ȃ��B[/2013-08-06] |
�N���X���̃��\�b�h��t�B�[���h�ϐ��́Ajavap�Ƃ����R�}���h�Ŋm�F�ł���B[2007-02-09]
JNI��C���ꑤ����Java�̃��\�b�h���Ăт������ɂ��������d��B
�g�p���@�F javap �N���X��
>cd C:\workspace\sample\classes >javap jp.hishidama.sample.Sample
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@