Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/AnnotationProcessor.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/AnnotationProcessor.java@07b61dff860f |
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; | |
24 | |
25 import java.util.*; | |
26 | |
27 import javax.lang.model.element.*; | |
28 import javax.lang.model.type.*; | |
29 | |
30 import com.oracle.truffle.dsl.processor.generator.*; | |
31 import com.oracle.truffle.dsl.processor.java.*; | |
32 import com.oracle.truffle.dsl.processor.java.model.*; | |
33 import com.oracle.truffle.dsl.processor.java.transform.*; | |
34 import com.oracle.truffle.dsl.processor.model.*; | |
35 import com.oracle.truffle.dsl.processor.parser.*; | |
36 | |
37 /** | |
38 * THIS IS NOT PUBLIC API. | |
39 */ | |
40 class AnnotationProcessor<M extends Template> { | |
41 | |
42 private final AbstractParser<M> parser; | |
43 private final CodeTypeElementFactory<M> factory; | |
44 | |
45 private final Set<String> processedElements = new HashSet<>(); | |
46 | |
47 public AnnotationProcessor(AbstractParser<M> parser, CodeTypeElementFactory<M> factory) { | |
48 this.parser = parser; | |
49 this.factory = factory; | |
50 } | |
51 | |
52 public AbstractParser<M> getParser() { | |
53 return parser; | |
54 } | |
55 | |
56 @SuppressWarnings({"unchecked"}) | |
57 public void process(Element element, boolean callback) { | |
58 // since it is not guaranteed to be called only once by the compiler | |
59 // we check for already processed elements to avoid errors when writing files. | |
60 if (!callback && element instanceof TypeElement) { | |
61 String qualifiedName = ElementUtils.getQualifiedName((TypeElement) element); | |
62 if (processedElements.contains(qualifiedName)) { | |
63 return; | |
64 } | |
65 processedElements.add(qualifiedName); | |
66 } | |
67 | |
68 ProcessorContext context = ProcessorContext.getInstance(); | |
69 TypeElement type = (TypeElement) element; | |
70 | |
71 M model = (M) context.getTemplate(type.asType(), false); | |
72 boolean firstRun = !context.containsTemplate(type); | |
73 | |
74 if (firstRun || !callback) { | |
75 context.registerTemplate(type, null); | |
76 model = parser.parse(element); | |
77 context.registerTemplate(type, model); | |
78 | |
79 if (model != null) { | |
80 CodeTypeElement unit; | |
81 try { | |
82 unit = factory.create(ProcessorContext.getInstance(), model); | |
83 } catch (Throwable e) { | |
84 throw new RuntimeException(String.format("Failed to write code for %s. Parserdump:%s.", ElementUtils.getQualifiedName(type), ""), e); | |
85 } | |
86 if (unit == null) { | |
87 return; | |
88 } | |
89 unit.setGeneratorAnnotationMirror(model.getTemplateTypeAnnotation()); | |
90 unit.setGeneratorElement(model.getTemplateType()); | |
91 | |
92 DeclaredType overrideType = (DeclaredType) context.getType(Override.class); | |
93 DeclaredType unusedType = (DeclaredType) context.getType(SuppressWarnings.class); | |
94 unit.accept(new GenerateOverrideVisitor(overrideType), null); | |
95 unit.accept(new FixWarningsVisitor(context.getEnvironment(), unusedType, overrideType), null); | |
96 | |
97 if (!callback) { | |
98 unit.accept(new CodeWriter(context.getEnvironment(), element), null); | |
99 } | |
100 } | |
101 } | |
102 } | |
103 } |