comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/ast/CodeTypeElement.java @ 7291:a748e4d44694

Truffle API to specify type-specalized Node classes; annotation processor for automatic code generation of the type-specialized Node classes during the build process
author Christian Humer <christian.humer@gmail.com>
date Fri, 21 Dec 2012 10:44:31 -0800
parents
children 6343a09b2ec1
comparison
equal deleted inserted replaced
7290:a81db08fe930 7291:a748e4d44694
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.codegen.processor.ast;
24
25 import java.util.*;
26
27 import javax.lang.model.element.*;
28 import javax.lang.model.type.*;
29 import javax.lang.model.util.*;
30
31 import com.oracle.truffle.codegen.processor.ast.CodeTypeMirror.DeclaredCodeTypeMirror;
32
33 public class CodeTypeElement extends CodeElement<Element> implements TypeElement {
34
35 private final List<? extends CodeImport> imports = parentableList(this, new ArrayList<CodeImport>());
36
37 private final PackageElement packageElement;
38
39 private final Name simpleName;
40 private final Name packageName;
41 private Name qualifiedName;
42
43 private final List<TypeMirror> implementsInterfaces = new ArrayList<>();
44 private final ElementKind kind;
45 private TypeMirror superClass;
46
47 private final DeclaredCodeTypeMirror mirror = new DeclaredCodeTypeMirror(this);
48
49 public CodeTypeElement(Set<Modifier> modifiers, ElementKind kind, PackageElement packageElement, String simpleName) {
50 super(modifiers);
51 this.kind = kind;
52 this.packageElement = packageElement;
53 this.simpleName = CodeNames.of(simpleName);
54 if (this.packageElement != null) {
55 this.packageName = packageElement.getQualifiedName();
56 } else {
57 this.packageName = CodeNames.of("default");
58 }
59 this.qualifiedName = createQualifiedName();
60 }
61
62 @Override
63 public TypeMirror asType() {
64 return mirror;
65 }
66
67 @Override
68 public ElementKind getKind() {
69 return kind;
70 }
71
72 public boolean containsField(String name) {
73 for (VariableElement field : getFields()) {
74 if (field.getSimpleName().toString().equals(name)) {
75 return true;
76 }
77 }
78 return false;
79 }
80
81 @Override
82 public NestingKind getNestingKind() {
83 return isTopLevelClass() ? NestingKind.TOP_LEVEL : NestingKind.LOCAL;
84 }
85
86 @Override
87 public Element getEnclosingElement() {
88 if (isTopLevelClass()) {
89 return packageElement;
90 } else {
91 return super.getEnclosingElement();
92 }
93 }
94
95 @Override
96 public TypeMirror getSuperclass() {
97 return superClass;
98 }
99
100 @Override
101 public List<TypeMirror> getInterfaces() {
102 return implementsInterfaces;
103 }
104
105 @Override
106 public List< ? extends TypeParameterElement> getTypeParameters() {
107 return Collections.emptyList();
108 }
109
110 public boolean isTopLevelClass() {
111 return super.getEnclosingElement() instanceof CodeCompilationUnit;
112 }
113
114 public CodeVariableElement getField(String name) {
115 for (VariableElement field : ElementFilter.fieldsIn(getEnclosedElements())) {
116 if (field.getSimpleName().toString().equals(name)) {
117 return (CodeVariableElement) field;
118 }
119 }
120 return null;
121 }
122
123 private Name createQualifiedName() {
124 TypeElement enclosingType = getEnclosingClass();
125 if (enclosingType == null) {
126 return CodeNames.of(packageName + "." + simpleName);
127 } else {
128 return CodeNames.of(enclosingType.getQualifiedName() + "." + simpleName);
129 }
130 }
131
132 @Override
133 void setEnclosingElement(Element element) {
134 super.setEnclosingElement(element);
135
136 // update qualified name on container change
137 this.qualifiedName = createQualifiedName();
138 }
139
140 public Name getPackageName() {
141 return packageName;
142 }
143
144 @Override
145 public Name getQualifiedName() {
146 return qualifiedName;
147 }
148
149 @Override
150 public Name getSimpleName() {
151 return simpleName;
152 }
153
154 public void setSuperClass(TypeMirror superType) {
155 this.superClass = superType;
156 }
157
158 public List<? extends CodeImport> getImports() {
159 return imports;
160 }
161
162 public List<TypeMirror> getImplements() {
163 return implementsInterfaces;
164 }
165
166 @Override
167 public int hashCode() {
168 return getQualifiedName().hashCode();
169 }
170
171 @Override
172 public boolean equals(Object obj) {
173 if (obj == this) {
174 return true;
175 } else if (obj instanceof TypeElement) {
176 return getQualifiedName().equals(((TypeElement) obj).getQualifiedName());
177 }
178 return false;
179 }
180
181 public List<VariableElement> getFields() {
182 return ElementFilter.fieldsIn(getEnclosedElements());
183 }
184
185 public List<ExecutableElement> getMethods() {
186 return ElementFilter.methodsIn(getEnclosedElements());
187 }
188
189 public List<TypeElement> getInnerClasses() {
190 return ElementFilter.typesIn(getEnclosedElements());
191 }
192
193 @Override
194 public String toString() {
195 return getQualifiedName().toString();
196 }
197
198 @Override
199 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
200 return v.visitType(this, p);
201 }
202
203 }