S-JIS[2007-11-10/2019-06-02] �ύX����
Java��JDK1.5���瓱�����ꂽ�A�m�e�[�V�����������ō����@�B
|
|
|
|
���A�m�e�[�V�����̎g�p���@�i�����A�m�e�[�V�����̎g�p��j |
�A�m�e�[�V�����͈��t����ׂɎg���̂ŁA�f�[�^�Ƃ������͎̂����Ȃ��i���ۂ����Ă����ǁj�B
�Ȃ̂ŁA�C���^�[�t�F�[�X�̂悤�ɒ�`����B
ExampleAnnotation.java�F
package jp.hishidama.example;
public @interface ExampleAnnotation {
}
���ɉ����w�肵�Ȃ��ƁA�쐬�����A�m�e�[�V�����́A�A�m�e�[�V�������g����ꏊ�Ȃ�ǂ��ɂł��������Ƃ��o����B
�g����A�m�e�[�V�������g����ꏊ�h�����肷��ɂ́A����A�m�e�[�V������`�̒��O���g����ꏊ�̈�iTarget�A�m�e�[�V�����j��t����B
import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target(ElementType.FIELD) //�t�B�[���h�ɂ����t������w�� public @interface LocationExampleAnnotation { }
�����̏ꏊ���w�肵�����ꍇ��@Target��( )����{ }��t���ăJ���}���ŕ����̎w������B
@Target( { ElementType.TYPE, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE }) public @interface LocationExampleAnnotation { }
ElementType | ver | ���� | �X�V�� |
---|---|---|---|
ElementType. TYPE |
1.5 | �N���X��`��C���^�[�t�F�[�X��`�i�A�m�e�[�V������enum���܂ށj | �@ |
ElementType. FIELD |
1.5 | �t�B�[���h�i�����o�[�ϐ��j��` | �@ |
ElementType. METHOD |
1.5 | ���\�b�h��` | �@ |
ElementType. PARAMETER |
1.5 | ���\�b�h�̈����̒�` | �@ |
ElementType. CONSTRUCTOR |
1.5 | �R���X�g���N�^�[��` | �@ |
ElementType. LOCAL_VARIABLE |
1.5 | ���[�J���ϐ��̒�` | �@ |
ElementType. ANNOTATION_TYPE |
1.5 | �A�m�e�[�V������` | �@ |
ElementType. PACKAGE |
1.5 | �p�b�P�[�W | �@ |
ElementType. TYPE_PARAMETER |
1.8 | �W�F�l���N�X�̌^������` | 2014-03-20 |
ElementType. TYPE_USE |
1.8 | �^�i�N���X�j���g����ꏊ�S�āi�N���X��`��[�J���ϐ������܂ށj | 2014-03-20 |
���ꂼ��̎w��̈Ӗ�����ꏊ�́A�ȉ��̂悤�ɂȂ��Ă���B
@LocationExampleAnnotation //TYPE�F�N���X��`��C���^�[�t�F�[�X��`������ꏊ
public class �A�m�e�[�V������t����T���v�� {
@LocationExampleAnnotation //FIELD�F�t�B�[���h�i�����o�[�ϐ��j��`
protected int value;
@LocationExampleAnnotation //CONSTRUCTOR�F�R���X�g���N�^�[��`
public �A�m�e�[�V������t����T���v��() {
}
@LocationExampleAnnotation //METHOD�F���\�b�h��`
public void ���\�b�h() {
}
public int �������胁�\�b�h(
@LocationExampleAnnotation //PARAMETER�F�p�����[�^�[�i���\�b�h�̈����j��`
int param
) {
@LocationExampleAnnotation //LOCAL_VARIABLE�F���[�J���ϐ���`
int local_var = 0;
for (@LocationExampleAnnotation int i = 0; i < 10; i++); //LOCAL_VARIABLE�F���[�J���ϐ���`[2008-12-08]
//�~ ���\�b�h�Ăяo���ɂ͕t�����Ȃ�
System.out.println(local_var);
//�~ ���ɂ͕t�����Ȃ�
for (int i = 0; i< 10; i++) ;
//�~ �u���b�N�ɂ��t�����Ȃ�
{
int n = 1;
System.out.println(n);
}
//�~ return�ɂ����R�t�����Ȃ�
return 1;
}
}
�Ȃ��A�f�t�H���g�ł́A1�̏ꏊ�ɓ����A�m�e�[�V�������������Ƃ͏o���Ȃ��B
���AJDK1.8�ȍ~�ł́A@Repeatable��t����ƁA1�̏ꏊ�ɂ��̃A�m�e�[�V�������������Ƃ��o����悤�ɂȂ�B[2014-03-20]
��L��Target�A�m�e�[�V�����̂悤�ɁA�A�m�e�[�V�����ɂ͈�����n�����Ƃ��o����B
@interface �A�m�e�[�V������ { �^ �ϐ���(); �^ �ϐ��Q(); �c }
�^�ɂ́A�v���~�e�B�u�^�EString�EClass�E�^�E�A�m�e�[�V�����A���Ƃ͂������ꎟ���z���̂ݎw��\�B[2008-07-05]
��`���ꂽ�ϐ�����ŁA���ϐ������uvalue�v�̂Ƃ������A��2�̂悤�Ɂi��1�̂悤�ȁu�ϐ��� = �v�Ƃ���������̌`�����j�ȗ��ł���B
@interface StringValueAnnotation { String value(); //������������Ƃ���� } class UseExample { @StringValueAnnotaion(value = "������") //�l���w�肷���1 int n; } class UseExample2 { @StringValueAnnotaion("������") //�l���w�肷���2 int n; }
�A�m�e�[�V�����Ɉ����������i�������w�肵�Ȃ��j�Ƃ��́A�g���ۂɊۊ��ʂ��ȗ��ł���B[2008-07-05]
@LocationExampleAnnotation �c���ʂ��ȗ� int field1; @LocationExampleAnnotation() �c���ʂ�t���� int field2;
��肤��l�����܂�Ȃ�A�^���g���̂��퓅��i�ł��傤�B
enum ExampleEnum { �l1, �l2, �c } @interface EnumAnnotation { ExampleEnum value(); } @EnumAnnotation(value = ExampleEnum.�l1) class EnumUse { }
��肤��l�̌^��z��ɂ���ƁA�����̒l���w��ł���悤�ɂȂ�B
@interface ValuesAnnotation { int[] value(); } @ValuesAnnotation({ 1,2,3 }) class ValuesUse { @ValuesAnnotation(value = { 1,2,3,4 }) int n; }
���̗�̈Ӗ������Ƃ��Ắu@ValuesAnnotation(value = new int[]{ 1,2,3,4 })
�v�ȂƎv�����ǁA���̏������͋�����Ȃ��炵���i�R���p�C���G���[�ɂȂ�j�B
�܂��A1���������l���w�肵�Ȃ��ꍇ�A�Ƃ�����{ }�͏ȗ��ł���B[2008-07-05]
@ValuesAnnotation(value = 1) int m; @ValuesAnnotation(1) int o;
default���w�肷��ƁA�A�m�e�[�V�������w�肷��ۂ̒l�w����ȗ����邱�Ƃ��o����悤�ɂȂ�B
@interface DefaultAnnotation { String value() default "�f�t�H���g�l"; }
// �f�t�H���g�l���g�p @DefaultAnnotation class DefaultExample1 { } // �f�t�H���g�l���g�p @DefaultAnnotation() class DefaultExample2 { } // �l���w�� @DefaultAnnotation("���ʂɒl�w��") class DefaultExample3 { }
�A�m�e�[�V������t�����N���X�ł́A�R���p�C������class�t�@�C�����ɂ����̃A�m�e�[�V�����̏�c��i�f�t�H���g�ł́j�B
�w�莟���class�t�@�C�����������������o����B���̎w��̓A�m�e�[�V������`���ɍs���B
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.CLASS) @interface ScopeAnnotation { }
@Retention�̒l | �A�m�e�[�V�������g����͈� | ���p�ړI |
---|---|---|
RetentionPolicy.SOURCE | �\�[�X���̂��B �R���p�C������ƁAclass�t�@�C�����ɂ͂��̃A�m�e�[�V�����̏��͎c��Ȃ��B |
�\�[�X��ǂݍ��ތn���̃c�[�����g�p����H |
RetentionPolicy.CLASS | @Retention���ȗ����ꂽ�ꍇ�̃f�t�H���g�l�B �R���p�C������class�t�@�C�����ɂ��̃A�m�e�[�V�����̏�ێ�����邪�A���s���ɂ͓ǂݍ��܂�Ȃ��B |
class�t�@�C����ǂݍ��ތn���̃c�[�����g�p����H �Ⴆ��Javassist�œǂݍ��ގ����o����B |
RetentionPolicy.RUNTIME | ���s������ɂ�JavaVM�ɂ��̃A�m�e�[�V�����̏�ǂݍ��܂��B ���t���N�V������p�������̏����擾���邱�Ƃ��o����B |
���s���̏��擾�B |
���s���ɂ��L���ɂȂ��Ă���A�m�e�[�V�����̏ꍇ�A���s�������t���N�V������p���Ă��̃A�m�e�[�V�����̏����擾���邱�Ƃ��o����B
�A�m�e�[�V�����̒�`�� | �A�m�e�[�V�����̎g�p�� | ���t���N�V�����ł̎擾�� |
---|---|---|
@Target(ElementType.TYPE) |
@MyTypeAnnotation |
Class<?> c = AnnotationExample.class; |
@Target(ElementType.FIELD) |
public class AnnotationExample { |
Class<?> c = AnnotationExample.class; |
@Target(ElementType.METHOD) |
public class AnnotationExample { |
Class<?> c = AnnotationExample.class; |
@Target(ElementType.PARAMETER) |
public class AnnotationExample { |
Class<?> c = AnnotationExample.class; |
@Target(ElementType.CONSTRUCTOR) |
public class AnnotationExample { |
Class<?> c = AnnotationExample.class; |
@Target(ElementType.LOCAL_VARIABLE) |
public class AnnotationExample { |
���[�J���ϐ��̎擾���@�͕s���B �i���Ԃ���s���ɂ͎��Ȃ��j |
@Target(ElementType.ANNOTATION_TYPE) |
@Retention(RetentionPolicy.RUNTIME) |
Class<?> c = AnnotationExample2.class; |
@Target(ElementType.PACKAGE) |
���p�b�P�[�W�̃A�m�e�[�V�����̗� | ���p�b�P�[�W�̃A�m�e�[�V�����̃��t���N�V�����̗� |
@Target(ElementType.TYPE_PARAMETER) |
public class
TypeParamExample<@MyTypeParamAnnotation T> { |
Class<?> c = TypeParamExample.class; |
public class AnnotationExample { |
Class<?> c = AnnotationExample.class; |
|
@Target(ElementType.TYPE_USE) |
public class AnnotationExample3 extends
@MyTypeUseAnnotation AnnotationExample { |
Class<?> c = AnnotationExample3.class; |
public class AnnotationExample4 implements
@MyTypeUseAnnotation Serializable { |
Class<?> c = AnnotationExample4.class; |
|
public @MyTypeUseAnnotation String method5() { |
Class<?> c = AnnotationExample.class; |
|
public class AnnotationExample { |
Class<?> c = AnnotationExample.class; |
�A�m�e�[�V�������擾�ł���N���X�iClass�EField�EMethod�EParameter�EAnnotatedType���j��AnnotatedElement�C���^�[�t�F�[�X���������Ă���̂ŁA���̃��\�b�h���g�����Ƃ��o����B[2016-12-31]
���\�b�h | ver | ���� |
---|---|---|
boolean isAnnotationPresent(�A�m�e�[�V�����N���X) |
1.5 | �w�肳�ꂽ�A�m�e�[�V�������t���Ă��邩�ǂ����B |
�A�m�e�[�V���� getAnnotation(�A�m�e�[�V�����N���X) |
1.5 | �w�肳�ꂽ�A�m�e�[�V������Ԃ��B �w�肳�ꂽ�A�m�e�[�V�������t���Ă��Ȃ��ꍇ��null��Ԃ��B |
Annotation[] getAnnotations() |
1.5 | �t���Ă���A�m�e�[�V�����ꗗ��Ԃ��B |
�A�m�e�[�V����[] getAnnotationsByType(�A�m�e�[�V�����N���X) |
1.8 | �@ |
�A�m�e�[�V���� getDeclaredAnnotation(�A�m�e�[�V�����N���X) |
1.8 | �@ |
�A�m�e�[�V����[] getDeclaredAnnotationsByType(�A�m�e�[�V�����N���X) |
1.8 | �@ |
Annotation[] getDeclaredAnnotations() |
1.5 | ���ڕt���Ă���A�m�e�[�V�����ꗗ��Ԃ��B |
���t���N�V�����ŁA�A�m�e�[�V�����ŗL�̏��i�l�j���擾���邱�Ƃ��o����B
�A�m�e�[�V������`�̃T���v���F
@Retention(RetentionPolicy.RUNTIME)
public @interface StringAnnotation {
String value();
}
@StringAnnotation("�N���X����[��") public class �T���v�� { @StringAnnotation("�R���X�g���N�_") public �T���v��() { } @StringAnnotation("�t�B�[���h���ł�") public int n; @StringAnnotation("���b�\�h") public void function( @StringAnnotation("�Ђ�����") int param, @StringAnnotation("����") int p2 ) { @StringAnnotation("���[�J���ϐ����Ȃ���") int a = 1; } }
���s���ɃA�m�e�[�V�����̓��e��\�������F
public static void main(String[] args) {
Class clazz = �T���v��.class;
dump("�N���X", clazz.getDeclaredAnnotations());
Constructor[] cs = clazz.getConstructors();
dump("�R���X�g���N�^�[", cs[0].getDeclaredAnnotations());
Field[] fs = clazz.getDeclaredFields();
dump("�t�B�[���h", fs[0].getDeclaredAnnotations());
Method[] ms = clazz.getDeclaredMethods();
dump("���\�b�h", ms[0].getDeclaredAnnotations());
Annotation[][] ma = ms[0].getParameterAnnotations();
dump("����1", ma[0]);
dump("����2", ma[1]);
//���[�J���ϐ��̃A�m�e�[�V�����͂ǂ�����Ď擾����낤�H�H
}
public static void dump(String message, Annotation[] as) { System.out.println(message); for (Annotation a : as) { dump1(a); } }
public static void dump1(Annotation a) { if (a instanceof StringAnnotation) { StringAnnotation s = (StringAnnotation)a; System.out.println(s.value()); } }
������������ƁA�C���^�[�t�F�[�X�ɒ�`���ꂽ�uvalue()�v�Ƃ������\�b�h���Ă�ł���悤�Ɍ�����B�i���ʂ�instanceof���g���邵�j
����������悤�ɂ���ׂɁA�A�m�e�[�V�����̒l��`���C���^�[�t�F�[�X��`���ۂ����Ă���낤�B
��Javassist��class�t�@�C�����̃A�m�e�[�V�������擾������@
�����Ȃ݂ɁAjavap�ł̓A�m�e�[�V�����̏��͏o�Ă��Ȃ��i�З͌�������ǁA�悭������Ȃ��c�j
�A�m�e�[�V�����̓p�b�P�[�W�ɕt���邱�Ƃ��o����B[2008-08-23]
�p�b�P�[�W�ɕt������A�m�e�[�V�����̗�F
package example.annotation.pack; @Target(ElementType.PACKAGE) @Retention(RetentionPolicy.RUNTIME) public @interface PackageAnnotation { String value(); }
�p�b�P�[�W�p�A�m�e�[�V�����́Apackage-info.java�Ƃ����\�[�X�t�@�C�������p�b�P�[�W���ɂ����t���邱�Ƃ��o���Ȃ��B
src/example/annotation/package-info.java�F
/**
* �p�b�P�[�W�ɃA�m�e�[�V������t�������
*/
@PackageAnnotation("�p�b�P�[�W�̃A�m�e�[�V����")
package example.annotation;
import example.annotation.pack.PackageAnnotation; //���̏ꍇ�ł��Aimport��pacakge���̌�ɏ���
�R���p�C������ƁA������package-info.class�������B
�iSun��javac�̏ꍇ�A�A�m�e�[�V�������t���Ă��Ȃ��ƁA�R���p�C�����Ă�package-info.class�͍���Ȃ��B
Eclipse3.2�̏ꍇ�́Apackage�ɑ���Javadoc�R�����g���A�m�e�[�V�������t���Ă�������͗l�j
��package-info.class�̒��g
�Ȃ��Apackage-info.java�̒��ɁA���ʂɃN���X���`�����肷�邱�Ƃ��o����B
�������Apublic�t���̃N���X�͒�`�ł��Ȃ��B�ipublic�̕t�����N���X�̓\�[�X�t�@�C�����ƈ�v�������K�v������Apackage-info�Ƃ����N���X���͒�`�ł��Ȃ��ipackage�͗\���Ȃ̂Ŏg���Ȃ��E�u-�v�������Z�̋L���Ȃ̂Ŏ��ʎq�ɂ͎g���Ȃ��j�ׁj
/** * �p�b�P�[�W�ɃA�m�e�[�V������2�t������� */ @PackageAnnotation("�p�b�P�[�W�̃A�m�e�[�V����") @PackAnn("package-info���Œ�`�����A�m�e�[�V����") package example.annotation; import example.annotation.pack.PackageAnnotation; //package-info.java���ŃA�m�e�[�V�������`���Ă݂�� @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PACKAGE) @interface PackAnn { String value(); }
�Ƃ͌����A����܂肱���Ƀv���O�����������ׂ�����Ȃ��낤�ȁ[�B
�p�b�P�[�W�p�A�m�e�[�V�����ł��ARetentionPolicy.RUNTIME���t�����Ă���A���s�������t���N�V�����ŃA�m�e�[�V���������擾���邱�Ƃ��o����B
Package p = Package.getPackage("example.annotation"); Annotation[] pa = p.getAnnotations(); dump("�p�b�P�[�W", pa);
���s���ʁF
@example.annotation.pack.PackageAnnotation(value=�p�b�P�[�W�̃A�m�e�[�V����) @example.annotation.PackAnn(value=package-info���Œ�`�����A�m�e�[�V����)
���Ȃ݂ɁA�R���p�C�����ꂽpackage-info.class�́A��̃C���^�[�t�F�[�X�Ƃ��č���Ă���B
���������āA�ȉ��̂悤�����t���N�V������package-info���擾���鎖���ł���B�擾���Ăǁ[�����ċC�͂��Ȃ��ł��Ȃ���(��)
Class c = Class.forName("example.annotation.package-info"); System.out.println(c);
���s���ʁF
interface example.annotation.package-info
package-info.class��javap�R�}���h�Ō���A�p�b�P�[�W�ɕt�����Ă���A�m�e�[�V�������m�F�ł���B[2019-06-02]
$ javap -v package-info �` Constant pool: �` #8 = Utf8 Lexample/annotation/MyVersion; #9 = Utf8 version #10 = Utf8 version-example �` RuntimeInvisibleAnnotations: 0: #8(#9=s#10)
�A�m�e�[�V������RetentionPolicy��RUNTIME�̏ꍇ��RuntimeVisibleAnnotations�ACLASS�̏ꍇ��RuntimeInvisibleAnnotations�̉��ɃA�m�e�[�V������\�������B�iSOURCE�̏ꍇ�͉����\������Ȃ��j
�u#�����v�͒萔�v�[���̒l���w���Ă���A��L�́u#8(#9=s#10)
�v�́uLexample/annotation/MyVersion(version="version-example")
�v�Ƃ������ƂɂȂ�悤���B
JDK1.8���O�́A1�̏ꏊ�ɓ����A�m�e�[�V�������������Ƃ͏o���Ȃ������B[2014-03-20]
JDK1.8�ȍ~�ł́A@Repeatable��t���ăA�m�e�[�V�������`����ƁA1�̏ꏊ�ɂ��̃A�m�e�[�V�������������Ƃ��o����悤�ɂȂ�B
@Repeatable���g���ɂ́A�u�����w�肵�����A�m�e�[�V�����v�Ɓu���̃A�m�e�[�V�������ێ�����A�m�e�[�V�����v���y�A�Œ�`����K�v������B
import java.lang.annotation.Repeatable;
/**
* ����ӏ��ɕ����w��\�ȃA�m�e�[�V����
*/
@Repeatable(RepeatValueHolderAnnotation.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatExampleAnnotation {
public String value();
}
/**
* �u�����w��\�ȃA�m�e�[�V�����v�̃f�[�^�����ۂɕ����ێ�����ׂ̃A�m�e�[�V����
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatValueHolderAnnotation {
public RepeatExampleAnnotation[] value();
}
@Repeatable�́A�����w�肵�����A�m�e�[�V�����ɕt����B
@Repeatable�̈����ɂ́A�g�����w�肵���A�m�e�[�V�����̏������ۂɕێ�����ׂ̃A�m�e�[�V�����h�̃N���X���w�肷��B
���́g�����w�肵���A�m�e�[�V�����̏������ۂɕێ�����ׂ̃A�m�e�[�V�����h�ł́Avalue()�̌^���g�����w�肵�����A�m�e�[�V�����̔z��h�Ƃ��Ē�`����B
�Q�l�F Java�`���[�g���A����Repeating Annotations
��L�̂悤�ɂ��Ē�`�����A�m�e�[�V�����́A�ȉ��̂悤�ɂ��Ďg�����Ƃ��o����B
@RepeatExampleAnnotation("abc") @RepeatExampleAnnotation("def") public class RepeatExample { }
����ӏ��ɕ�����`���ꂽ�A�m�e�[�V�����́A���t���N�V�����ňȉ��̂悤�ɂ��Ď擾���邱�Ƃ��o����B
�� | �擾������� | ���l |
---|---|---|
Annotation[] as =
RepeatExample.class.getAnnotations(); |
RepeatValueHolderAnnotation (value = [ |
RepeatExample�Ɏw�肳��Ă���A�m�e�[�V���������ڎ���̂ł͂Ȃ��A ������ێ����Ă���A�m�e�[�V����������B |
RepeatValueHolderAnnotation[] as = RepeatExample.class.getAnnotationsByType(RepeatValueHolderAnnotation.class); |
���� | getAnnotationsByType()���ƁA�����Ŏw�肳�ꂽ�A�m�e�[�V�����N���X�̂��̂����擾�ł���B |
RepeatValueHolderAnnotation a = RepeatExample.class.getAnnotation(RepeatValueHolderAnnotation.class); |
���� | �@ |
RepeatExampleAnnotation[] as = RepeatExample.class.getAnnotationsByType(RepeatExampleAnnotation.class); |
[ |
getAnnotationsByType()���ƁARepeatExample�ɒ��ڎw�肳�ꂽ�A�m�e�[�V�������擾���邱�Ƃ��o����B |
RepeatExampleAnnotation a = RepeatExample.class.getAnnotation(RepeatExampleAnnotation.class); |
null |
getAnnotation()�ł́A�����w�肳�ꂽ�A�m�e�[�V�������擾���邱�Ƃ͏o���Ȃ��B |
�v����ɁA�\�[�X��͕����̃A�m�e�[�V�����ڎw�肵�Ă���悤�Ɍ����邪�A
���ۂɂ͈ÖٓI�Ɂg�����f�[�^��ێ�����A�m�e�[�V�����h���w�肳��A���̓����f�[�^�Ƃ��ĕ����̃A�m�e�[�V������ێ����Ă���悤���B
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@
�@