Debian Codegraph @

src/org/intellij/lang/annotations/MagicConstant.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * Copyright 2000-2012 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.intellij.lang.annotations;
17
18 import java.lang.annotation.*;
19
20 /**
21 * <p>This annotation intended to help IDEA to detect and auto-complete int and String constants used as an enumeration.
22 * For example, in the {@link java.awt.Label#Label(String, int)} constructor the <tt><b>alignment</b></tt> parameter can be one of the following
23 * int constants: {@link java.awt.Label#LEFT}, {@link java.awt.Label#CENTER} or {@link java.awt.Label#RIGHT}
24 *
25 * <p>So, if <tt>@MagicConstant</tt> annotation applied to this constructor, IDEA will check the constructor usages for the allowed values.
26 * <p>E.g.<br>
27 * <pre>{@code
28 * new Label("text", 0); // 0 is not allowed
29 * new Label("text", Label.LEFT); // OK
30 * }</pre>
31 *
32 * <p>
33 * <tt>@MagicConstant</tt> can be applied to:
34 * <ul>
35 * <li> Field, local variable, parameter.
36 *
37 * <br>E.g. <br>
38 * <pre>{@code @MagicConstant(intValues = {TOP, CENTER, BOTTOM})
39 * int textPosition;
40 * }</pre>
41 * IDEA will check expressions assigned to the variable for allowed values:
42 * <pre>{@code
43 * textPosition = 0; // not allowed
44 * textPosition = TOP; // OK
45 * }</pre>
46 *
47 * <li> Method
48 *
49 * <br>E.g.<br>
50 * <pre>{@code @MagicConstant(flagsFromClass = java.lang.reflect.Modifier.class)
51 * public native int getModifiers();
52 * }</pre>
53 *
54 * IDEA will analyse getModifiers() method calls and check if its return value is used with allowed values:<br>
55 * <pre>{@code
56 * if (aClass.getModifiers() == 3) // not allowed
57 * if (aClass.getModifiers() == Modifier.PUBLIC) // OK
58 * }</pre>
59 *
60 * <li>Annotation class<br>
61 * Annotation class annotated with <tt>@MagicConstant</tt> created alias you can use to annotate
62 * everything as if it was annotated with <tt>@MagicConstant</tt> itself.
63 *
64 * <br>E.g.<br>
65 * <pre>{@code @MagicConstant(flags = {Font.PLAIN, Font.BOLD, Font.ITALIC}) }</pre>
66 * <pre>{@code @interface FontStyle {} }</pre>
67 *
68 * IDEA will check constructs annotated with @FontStyle for allowed values:<br>
69 * <tt>@FontStyle int myStyle = 3; // not allowed<br></tt>
70 * <tt>@FontStyle int myStyle = Font.BOLD | Font.ITALIC; // OK</tt><br>
71 * </tt>
72 *
73 * </ul>
74 *
75 * The <tt>@MagicConstant</tt> annotation has SOURCE retention, i.e. it is removed upon compilation and does not create any runtime overhead.
76 */
77 @Retention(RetentionPolicy.SOURCE)
78 @Target({
79 ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE,
80 ElementType.ANNOTATION_TYPE,
81 ElementType.METHOD
82 })
83 public @interface MagicConstant {
84 /**
85 * @return int values (typically named constants) which are allowed here.
86 * E.g.
87 * <tt>
88 * void setConfirmOpenNewProject(@MagicConstant(intValues = {OPEN_PROJECT_ASK, OPEN_PROJECT_NEW_WINDOW, OPEN_PROJECT_SAME_WINDOW})
89 * int confirmOpenNewProject);
90 * </tt>
91 */
92 long[] intValues() default {};
93
94 /**
95 * @return String values (typically named constants) which are allowed here.
96 */
97 String[] stringValues() default {};
98
99 /**
100 * @return allowed int flags (i.e. values (typically named constants) which can be combined with bitwise or operator (|).
101 * Also 0 and -1 are considered allowed.
102 * E.g.
103 * <tt>
104 * @MagicConstant(flags = {HierarchyEvent.PARENT_CHANGED,HierarchyEvent.DISPLAYABILITY_CHANGED,HierarchyEvent.SHOWING_CHANGED})
105 * int hFlags;
106 *
107 * hFlags = 3; // not allowed
108 * if (hFlags & (HierarchyEvent.PARENT_CHANGED | HierarchyEvent.SHOWING_CHANGED) != 0); // OK
109 * </tt>
110 */
111 long[] flags() default {};
112
113 /**
114 * @return allowed values which are defined in the specified class public static final constants.
115 *
116 * E.g.
117 * <tt>
118 * @MagicConstant(valuesFromClass = Cursor.class)
119 * int cursorType;
120 *
121 * cursorType = 11; // not allowed;
122 * cursorType = Cursor.E_RESIZE_CURSOR; // OK
123 * </tt>
124 */
125 Class valuesFromClass() default void.class;
126
127
128 /**
129 * @return allowed int flags which are defined in the specified class public static final constants.
130 *
131 * E.g.
132 * <tt>@MagicConstant(flagsFromClass = java.awt.InputEvent.class)
133 * int eventMask;
134 *
135 * eventMask = 10; // not allowed;
136 * eventMask = InputEvent.CTRL_MASK | InputEvent.ALT_MASK; // OK
137 * </tt>
138 */
139 Class flagsFromClass() default void.class;
140 }