Skip to content

Commit 2061f5b

Browse files
Adam Aroldmaibin
authored andcommitted
adding built-in java annotation examples (eugenp#3087)
1 parent dba132f commit 2061f5b

12 files changed

Lines changed: 106 additions & 0 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.annotations;
2+
3+
@Deprecated
4+
class ClassWithAnnotation {
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.annotations;
2+
3+
class ClassWithDeprecatedMethod {
4+
5+
@Deprecated
6+
static void deprecatedMethod() {
7+
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.annotations;
2+
3+
class ClassWithSafeVarargs<T> {
4+
5+
@SafeVarargs
6+
final void iterateOverVarargs(T... args) {
7+
for (T x : args) {
8+
// do stuff with x
9+
}
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.annotations;
2+
3+
class ClassWithSuppressWarnings {
4+
5+
@SuppressWarnings("deprecation")
6+
void useDeprecatedMethod() {
7+
ClassWithDeprecatedMethod.deprecatedMethod(); // no warning is generated here
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.annotations;
2+
3+
@FunctionalInterface
4+
interface IntConsumer {
5+
6+
void accept(Integer number);
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.annotations;
2+
3+
import java.lang.annotation.Repeatable;
4+
5+
@Repeatable(Intervals.class)
6+
@interface Interval {
7+
int hour() default 1;
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.annotations;
2+
3+
public class IntervalUsage {
4+
5+
@Interval(hour = 17)
6+
@Interval(hour = 13)
7+
void doPeriodicCleanup() {
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.annotations;
2+
3+
@interface Intervals {
4+
Interval[] value();
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.annotations;
2+
3+
import java.lang.annotation.*;
4+
5+
@Inherited
6+
@Documented
7+
@Retention(RetentionPolicy.RUNTIME)
8+
@Target({ElementType.LOCAL_VARIABLE, ElementType.FIELD})
9+
@interface MyAnnotation {
10+
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.annotations;
2+
3+
class MyAnnotationTarget {
4+
5+
// this is OK
6+
@MyAnnotation
7+
String someField;
8+
9+
// @MyAnnotation <- this is invalid usage!
10+
void doSomething() {
11+
12+
// this also works
13+
@MyAnnotation
14+
String localVariable;
15+
}
16+
}

0 commit comments

Comments
 (0)