Skip to content

Instantly share code, notes, and snippets.

@Microtribute
Last active April 23, 2020 19:28
Show Gist options
  • Save Microtribute/ffc6a747f6a98f809623367f69cf7e9e to your computer and use it in GitHub Desktop.
Save Microtribute/ffc6a747f6a98f809623367f69cf7e9e to your computer and use it in GitHub Desktop.
Java Annotation Example
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};
}
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();
}
}
package com.example.demo;
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Inherited
public @interface Couples {
Coupled[] value();
}
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";
}
}
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]";
}
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