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

Thu, 01 Nov 2012 10:48:36 +0100

author
ohrstrom
date
Thu, 01 Nov 2012 10:48:36 +0100
changeset 1384
bf54daa9dcd8
parent 1374
c002fdee76fd
child 1464
f72c9c5aeaef
permissions
-rw-r--r--

7153951: Add new lint option -Xlint:auxiliaryclass
Reviewed-by: jjg, mcimadamore, forax

duke@1 1 /*
jfranck@1313 2 * Copyright (c) 2003, 2012, 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
duke@1 63
duke@1 64 /** The value for an annotation element of primitive type or String. */
duke@1 65 public static class Constant extends Attribute {
duke@1 66 public final Object value;
duke@1 67 public void accept(Visitor v) { v.visitConstant(this); }
duke@1 68 public Constant(Type type, Object value) {
duke@1 69 super(type);
duke@1 70 this.value = value;
duke@1 71 }
duke@1 72 public String toString() {
duke@1 73 return Constants.format(value, type);
duke@1 74 }
duke@1 75 public Object getValue() {
duke@1 76 return Constants.decode(value, type);
duke@1 77 }
duke@1 78 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 79 if (value instanceof String)
duke@1 80 return v.visitString((String) value, p);
duke@1 81 if (value instanceof Integer) {
duke@1 82 int i = (Integer) value;
duke@1 83 switch (type.tag) {
duke@1 84 case BOOLEAN: return v.visitBoolean(i != 0, p);
duke@1 85 case CHAR: return v.visitChar((char) i, p);
duke@1 86 case BYTE: return v.visitByte((byte) i, p);
duke@1 87 case SHORT: return v.visitShort((short) i, p);
duke@1 88 case INT: return v.visitInt(i, p);
duke@1 89 }
duke@1 90 }
duke@1 91 switch (type.tag) {
duke@1 92 case LONG: return v.visitLong((Long) value, p);
duke@1 93 case FLOAT: return v.visitFloat((Float) value, p);
duke@1 94 case DOUBLE: return v.visitDouble((Double) value, p);
duke@1 95 }
duke@1 96 throw new AssertionError("Bad annotation element value: " + value);
duke@1 97 }
duke@1 98 }
duke@1 99
duke@1 100 /** The value for an annotation element of type java.lang.Class,
duke@1 101 * represented as a ClassSymbol.
duke@1 102 */
duke@1 103 public static class Class extends Attribute {
jfranck@1313 104 public final Type classType;
duke@1 105 public void accept(Visitor v) { v.visitClass(this); }
duke@1 106 public Class(Types types, Type type) {
duke@1 107 super(makeClassType(types, type));
jfranck@1313 108 this.classType = type;
duke@1 109 }
duke@1 110 static Type makeClassType(Types types, Type type) {
duke@1 111 Type arg = type.isPrimitive()
duke@1 112 ? types.boxedClass(type).type
duke@1 113 : types.erasure(type);
duke@1 114 return new Type.ClassType(types.syms.classType.getEnclosingType(),
duke@1 115 List.of(arg),
duke@1 116 types.syms.classType.tsym);
duke@1 117 }
duke@1 118 public String toString() {
jfranck@1313 119 return classType + ".class";
duke@1 120 }
duke@1 121 public Type getValue() {
jfranck@1313 122 return classType;
duke@1 123 }
duke@1 124 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
jfranck@1313 125 return v.visitType(classType, p);
duke@1 126 }
duke@1 127 }
duke@1 128
duke@1 129 /** A compound annotation element value, the type of which is an
duke@1 130 * attribute interface.
duke@1 131 */
duke@1 132 public static class Compound extends Attribute implements AnnotationMirror {
duke@1 133 /** The attributes values, as pairs. Each pair contains a
duke@1 134 * reference to the accessing method in the attribute interface
duke@1 135 * and the value to be returned when that method is called to
duke@1 136 * access this attribute.
duke@1 137 */
duke@1 138 public final List<Pair<MethodSymbol,Attribute>> values;
duke@1 139 public Compound(Type type,
duke@1 140 List<Pair<MethodSymbol,Attribute>> values) {
duke@1 141 super(type);
duke@1 142 this.values = values;
duke@1 143 }
duke@1 144 public void accept(Visitor v) { v.visitCompound(this); }
duke@1 145
duke@1 146 /**
duke@1 147 * Returns a string representation of this annotation.
duke@1 148 * String is of one of the forms:
duke@1 149 * @com.example.foo(name1=val1, name2=val2)
duke@1 150 * @com.example.foo(val)
duke@1 151 * @com.example.foo
duke@1 152 * Omit parens for marker annotations, and omit "value=" when allowed.
duke@1 153 */
duke@1 154 public String toString() {
duke@1 155 StringBuilder buf = new StringBuilder();
duke@1 156 buf.append("@");
duke@1 157 buf.append(type);
duke@1 158 int len = values.length();
duke@1 159 if (len > 0) {
duke@1 160 buf.append('(');
duke@1 161 boolean first = true;
duke@1 162 for (Pair<MethodSymbol, Attribute> value : values) {
duke@1 163 if (!first) buf.append(", ");
duke@1 164 first = false;
duke@1 165
duke@1 166 Name name = value.fst.name;
jjg@113 167 if (len > 1 || name != name.table.names.value) {
duke@1 168 buf.append(name);
duke@1 169 buf.append('=');
duke@1 170 }
duke@1 171 buf.append(value.snd);
duke@1 172 }
duke@1 173 buf.append(')');
duke@1 174 }
duke@1 175 return buf.toString();
duke@1 176 }
duke@1 177
duke@1 178 public Attribute member(Name member) {
duke@1 179 for (Pair<MethodSymbol,Attribute> pair : values)
duke@1 180 if (pair.fst.name == member) return pair.snd;
duke@1 181 return null;
duke@1 182 }
duke@1 183
duke@1 184 public Attribute.Compound getValue() {
duke@1 185 return this;
duke@1 186 }
duke@1 187
duke@1 188 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 189 return v.visitAnnotation(this, p);
duke@1 190 }
duke@1 191
duke@1 192 public DeclaredType getAnnotationType() {
duke@1 193 return (DeclaredType) type;
duke@1 194 }
duke@1 195
duke@1 196 public Map<MethodSymbol, Attribute> getElementValues() {
duke@1 197 Map<MethodSymbol, Attribute> valmap =
duke@1 198 new LinkedHashMap<MethodSymbol, Attribute>();
duke@1 199 for (Pair<MethodSymbol, Attribute> value : values)
duke@1 200 valmap.put(value.fst, value.snd);
duke@1 201 return valmap;
duke@1 202 }
duke@1 203 }
duke@1 204
duke@1 205 /** The value for an annotation element of an array type.
duke@1 206 */
duke@1 207 public static class Array extends Attribute {
duke@1 208 public final Attribute[] values;
duke@1 209 public Array(Type type, Attribute[] values) {
duke@1 210 super(type);
duke@1 211 this.values = values;
duke@1 212 }
jfranck@1313 213
jfranck@1313 214 public Array(Type type, List<Attribute> values) {
jfranck@1313 215 super(type);
jfranck@1313 216 this.values = values.toArray(new Attribute[values.size()]);
jfranck@1313 217 }
jfranck@1313 218
duke@1 219 public void accept(Visitor v) { v.visitArray(this); }
duke@1 220 public String toString() {
duke@1 221 StringBuilder buf = new StringBuilder();
duke@1 222 buf.append('{');
duke@1 223 boolean first = true;
duke@1 224 for (Attribute value : values) {
duke@1 225 if (!first)
duke@1 226 buf.append(", ");
duke@1 227 first = false;
duke@1 228 buf.append(value);
duke@1 229 }
duke@1 230 buf.append('}');
duke@1 231 return buf.toString();
duke@1 232 }
duke@1 233 public List<Attribute> getValue() {
duke@1 234 return List.from(values);
duke@1 235 }
duke@1 236 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 237 return v.visitArray(getValue(), p);
duke@1 238 }
duke@1 239 }
duke@1 240
duke@1 241 /** The value for an annotation element of an enum type.
duke@1 242 */
duke@1 243 public static class Enum extends Attribute {
duke@1 244 public VarSymbol value;
duke@1 245 public Enum(Type type, VarSymbol value) {
duke@1 246 super(type);
jjg@816 247 this.value = Assert.checkNonNull(value);
duke@1 248 }
duke@1 249 public void accept(Visitor v) { v.visitEnum(this); }
duke@1 250 public String toString() {
duke@1 251 return value.enclClass() + "." + value; // qualified name
duke@1 252 }
duke@1 253 public VarSymbol getValue() {
duke@1 254 return value;
duke@1 255 }
duke@1 256 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 257 return v.visitEnumConstant(value, p);
duke@1 258 }
duke@1 259 }
duke@1 260
duke@1 261 public static class Error extends Attribute {
duke@1 262 public Error(Type type) {
duke@1 263 super(type);
duke@1 264 }
duke@1 265 public void accept(Visitor v) { v.visitError(this); }
duke@1 266 public String toString() {
duke@1 267 return "<error>";
duke@1 268 }
duke@1 269 public String getValue() {
duke@1 270 return toString();
duke@1 271 }
duke@1 272 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 273 return v.visitString(toString(), p);
duke@1 274 }
duke@1 275 }
duke@1 276
duke@1 277 /** A visitor type for dynamic dispatch on the kind of attribute value. */
duke@1 278 public static interface Visitor {
duke@1 279 void visitConstant(Attribute.Constant value);
duke@1 280 void visitClass(Attribute.Class clazz);
duke@1 281 void visitCompound(Attribute.Compound compound);
duke@1 282 void visitArray(Attribute.Array array);
duke@1 283 void visitEnum(Attribute.Enum e);
duke@1 284 void visitError(Attribute.Error e);
duke@1 285 }
jjg@657 286
jjg@657 287 /** A mirror of java.lang.annotation.RetentionPolicy. */
jjg@657 288 public static enum RetentionPolicy {
jjg@657 289 SOURCE,
jjg@657 290 CLASS,
jjg@657 291 RUNTIME
jjg@657 292 }
duke@1 293 }

mercurial