Annotations
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
An annotation is a label in the code with @. By itself it does nothing, but it's read by the compiler, by tools and
by the program itself:
@Override // the compiler checks that the method really overrides the parent's one
@Deprecated // "outdated" — a warning for those who call it
@SuppressWarnings("unchecked")
@FunctionalInterface // the interface must have exactly one abstract method
Your own annotation is declared with @interface:
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME) // keep it until run time — otherwise you won't see it
@Target(ElementType.METHOD) // put it only on methods
@interface Command {
String value(); // a parameter: @Command("help")
int cost() default 0;
}
At run time it's read through reflection: method.getAnnotation(Command.class). This is how
frameworks work: Spring (@GetMapping), JUnit (@Test), Minecraft mods (@Mod, @SubscribeEvent).
Task. Finish the @Command annotation (the command's name and cost, 0 by default) and mark the methods of
Shop with it. Through reflection the program collects the commands and carries out the input. Methods without the annotation don't
count as commands.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.