1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | package com.github.javaparser.ast; |
22 | |
23 | import com.github.javaparser.TokenRange; |
24 | import com.github.javaparser.ast.observer.ObservableProperty; |
25 | import com.github.javaparser.ast.visitor.CloneVisitor; |
26 | import com.github.javaparser.ast.visitor.GenericVisitor; |
27 | import com.github.javaparser.ast.visitor.VoidVisitor; |
28 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
29 | import com.github.javaparser.metamodel.ModifierMetaModel; |
30 | import java.util.Arrays; |
31 | import static com.github.javaparser.ast.NodeList.toNodeList; |
32 | import static com.github.javaparser.utils.Utils.assertNotNull; |
33 | import com.github.javaparser.ast.Node; |
34 | import com.github.javaparser.ast.Generated; |
35 | |
36 | /** |
37 | * A modifier, like private, public, or volatile. |
38 | */ |
39 | public class Modifier extends Node { |
40 | |
41 | public static Modifier publicModifier() { |
42 | return new Modifier(Keyword.PUBLIC); |
43 | } |
44 | |
45 | public static Modifier protectedModifier() { |
46 | return new Modifier(Keyword.PROTECTED); |
47 | } |
48 | |
49 | public static Modifier privateModifier() { |
50 | return new Modifier(Keyword.PRIVATE); |
51 | } |
52 | |
53 | public static Modifier abstractModifier() { |
54 | return new Modifier(Keyword.ABSTRACT); |
55 | } |
56 | |
57 | public static Modifier staticModifier() { |
58 | return new Modifier(Keyword.STATIC); |
59 | } |
60 | |
61 | public static Modifier finalModifier() { |
62 | return new Modifier(Keyword.FINAL); |
63 | } |
64 | |
65 | public static Modifier transientModifier() { |
66 | return new Modifier(Keyword.TRANSIENT); |
67 | } |
68 | |
69 | public static Modifier volatileModifier() { |
70 | return new Modifier(Keyword.VOLATILE); |
71 | } |
72 | |
73 | public static Modifier synchronizedModifier() { |
74 | return new Modifier(Keyword.SYNCHRONIZED); |
75 | } |
76 | |
77 | public static Modifier nativeModifier() { |
78 | return new Modifier(Keyword.NATIVE); |
79 | } |
80 | |
81 | public static Modifier strictfpModifier() { |
82 | return new Modifier(Keyword.STRICTFP); |
83 | } |
84 | |
85 | public static Modifier transitiveModifier() { |
86 | return new Modifier(Keyword.TRANSITIVE); |
87 | } |
88 | |
89 | /** |
90 | * The Java modifier keywords. |
91 | */ |
92 | public enum Keyword { |
93 | |
94 | DEFAULT("default"), |
95 | PUBLIC("public"), |
96 | PROTECTED("protected"), |
97 | PRIVATE("private"), |
98 | ABSTRACT("abstract"), |
99 | STATIC("static"), |
100 | FINAL("final"), |
101 | TRANSIENT("transient"), |
102 | VOLATILE("volatile"), |
103 | SYNCHRONIZED("synchronized"), |
104 | NATIVE("native"), |
105 | STRICTFP("strictfp"), |
106 | TRANSITIVE("transitive"); |
107 | |
108 | private final String codeRepresentation; |
109 | |
110 | Keyword(String codeRepresentation) { |
111 | this.codeRepresentation = codeRepresentation; |
112 | } |
113 | |
114 | /** |
115 | * @return the Java keyword represented by this enum constant. |
116 | */ |
117 | public String asString() { |
118 | return codeRepresentation; |
119 | } |
120 | } |
121 | |
122 | private Keyword keyword; |
123 | |
124 | public Modifier() { |
125 | this(Keyword.PUBLIC); |
126 | } |
127 | |
128 | @AllFieldsConstructor |
129 | public Modifier(Keyword keyword) { |
130 | this(null, keyword); |
131 | } |
132 | |
133 | /** |
134 | * This constructor is used by the parser and is considered private. |
135 | */ |
136 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
137 | public Modifier(TokenRange tokenRange, Keyword keyword) { |
138 | super(tokenRange); |
139 | setKeyword(keyword); |
140 | customInitialization(); |
141 | } |
142 | |
143 | @Override |
144 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
145 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
146 | return v.visit(this, arg); |
147 | } |
148 | |
149 | @Override |
150 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
151 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
152 | v.visit(this, arg); |
153 | } |
154 | |
155 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
156 | public Keyword getKeyword() { |
157 | return keyword; |
158 | } |
159 | |
160 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
161 | public Modifier setKeyword(final Keyword keyword) { |
162 | assertNotNull(keyword); |
163 | if (keyword == this.keyword) { |
164 | return (Modifier) this; |
165 | } |
166 | notifyPropertyChange(ObservableProperty.KEYWORD, this.keyword, keyword); |
167 | this.keyword = keyword; |
168 | return this; |
169 | } |
170 | |
171 | /** |
172 | * Utility method that instantiaties "Modifier"s for the keywords, |
173 | * and puts them in a NodeList. |
174 | */ |
175 | public static NodeList<Modifier> createModifierList(Modifier.Keyword... modifiers) { |
176 | return Arrays.stream(modifiers).map(Modifier::new).collect(toNodeList()); |
177 | } |
178 | |
179 | @Override |
180 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
181 | public boolean remove(Node node) { |
182 | if (node == null) |
183 | return false; |
184 | return super.remove(node); |
185 | } |
186 | |
187 | @Override |
188 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
189 | public boolean replace(Node node, Node replacementNode) { |
190 | if (node == null) |
191 | return false; |
192 | return super.replace(node, replacementNode); |
193 | } |
194 | |
195 | @Override |
196 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
197 | public Modifier clone() { |
198 | return (Modifier) accept(new CloneVisitor(), null); |
199 | } |
200 | |
201 | @Override |
202 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
203 | public ModifierMetaModel getMetaModel() { |
204 | return JavaParserMetaModel.modifierMetaModel; |
205 | } |
206 | } |
207 |
Members