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

Thu, 21 Feb 2013 17:49:56 -0800

author
lana
date
Thu, 21 Feb 2013 17:49:56 -0800
changeset 1603
6118072811e5
parent 1521
71f35e4b93a5
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

Merge

duke@1 1 /*
jjg@1521 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * 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 /** An annotation value.
duke@1 38 *
jjg@581 39 * <p><b>This is NOT part of any supported API.
jjg@581 40 * If you write code that depends on this, you do so at your own risk.
duke@1 41 * This code and its internal interfaces are subject to change or
duke@1 42 * deletion without notice.</b>
duke@1 43 */
duke@1 44 public abstract class Attribute implements AnnotationValue {
duke@1 45
duke@1 46 /** The type of the annotation element. */
duke@1 47 public Type type;
duke@1 48
duke@1 49 public Attribute(Type type) {
duke@1 50 this.type = type;
duke@1 51 }
duke@1 52
duke@1 53 public abstract void accept(Visitor v);
duke@1 54
duke@1 55 public Object getValue() {
duke@1 56 throw new UnsupportedOperationException();
duke@1 57 }
duke@1 58
duke@1 59 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 60 throw new UnsupportedOperationException();
duke@1 61 }
duke@1 62
jfranck@1464 63 public boolean isSynthesized() {
jfranck@1464 64 return false;
jfranck@1464 65 }
duke@1 66
duke@1 67 /** The value for an annotation element of primitive type or String. */
duke@1 68 public static class Constant extends Attribute {
duke@1 69 public final Object value;
duke@1 70 public void accept(Visitor v) { v.visitConstant(this); }
duke@1 71 public Constant(Type type, Object value) {
duke@1 72 super(type);
duke@1 73 this.value = value;
duke@1 74 }
duke@1 75 public String toString() {
duke@1 76 return Constants.format(value, type);
duke@1 77 }
duke@1 78 public Object getValue() {
duke@1 79 return Constants.decode(value, type);
duke@1 80 }
duke@1 81 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 82 if (value instanceof String)
duke@1 83 return v.visitString((String) value, p);
duke@1 84 if (value instanceof Integer) {
duke@1 85 int i = (Integer) value;
duke@1 86 switch (type.tag) {
duke@1 87 case BOOLEAN: return v.visitBoolean(i != 0, p);
duke@1 88 case CHAR: return v.visitChar((char) i, p);
duke@1 89 case BYTE: return v.visitByte((byte) i, p);
duke@1 90 case SHORT: return v.visitShort((short) i, p);
duke@1 91 case INT: return v.visitInt(i, p);
duke@1 92 }
duke@1 93 }
duke@1 94 switch (type.tag) {
duke@1 95 case LONG: return v.visitLong((Long) value, p);
duke@1 96 case FLOAT: return v.visitFloat((Float) value, p);
duke@1 97 case DOUBLE: return v.visitDouble((Double) value, p);
duke@1 98 }
duke@1 99 throw new AssertionError("Bad annotation element value: " + value);
duke@1 100 }
duke@1 101 }
duke@1 102
duke@1 103 /** The value for an annotation element of type java.lang.Class,
duke@1 104 * represented as a ClassSymbol.
duke@1 105 */
duke@1 106 public static class Class extends Attribute {
jfranck@1313 107 public final Type classType;
duke@1 108 public void accept(Visitor v) { v.visitClass(this); }
duke@1 109 public Class(Types types, Type type) {
duke@1 110 super(makeClassType(types, type));
jfranck@1313 111 this.classType = type;
duke@1 112 }
duke@1 113 static Type makeClassType(Types types, Type type) {
duke@1 114 Type arg = type.isPrimitive()
duke@1 115 ? types.boxedClass(type).type
duke@1 116 : types.erasure(type);
duke@1 117 return new Type.ClassType(types.syms.classType.getEnclosingType(),
duke@1 118 List.of(arg),
duke@1 119 types.syms.classType.tsym);
duke@1 120 }
duke@1 121 public String toString() {
jfranck@1313 122 return classType + ".class";
duke@1 123 }
duke@1 124 public Type getValue() {
jfranck@1313 125 return classType;
duke@1 126 }
duke@1 127 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
jfranck@1313 128 return v.visitType(classType, p);
duke@1 129 }
duke@1 130 }
duke@1 131
duke@1 132 /** A compound annotation element value, the type of which is an
duke@1 133 * attribute interface.
duke@1 134 */
duke@1 135 public static class Compound extends Attribute implements AnnotationMirror {
duke@1 136 /** The attributes values, as pairs. Each pair contains a
duke@1 137 * reference to the accessing method in the attribute interface
duke@1 138 * and the value to be returned when that method is called to
duke@1 139 * access this attribute.
duke@1 140 */
duke@1 141 public final List<Pair<MethodSymbol,Attribute>> values;
jfranck@1464 142
jfranck@1464 143 private boolean synthesized = false;
jfranck@1464 144
jfranck@1464 145 @Override
jfranck@1464 146 public boolean isSynthesized() {
jfranck@1464 147 return synthesized;
jfranck@1464 148 }
jfranck@1464 149
jfranck@1464 150 public void setSynthesized(boolean synthesized) {
jfranck@1464 151 this.synthesized = synthesized;
jfranck@1464 152 }
jfranck@1464 153
duke@1 154 public Compound(Type type,
duke@1 155 List<Pair<MethodSymbol,Attribute>> values) {
duke@1 156 super(type);
duke@1 157 this.values = values;
duke@1 158 }
duke@1 159 public void accept(Visitor v) { v.visitCompound(this); }
duke@1 160
duke@1 161 /**
duke@1 162 * Returns a string representation of this annotation.
duke@1 163 * String is of one of the forms:
duke@1 164 * @com.example.foo(name1=val1, name2=val2)
duke@1 165 * @com.example.foo(val)
duke@1 166 * @com.example.foo
duke@1 167 * Omit parens for marker annotations, and omit "value=" when allowed.
duke@1 168 */
duke@1 169 public String toString() {
duke@1 170 StringBuilder buf = new StringBuilder();
duke@1 171 buf.append("@");
duke@1 172 buf.append(type);
duke@1 173 int len = values.length();
duke@1 174 if (len > 0) {
duke@1 175 buf.append('(');
duke@1 176 boolean first = true;
duke@1 177 for (Pair<MethodSymbol, Attribute> value : values) {
duke@1 178 if (!first) buf.append(", ");
duke@1 179 first = false;
duke@1 180
duke@1 181 Name name = value.fst.name;
jjg@113 182 if (len > 1 || name != name.table.names.value) {
duke@1 183 buf.append(name);
duke@1 184 buf.append('=');
duke@1 185 }
duke@1 186 buf.append(value.snd);
duke@1 187 }
duke@1 188 buf.append(')');
duke@1 189 }
duke@1 190 return buf.toString();
duke@1 191 }
duke@1 192
duke@1 193 public Attribute member(Name member) {
duke@1 194 for (Pair<MethodSymbol,Attribute> pair : values)
duke@1 195 if (pair.fst.name == member) return pair.snd;
duke@1 196 return null;
duke@1 197 }
duke@1 198
duke@1 199 public Attribute.Compound getValue() {
duke@1 200 return this;
duke@1 201 }
duke@1 202
duke@1 203 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 204 return v.visitAnnotation(this, p);
duke@1 205 }
duke@1 206
duke@1 207 public DeclaredType getAnnotationType() {
duke@1 208 return (DeclaredType) type;
duke@1 209 }
duke@1 210
duke@1 211 public Map<MethodSymbol, Attribute> getElementValues() {
duke@1 212 Map<MethodSymbol, Attribute> valmap =
duke@1 213 new LinkedHashMap<MethodSymbol, Attribute>();
duke@1 214 for (Pair<MethodSymbol, Attribute> value : values)
duke@1 215 valmap.put(value.fst, value.snd);
duke@1 216 return valmap;
duke@1 217 }
duke@1 218 }
duke@1 219
jjg@1521 220 public static class TypeCompound extends Compound {
jjg@1521 221 public TypeAnnotationPosition position;
jjg@1521 222 public TypeCompound(Compound compound,
jjg@1521 223 TypeAnnotationPosition position) {
jjg@1521 224 this(compound.type, compound.values, position);
jjg@1521 225 }
jjg@1521 226 public TypeCompound(Type type,
jjg@1521 227 List<Pair<MethodSymbol, Attribute>> values,
jjg@1521 228 TypeAnnotationPosition position) {
jjg@1521 229 super(type, values);
jjg@1521 230 this.position = position;
jjg@1521 231 }
jjg@1521 232
jjg@1521 233 }
jjg@1521 234
duke@1 235 /** The value for an annotation element of an array type.
duke@1 236 */
duke@1 237 public static class Array extends Attribute {
duke@1 238 public final Attribute[] values;
duke@1 239 public Array(Type type, Attribute[] values) {
duke@1 240 super(type);
duke@1 241 this.values = values;
duke@1 242 }
jfranck@1313 243
jfranck@1313 244 public Array(Type type, List<Attribute> values) {
jfranck@1313 245 super(type);
jfranck@1313 246 this.values = values.toArray(new Attribute[values.size()]);
jfranck@1313 247 }
jfranck@1313 248
duke@1 249 public void accept(Visitor v) { v.visitArray(this); }
duke@1 250 public String toString() {
duke@1 251 StringBuilder buf = new StringBuilder();
duke@1 252 buf.append('{');
duke@1 253 boolean first = true;
duke@1 254 for (Attribute value : values) {
duke@1 255 if (!first)
duke@1 256 buf.append(", ");
duke@1 257 first = false;
duke@1 258 buf.append(value);
duke@1 259 }
duke@1 260 buf.append('}');
duke@1 261 return buf.toString();
duke@1 262 }
duke@1 263 public List<Attribute> getValue() {
duke@1 264 return List.from(values);
duke@1 265 }
duke@1 266 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 267 return v.visitArray(getValue(), p);
duke@1 268 }
duke@1 269 }
duke@1 270
duke@1 271 /** The value for an annotation element of an enum type.
duke@1 272 */
duke@1 273 public static class Enum extends Attribute {
duke@1 274 public VarSymbol value;
duke@1 275 public Enum(Type type, VarSymbol value) {
duke@1 276 super(type);
jjg@816 277 this.value = Assert.checkNonNull(value);
duke@1 278 }
duke@1 279 public void accept(Visitor v) { v.visitEnum(this); }
duke@1 280 public String toString() {
duke@1 281 return value.enclClass() + "." + value; // qualified name
duke@1 282 }
duke@1 283 public VarSymbol getValue() {
duke@1 284 return value;
duke@1 285 }
duke@1 286 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 287 return v.visitEnumConstant(value, p);
duke@1 288 }
duke@1 289 }
duke@1 290
duke@1 291 public static class Error extends Attribute {
duke@1 292 public Error(Type type) {
duke@1 293 super(type);
duke@1 294 }
duke@1 295 public void accept(Visitor v) { v.visitError(this); }
duke@1 296 public String toString() {
duke@1 297 return "<error>";
duke@1 298 }
duke@1 299 public String getValue() {
duke@1 300 return toString();
duke@1 301 }
duke@1 302 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 303 return v.visitString(toString(), p);
duke@1 304 }
duke@1 305 }
duke@1 306
duke@1 307 /** A visitor type for dynamic dispatch on the kind of attribute value. */
duke@1 308 public static interface Visitor {
duke@1 309 void visitConstant(Attribute.Constant value);
duke@1 310 void visitClass(Attribute.Class clazz);
duke@1 311 void visitCompound(Attribute.Compound compound);
duke@1 312 void visitArray(Attribute.Array array);
duke@1 313 void visitEnum(Attribute.Enum e);
duke@1 314 void visitError(Attribute.Error e);
duke@1 315 }
jjg@657 316
jjg@657 317 /** A mirror of java.lang.annotation.RetentionPolicy. */
jjg@657 318 public static enum RetentionPolicy {
jjg@657 319 SOURCE,
jjg@657 320 CLASS,
jjg@657 321 RUNTIME
jjg@657 322 }
duke@1 323 }

mercurial