src/share/classes/com/sun/tools/javac/code/Attribute.java

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 113
eff38cc97183
child 308
03944ee4fac4
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

duke@1 1 /*
xdono@117 2 * Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.code;
duke@1 27
duke@1 28 import java.util.LinkedHashMap;
duke@1 29 import java.util.Map;
duke@1 30 import javax.lang.model.element.AnnotationMirror;
duke@1 31 import javax.lang.model.element.AnnotationValue;
duke@1 32 import javax.lang.model.element.AnnotationValueVisitor;
duke@1 33 import javax.lang.model.type.DeclaredType;
duke@1 34 import com.sun.tools.javac.code.Symbol.*;
duke@1 35 import com.sun.tools.javac.util.*;
duke@1 36
duke@1 37 import static com.sun.tools.javac.code.TypeTags.*;
duke@1 38
duke@1 39 /** An annotation value.
duke@1 40 *
duke@1 41 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 42 * you write code that depends on this, you do so at your own risk.
duke@1 43 * This code and its internal interfaces are subject to change or
duke@1 44 * deletion without notice.</b>
duke@1 45 */
duke@1 46 public abstract class Attribute implements AnnotationValue {
duke@1 47
duke@1 48 /** The type of the annotation element. */
duke@1 49 public Type type;
duke@1 50
duke@1 51 public Attribute(Type type) {
duke@1 52 this.type = type;
duke@1 53 }
duke@1 54
duke@1 55 public abstract void accept(Visitor v);
duke@1 56
duke@1 57 public Object getValue() {
duke@1 58 throw new UnsupportedOperationException();
duke@1 59 }
duke@1 60
duke@1 61 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 62 throw new UnsupportedOperationException();
duke@1 63 }
duke@1 64
duke@1 65
duke@1 66 /** The value for an annotation element of primitive type or String. */
duke@1 67 public static class Constant extends Attribute {
duke@1 68 public final Object value;
duke@1 69 public void accept(Visitor v) { v.visitConstant(this); }
duke@1 70 public Constant(Type type, Object value) {
duke@1 71 super(type);
duke@1 72 this.value = value;
duke@1 73 }
duke@1 74 public String toString() {
duke@1 75 return Constants.format(value, type);
duke@1 76 }
duke@1 77 public Object getValue() {
duke@1 78 return Constants.decode(value, type);
duke@1 79 }
duke@1 80 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 81 if (value instanceof String)
duke@1 82 return v.visitString((String) value, p);
duke@1 83 if (value instanceof Integer) {
duke@1 84 int i = (Integer) value;
duke@1 85 switch (type.tag) {
duke@1 86 case BOOLEAN: return v.visitBoolean(i != 0, p);
duke@1 87 case CHAR: return v.visitChar((char) i, p);
duke@1 88 case BYTE: return v.visitByte((byte) i, p);
duke@1 89 case SHORT: return v.visitShort((short) i, p);
duke@1 90 case INT: return v.visitInt(i, p);
duke@1 91 }
duke@1 92 }
duke@1 93 switch (type.tag) {
duke@1 94 case LONG: return v.visitLong((Long) value, p);
duke@1 95 case FLOAT: return v.visitFloat((Float) value, p);
duke@1 96 case DOUBLE: return v.visitDouble((Double) value, p);
duke@1 97 }
duke@1 98 throw new AssertionError("Bad annotation element value: " + value);
duke@1 99 }
duke@1 100 }
duke@1 101
duke@1 102 /** The value for an annotation element of type java.lang.Class,
duke@1 103 * represented as a ClassSymbol.
duke@1 104 */
duke@1 105 public static class Class extends Attribute {
duke@1 106 public final Type type;
duke@1 107 public void accept(Visitor v) { v.visitClass(this); }
duke@1 108 public Class(Types types, Type type) {
duke@1 109 super(makeClassType(types, type));
duke@1 110 this.type = type;
duke@1 111 }
duke@1 112 static Type makeClassType(Types types, Type type) {
duke@1 113 Type arg = type.isPrimitive()
duke@1 114 ? types.boxedClass(type).type
duke@1 115 : types.erasure(type);
duke@1 116 return new Type.ClassType(types.syms.classType.getEnclosingType(),
duke@1 117 List.of(arg),
duke@1 118 types.syms.classType.tsym);
duke@1 119 }
duke@1 120 public String toString() {
duke@1 121 return type + ".class";
duke@1 122 }
duke@1 123 public Type getValue() {
duke@1 124 return type;
duke@1 125 }
duke@1 126 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 127 return v.visitType(type, p);
duke@1 128 }
duke@1 129 }
duke@1 130
duke@1 131 /** A compound annotation element value, the type of which is an
duke@1 132 * attribute interface.
duke@1 133 */
duke@1 134 public static class Compound extends Attribute implements AnnotationMirror {
duke@1 135 /** The attributes values, as pairs. Each pair contains a
duke@1 136 * reference to the accessing method in the attribute interface
duke@1 137 * and the value to be returned when that method is called to
duke@1 138 * access this attribute.
duke@1 139 */
duke@1 140 public final List<Pair<MethodSymbol,Attribute>> values;
duke@1 141 public Compound(Type type,
duke@1 142 List<Pair<MethodSymbol,Attribute>> values) {
duke@1 143 super(type);
duke@1 144 this.values = values;
duke@1 145 }
duke@1 146 public void accept(Visitor v) { v.visitCompound(this); }
duke@1 147
duke@1 148 /**
duke@1 149 * Returns a string representation of this annotation.
duke@1 150 * String is of one of the forms:
duke@1 151 * @com.example.foo(name1=val1, name2=val2)
duke@1 152 * @com.example.foo(val)
duke@1 153 * @com.example.foo
duke@1 154 * Omit parens for marker annotations, and omit "value=" when allowed.
duke@1 155 */
duke@1 156 public String toString() {
duke@1 157 StringBuilder buf = new StringBuilder();
duke@1 158 buf.append("@");
duke@1 159 buf.append(type);
duke@1 160 int len = values.length();
duke@1 161 if (len > 0) {
duke@1 162 buf.append('(');
duke@1 163 boolean first = true;
duke@1 164 for (Pair<MethodSymbol, Attribute> value : values) {
duke@1 165 if (!first) buf.append(", ");
duke@1 166 first = false;
duke@1 167
duke@1 168 Name name = value.fst.name;
jjg@113 169 if (len > 1 || name != name.table.names.value) {
duke@1 170 buf.append(name);
duke@1 171 buf.append('=');
duke@1 172 }
duke@1 173 buf.append(value.snd);
duke@1 174 }
duke@1 175 buf.append(')');
duke@1 176 }
duke@1 177 return buf.toString();
duke@1 178 }
duke@1 179
duke@1 180 public Attribute member(Name member) {
duke@1 181 for (Pair<MethodSymbol,Attribute> pair : values)
duke@1 182 if (pair.fst.name == member) return pair.snd;
duke@1 183 return null;
duke@1 184 }
duke@1 185
duke@1 186 public Attribute.Compound getValue() {
duke@1 187 return this;
duke@1 188 }
duke@1 189
duke@1 190 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 191 return v.visitAnnotation(this, p);
duke@1 192 }
duke@1 193
duke@1 194 public DeclaredType getAnnotationType() {
duke@1 195 return (DeclaredType) type;
duke@1 196 }
duke@1 197
duke@1 198 public Map<MethodSymbol, Attribute> getElementValues() {
duke@1 199 Map<MethodSymbol, Attribute> valmap =
duke@1 200 new LinkedHashMap<MethodSymbol, Attribute>();
duke@1 201 for (Pair<MethodSymbol, Attribute> value : values)
duke@1 202 valmap.put(value.fst, value.snd);
duke@1 203 return valmap;
duke@1 204 }
duke@1 205 }
duke@1 206
duke@1 207 /** The value for an annotation element of an array type.
duke@1 208 */
duke@1 209 public static class Array extends Attribute {
duke@1 210 public final Attribute[] values;
duke@1 211 public Array(Type type, Attribute[] values) {
duke@1 212 super(type);
duke@1 213 this.values = values;
duke@1 214 }
duke@1 215 public void accept(Visitor v) { v.visitArray(this); }
duke@1 216 public String toString() {
duke@1 217 StringBuilder buf = new StringBuilder();
duke@1 218 buf.append('{');
duke@1 219 boolean first = true;
duke@1 220 for (Attribute value : values) {
duke@1 221 if (!first)
duke@1 222 buf.append(", ");
duke@1 223 first = false;
duke@1 224 buf.append(value);
duke@1 225 }
duke@1 226 buf.append('}');
duke@1 227 return buf.toString();
duke@1 228 }
duke@1 229 public List<Attribute> getValue() {
duke@1 230 return List.from(values);
duke@1 231 }
duke@1 232 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 233 return v.visitArray(getValue(), p);
duke@1 234 }
duke@1 235 }
duke@1 236
duke@1 237 /** The value for an annotation element of an enum type.
duke@1 238 */
duke@1 239 public static class Enum extends Attribute {
duke@1 240 public VarSymbol value;
duke@1 241 public Enum(Type type, VarSymbol value) {
duke@1 242 super(type);
duke@1 243 assert value != null;
duke@1 244 this.value = value;
duke@1 245 }
duke@1 246 public void accept(Visitor v) { v.visitEnum(this); }
duke@1 247 public String toString() {
duke@1 248 return value.enclClass() + "." + value; // qualified name
duke@1 249 }
duke@1 250 public VarSymbol getValue() {
duke@1 251 return value;
duke@1 252 }
duke@1 253 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 254 return v.visitEnumConstant(value, p);
duke@1 255 }
duke@1 256 }
duke@1 257
duke@1 258 public static class Error extends Attribute {
duke@1 259 public Error(Type type) {
duke@1 260 super(type);
duke@1 261 }
duke@1 262 public void accept(Visitor v) { v.visitError(this); }
duke@1 263 public String toString() {
duke@1 264 return "<error>";
duke@1 265 }
duke@1 266 public String getValue() {
duke@1 267 return toString();
duke@1 268 }
duke@1 269 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 270 return v.visitString(toString(), p);
duke@1 271 }
duke@1 272 }
duke@1 273
duke@1 274 /** A visitor type for dynamic dispatch on the kind of attribute value. */
duke@1 275 public static interface Visitor {
duke@1 276 void visitConstant(Attribute.Constant value);
duke@1 277 void visitClass(Attribute.Class clazz);
duke@1 278 void visitCompound(Attribute.Compound compound);
duke@1 279 void visitArray(Attribute.Array array);
duke@1 280 void visitEnum(Attribute.Enum e);
duke@1 281 void visitError(Attribute.Error e);
duke@1 282 }
duke@1 283 }

mercurial