This annotation intended to help IntelliJ IDEA and other IDEs to detect and auto-complete int and String constants used as an enumeration.
For example, in the java. constructor the alignment parameter can be one of the following
int constants: java., java. or java.
So, if @MagicConstant annotation applied to this constructor, the IDE 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;
The IDE 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();
The IDE 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 created alias you can use to annotate
everything as if it was annotated with @MagicConstant itself.
@MagicConstant(flags = {Font.PLAIN, Font.BOLD, Font.ITALIC})
@interface FontStyle {}
The IDE will check constructs annotated with @FontStyle for allowed values:@FontStyle int myStyle = 3; // not allowed
@FontStyle int myStyle = Font.BOLD | Font.ITALIC; // OK@MagicConstant annotation has SOURCE retention, i.e. it is removed upon compilation and does not create any runtime overhead.
| Modifier and Type | Method and Description |
|---|---|
| public long[] | Returns: allowed int flags (i.e. values (typically named constants) which can be combined with bitwise OR operator (|). The difference from theintValues() is that flags are allowed to be combined (via plus:+ or bitwise OR: |) whereas values aren't.
The literals "0" and "-1" are also allowed to denote absence and presense of all flags respectively.
E.g.
|
| public Class | Returns: allowed int flags which are defined in the specified class static final constants. The difference from thevaluesFromClass() is that flags are allowed to be combined (via plus:+ or bitwise OR: |) whereas values aren't.
The literals "0" and "-1" are also allowed to denote absence and presense of all flags respectively.
E.g.
|
| public long[] | Returns: int values (typically named constants) which are allowed here. E.g.
|
| public @NonNls String[] | |
| public Class | Returns: allowed values which are defined in the specified class static final constants. E.g.
|
| flags | back to summary |
|---|---|
public long[] flags()
| |
| flagsFromClass | back to summary |
|---|---|
public Class
| |
| intValues | back to summary |
|---|---|
public long[] intValues()
| |
| stringValues | back to summary |
|---|---|
| public @NonNls String[] stringValues() | |
| valuesFromClass | back to summary |
|---|---|
public Class
| |