Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/MethodSpec.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/model/MethodSpec.java@18c0f02fa4d2 |
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.model; | |
24 | |
25 import java.util.*; | |
26 | |
27 import javax.lang.model.type.*; | |
28 | |
29 import com.oracle.truffle.dsl.processor.java.*; | |
30 | |
31 public class MethodSpec { | |
32 | |
33 private final ParameterSpec returnType; | |
34 private final List<ParameterSpec> optional = new ArrayList<>(); | |
35 private final List<ParameterSpec> required = new ArrayList<>(); | |
36 private final List<ParameterSpec> annotations = new ArrayList<>(); | |
37 | |
38 private boolean ignoreAdditionalParameters; | |
39 private boolean ignoreAdditionalSpecifications; | |
40 private boolean variableRequiredParameters; | |
41 | |
42 private List<TypeDef> typeDefinitions; | |
43 | |
44 public MethodSpec(ParameterSpec returnType) { | |
45 this.returnType = returnType; | |
46 } | |
47 | |
48 public void setVariableRequiredParameters(boolean variableRequiredParameters) { | |
49 this.variableRequiredParameters = variableRequiredParameters; | |
50 } | |
51 | |
52 public boolean isVariableRequiredParameters() { | |
53 return variableRequiredParameters; | |
54 } | |
55 | |
56 public void setIgnoreAdditionalParameters(boolean ignoreAdditionalParameter) { | |
57 this.ignoreAdditionalParameters = ignoreAdditionalParameter; | |
58 } | |
59 | |
60 public boolean isIgnoreAdditionalParameters() { | |
61 return ignoreAdditionalParameters; | |
62 } | |
63 | |
64 public void addOptional(ParameterSpec spec) { | |
65 optional.add(spec); | |
66 } | |
67 | |
68 public ParameterSpec addRequired(ParameterSpec spec) { | |
69 required.add(spec); | |
70 return spec; | |
71 } | |
72 | |
73 public List<ParameterSpec> getAnnotations() { | |
74 return annotations; | |
75 } | |
76 | |
77 public ParameterSpec getReturnType() { | |
78 return returnType; | |
79 } | |
80 | |
81 public List<ParameterSpec> getRequired() { | |
82 return required; | |
83 } | |
84 | |
85 public List<ParameterSpec> getOptional() { | |
86 return optional; | |
87 } | |
88 | |
89 public List<ParameterSpec> getAll() { | |
90 List<ParameterSpec> specs = new ArrayList<>(); | |
91 specs.add(getReturnType()); | |
92 specs.addAll(getOptional()); | |
93 specs.addAll(getRequired()); | |
94 return specs; | |
95 } | |
96 | |
97 public void applyTypeDefinitions(String prefix) { | |
98 this.typeDefinitions = createTypeDefinitions(prefix); | |
99 } | |
100 | |
101 private List<TypeDef> createTypeDefinitions(String prefix) { | |
102 List<TypeDef> typeDefs = new ArrayList<>(); | |
103 | |
104 int defIndex = 0; | |
105 for (ParameterSpec spec : getAll()) { | |
106 Collection<TypeMirror> allowedTypes = spec.getAllowedTypes(); | |
107 Collection<TypeMirror> types = spec.getAllowedTypes(); | |
108 if (types != null && allowedTypes.size() > 1) { | |
109 TypeDef foundDef = null; | |
110 for (TypeDef def : typeDefs) { | |
111 if (allowedTypes.equals(def.getTypes())) { | |
112 foundDef = def; | |
113 break; | |
114 } | |
115 } | |
116 if (foundDef == null) { | |
117 foundDef = new TypeDef(types, prefix + defIndex); | |
118 typeDefs.add(foundDef); | |
119 defIndex++; | |
120 } | |
121 | |
122 spec.setTypeDefinition(foundDef); | |
123 } | |
124 } | |
125 | |
126 return typeDefs; | |
127 } | |
128 | |
129 public String toSignatureString(String methodName) { | |
130 StringBuilder b = new StringBuilder(); | |
131 b.append(" "); | |
132 b.append(createTypeSignature(returnType, true)); | |
133 | |
134 b.append(" "); | |
135 b.append(methodName); | |
136 b.append("("); | |
137 | |
138 String sep = ""; | |
139 | |
140 for (ParameterSpec optionalSpec : getOptional()) { | |
141 b.append(sep); | |
142 b.append("["); | |
143 b.append(createTypeSignature(optionalSpec, false)); | |
144 b.append("]"); | |
145 sep = ", "; | |
146 } | |
147 | |
148 for (int i = 0; i < getRequired().size(); i++) { | |
149 ParameterSpec requiredSpec = getRequired().get(i); | |
150 b.append(sep); | |
151 | |
152 if (isVariableRequiredParameters() && i == getRequired().size() - 1) { | |
153 b.append(("{")); | |
154 } | |
155 b.append(createTypeSignature(requiredSpec, false)); | |
156 if (isVariableRequiredParameters() && i == getRequired().size() - 1) { | |
157 b.append(("}")); | |
158 } | |
159 | |
160 sep = ", "; | |
161 } | |
162 | |
163 b.append(")"); | |
164 | |
165 if (typeDefinitions != null && !typeDefinitions.isEmpty()) { | |
166 b.append("\n\n"); | |
167 | |
168 String lineSep = ""; | |
169 for (TypeDef def : typeDefinitions) { | |
170 b.append(lineSep); | |
171 b.append(" <").append(def.getName()).append(">"); | |
172 b.append(" = {"); | |
173 String separator = ""; | |
174 for (TypeMirror type : def.getTypes()) { | |
175 b.append(separator).append(ElementUtils.getSimpleName(type)); | |
176 separator = ", "; | |
177 } | |
178 b.append("}"); | |
179 lineSep = "\n"; | |
180 | |
181 } | |
182 } | |
183 return b.toString(); | |
184 } | |
185 | |
186 private static String createTypeSignature(ParameterSpec spec, boolean typeOnly) { | |
187 StringBuilder builder = new StringBuilder(); | |
188 TypeDef foundTypeDef = spec.getTypeDefinition(); | |
189 if (foundTypeDef != null) { | |
190 builder.append("<" + foundTypeDef.getName() + ">"); | |
191 } else if (spec.getAllowedTypes().size() >= 1) { | |
192 builder.append(ElementUtils.getSimpleName(spec.getAllowedTypes().iterator().next())); | |
193 } else { | |
194 builder.append("void"); | |
195 } | |
196 if (!typeOnly) { | |
197 builder.append(" "); | |
198 builder.append(spec.getName()); | |
199 } | |
200 return builder.toString(); | |
201 } | |
202 | |
203 @Override | |
204 public String toString() { | |
205 return toSignatureString("methodName"); | |
206 } | |
207 | |
208 static final class TypeDef { | |
209 | |
210 private final Collection<TypeMirror> types; | |
211 private final String name; | |
212 | |
213 private TypeDef(Collection<TypeMirror> types, String name) { | |
214 this.types = types; | |
215 this.name = name; | |
216 } | |
217 | |
218 public Collection<TypeMirror> getTypes() { | |
219 return types; | |
220 } | |
221 | |
222 public String getName() { | |
223 return name; | |
224 } | |
225 } | |
226 | |
227 public void setIgnoreAdditionalSpecifications(boolean ignoreAdditoinalSpecifications) { | |
228 this.ignoreAdditionalSpecifications = ignoreAdditoinalSpecifications; | |
229 } | |
230 | |
231 public boolean isIgnoreAdditionalSpecifications() { | |
232 return ignoreAdditionalSpecifications; | |
233 } | |
234 | |
235 } |