Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/model/CodeElement.java @ 16759:23415229349b
Truffle-DSL: new package structure.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 11 Aug 2014 15:57:14 +0200 |
parents | graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/ast/CodeElement.java@0cd0bdedd4ad |
children | 224a89858453 |
comparison
equal
deleted
inserted
replaced
16758:c5f8eeb3cbc8 | 16759:23415229349b |
---|---|
1 /* | |
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.dsl.processor.java.model; | |
24 | |
25 import java.io.*; | |
26 import java.lang.annotation.*; | |
27 import java.util.*; | |
28 | |
29 import javax.lang.model.element.*; | |
30 | |
31 import com.oracle.truffle.dsl.processor.java.transform.*; | |
32 | |
33 public abstract class CodeElement<E extends Element> implements Element, GeneratedElement { | |
34 | |
35 private final Set<Modifier> modifiers; | |
36 private List<AnnotationMirror> annotations; | |
37 private List<E> enclosedElements; | |
38 | |
39 private Element enclosingElement; | |
40 | |
41 private Element generatorElement; | |
42 private AnnotationMirror generatorAnnotationMirror; | |
43 | |
44 public CodeElement() { | |
45 this.modifiers = new LinkedHashSet<>(); | |
46 } | |
47 | |
48 public CodeElement(Set<Modifier> modifiers) { | |
49 this.modifiers = new LinkedHashSet<>(modifiers); | |
50 } | |
51 | |
52 @Override | |
53 public void setGeneratorAnnotationMirror(AnnotationMirror mirror) { | |
54 this.generatorAnnotationMirror = mirror; | |
55 } | |
56 | |
57 @Override | |
58 public void setGeneratorElement(Element element) { | |
59 this.generatorElement = element; | |
60 } | |
61 | |
62 @Override | |
63 public AnnotationMirror getGeneratorAnnotationMirror() { | |
64 return generatorAnnotationMirror; | |
65 } | |
66 | |
67 @Override | |
68 public Element getGeneratorElement() { | |
69 return generatorElement; | |
70 } | |
71 | |
72 public E add(E element) { | |
73 if (element == null) { | |
74 throw new NullPointerException(); | |
75 } | |
76 getEnclosedElements().add(element); | |
77 return element; | |
78 } | |
79 | |
80 public E addOptional(E element) { | |
81 if (element != null) { | |
82 add(element); | |
83 } | |
84 return element; | |
85 } | |
86 | |
87 public void remove(E element) { | |
88 getEnclosedElements().remove(element); | |
89 } | |
90 | |
91 @Override | |
92 public Set<Modifier> getModifiers() { | |
93 return modifiers; | |
94 } | |
95 | |
96 @Override | |
97 public List<E> getEnclosedElements() { | |
98 if (enclosedElements == null) { | |
99 enclosedElements = parentableList(this, new ArrayList<E>()); | |
100 } | |
101 return enclosedElements; | |
102 } | |
103 | |
104 @Override | |
105 public List<AnnotationMirror> getAnnotationMirrors() { | |
106 if (annotations == null) { | |
107 annotations = parentableList(this, new ArrayList<AnnotationMirror>()); | |
108 } | |
109 return annotations; | |
110 } | |
111 | |
112 /** | |
113 * Support JDK8 langtools. | |
114 * | |
115 * @param annotationType | |
116 */ | |
117 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) { | |
118 throw new UnsupportedOperationException(); | |
119 } | |
120 | |
121 /** | |
122 * Support for some JDK8 builds. (remove after jdk8 is released) | |
123 * | |
124 * @param annotationType | |
125 */ | |
126 public <A extends Annotation> A[] getAnnotations(Class<A> annotationType) { | |
127 throw new UnsupportedOperationException(); | |
128 } | |
129 | |
130 /** | |
131 * Support for some JDK8 builds. (remove after jdk8 is released) | |
132 * | |
133 * @param annotationType | |
134 */ | |
135 public <A extends Annotation> A getAnnotation(Class<A> annotationType) { | |
136 throw new UnsupportedOperationException(); | |
137 } | |
138 | |
139 public void addAnnotationMirror(AnnotationMirror annotationMirror) { | |
140 getAnnotationMirrors().add(annotationMirror); | |
141 } | |
142 | |
143 protected void setEnclosingElement(Element parent) { | |
144 this.enclosingElement = parent; | |
145 } | |
146 | |
147 public Element getEnclosingElement() { | |
148 return enclosingElement; | |
149 } | |
150 | |
151 public CodeTypeElement getEnclosingClass() { | |
152 Element p = enclosingElement; | |
153 while (p != null && p.getKind() != ElementKind.CLASS && p.getKind() != ElementKind.ENUM) { | |
154 p = p.getEnclosingElement(); | |
155 } | |
156 return (CodeTypeElement) p; | |
157 } | |
158 | |
159 <T> List<T> parentableList(Element parent, List<T> list) { | |
160 return new ParentableList<>(parent, list); | |
161 } | |
162 | |
163 @Override | |
164 public String toString() { | |
165 StringBuilderCodeWriter codeWriter = new StringBuilderCodeWriter(); | |
166 accept(codeWriter, null); | |
167 return codeWriter.getString(); | |
168 } | |
169 | |
170 private static class StringBuilderCodeWriter extends AbstractCodeWriter { | |
171 | |
172 public StringBuilderCodeWriter() { | |
173 this.writer = new CharArrayWriter(); | |
174 } | |
175 | |
176 @Override | |
177 protected Writer createWriter(CodeTypeElement clazz) throws IOException { | |
178 return writer; | |
179 } | |
180 | |
181 public String getString() { | |
182 return new String(((CharArrayWriter) writer).toCharArray()).trim(); | |
183 } | |
184 | |
185 } | |
186 | |
187 private static class ParentableList<T> implements List<T> { | |
188 | |
189 private final Element parent; | |
190 private final List<T> delegate; | |
191 | |
192 public ParentableList(Element parent, List<T> delegate) { | |
193 this.parent = parent; | |
194 this.delegate = delegate; | |
195 } | |
196 | |
197 private void addImpl(T element) { | |
198 if (element != null) { | |
199 if (element instanceof CodeElement<?>) { | |
200 ((CodeElement<?>) element).setEnclosingElement(parent); | |
201 } | |
202 } | |
203 } | |
204 | |
205 private static void removeImpl(Object element) { | |
206 if (element instanceof CodeElement<?>) { | |
207 ((CodeElement<?>) element).setEnclosingElement(null); | |
208 } | |
209 } | |
210 | |
211 @Override | |
212 public int size() { | |
213 return delegate.size(); | |
214 } | |
215 | |
216 @Override | |
217 public boolean isEmpty() { | |
218 return delegate.isEmpty(); | |
219 } | |
220 | |
221 @Override | |
222 public boolean contains(Object o) { | |
223 return delegate.contains(o); | |
224 } | |
225 | |
226 @Override | |
227 public Iterator<T> iterator() { | |
228 return delegate.iterator(); | |
229 } | |
230 | |
231 @Override | |
232 public Object[] toArray() { | |
233 return delegate.toArray(); | |
234 } | |
235 | |
236 @Override | |
237 public <E> E[] toArray(E[] a) { | |
238 return delegate.toArray(a); | |
239 } | |
240 | |
241 @Override | |
242 public boolean add(T e) { | |
243 addImpl(e); | |
244 return delegate.add(e); | |
245 } | |
246 | |
247 @Override | |
248 public boolean remove(Object o) { | |
249 boolean removed = delegate.remove(o); | |
250 if (removed) { | |
251 removeImpl(o); | |
252 } | |
253 return removed; | |
254 } | |
255 | |
256 @Override | |
257 public boolean containsAll(Collection<?> c) { | |
258 return delegate.containsAll(c); | |
259 } | |
260 | |
261 @Override | |
262 public boolean addAll(Collection<? extends T> c) { | |
263 if (c != null) { | |
264 for (T t : c) { | |
265 addImpl(t); | |
266 } | |
267 } | |
268 return delegate.addAll(c); | |
269 } | |
270 | |
271 @Override | |
272 public boolean addAll(int index, Collection<? extends T> c) { | |
273 if (c != null) { | |
274 for (T t : c) { | |
275 addImpl(t); | |
276 } | |
277 } | |
278 return delegate.addAll(index, c); | |
279 } | |
280 | |
281 @Override | |
282 public boolean removeAll(Collection<?> c) { | |
283 if (c != null) { | |
284 for (Object t : c) { | |
285 removeImpl(t); | |
286 } | |
287 } | |
288 return delegate.removeAll(c); | |
289 } | |
290 | |
291 @Override | |
292 public String toString() { | |
293 return delegate.toString(); | |
294 } | |
295 | |
296 @Override | |
297 public boolean retainAll(Collection<?> c) { | |
298 throw new UnsupportedOperationException("Not supported by parentable list"); | |
299 } | |
300 | |
301 @Override | |
302 public void clear() { | |
303 for (Object e : this) { | |
304 removeImpl(e); | |
305 } | |
306 delegate.clear(); | |
307 } | |
308 | |
309 @Override | |
310 public T get(int index) { | |
311 return delegate.get(index); | |
312 } | |
313 | |
314 @Override | |
315 public T set(int index, T element) { | |
316 removeImpl(delegate.get(index)); | |
317 addImpl(element); | |
318 return delegate.set(index, element); | |
319 } | |
320 | |
321 @Override | |
322 public void add(int index, T element) { | |
323 addImpl(element); | |
324 delegate.add(index, element); | |
325 } | |
326 | |
327 @Override | |
328 public T remove(int index) { | |
329 T element = delegate.remove(index); | |
330 removeImpl(element); | |
331 return element; | |
332 } | |
333 | |
334 @Override | |
335 public int indexOf(Object o) { | |
336 return delegate.indexOf(o); | |
337 } | |
338 | |
339 @Override | |
340 public int lastIndexOf(Object o) { | |
341 return delegate.lastIndexOf(o); | |
342 } | |
343 | |
344 @Override | |
345 public ListIterator<T> listIterator() { | |
346 return delegate.listIterator(); | |
347 } | |
348 | |
349 @Override | |
350 public ListIterator<T> listIterator(int index) { | |
351 return delegate.listIterator(index); | |
352 } | |
353 | |
354 @Override | |
355 public List<T> subList(int fromIndex, int toIndex) { | |
356 return new ParentableList<>(parent, delegate.subList(fromIndex, toIndex)); | |
357 } | |
358 | |
359 } | |
360 | |
361 } |