You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 12-annotations.md
+19-7Lines changed: 19 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,8 @@
1
1
Annotation Improvements
2
2
-------
3
3
4
-
There are couple of improvements made in the annotation mechanism in Java 8. These are:
4
+
There are couple of improvements made in the annotation mechanism in Java 8.
5
+
These are:
5
6
6
7
1. Repeatable annotations
7
8
2. Type annotations
@@ -10,7 +11,10 @@ There are couple of improvements made in the annotation mechanism in Java 8. The
10
11
11
12
## Repeatable annotations
12
13
13
-
Before Java 8, it was not possible to use same annotation twice at the same location i.e. you can't use annotation `@Foo` twice on a method. If you have used JPA then you would have use an annotation called `@JoinColumns` that allows wraps multiple `@JoinColumn` annotation as shown below.
14
+
Before Java 8, it was not possible to use same annotation twice at the same
15
+
location i.e. you can't use annotation `@Foo` twice on a method. If you have
16
+
used JPA then you would have use an annotation called `@JoinColumns` that allows
17
+
wraps multiple `@JoinColumn` annotation as shown below.
14
18
15
19
```java
16
20
@ManyToOne
@@ -30,14 +34,18 @@ In Java 8, you can write the same as shown below because with Java 8 you can rep
30
34
publicAddress getAddress() { return address; }
31
35
```
32
36
33
-
34
-
***With Java 8 you can use same annotation type multiple times possibly with different arguments at any location(class, method, field declaration) in your Java code.***
37
+
***With Java 8 you can use same annotation type multiple times possibly with
38
+
different arguments at any location(class, method, field declaration) in your
39
+
Java code.***
35
40
36
41
### Writing your own repeatable Annotations
37
42
38
43
To write your own repeatable annotation you have to do the following:
39
44
40
-
**Step 1:** Create an annotation with `@Repeatable` annotation as shown below. `@Repeatable` annotation requires you to specify a mandatory value for the container type that will contain the annotation. We will create container annotation in step 2.
45
+
**Step 1:** Create an annotation with `@Repeatable` annotation as shown below.
46
+
`@Repeatable` annotation requires you to specify a mandatory value for the
47
+
container type that will contain the annotation. We will create container
48
+
annotation in step 2.
41
49
42
50
```java
43
51
importjava.lang.annotation.*;
@@ -70,7 +78,10 @@ public void manage() {
70
78
}
71
79
```
72
80
73
-
If you have to find all the repeatable annotations on a method then you can use `getAnnotationsByType` method that is now available on `java.lang.Class` and `java.lang.reflect.Method`. To print all the vm names, you can write code as shown below.
81
+
If you have to find all the repeatable annotations on a method then you can use
82
+
`getAnnotationsByType` method that is now available on `java.lang.Class` and
83
+
`java.lang.reflect.Method`. To print all the vm names, you can write code as
0 commit comments