Last active
April 23, 2020 19:28
-
-
Save Microtribute/ffc6a747f6a98f809623367f69cf7e9e to your computer and use it in GitHub Desktop.
Java Annotation Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Target({ElementType.TYPE, ElementType.TYPE_USE}) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface Alien { | |
@Required int @Required [] even() default {1, 3, 5}; | |
// C-Style array element declaration (or extended dimensions according to aspectj) | |
@Required int odd() @Required [] default {2, 4, 6}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.checkerframework.checker.index.qual.Positive; | |
import java.lang.annotation.*; | |
@Documented | |
@Retention(RetentionPolicy.RUNTIME) | |
@Coupled(@Coupled.CouplingMethod("virtual")) | |
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.TYPE_USE}) | |
@Inherited | |
@Repeatable(Couples.class) | |
public @interface Coupled { | |
CouplingMethod value() default @CouplingMethod("default"); | |
@Positive int[] arguments() default {1, 2, 3}; | |
@interface CouplingMethod { | |
String value(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import java.lang.annotation.*; | |
@Documented | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) | |
@Inherited | |
public @interface Couples { | |
Coupled[] value(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.jetbrains.annotations.Contract; | |
import org.jetbrains.annotations.NotNull; | |
@Alien | |
public class Main { | |
public static void main(String[] args) throws NoSuchMethodException { | |
var method1 = Main.class.getMethod("getRequiredString"); | |
var method2 = Main.class.getMethod("getRequiredString2"); | |
/* | |
The output is: | |
1 | |
2 | |
1 | |
3 | |
*/ | |
System.out.println(method1.getAnnotations().length); | |
System.out.println(method2.getAnnotations().length); | |
System.out.println(method1.getAnnotatedReturnType().getAnnotations().length); | |
System.out.println(method2.getAnnotatedReturnType().getAnnotations().length); | |
} | |
@Contract(value = " -> new", pure = true) | |
public static @Required int @Required @NotNull [] createIntArray() { | |
return new int[]{1, 2, 3}; | |
} | |
@Contract(value = " -> new", pure = true) | |
public static @Required @NotNull int createIntArrayAlternate() @Required [] { | |
return new int[]{1, 2, 3}; | |
} | |
@Preliminary | |
public String getRequiredString() { | |
return "okay"; | |
} | |
public @Preliminary @Required @Coupled String getRequiredString2() { | |
return "wow"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.Target; | |
import static java.lang.annotation.RetentionPolicy.RUNTIME; | |
@Target({ElementType.TYPE, ElementType.TYPE_USE, ElementType.METHOD, ElementType.PARAMETER}) | |
@Retention(RUNTIME) | |
public @interface Preliminary { | |
String value() default "[default]"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Target({ElementType.TYPE, ElementType.TYPE_USE, ElementType.METHOD, ElementType.PARAMETER}) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface Required { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment