Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dd4ea0869ed69591b175cd9214bdce7dff318bcd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*******************************************************************************
 * Copyright (c) 2000, 2019 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     IBM Corporation - added constant AccDefault
 *     IBM Corporation - added constants AccBridge and AccVarargs for J2SE 1.5
 *******************************************************************************/
package org.eclipse.jdt.core;

import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;

/**
 * Utility class for decoding modifier flags in Java elements.
 * <p>
 * This class provides static methods only.
 * </p>
 * <p>
 * Note that the numeric values of these flags match the ones for class files
 * as described in the Java Virtual Machine Specification (except for
 * {@link #AccDeprecated}, {@link #AccAnnotationDefault}, and {@link #AccDefaultMethod}).
 * </p>
 * <p>
 * The AST class <code>Modifier</code> provides
 * similar functionality as this class, only in the
 * <code>org.eclipse.jdt.core.dom</code> package.
 * </p>
 *
 * @see IMember#getFlags()
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public final class Flags {

	/**
	 * Constant representing the absence of any flag.
	 * @since 3.0
	 */
	public static final int AccDefault = ClassFileConstants.AccDefault;
	/**
	 * Public access flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccPublic = ClassFileConstants.AccPublic;
	/**
	 * Private access flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccPrivate = ClassFileConstants.AccPrivate;
	/**
	 * Protected access flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccProtected = ClassFileConstants.AccProtected;
	/**
	 * Static access flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccStatic = ClassFileConstants.AccStatic;
	/**
	 * Final access flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccFinal = ClassFileConstants.AccFinal;
	/**
	 * Synchronized access flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccSynchronized = ClassFileConstants.AccSynchronized;
	/**
	 * Volatile property flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccVolatile = ClassFileConstants.AccVolatile;
	/**
	 * Transient property flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccTransient = ClassFileConstants.AccTransient;
	/**
	 * Native property flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccNative = ClassFileConstants.AccNative;
	/**
	 * Interface property flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccInterface = ClassFileConstants.AccInterface;
	/**
	 * Abstract property flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccAbstract = ClassFileConstants.AccAbstract;
	/**
	 * Strictfp property flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccStrictfp = ClassFileConstants.AccStrictfp;
	/**
	 * Super property flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccSuper = ClassFileConstants.AccSuper;
	/**
	 * Synthetic property flag. See The Java Virtual Machine Specification for more details.
	 * @since 2.0
	 */
	public static final int AccSynthetic = ClassFileConstants.AccSynthetic;
	/**
	 * Deprecated property flag.
	 * <p>
	 * Note that this flag's value is internal and is not defined in the
	 * Virtual Machine specification.
	 * </p>
	 * @since 2.0
	 */
	public static final int AccDeprecated = ClassFileConstants.AccDeprecated;

	/**
	 * Bridge method property flag (added in J2SE 1.5). Used to flag a compiler-generated
	 * bridge methods.
	 * See The Java Virtual Machine Specification for more details.
	 * @since 3.0
	 */
	public static final int AccBridge = ClassFileConstants.AccBridge;

	/**
	 * Varargs method property flag (added in J2SE 1.5).
	 * Used to flag variable arity method declarations.
	 * See The Java Virtual Machine Specification for more details.
	 * @since 3.0
	 */
	public static final int AccVarargs = ClassFileConstants.AccVarargs;

	/**
	 * Enum property flag (added in J2SE 1.5).
	 * See The Java Virtual Machine Specification for more details.
	 * @since 3.0
	 */
	public static final int AccEnum = ClassFileConstants.AccEnum;

	/**
	 * Annotation property flag (added in J2SE 1.5).
	 * See The Java Virtual Machine Specification for more details.
	 * @since 3.0
	 */
	public static final int AccAnnotation = ClassFileConstants.AccAnnotation;

	/**
	 * Default method property flag.
	 * <p>
	 * Note that this flag's value is internal and is not defined in the
	 * Virtual Machine specification.
	 * </p>
	 * @since 3.10
	 */
	public static final int AccDefaultMethod = ExtraCompilerModifiers.AccDefaultMethod;

	/**
	 * Annotation method default property flag.
	 * Used to flag annotation type methods that declare a default value.
	 * <p>
	 * Note that this flag's value is internal and is not defined in the
	 * Virtual Machine specification.
	 * </p>
	 * @since 3.10
	 */
	public static final int AccAnnotationDefault = ClassFileConstants.AccAnnotationDefault;
	
	/**
	 * Module declaration property flag.
	 * Used to flag a compilation unit or a class file that contains a module declaration.
	 * 
	 * @since 3.14
	 */
	public static final int AccModule = ClassFileConstants.AccModule;
	
	/**
	 * Not instantiable.
	 */
	private Flags() {
		// Not instantiable
	}
	/**
	 * Returns whether the given integer includes the <code>abstract</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>abstract</code> modifier is included
	 */
	public static boolean isAbstract(int flags) {
		return (flags & AccAbstract) != 0;
	}
	/**
	 * Returns whether the given integer includes the indication that the
	 * element is deprecated (<code>@deprecated</code> tag in Javadoc comment).
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the element is marked as deprecated
	 */
	public static boolean isDeprecated(int flags) {
		return (flags & AccDeprecated) != 0;
	}
	/**
	 * Returns whether the given integer includes the <code>final</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>final</code> modifier is included
	 */
	public static boolean isFinal(int flags) {
		return (flags & AccFinal) != 0;
	}
	/**
	 * Returns whether the given integer includes the <code>interface</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>interface</code> modifier is included
	 * @since 2.0
	 */
	public static boolean isInterface(int flags) {
		return (flags & AccInterface) != 0;
	}
	/**
	 * Returns whether the given integer includes the <code>native</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>native</code> modifier is included
	 */
	public static boolean isNative(int flags) {
		return (flags & AccNative) != 0;
	}
	/**
	 * Returns whether the given integer does not include one of the
	 * <code>public</code>, <code>private</code>, or <code>protected</code> flags.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if no visibility flag is set
	 * @since 3.2
	 */
	public static boolean isPackageDefault(int flags) {
		return (flags & (AccPublic | AccPrivate | AccProtected)) == 0;
	}
	/**
	 * Returns whether the given integer includes the <code>private</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>private</code> modifier is included
	 */
	public static boolean isPrivate(int flags) {
		return (flags & AccPrivate) != 0;
	}
	/**
	 * Returns whether the given integer includes the <code>protected</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>protected</code> modifier is included
	 */
	public static boolean isProtected(int flags) {
		return (flags & AccProtected) != 0;
	}
	/**
	 * Returns whether the given integer includes the <code>public</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>public</code> modifier is included
	 */
	public static boolean isPublic(int flags) {
		return (flags & AccPublic) != 0;
	}
	/**
	 * Returns whether the given integer includes the <code>static</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>static</code> modifier is included
	 */
	public static boolean isStatic(int flags) {
		return (flags & AccStatic) != 0;
	}
	/**
	 * Returns whether the given integer includes the <code>super</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>super</code> modifier is included
	 * @since 3.2
	 */
	public static boolean isSuper(int flags) {
		return (flags & AccSuper) != 0;
	}
	/**
	 * Returns whether the given integer includes the <code>strictfp</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>strictfp</code> modifier is included
	 */
	public static boolean isStrictfp(int flags) {
		return (flags & AccStrictfp) != 0;
	}
	/**
	 * Returns whether the given integer includes the <code>synchronized</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>synchronized</code> modifier is included
	 */
	public static boolean isSynchronized(int flags) {
		return (flags & AccSynchronized) != 0;
	}
	/**
	 * Returns whether the given integer includes the indication that the
	 * element is synthetic.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the element is marked synthetic
	 */
	public static boolean isSynthetic(int flags) {
		return (flags & AccSynthetic) != 0;
	}
	/**
	 * Returns whether the given integer includes the <code>transient</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>transient</code> modifier is included
	 */
	public static boolean isTransient(int flags) {
		return (flags & AccTransient) != 0;
	}
	/**
	 * Returns whether the given integer includes the <code>volatile</code> modifier.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>volatile</code> modifier is included
	 */
	public static boolean isVolatile(int flags) {
		return (flags & AccVolatile) != 0;
	}

	/**
	 * Returns whether the given integer has the <code>AccBridge</code>
	 * bit set.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>AccBridge</code> flag is included
	 * @see #AccBridge
	 * @since 3.0
	 */
	public static boolean isBridge(int flags) {
		return (flags & AccBridge) != 0;
	}

	/**
	 * Returns whether the given integer has the <code>AccVarargs</code>
	 * bit set.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>AccVarargs</code> flag is included
	 * @see #AccVarargs
	 * @since 3.0
	 */
	public static boolean isVarargs(int flags) {
		return (flags & AccVarargs) != 0;
	}

	/**
	 * Returns whether the given integer has the <code>AccEnum</code>
	 * bit set.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>AccEnum</code> flag is included
	 * @see #AccEnum
	 * @since 3.0
	 */
	public static boolean isEnum(int flags) {
		return (flags & AccEnum) != 0;
	}

	/**
	 * Returns whether the given integer has the <code>AccAnnotation</code>
	 * bit set.
	 *
	 * @param flags the flags
	 * @return <code>true</code> if the <code>AccAnnotation</code> flag is included
	 * @see #AccAnnotation
	 * @since 3.0
	 */
	public static boolean isAnnotation(int flags) {
		return (flags & AccAnnotation) != 0;
	}

	/**
	 * Returns whether the given integer has the <code>AccDefaultMethod</code>
	 * bit set. Note that this flag represents the usage of the 'default' keyword
	 * on a method and should not be confused with the 'package' access visibility (which used to be called 'default access').
	 *
	 * @return <code>true</code> if the <code>AccDefaultMethod</code> flag is included
	 * @see #AccDefaultMethod
	 * @since 3.10
	 */
	public static boolean isDefaultMethod(int flags) {
		return (flags & AccDefaultMethod) != 0;
	}

	/**
	 * Returns whether the given integer has the <code>AccAnnnotationDefault</code>
	 * bit set.
	 *
	 * @return <code>true</code> if the <code>AccAnnotationDefault</code> flag is included
	 * @see #AccAnnotationDefault
	 * @since 3.10
	 */
	public static boolean isAnnnotationDefault(int flags) {
		return (flags & AccAnnotationDefault) != 0;
	}
	
	/**
	 * Returns whether the given integer has the <code>AccModule</code>
	 * bit set.
	 *
	 * @return <code>true</code> if the <code>AccModule</code> flag is included
	 * @see #AccModule
	 * @since 3.14
	 */	
	public static boolean isModule(int flags) {
		return (flags & AccModule) != 0;
	}

	/**
	 * Returns a standard string describing the given modifier flags.
	 * Only modifier flags are included in the output; deprecated,
	 * synthetic, bridge, etc. flags are ignored.
	 * <p>
	 * The flags are output in the following order:
	 * <pre> public protected private
	 * abstract default static final synchronized native strictfp transient volatile</pre>
	 * <p>
	 * This order is consistent with the recommendations in JLS8 ("*Modifier:" rules in chapters 8 and 9).
	 * </p>
	 * <p>
	 * Note that the flags of a method can include the AccVarargs flag that has no standard description. Since the AccVarargs flag has the same value as
	 * the AccTransient flag (valid for fields only), attempting to get the description of method modifiers with the AccVarargs flag set would result in an
	 * unexpected description. Clients should ensure that the AccVarargs is not included in the flags of a method as follows:
	 * <pre>
	 * IMethod method = ...
	 * int flags = method.getFlags() & ~Flags.AccVarargs;
	 * return Flags.toString(flags);
	 * </pre>
	 * <p>
	 * Examples results:
	 * <pre>
	 *	  <code>"public static final"</code>
	 *	  <code>"private native"</code>
	 * </pre>
	 *
	 * @param flags the flags
	 * @return the standard string representation of the given flags
	 */
	public static String toString(int flags) {
		StringBuffer sb = new StringBuffer();

		if (isPublic(flags))
			sb.append("public "); //$NON-NLS-1$
		if (isProtected(flags))
			sb.append("protected "); //$NON-NLS-1$
		if (isPrivate(flags))
			sb.append("private "); //$NON-NLS-1$
		if (isAbstract(flags))
			sb.append("abstract "); //$NON-NLS-1$
		if (isDefaultMethod(flags))
			sb.append("default "); //$NON-NLS-1$
		if (isStatic(flags))
			sb.append("static "); //$NON-NLS-1$
		if (isFinal(flags))
			sb.append("final "); //$NON-NLS-1$
		if (isSynchronized(flags))
			sb.append("synchronized "); //$NON-NLS-1$
		if (isNative(flags))
			sb.append("native "); //$NON-NLS-1$
		if (isStrictfp(flags))
			sb.append("strictfp "); //$NON-NLS-1$
		if (isTransient(flags))
			sb.append("transient "); //$NON-NLS-1$
		if (isVolatile(flags))
			sb.append("volatile "); //$NON-NLS-1$
		int len = sb.length();
		if (len == 0)
			return ""; //$NON-NLS-1$
		sb.setLength(len - 1);
		return sb.toString();
	}
}

Back to the top