Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/model/CodeElement.java @ 21951:9c8c0937da41
Moving all sources into truffle subdirectory
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 17 Jun 2015 10:58:08 +0200 |
parents | graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/model/CodeElement.java@a665483c3881 |
children | dc83cc1f94f2 |
comparison
equal
deleted
inserted
replaced
21950:2a5011c7e641 | 21951:9c8c0937da41 |
---|---|
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(Set<Modifier> modifiers) { | |
45 this.modifiers = new LinkedHashSet<>(modifiers); | |
46 } | |
47 | |
48 @Override | |
49 public void setGeneratorAnnotationMirror(AnnotationMirror mirror) { | |
50 this.generatorAnnotationMirror = mirror; | |
51 } | |
52 | |
53 @Override | |
54 public void setGeneratorElement(Element element) { | |
55 this.generatorElement = element; | |
56 } | |
57 | |
58 @Override | |
59 public AnnotationMirror getGeneratorAnnotationMirror() { | |
60 return generatorAnnotationMirror; | |
61 } | |
62 | |
63 @Override | |
64 public Element getGeneratorElement() { | |
65 return generatorElement; | |
66 } | |
67 | |
68 public <T extends E> T add(T element) { | |
69 if (element == null) { | |
70 throw new NullPointerException(); | |
71 } | |
72 getEnclosedElements().add(element); | |
73 return element; | |
74 } | |
75 | |
76 public <T extends E> T addOptional(T element) { | |
77 if (element != null) { | |
78 add(element); | |
79 } | |
80 return element; | |
81 } | |
82 | |
83 public void remove(E element) { | |
84 getEnclosedElements().remove(element); | |
85 } | |
86 | |
87 @Override | |
88 public Set<Modifier> getModifiers() { | |
89 return modifiers; | |
90 } | |
91 | |
92 @Override | |
93 public List<E> getEnclosedElements() { | |
94 if (enclosedElements == null) { | |
95 enclosedElements = parentableList(this, new ArrayList<E>()); | |
96 } | |
97 return enclosedElements; | |
98 } | |
99 | |
100 @Override | |
101 public List<AnnotationMirror> getAnnotationMirrors() { | |
102 if (annotations == null) { | |
103 annotations = parentableList(this, new ArrayList<AnnotationMirror>()); | |
104 } | |
105 return annotations; | |
106 } | |
107 | |
108 /** | |
109 * Support JDK8 langtools. | |
110 * | |
111 * @param annotationType | |
112 */ | |
113 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) { | |
114 throw new UnsupportedOperationException(); | |
115 } | |
116 | |
117 /** | |
118 * Support for some JDK8 builds. (remove after jdk8 is released) | |
119 * | |
120 * @param annotationType | |
121 */ | |
122 public <A extends Annotation> A[] getAnnotations(Class<A> annotationType) { | |
123 throw new UnsupportedOperationException(); | |
124 } | |
125 | |
126 /** | |
127 * Support for some JDK8 builds. (remove after jdk8 is released) | |
128 * | |
129 * @param annotationType | |
130 */ | |
131 public <A extends Annotation> A getAnnotation(Class<A> annotationType) { | |
132 throw new UnsupportedOperationException(); | |
133 } | |
134 | |
135 public void addAnnotationMirror(AnnotationMirror annotationMirror) { | |
136 getAnnotationMirrors().add(annotationMirror); | |
137 } | |
138 | |
139 public void setEnclosingElement(Element parent) { | |
140 this.enclosingElement = parent; | |
141 } | |
142 | |
143 public Element getEnclosingElement() { | |
144 return enclosingElement; | |
145 } | |
146 | |
147 public CodeTypeElement getEnclosingClass() { | |
148 Element p = enclosingElement; | |
149 while (p != null && p.getKind() != ElementKind.CLASS && p.getKind() != ElementKind.ENUM) { | |
150 p = p.getEnclosingElement(); | |
151 } | |
152 return (CodeTypeElement) p; | |
153 } | |
154 | |
155 <T> List<T> parentableList(Element parent, List<T> list) { | |
156 return new ParentableList<>(parent, list); | |
157 } | |
158 | |
159 @Override | |
160 public String toString() { | |
161 StringBuilderCodeWriter codeWriter = new StringBuilderCodeWriter(); | |
162 accept(codeWriter, null); | |
163 return codeWriter.getString(); | |
164 } | |
165 | |
166 private static class StringBuilderCodeWriter extends AbstractCodeWriter { | |
167 | |
168 public StringBuilderCodeWriter() { | |
169 this.writer = new CharArrayWriter(); | |
170 } | |
171 | |
172 @Override | |
173 protected Writer createWriter(CodeTypeElement clazz) throws IOException { | |
174 return writer; | |
175 } | |
176 | |
177 public String getString() { | |
178 return new String(((CharArrayWriter) writer).toCharArray()).trim(); | |
179 } | |
180 | |
181 } | |
182 | |
183 private static class ParentableList<T> implements List<T> { | |
184 | |
185 private final Element parent; | |
186 private final List<T> delegate; | |
187 | |
188 public ParentableList(Element parent, List<T> delegate) { | |
189 this.parent = parent; | |
190 this.delegate = delegate; | |
191 } | |
192 | |
193 private void addImpl(T element) { | |
194 if (element != null) { | |
195 if (element instanceof CodeElement<?>) { | |
196 ((CodeElement<?>) element).setEnclosingElement(parent); | |
197 } | |
198 } | |
199 } | |
200 | |
201 private static void removeImpl(Object element) { | |
202 if (element instanceof CodeElement<?>) { | |
203 ((CodeElement<?>) element).setEnclosingElement(null); | |
204 } | |
205 } | |
206 | |
207 @Override | |
208 public int size() { | |
209 return delegate.size(); | |
210 } | |
211 | |
212 @Override | |
213 public boolean isEmpty() { | |
214 return delegate.isEmpty(); | |
215 } | |
216 | |
217 @Override | |
218 public boolean contains(Object o) { | |
219 return delegate.contains(o); | |
220 } | |
221 | |
222 @Override | |
223 public Iterator<T> iterator() { | |
224 return delegate.iterator(); | |
225 } | |
226 | |
227 @Override | |
228 public Object[] toArray() { | |
229 return delegate.toArray(); | |
230 } | |
231 | |
232 @Override | |
233 public <E> E[] toArray(E[] a) { | |
234 return delegate.toArray(a); | |
235 } | |
236 | |
237 @Override | |
238 public boolean add(T e) { | |
239 addImpl(e); | |
240 return delegate.add(e); | |
241 } | |
242 | |
243 @Override | |
244 public boolean remove(Object o) { | |
245 boolean removed = delegate.remove(o); | |
246 if (removed) { | |
247 removeImpl(o); | |
248 } | |
249 return removed; | |
250 } | |
251 | |
252 @Override | |
253 public boolean containsAll(Collection<?> c) { | |
254 return delegate.containsAll(c); | |
255 } | |
256 | |
257 @Override | |
258 public boolean addAll(Collection<? extends T> c) { | |
259 if (c != null) { | |
260 for (T t : c) { | |
261 addImpl(t); | |
262 } | |
263 } | |
264 return delegate.addAll(c); | |
265 } | |
266 | |
267 @Override | |
268 public boolean addAll(int index, Collection<? extends T> c) { | |
269 if (c != null) { | |
270 for (T t : c) { | |
271 addImpl(t); | |
272 } | |
273 } | |
274 return delegate.addAll(index, c); | |
275 } | |
276 | |
277 @Override | |
278 public boolean removeAll(Collection<?> c) { | |
279 if (c != null) { | |
280 for (Object t : c) { | |
281 removeImpl(t); | |
282 } | |
283 } | |
284 return delegate.removeAll(c); | |
285 } | |
286 | |
287 @Override | |
288 public String toString() { | |
289 return delegate.toString(); | |
290 } | |
291 | |
292 @Override | |
293 public boolean retainAll(Collection<?> c) { | |
294 throw new UnsupportedOperationException("Not supported by parentable list"); | |
295 } | |
296 | |
297 @Override | |
298 public void clear() { | |
299 for (Object e : this) { | |
300 removeImpl(e); | |
301 } | |
302 delegate.clear(); | |
303 } | |
304 | |
305 @Override | |
306 public T get(int index) { | |
307 return delegate.get(index); | |
308 } | |
309 | |
310 @Override | |
311 public T set(int index, T element) { | |
312 removeImpl(delegate.get(index)); | |
313 addImpl(element); | |
314 return delegate.set(index, element); | |
315 } | |
316 | |
317 @Override | |
318 public void add(int index, T element) { | |
319 addImpl(element); | |
320 delegate.add(index, element); | |
321 } | |
322 | |
323 @Override | |
324 public T remove(int index) { | |
325 T element = delegate.remove(index); | |
326 removeImpl(element); | |
327 return element; | |
328 } | |
329 | |
330 @Override | |
331 public int indexOf(Object o) { | |
332 return delegate.indexOf(o); | |
333 } | |
334 | |
335 @Override | |
336 public int lastIndexOf(Object o) { | |
337 return delegate.lastIndexOf(o); | |
338 } | |
339 | |
340 @Override | |
341 public ListIterator<T> listIterator() { | |
342 return delegate.listIterator(); | |
343 } | |
344 | |
345 @Override | |
346 public ListIterator<T> listIterator(int index) { | |
347 return delegate.listIterator(index); | |
348 } | |
349 | |
350 @Override | |
351 public List<T> subList(int fromIndex, int toIndex) { | |
352 return new ParentableList<>(parent, delegate.subList(fromIndex, toIndex)); | |
353 } | |
354 | |
355 } | |
356 | |
357 } |