comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/parser/SpecializationMethodParser.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/SpecializationMethodParser.java@476374f3fe9a
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
1 /*
2 * Copyright (c) 2012, 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
31 import com.oracle.truffle.api.dsl.*;
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.SpecializationData.SpecializationKind;
36
37 public class SpecializationMethodParser extends NodeMethodParser<SpecializationData> {
38
39 public SpecializationMethodParser(ProcessorContext context, NodeData operation) {
40 super(context, operation);
41 }
42
43 @Override
44 public MethodSpec createSpecification(ExecutableElement method, AnnotationMirror mirror) {
45 MethodSpec spec = createDefaultMethodSpec(method, mirror, true, null);
46 spec.getAnnotations().add(new AnnotatedParameterSpec(getContext().getDeclaredType(Cached.class)));
47 return spec;
48 }
49
50 @Override
51 public SpecializationData create(TemplateMethod method, boolean invalid) {
52 return parseSpecialization(method);
53 }
54
55 @Override
56 public Class<? extends Annotation> getAnnotationType() {
57 return Specialization.class;
58 }
59
60 private SpecializationData parseSpecialization(TemplateMethod method) {
61 List<SpecializationThrowsData> exceptionData = new ArrayList<>();
62 if (method.getMethod() != null) {
63 AnnotationValue rewriteValue = ElementUtils.getAnnotationValue(method.getMarkerAnnotation(), "rewriteOn");
64 List<TypeMirror> exceptionTypes = ElementUtils.getAnnotationValueList(TypeMirror.class, method.getMarkerAnnotation(), "rewriteOn");
65 List<TypeMirror> rewriteOnTypes = new ArrayList<>();
66 for (TypeMirror exceptionType : exceptionTypes) {
67 SpecializationThrowsData throwsData = new SpecializationThrowsData(method.getMarkerAnnotation(), rewriteValue, exceptionType);
68 if (!ElementUtils.canThrowType(method.getMethod().getThrownTypes(), exceptionType)) {
69 method.addError("A rewriteOn checked exception was specified but not thrown in the method's throws clause. The @%s method must specify a throws clause with the exception type '%s'.",
70 Specialization.class.getSimpleName(), ElementUtils.getQualifiedName(exceptionType));
71 }
72 rewriteOnTypes.add(throwsData.getJavaClass());
73 exceptionData.add(throwsData);
74 }
75
76 for (TypeMirror typeMirror : method.getMethod().getThrownTypes()) {
77 if (!ElementUtils.canThrowType(rewriteOnTypes, typeMirror)) {
78 method.addError(rewriteValue, "A checked exception '%s' is thrown but is not specified using the rewriteOn property. "
79 + "Checked exceptions that are not used for rewriting are not handled by the DSL. Use RuntimeExceptions for this purpose instead.",
80 ElementUtils.getQualifiedName(typeMirror));
81 }
82 }
83
84 Collections.sort(exceptionData, new Comparator<SpecializationThrowsData>() {
85
86 @Override
87 public int compare(SpecializationThrowsData o1, SpecializationThrowsData o2) {
88 return ElementUtils.compareByTypeHierarchy(o1.getJavaClass(), o2.getJavaClass());
89 }
90 });
91 }
92 SpecializationData specialization = new SpecializationData(getNode(), method, SpecializationKind.SPECIALIZED, exceptionData);
93
94 if (method.getMethod() != null) {
95 String insertBeforeName = ElementUtils.getAnnotationValue(String.class, method.getMarkerAnnotation(), "insertBefore");
96 if (!insertBeforeName.equals("")) {
97 specialization.setInsertBeforeName(insertBeforeName);
98 }
99
100 List<String> containsDefs = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "contains");
101 Set<String> containsNames = specialization.getContainsNames();
102 containsNames.clear();
103 if (containsDefs != null) {
104 for (String include : containsDefs) {
105 if (!containsNames.contains(include)) {
106 specialization.getContainsNames().add(include);
107 } else {
108 AnnotationValue value = ElementUtils.getAnnotationValue(specialization.getMarkerAnnotation(), "contains");
109 specialization.addError(value, "Duplicate contains declaration '%s'.", include);
110 }
111 }
112
113 }
114 }
115
116 return specialization;
117 }
118 }