回答編集履歴
2
一文字変数は許されません。
answer
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
```java
|
2
|
-
for (Annotation
|
2
|
+
for (Annotation annotation : this.getClass().getAnnotations()) {
|
3
|
-
System.out.println("Type: " +
|
3
|
+
System.out.println("Type: " + annotation.annotationType().getName());
|
4
|
-
if (
|
4
|
+
if (annotation.annotationType().equals(Description.class)) {
|
5
|
-
Description
|
5
|
+
Description description = (Description) annotation;
|
6
|
-
System.out.println("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
追記です。
answer
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
```java
|
2
2
|
for (Annotation a : this.getClass().getAnnotations()) {
|
3
|
-
|
3
|
+
System.out.println("Type: " + a.annotationType().getName());
|
4
|
-
|
4
|
+
if (a.annotationType().equals(Description.class)) {
|
5
|
-
|
5
|
+
Description b = (Description) a;
|
6
|
-
|
6
|
+
System.out.println("Value: " + b.value());
|
7
|
-
|
7
|
+
}
|
8
|
-
|
8
|
+
}
|
9
9
|
```
|
10
|
-
リフレクションですね。(こちら事情ですが、携帯からなので打ちづらい)
|
10
|
+
リフレクションですね。(こちら事情ですが、携帯からなので打ちづらい)
|
11
|
+
thisを任意クラスのオブジェクトにすることで可能です。
|