@Retention(value=SOURCE) @Target(value={FIELD,PARAMETER,LOCAL_VARIABLE,ANNOTATION_TYPE,METHOD}) public @interface MagicConstant
This annotation intended to help IDEA to detect and auto-complete int and String constants used as an enumeration.
For example, in the Label(String, int)
constructor the alignment parameter can be one of the following
int constants: Label.LEFT
, Label.CENTER
or Label.RIGHT
So, if @MagicConstant annotation applied to this constructor, IDEA will check the constructor usages for the allowed values.
E.g.
new Label("text", 0); // 0 is not allowed
new Label("text", Label.LEFT); // OK
@MagicConstant can be applied to:
@MagicConstant(intValues = {TOP, CENTER, BOTTOM})
int textPosition;
IDEA will check expressions assigned to the variable for allowed values:
textPosition = 0; // not allowed
textPosition = TOP; // OK
@MagicConstant(flagsFromClass = java.lang.reflect.Modifier.class)
public native int getModifiers();
IDEA will analyse getModifiers() method calls and check if its return value is used with allowed values:
if (aClass.getModifiers() == 3) // not allowed
if (aClass.getModifiers() == Modifier.PUBLIC) // OK
@MagicConstant(flags = {Font.PLAIN, Font.BOLD, Font.ITALIC})
@interface FontStyle {}
IDEA will check constructs annotated with @FontStyle for allowed values:Modifier and Type | Optional Element and Description |
---|---|
long[] |
flags |
Class |
flagsFromClass |
long[] |
intValues |
String[] |
stringValues |
Class |
valuesFromClass |
public abstract long[] intValues
void setConfirmOpenNewProject(@MagicConstant(intValues = {OPEN_PROJECT_ASK, OPEN_PROJECT_NEW_WINDOW, OPEN_PROJECT_SAME_WINDOW})
int confirmOpenNewProject);
public abstract String[] stringValues
public abstract long[] flags
@MagicConstant(flags = {HierarchyEvent.PARENT_CHANGED,HierarchyEvent.DISPLAYABILITY_CHANGED,HierarchyEvent.SHOWING_CHANGED})
int hFlags;
hFlags = 3; // not allowed
if (hFlags & (HierarchyEvent.PARENT_CHANGED | HierarchyEvent.SHOWING_CHANGED) != 0); // OK
public abstract Class valuesFromClass
@MagicConstant(valuesFromClass = Cursor.class)
int cursorType;
cursorType = 11; // not allowed;
cursorType = Cursor.E_RESIZE_CURSOR; // OK
public abstract Class flagsFromClass
@MagicConstant(flagsFromClass = java.awt.InputEvent.class)
int eventMask;
eventMask = 10; // not allowed;
eventMask = InputEvent.CTRL_MASK | InputEvent.ALT_MASK; // OK
Copyright © 2022. All rights reserved.