comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/parser/TemplateMethodParser.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/TemplateMethodParser.java@62c43fcf5be2
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.lang.model.element.*;
29 import javax.lang.model.type.*;
30 import javax.lang.model.util.*;
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
36 public abstract class TemplateMethodParser<T extends Template, E extends TemplateMethod> {
37
38 protected final T template;
39 private final ProcessorContext context;
40 private final MethodSpecParser parser;
41
42 private boolean parseNullOnError;
43
44 public TemplateMethodParser(ProcessorContext context, T template) {
45 this.template = template;
46 this.context = context;
47 this.parser = new MethodSpecParser(template);
48 }
49
50 public void setParseNullOnError(boolean parseNullOnError) {
51 this.parseNullOnError = parseNullOnError;
52 }
53
54 public boolean isParseNullOnError() {
55 return parseNullOnError;
56 }
57
58 public MethodSpecParser getParser() {
59 return parser;
60 }
61
62 public ProcessorContext getContext() {
63 return context;
64 }
65
66 public TypeSystemData getTypeSystem() {
67 return template.getTypeSystem();
68 }
69
70 public abstract MethodSpec createSpecification(ExecutableElement method, AnnotationMirror mirror);
71
72 public abstract E create(TemplateMethod method, boolean invalid);
73
74 public abstract boolean isParsable(ExecutableElement method);
75
76 public Class<? extends Annotation> getAnnotationType() {
77 return null;
78 }
79
80 public final List<E> parse(List<? extends Element> elements) {
81 List<ExecutableElement> methods = new ArrayList<>();
82 methods.addAll(ElementFilter.methodsIn(elements));
83
84 List<E> parsedMethods = new ArrayList<>();
85 boolean valid = true;
86 int naturalOrder = 0;
87 for (ExecutableElement method : methods) {
88 if (!isParsable(method)) {
89 continue;
90 }
91
92 Class<? extends Annotation> annotationType = getAnnotationType();
93 AnnotationMirror mirror = null;
94 if (annotationType != null) {
95 mirror = ElementUtils.findAnnotationMirror(getContext().getEnvironment(), method, annotationType);
96 }
97
98 E parsedMethod = parse(naturalOrder, method, mirror);
99
100 if (method.getModifiers().contains(Modifier.PRIVATE) && parser.isEmitErrors()) {
101 parsedMethod.addError("Method annotated with @%s must not be private.", getAnnotationType().getSimpleName());
102 parsedMethods.add(parsedMethod);
103 valid = false;
104 continue;
105 }
106
107 if (parsedMethod != null) {
108 parsedMethods.add(parsedMethod);
109 } else {
110 valid = false;
111 }
112 naturalOrder++;
113 }
114 Collections.sort(parsedMethods);
115
116 if (!valid && isParseNullOnError()) {
117 return null;
118 }
119 return parsedMethods;
120 }
121
122 private E parse(int naturalOrder, ExecutableElement method, AnnotationMirror annotation) {
123 MethodSpec methodSpecification = createSpecification(method, annotation);
124 if (methodSpecification == null) {
125 return null;
126 }
127
128 TemplateMethod templateMethod = parser.parse(methodSpecification, method, annotation, naturalOrder);
129 if (templateMethod != null) {
130 return create(templateMethod, templateMethod.hasErrors());
131 }
132 return null;
133 }
134
135 public final E create(String id, int naturalOrder, ExecutableElement methodMetadata, AnnotationMirror mirror, TypeMirror returnType, List<VariableElement> parameterTypes) {
136 TemplateMethod method = parser.parseImpl(createSpecification(methodMetadata, mirror), naturalOrder, id, methodMetadata, mirror, returnType, parameterTypes);
137 if (method != null) {
138 return create(method, method.hasErrors());
139 }
140 return null;
141 }
142 }