Mercurial > hg > truffle
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/model/CodeExecutableElement.java @ 16759:23415229349b
Truffle-DSL: new package structure.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 11 Aug 2014 15:57:14 +0200 |
parents | graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/ast/CodeExecutableElement.java@0cd0bdedd4ad |
children | 62cfffca9be2 |
comparison
equal
deleted
inserted
replaced
16758:c5f8eeb3cbc8 | 16759:23415229349b |
---|---|
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.util.*; | |
26 | |
27 import javax.annotation.processing.*; | |
28 import javax.lang.model.element.*; | |
29 import javax.lang.model.type.*; | |
30 | |
31 import com.oracle.truffle.dsl.processor.java.*; | |
32 | |
33 public class CodeExecutableElement extends CodeElement<Element> implements ExecutableElement { | |
34 | |
35 private final List<TypeMirror> throwables = new ArrayList<>(); | |
36 private final List<VariableElement> parameters = parentableList(this, new ArrayList<VariableElement>()); | |
37 | |
38 private TypeMirror returnType; | |
39 private Name name; | |
40 | |
41 private CodeTree bodyTree; | |
42 private String body; | |
43 private AnnotationValue defaultValue; | |
44 private boolean varArgs; | |
45 | |
46 public CodeExecutableElement(TypeMirror returnType, String name) { | |
47 super(ElementUtils.modifiers()); | |
48 this.returnType = returnType; | |
49 this.name = CodeNames.of(name); | |
50 } | |
51 | |
52 public CodeExecutableElement(Set<Modifier> modifiers, TypeMirror returnType, String name, CodeVariableElement... parameters) { | |
53 super(modifiers); | |
54 this.returnType = returnType; | |
55 this.name = CodeNames.of(name); | |
56 for (CodeVariableElement codeParameter : parameters) { | |
57 addParameter(codeParameter); | |
58 } | |
59 } | |
60 | |
61 /* Support JDK8 langtools. */ | |
62 public boolean isDefault() { | |
63 return false; | |
64 } | |
65 | |
66 @Override | |
67 public List<TypeMirror> getThrownTypes() { | |
68 return throwables; | |
69 } | |
70 | |
71 @Override | |
72 public TypeMirror asType() { | |
73 return returnType; | |
74 } | |
75 | |
76 @Override | |
77 public ElementKind getKind() { | |
78 if (getReturnType() == null) { | |
79 return ElementKind.CONSTRUCTOR; | |
80 } else { | |
81 return ElementKind.METHOD; | |
82 } | |
83 } | |
84 | |
85 @Override | |
86 public List<? extends TypeParameterElement> getTypeParameters() { | |
87 return Collections.emptyList(); | |
88 } | |
89 | |
90 public void setVarArgs(boolean varargs) { | |
91 this.varArgs = varargs; | |
92 } | |
93 | |
94 @Override | |
95 public boolean isVarArgs() { | |
96 return varArgs; | |
97 } | |
98 | |
99 public void setDefaultValue(AnnotationValue defaultValue) { | |
100 this.defaultValue = defaultValue; | |
101 } | |
102 | |
103 @Override | |
104 public AnnotationValue getDefaultValue() { | |
105 return defaultValue; | |
106 } | |
107 | |
108 @Override | |
109 public Name getSimpleName() { | |
110 return name; | |
111 } | |
112 | |
113 public CodeTreeBuilder getBuilder() { | |
114 CodeTree tree = this.bodyTree; | |
115 return createBuilder().tree(tree); | |
116 } | |
117 | |
118 public CodeTreeBuilder createBuilder() { | |
119 CodeTreeBuilder builder = new CodeTreeBuilder(null); | |
120 this.bodyTree = builder.getTree(); | |
121 this.bodyTree.setEnclosingElement(this); | |
122 this.body = null; | |
123 return builder; | |
124 } | |
125 | |
126 public void setBodyTree(CodeTree body) { | |
127 this.bodyTree = body; | |
128 } | |
129 | |
130 public CodeTree getBodyTree() { | |
131 return bodyTree; | |
132 } | |
133 | |
134 public TypeMirror getReturnType() { | |
135 return returnType; | |
136 } | |
137 | |
138 @Override | |
139 public List<VariableElement> getParameters() { | |
140 return parameters; | |
141 } | |
142 | |
143 public TypeMirror[] getParameterTypes() { | |
144 TypeMirror[] types = new TypeMirror[getParameters().size()]; | |
145 for (int i = 0; i < types.length; i++) { | |
146 types[i] = parameters.get(i).asType(); | |
147 } | |
148 return types; | |
149 } | |
150 | |
151 public void setReturnType(TypeMirror type) { | |
152 returnType = type; | |
153 } | |
154 | |
155 public void addParameter(VariableElement parameter) { | |
156 parameters.add(parameter); | |
157 } | |
158 | |
159 public void addThrownType(TypeMirror thrownType) { | |
160 throwables.add(thrownType); | |
161 } | |
162 | |
163 public void setSimpleName(Name name) { | |
164 this.name = name; | |
165 } | |
166 | |
167 public void setBody(String body) { | |
168 this.body = body; | |
169 } | |
170 | |
171 public String getBody() { | |
172 return body; | |
173 } | |
174 | |
175 @Override | |
176 public <R, P> R accept(ElementVisitor<R, P> v, P p) { | |
177 return v.visitExecutable(this, p); | |
178 } | |
179 | |
180 public static CodeExecutableElement clone(@SuppressWarnings("unused") ProcessingEnvironment env, ExecutableElement method) { | |
181 CodeExecutableElement copy = new CodeExecutableElement(method.getReturnType(), method.getSimpleName().toString()); | |
182 for (TypeMirror thrownType : method.getThrownTypes()) { | |
183 copy.addThrownType(thrownType); | |
184 } | |
185 copy.setDefaultValue(method.getDefaultValue()); | |
186 | |
187 for (AnnotationMirror mirror : method.getAnnotationMirrors()) { | |
188 copy.addAnnotationMirror(mirror); | |
189 } | |
190 for (VariableElement var : method.getParameters()) { | |
191 copy.addParameter(CodeVariableElement.clone(var)); | |
192 } | |
193 for (Element element : method.getEnclosedElements()) { | |
194 copy.add(element); | |
195 } | |
196 copy.getModifiers().addAll(method.getModifiers()); | |
197 copy.setVarArgs(method.isVarArgs()); | |
198 return copy; | |
199 } | |
200 | |
201 public TypeMirror getReceiverType() { | |
202 throw new UnsupportedOperationException(); | |
203 } | |
204 } |