Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/parser/AbstractParser.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/parser/AbstractParser.java@13cf9b6b325c |
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.parser; | |
24 | |
25 import java.lang.annotation.*; | |
26 import java.util.*; | |
27 | |
28 import javax.annotation.processing.*; | |
29 import javax.lang.model.element.*; | |
30 import javax.tools.Diagnostic.Kind; | |
31 | |
32 import com.oracle.truffle.dsl.processor.*; | |
33 import com.oracle.truffle.dsl.processor.java.*; | |
34 import com.oracle.truffle.dsl.processor.model.*; | |
35 import com.oracle.truffle.dsl.processor.model.MessageContainer.Message; | |
36 | |
37 /** | |
38 * THIS IS NOT PUBLIC API. | |
39 */ | |
40 public abstract class AbstractParser<M extends MessageContainer> { | |
41 | |
42 protected final ProcessorContext context; | |
43 protected final ProcessingEnvironment processingEnv; | |
44 | |
45 protected final Log log; | |
46 | |
47 public AbstractParser() { | |
48 this.context = ProcessorContext.getInstance(); | |
49 this.processingEnv = context.getEnvironment(); | |
50 this.log = context.getLog(); | |
51 } | |
52 | |
53 public final M parse(Element element) { | |
54 M model = null; | |
55 try { | |
56 AnnotationMirror mirror = null; | |
57 if (getAnnotationType() != null) { | |
58 mirror = ElementUtils.findAnnotationMirror(processingEnv, element.getAnnotationMirrors(), getAnnotationType()); | |
59 } | |
60 | |
61 if (!context.getTruffleTypes().verify(context, element, mirror)) { | |
62 return null; | |
63 } | |
64 model = parse(element, mirror); | |
65 if (model == null) { | |
66 return null; | |
67 } | |
68 | |
69 redirectMessages(new HashSet<MessageContainer>(), model, model); | |
70 model.emitMessages(context, log); | |
71 return filterErrorElements(model); | |
72 } catch (CompileErrorException e) { | |
73 log.message(Kind.WARNING, element, null, null, "The truffle processor could not parse class due to error: %s", e.getMessage()); | |
74 return null; | |
75 } | |
76 } | |
77 | |
78 private void redirectMessages(Set<MessageContainer> visitedSinks, MessageContainer model, MessageContainer baseContainer) { | |
79 List<Message> messages = model.getMessages(); | |
80 for (int i = messages.size() - 1; i >= 0; i--) { | |
81 Message message = messages.get(i); | |
82 if (!ElementUtils.isEnclosedIn(baseContainer.getMessageElement(), message.getOriginalContainer().getMessageElement())) { | |
83 // redirect message | |
84 MessageContainer original = message.getOriginalContainer(); | |
85 String text = wrapText(original.getMessageElement(), original.getMessageAnnotation(), message.getText()); | |
86 Message redirectedMessage = new Message(null, null, baseContainer, text, message.getKind()); | |
87 model.getMessages().remove(i); | |
88 baseContainer.getMessages().add(redirectedMessage); | |
89 } | |
90 } | |
91 | |
92 for (MessageContainer childContainer : model) { | |
93 if (visitedSinks.contains(childContainer)) { | |
94 continue; | |
95 } | |
96 visitedSinks.add(childContainer); | |
97 | |
98 MessageContainer newBase = baseContainer; | |
99 if (childContainer.getBaseContainer() != null) { | |
100 newBase = childContainer.getBaseContainer(); | |
101 } | |
102 redirectMessages(visitedSinks, childContainer, newBase); | |
103 } | |
104 } | |
105 | |
106 private static String wrapText(Element element, AnnotationMirror mirror, String text) { | |
107 StringBuilder b = new StringBuilder(); | |
108 if (element != null) { | |
109 if (element.getKind() == ElementKind.METHOD) { | |
110 b.append("Method " + ElementUtils.createReferenceName((ExecutableElement) element)); | |
111 } else { | |
112 b.append("Element " + element.getSimpleName()); | |
113 } | |
114 } | |
115 if (mirror != null) { | |
116 b.append(" at annotation @" + ElementUtils.getSimpleName(mirror.getAnnotationType()).trim()); | |
117 } | |
118 | |
119 if (b.length() > 0) { | |
120 b.append(" is erroneous: ").append(text); | |
121 return b.toString(); | |
122 } else { | |
123 return text; | |
124 } | |
125 } | |
126 | |
127 protected M filterErrorElements(M model) { | |
128 return model.hasErrors() ? null : model; | |
129 } | |
130 | |
131 protected abstract M parse(Element element, AnnotationMirror mirror); | |
132 | |
133 public abstract Class<? extends Annotation> getAnnotationType(); | |
134 | |
135 public boolean isDelegateToRootDeclaredType() { | |
136 return false; | |
137 } | |
138 | |
139 public List<Class<? extends Annotation>> getAllAnnotationTypes() { | |
140 List<Class<? extends Annotation>> list = new ArrayList<>(); | |
141 if (getAnnotationType() != null) { | |
142 list.add(getAnnotationType()); | |
143 } | |
144 list.addAll(getTypeDelegatedAnnotationTypes()); | |
145 return list; | |
146 } | |
147 | |
148 public List<Class<? extends Annotation>> getTypeDelegatedAnnotationTypes() { | |
149 return Collections.emptyList(); | |
150 } | |
151 | |
152 } |