teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

一文字変数は許されません。

2018/02/04 13:54

投稿

smk7758
smk7758

スコア36

answer CHANGED
@@ -1,11 +1,12 @@
1
1
  ```java
2
- for (Annotation a : this.getClass().getAnnotations()) {
2
+ for (Annotation annotation : this.getClass().getAnnotations()) {
3
- System.out.println("Type: " + a.annotationType().getName());
3
+ System.out.println("Type: " + annotation.annotationType().getName());
4
- if (a.annotationType().equals(Description.class)) {
4
+ if (annotation.annotationType().equals(Description.class)) {
5
- Description b = (Description) a;
5
+ Description description = (Description) annotation;
6
- System.out.println("Value: " + b.value());
6
+ System.out.println("Value: " + description.value());
7
7
  }
8
8
  }
9
9
  ```
10
10
  リフレクションですね。(こちら事情ですが、携帯からなので打ちづらい)
11
- thisを任意クラスのオブジェクトにすることで可能です。
11
+ thisを任意クラスのオブジェクト, Descriptionを任意の注釈にすることで可能です。
12
+ またvalue()のように値を返すメソッドがない場合は別の対応が必要です。(privateフィールドにアクセスかなぁ)

1

追記です。

2018/02/04 13:54

投稿

smk7758
smk7758

スコア36

answer CHANGED
@@ -1,10 +1,11 @@
1
1
  ```java
2
2
  for (Annotation a : this.getClass().getAnnotations()) {
3
- System.out.println("Type: " + a.annotationType().getName());
3
+ System.out.println("Type: " + a.annotationType().getName());
4
- if (a.annotationType().equals(Description.class)) {
4
+ if (a.annotationType().equals(Description.class)) {
5
- Description b = (Description) a;
5
+ Description b = (Description) a;
6
- System.out.println("Value: " + b.value());
6
+ System.out.println("Value: " + b.value());
7
- }
7
+ }
8
- }
8
+ }
9
9
  ```
10
- リフレクションですね。(こちら事情ですが、携帯からなので打ちづらい)
10
+ リフレクションですね。(こちら事情ですが、携帯からなので打ちづらい)
11
+ thisを任意クラスのオブジェクトにすることで可能です。