adding built-in java annotation examples (#3087)

This commit is contained in:
Adam Arold 2017-11-20 20:11:34 +01:00 committed by maibin
parent dba132f7de
commit 2061f5b7eb
12 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package com.baeldung.annotations;
@Deprecated
class ClassWithAnnotation {
}

View File

@ -0,0 +1,9 @@
package com.baeldung.annotations;
class ClassWithDeprecatedMethod {
@Deprecated
static void deprecatedMethod() {
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.annotations;
class ClassWithSafeVarargs<T> {
@SafeVarargs
final void iterateOverVarargs(T... args) {
for (T x : args) {
// do stuff with x
}
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.annotations;
class ClassWithSuppressWarnings {
@SuppressWarnings("deprecation")
void useDeprecatedMethod() {
ClassWithDeprecatedMethod.deprecatedMethod(); // no warning is generated here
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.annotations;
@FunctionalInterface
interface IntConsumer {
void accept(Integer number);
}

View File

@ -0,0 +1,8 @@
package com.baeldung.annotations;
import java.lang.annotation.Repeatable;
@Repeatable(Intervals.class)
@interface Interval {
int hour() default 1;
}

View File

@ -0,0 +1,9 @@
package com.baeldung.annotations;
public class IntervalUsage {
@Interval(hour = 17)
@Interval(hour = 13)
void doPeriodicCleanup() {
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.annotations;
@interface Intervals {
Interval[] value();
}

View File

@ -0,0 +1,11 @@
package com.baeldung.annotations;
import java.lang.annotation.*;
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.LOCAL_VARIABLE, ElementType.FIELD})
@interface MyAnnotation {
}

View File

@ -0,0 +1,16 @@
package com.baeldung.annotations;
class MyAnnotationTarget {
// this is OK
@MyAnnotation
String someField;
// @MyAnnotation <- this is invalid usage!
void doSomething() {
// this also works
@MyAnnotation
String localVariable;
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.annotations;
interface MyOperation {
void perform();
}

View File

@ -0,0 +1,9 @@
package com.baeldung.annotations;
class MyOperationImpl implements MyOperation {
@Override
public void perform() {
}
}