1 /*
2 * Copyright 2014 - 2020 Rafael Winterhalter
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 net.bytebuddy.description.modifier;
17
18 import net.bytebuddy.jar.asm.Opcodes;
19
20 /**
21 * Describes a type's manifestation, i.e. if a type is final, abstract, an interface or neither.
22 */
23 public enum TypeManifestation implements ModifierContributor.ForType {
24
25 /**
26 * Modifier for a non-final, non-abstract, non-interface, non-enum type. (This is the default modifier.)
27 */
28 PLAIN(EMPTY_MASK),
29
30 /**
31 * Modifier for a final class.
32 */
33 FINAL(Opcodes.ACC_FINAL),
34
35 /**
36 * Modifier for an abstract class.
37 */
38 ABSTRACT(Opcodes.ACC_ABSTRACT),
39
40 /**
41 * Modifier for an interface.
42 */
43 INTERFACE(Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT),
44
45 /**
46 * Modifier for an annotation.
47 */
48 ANNOTATION(Opcodes.ACC_ANNOTATION | Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT);
49
50 /**
51 * The mask the modifier contributor.
52 */
53 private final int mask;
54
55 /**
56 * Creates a new type manifestation.
57 *
58 * @param mask The modifier mask of this instance.
59 */
60 TypeManifestation(int mask) {
61 this.mask = mask;
62 }
63
64 /**
65 * {@inheritDoc}
66 */
67 public int getMask() {
68 return mask;
69 }
70
71 /**
72 * {@inheritDoc}
73 */
74 public int getRange() {
75 return Opcodes.ACC_FINAL | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_ANNOTATION;
76 }
77
78 /**
79 * {@inheritDoc}
80 */
81 public boolean isDefault() {
82 return this == PLAIN;
83 }
84
85 /**
86 * Returns {@code true} if a type represents a {@code final} type.
87 *
88 * @return {@code true} if a type represents a {@code final} type.
89 */
90 public boolean isFinal() {
91 return (mask & Opcodes.ACC_FINAL) != 0;
92 }
93
94 /**
95 * Returns {@code true} if a type represents an {@code abstract} type but not an interface type.
96 *
97 * @return {@code true} if a type represents an {@code abstract} type but not an interface type.
98 */
99 public boolean isAbstract() {
100 return (mask & Opcodes.ACC_ABSTRACT) != 0 && !isInterface();
101 }
102
103 /**
104 * Returns {@code true} if a type represents an interface type.
105 *
106 * @return {@code true} if a type represents an interface type.
107 */
108 public boolean isInterface() {
109 return (mask & Opcodes.ACC_INTERFACE) != 0;
110 }
111
112 /**
113 * Returns {@code true} if a type represents an annotation type.
114 *
115 * @return {@code true} if a type represents an annotation type.
116 */
117 public boolean isAnnotation() {
118 return (mask & Opcodes.ACC_ANNOTATION) != 0;
119 }
120 }
121