From 2061f5b7eb84fce002f924a4862059f6dca35a3a Mon Sep 17 00:00:00 2001 From: Adam Arold Date: Mon, 20 Nov 2017 20:11:34 +0100 Subject: [PATCH] adding built-in java annotation examples (#3087) --- .../annotations/ClassWithAnnotation.java | 5 +++++ .../annotations/ClassWithDeprecatedMethod.java | 9 +++++++++ .../annotations/ClassWithSafeVarargs.java | 11 +++++++++++ .../annotations/ClassWithSuppressWarnings.java | 9 +++++++++ .../com/baeldung/annotations/IntConsumer.java | 8 ++++++++ .../java/com/baeldung/annotations/Interval.java | 8 ++++++++ .../com/baeldung/annotations/IntervalUsage.java | 9 +++++++++ .../java/com/baeldung/annotations/Intervals.java | 5 +++++ .../com/baeldung/annotations/MyAnnotation.java | 11 +++++++++++ .../baeldung/annotations/MyAnnotationTarget.java | 16 ++++++++++++++++ .../com/baeldung/annotations/MyOperation.java | 6 ++++++ .../baeldung/annotations/MyOperationImpl.java | 9 +++++++++ 12 files changed, 106 insertions(+) create mode 100644 core-java-8/src/main/java/com/baeldung/annotations/ClassWithAnnotation.java create mode 100644 core-java-8/src/main/java/com/baeldung/annotations/ClassWithDeprecatedMethod.java create mode 100644 core-java-8/src/main/java/com/baeldung/annotations/ClassWithSafeVarargs.java create mode 100644 core-java-8/src/main/java/com/baeldung/annotations/ClassWithSuppressWarnings.java create mode 100644 core-java-8/src/main/java/com/baeldung/annotations/IntConsumer.java create mode 100644 core-java-8/src/main/java/com/baeldung/annotations/Interval.java create mode 100644 core-java-8/src/main/java/com/baeldung/annotations/IntervalUsage.java create mode 100644 core-java-8/src/main/java/com/baeldung/annotations/Intervals.java create mode 100644 core-java-8/src/main/java/com/baeldung/annotations/MyAnnotation.java create mode 100644 core-java-8/src/main/java/com/baeldung/annotations/MyAnnotationTarget.java create mode 100644 core-java-8/src/main/java/com/baeldung/annotations/MyOperation.java create mode 100644 core-java-8/src/main/java/com/baeldung/annotations/MyOperationImpl.java diff --git a/core-java-8/src/main/java/com/baeldung/annotations/ClassWithAnnotation.java b/core-java-8/src/main/java/com/baeldung/annotations/ClassWithAnnotation.java new file mode 100644 index 0000000000..034e6785a4 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/annotations/ClassWithAnnotation.java @@ -0,0 +1,5 @@ +package com.baeldung.annotations; + +@Deprecated +class ClassWithAnnotation { +} diff --git a/core-java-8/src/main/java/com/baeldung/annotations/ClassWithDeprecatedMethod.java b/core-java-8/src/main/java/com/baeldung/annotations/ClassWithDeprecatedMethod.java new file mode 100644 index 0000000000..6f5da03b74 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/annotations/ClassWithDeprecatedMethod.java @@ -0,0 +1,9 @@ +package com.baeldung.annotations; + +class ClassWithDeprecatedMethod { + + @Deprecated + static void deprecatedMethod() { + + } +} diff --git a/core-java-8/src/main/java/com/baeldung/annotations/ClassWithSafeVarargs.java b/core-java-8/src/main/java/com/baeldung/annotations/ClassWithSafeVarargs.java new file mode 100644 index 0000000000..cfa91f5951 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/annotations/ClassWithSafeVarargs.java @@ -0,0 +1,11 @@ +package com.baeldung.annotations; + +class ClassWithSafeVarargs { + + @SafeVarargs + final void iterateOverVarargs(T... args) { + for (T x : args) { + // do stuff with x + } + } +} diff --git a/core-java-8/src/main/java/com/baeldung/annotations/ClassWithSuppressWarnings.java b/core-java-8/src/main/java/com/baeldung/annotations/ClassWithSuppressWarnings.java new file mode 100644 index 0000000000..fe22ec1c24 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/annotations/ClassWithSuppressWarnings.java @@ -0,0 +1,9 @@ +package com.baeldung.annotations; + +class ClassWithSuppressWarnings { + + @SuppressWarnings("deprecation") + void useDeprecatedMethod() { + ClassWithDeprecatedMethod.deprecatedMethod(); // no warning is generated here + } +} diff --git a/core-java-8/src/main/java/com/baeldung/annotations/IntConsumer.java b/core-java-8/src/main/java/com/baeldung/annotations/IntConsumer.java new file mode 100644 index 0000000000..4f16b27281 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/annotations/IntConsumer.java @@ -0,0 +1,8 @@ +package com.baeldung.annotations; + +@FunctionalInterface +interface IntConsumer { + + void accept(Integer number); + +} diff --git a/core-java-8/src/main/java/com/baeldung/annotations/Interval.java b/core-java-8/src/main/java/com/baeldung/annotations/Interval.java new file mode 100644 index 0000000000..f73e6e5b14 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/annotations/Interval.java @@ -0,0 +1,8 @@ +package com.baeldung.annotations; + +import java.lang.annotation.Repeatable; + +@Repeatable(Intervals.class) +@interface Interval { + int hour() default 1; +} diff --git a/core-java-8/src/main/java/com/baeldung/annotations/IntervalUsage.java b/core-java-8/src/main/java/com/baeldung/annotations/IntervalUsage.java new file mode 100644 index 0000000000..2e11de8215 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/annotations/IntervalUsage.java @@ -0,0 +1,9 @@ +package com.baeldung.annotations; + +public class IntervalUsage { + + @Interval(hour = 17) + @Interval(hour = 13) + void doPeriodicCleanup() { + } +} diff --git a/core-java-8/src/main/java/com/baeldung/annotations/Intervals.java b/core-java-8/src/main/java/com/baeldung/annotations/Intervals.java new file mode 100644 index 0000000000..af469f18cc --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/annotations/Intervals.java @@ -0,0 +1,5 @@ +package com.baeldung.annotations; + +@interface Intervals { + Interval[] value(); +} diff --git a/core-java-8/src/main/java/com/baeldung/annotations/MyAnnotation.java b/core-java-8/src/main/java/com/baeldung/annotations/MyAnnotation.java new file mode 100644 index 0000000000..6e71f446b0 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/annotations/MyAnnotation.java @@ -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 { + +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/annotations/MyAnnotationTarget.java b/core-java-8/src/main/java/com/baeldung/annotations/MyAnnotationTarget.java new file mode 100644 index 0000000000..37f40a624e --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/annotations/MyAnnotationTarget.java @@ -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; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/annotations/MyOperation.java b/core-java-8/src/main/java/com/baeldung/annotations/MyOperation.java new file mode 100644 index 0000000000..a4385bc786 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/annotations/MyOperation.java @@ -0,0 +1,6 @@ +package com.baeldung.annotations; + +interface MyOperation { + + void perform(); +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/annotations/MyOperationImpl.java b/core-java-8/src/main/java/com/baeldung/annotations/MyOperationImpl.java new file mode 100644 index 0000000000..e6a8ce76d3 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/annotations/MyOperationImpl.java @@ -0,0 +1,9 @@ +package com.baeldung.annotations; + +class MyOperationImpl implements MyOperation { + + @Override + public void perform() { + + } +} \ No newline at end of file