comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/model/CodeVariableElement.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/java/model/CodeVariableElement.java@b31b2f289e7d
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.java.model;
24
25 import java.util.*;
26
27 import javax.lang.model.element.*;
28 import javax.lang.model.type.*;
29
30 import com.oracle.truffle.dsl.processor.java.*;
31
32 public final class CodeVariableElement extends CodeElement<Element> implements VariableElement {
33
34 private Name name;
35 private TypeMirror type;
36 private Object constantValue;
37
38 private CodeTree init;
39
40 public CodeVariableElement(TypeMirror type, String name) {
41 super(ElementUtils.modifiers());
42 this.type = type;
43 this.name = CodeNames.of(name);
44 }
45
46 public CodeVariableElement(Set<Modifier> modifiers, TypeMirror type, String name) {
47 super(modifiers);
48 this.type = type;
49 this.name = CodeNames.of(name);
50 }
51
52 public CodeVariableElement(Set<Modifier> modifiers, TypeMirror type, String name, String init) {
53 this(modifiers, type, name);
54 if (init != null) {
55 this.init = new CodeTree(null, CodeTreeKind.STRING, null, init);
56 }
57 }
58
59 public CodeTreeBuilder createInitBuilder() {
60 CodeTreeBuilder builder = new CodeTreeBuilder(null);
61 builder.setEnclosingElement(this);
62 init = builder.getTree();
63 return builder;
64 }
65
66 public void setInit(CodeTree init) {
67 this.init = init;
68 }
69
70 public CodeTree getInit() {
71 return init;
72 }
73
74 public Name getSimpleName() {
75 return name;
76 }
77
78 public TypeMirror getType() {
79 return type;
80 }
81
82 @Override
83 public TypeMirror asType() {
84 return type;
85 }
86
87 @Override
88 public String toString() {
89 return super.toString() + "/* " + ElementUtils.getSimpleName(type) + "*/";
90 }
91
92 @Override
93 public ElementKind getKind() {
94 if (getEnclosingElement() instanceof ExecutableElement) {
95 return ElementKind.PARAMETER;
96 } else if (getEnclosingElement() instanceof TypeElement) {
97 return ElementKind.FIELD;
98 } else {
99 return ElementKind.PARAMETER;
100 }
101 }
102
103 public void setConstantValue(Object constantValue) {
104 this.constantValue = constantValue;
105 }
106
107 @Override
108 public Object getConstantValue() {
109 return constantValue;
110 }
111
112 public String getName() {
113 return getSimpleName().toString();
114 }
115
116 public void setSimpleName(Name name) {
117 this.name = name;
118 }
119
120 public void setName(String name) {
121 this.name = CodeNames.of(name);
122 }
123
124 public void setType(TypeMirror type) {
125 this.type = type;
126 }
127
128 @Override
129 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
130 return v.visitVariable(this, p);
131 }
132
133 public static CodeVariableElement clone(VariableElement var) {
134 CodeVariableElement copy = new CodeVariableElement(var.getModifiers(), var.asType(), var.getSimpleName().toString());
135 copy.setConstantValue(var.getConstantValue());
136 for (AnnotationMirror mirror : var.getAnnotationMirrors()) {
137 copy.addAnnotationMirror(mirror);
138 }
139 for (Element element : var.getEnclosedElements()) {
140 copy.add(element);
141 }
142 return copy;
143 }
144
145 }