回答編集履歴

2

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

2018/02/04 13:54

投稿

smk7758
smk7758

スコア36

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

1

追記です。

2018/02/04 13:54

投稿

smk7758
smk7758

スコア36

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