「Java8」クラスのメソッドにアノテーションを付ける
環境
JDK1.8
Eclipse2019
書式
1.独自のアノテーションの定義
public @interface アノテーション名{
}
@Retention(アノテーションを保持する範囲)
RetentionPolicy.RUNTIME
リフレクションを使うとアノテーションの情報を取得できます。
2.@Target(アノテーションを適用する場所)
ElementType.TYPE
クラス、インターフェース、アノテーション、enum型に適用します。
ElementType.METHOD
メソッドに適用します。
使用例
1.独自のアノテーションの定義
package com.arkgame.study;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//アノテーションの範囲
@Retention(RetentionPolicy.RUNTIME)
//アノテーションを適用する場所 クラスとメソッド
@Target({
ElementType.TYPE,
ElementType.METHOD })
//アノテーションの定義
public @interface AnnoUser {
String sno() default "1001";
String user();
}
package com.arkgame.study;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//アノテーションの範囲
@Retention(RetentionPolicy.RUNTIME)
//アノテーションを適用する場所 クラスとメソッド
@Target({
ElementType.TYPE,
ElementType.METHOD })
//アノテーションの定義
public @interface AnnoUser {
String sno() default "1001";
String user();
}
package com.arkgame.study; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //アノテーションの範囲 @Retention(RetentionPolicy.RUNTIME) //アノテーションを適用する場所 クラスとメソッド @Target({ ElementType.TYPE, ElementType.METHOD }) //アノテーションの定義 public @interface AnnoUser { String sno() default "1001"; String user(); }
2.クラスのメソッドにアノテーションを付ける
関数
public Method getMethod(String name,Class<?>... parameterTypes)
throws NoSuchMethodException,SecurityException
変数名.getMethod(メソッド名, new Class[] {});
public Method getMethod(String name,Class<?>... parameterTypes)
throws NoSuchMethodException,SecurityException
変数名.getMethod(メソッド名, new Class[] {});
public Method getMethod(String name,Class<?>... parameterTypes) throws NoSuchMethodException,SecurityException 変数名.getMethod(メソッド名, new Class[] {});
サンプルコード
package com.arkgame.study;
import java.lang.reflect.Method;
class Demo {
//メソッドfunAにアノテーションを付ける
@AnnoUser(sno = "8008", user = "大橋 次郎")
public void funA() {
System.out.println("test message");
}
}
public class UserTest {
public static void main(String[] args) throws Exception {
//アノテーションの値を取得するためにリフレクションを使用する
Class<?> ca = Class.forName("com.arkgame.study.Demo");
Method method = ca.getMethod("funA", new Class[] {});
//getAnnotationメソッドでアノテーションを取得
AnnoUser au = (AnnoUser) method.getAnnotation(AnnoUser.class);
//取得したアノテーションの値を出力する
System.out.println("ユーザー名前: " + au.user());
System.out.println("ユーザー番号:" + au.sno());
}
}
package com.arkgame.study;
import java.lang.reflect.Method;
class Demo {
//メソッドfunAにアノテーションを付ける
@AnnoUser(sno = "8008", user = "大橋 次郎")
public void funA() {
System.out.println("test message");
}
}
public class UserTest {
public static void main(String[] args) throws Exception {
//アノテーションの値を取得するためにリフレクションを使用する
Class<?> ca = Class.forName("com.arkgame.study.Demo");
Method method = ca.getMethod("funA", new Class[] {});
//getAnnotationメソッドでアノテーションを取得
AnnoUser au = (AnnoUser) method.getAnnotation(AnnoUser.class);
//取得したアノテーションの値を出力する
System.out.println("ユーザー名前: " + au.user());
System.out.println("ユーザー番号:" + au.sno());
}
}
package com.arkgame.study; import java.lang.reflect.Method; class Demo { //メソッドfunAにアノテーションを付ける @AnnoUser(sno = "8008", user = "大橋 次郎") public void funA() { System.out.println("test message"); } } public class UserTest { public static void main(String[] args) throws Exception { //アノテーションの値を取得するためにリフレクションを使用する Class<?> ca = Class.forName("com.arkgame.study.Demo"); Method method = ca.getMethod("funA", new Class[] {}); //getAnnotationメソッドでアノテーションを取得 AnnoUser au = (AnnoUser) method.getAnnotation(AnnoUser.class); //取得したアノテーションの値を出力する System.out.println("ユーザー名前: " + au.user()); System.out.println("ユーザー番号:" + au.sno()); } }
実行結果
ユーザー名前: 大橋 次郎
ユーザー番号:8008