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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.code;
aoqi@0 27
aoqi@0 28 import java.util.LinkedHashMap;
aoqi@0 29 import java.util.Map;
aoqi@0 30 import javax.lang.model.element.AnnotationMirror;
aoqi@0 31 import javax.lang.model.element.AnnotationValue;
aoqi@0 32 import javax.lang.model.element.AnnotationValueVisitor;
aoqi@0 33 import javax.lang.model.type.DeclaredType;
aoqi@0 34 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 35 import com.sun.tools.javac.util.*;
aoqi@0 36
aoqi@0 37 /** An annotation value.
aoqi@0 38 *
aoqi@0 39 * <p><b>This is NOT part of any supported API.
aoqi@0 40 * If you write code that depends on this, you do so at your own risk.
aoqi@0 41 * This code and its internal interfaces are subject to change or
aoqi@0 42 * deletion without notice.</b>
aoqi@0 43 */
aoqi@0 44 public abstract class Attribute implements AnnotationValue {
aoqi@0 45
aoqi@0 46 /** The type of the annotation element. */
aoqi@0 47 public Type type;
aoqi@0 48
aoqi@0 49 public Attribute(Type type) {
aoqi@0 50 this.type = type;
aoqi@0 51 }
aoqi@0 52
aoqi@0 53 public abstract void accept(Visitor v);
aoqi@0 54
aoqi@0 55 public Object getValue() {
aoqi@0 56 throw new UnsupportedOperationException();
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
aoqi@0 60 throw new UnsupportedOperationException();
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 public boolean isSynthesized() {
aoqi@0 64 return false;
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 public TypeAnnotationPosition getPosition() { return null; };
aoqi@0 68
aoqi@0 69 /** The value for an annotation element of primitive type or String. */
aoqi@0 70 public static class Constant extends Attribute {
aoqi@0 71 public final Object value;
aoqi@0 72 public void accept(Visitor v) { v.visitConstant(this); }
aoqi@0 73 public Constant(Type type, Object value) {
aoqi@0 74 super(type);
aoqi@0 75 this.value = value;
aoqi@0 76 }
aoqi@0 77 public String toString() {
aoqi@0 78 return Constants.format(value, type);
aoqi@0 79 }
aoqi@0 80 public Object getValue() {
aoqi@0 81 return Constants.decode(value, type);
aoqi@0 82 }
aoqi@0 83 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
aoqi@0 84 if (value instanceof String)
aoqi@0 85 return v.visitString((String) value, p);
aoqi@0 86 if (value instanceof Integer) {
aoqi@0 87 int i = (Integer) value;
aoqi@0 88 switch (type.getTag()) {
aoqi@0 89 case BOOLEAN: return v.visitBoolean(i != 0, p);
aoqi@0 90 case CHAR: return v.visitChar((char) i, p);
aoqi@0 91 case BYTE: return v.visitByte((byte) i, p);
aoqi@0 92 case SHORT: return v.visitShort((short) i, p);
aoqi@0 93 case INT: return v.visitInt(i, p);
aoqi@0 94 }
aoqi@0 95 }
aoqi@0 96 switch (type.getTag()) {
aoqi@0 97 case LONG: return v.visitLong((Long) value, p);
aoqi@0 98 case FLOAT: return v.visitFloat((Float) value, p);
aoqi@0 99 case DOUBLE: return v.visitDouble((Double) value, p);
aoqi@0 100 }
aoqi@0 101 throw new AssertionError("Bad annotation element value: " + value);
aoqi@0 102 }
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 /** The value for an annotation element of type java.lang.Class,
aoqi@0 106 * represented as a ClassSymbol.
aoqi@0 107 */
aoqi@0 108 public static class Class extends Attribute {
aoqi@0 109 public final Type classType;
aoqi@0 110 public void accept(Visitor v) { v.visitClass(this); }
aoqi@0 111 public Class(Types types, Type type) {
aoqi@0 112 super(makeClassType(types, type));
aoqi@0 113 this.classType = type;
aoqi@0 114 }
aoqi@0 115 static Type makeClassType(Types types, Type type) {
aoqi@0 116 Type arg = type.isPrimitive()
aoqi@0 117 ? types.boxedClass(type).type
aoqi@0 118 : types.erasure(type);
aoqi@0 119 return new Type.ClassType(types.syms.classType.getEnclosingType(),
aoqi@0 120 List.of(arg),
aoqi@0 121 types.syms.classType.tsym);
aoqi@0 122 }
aoqi@0 123 public String toString() {
aoqi@0 124 return classType + ".class";
aoqi@0 125 }
aoqi@0 126 public Type getValue() {
aoqi@0 127 return classType;
aoqi@0 128 }
aoqi@0 129 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
aoqi@0 130 return v.visitType(classType, p);
aoqi@0 131 }
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 /** A compound annotation element value, the type of which is an
aoqi@0 135 * attribute interface.
aoqi@0 136 */
aoqi@0 137 public static class Compound extends Attribute implements AnnotationMirror {
aoqi@0 138 /** The attributes values, as pairs. Each pair contains a
aoqi@0 139 * reference to the accessing method in the attribute interface
aoqi@0 140 * and the value to be returned when that method is called to
aoqi@0 141 * access this attribute.
aoqi@0 142 */
aoqi@0 143 public final List<Pair<MethodSymbol,Attribute>> values;
aoqi@0 144
aoqi@0 145 private boolean synthesized = false;
aoqi@0 146
aoqi@0 147 @Override
aoqi@0 148 public boolean isSynthesized() {
aoqi@0 149 return synthesized;
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 public void setSynthesized(boolean synthesized) {
aoqi@0 153 this.synthesized = synthesized;
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 public Compound(Type type,
aoqi@0 157 List<Pair<MethodSymbol,Attribute>> values) {
aoqi@0 158 super(type);
aoqi@0 159 this.values = values;
aoqi@0 160 }
aoqi@0 161 public void accept(Visitor v) { v.visitCompound(this); }
aoqi@0 162
aoqi@0 163 /**
aoqi@0 164 * Returns a string representation of this annotation.
aoqi@0 165 * String is of one of the forms:
aoqi@0 166 * @com.example.foo(name1=val1, name2=val2)
aoqi@0 167 * @com.example.foo(val)
aoqi@0 168 * @com.example.foo
aoqi@0 169 * Omit parens for marker annotations, and omit "value=" when allowed.
aoqi@0 170 */
aoqi@0 171 public String toString() {
aoqi@0 172 StringBuilder buf = new StringBuilder();
aoqi@0 173 buf.append("@");
aoqi@0 174 buf.append(type);
aoqi@0 175 int len = values.length();
aoqi@0 176 if (len > 0) {
aoqi@0 177 buf.append('(');
aoqi@0 178 boolean first = true;
aoqi@0 179 for (Pair<MethodSymbol, Attribute> value : values) {
aoqi@0 180 if (!first) buf.append(", ");
aoqi@0 181 first = false;
aoqi@0 182
aoqi@0 183 Name name = value.fst.name;
aoqi@0 184 if (len > 1 || name != name.table.names.value) {
aoqi@0 185 buf.append(name);
aoqi@0 186 buf.append('=');
aoqi@0 187 }
aoqi@0 188 buf.append(value.snd);
aoqi@0 189 }
aoqi@0 190 buf.append(')');
aoqi@0 191 }
aoqi@0 192 return buf.toString();
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 public Attribute member(Name member) {
aoqi@0 196 Pair<MethodSymbol,Attribute> res = getElemPair(member);
aoqi@0 197 return res == null ? null : res.snd;
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 private Pair<MethodSymbol, Attribute> getElemPair(Name member) {
aoqi@0 201 for (Pair<MethodSymbol,Attribute> pair : values)
aoqi@0 202 if (pair.fst.name == member) return pair;
aoqi@0 203 return null;
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 public Attribute.Compound getValue() {
aoqi@0 207 return this;
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
aoqi@0 211 return v.visitAnnotation(this, p);
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 public DeclaredType getAnnotationType() {
aoqi@0 215 return (DeclaredType) type;
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 @Override
aoqi@0 219 public TypeAnnotationPosition getPosition() {
aoqi@0 220 if (values.size() != 0) {
aoqi@0 221 Name valueName = values.head.fst.name.table.names.value;
aoqi@0 222 Pair<MethodSymbol, Attribute> res = getElemPair(valueName);
aoqi@0 223 return res == null ? null : res.snd.getPosition();
aoqi@0 224 }
aoqi@0 225 return null;
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 public Map<MethodSymbol, Attribute> getElementValues() {
aoqi@0 229 Map<MethodSymbol, Attribute> valmap =
aoqi@0 230 new LinkedHashMap<MethodSymbol, Attribute>();
aoqi@0 231 for (Pair<MethodSymbol, Attribute> value : values)
aoqi@0 232 valmap.put(value.fst, value.snd);
aoqi@0 233 return valmap;
aoqi@0 234 }
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 public static class TypeCompound extends Compound {
aoqi@0 238 public TypeAnnotationPosition position;
aoqi@0 239
aoqi@0 240 public TypeCompound(Compound compound,
aoqi@0 241 TypeAnnotationPosition position) {
aoqi@0 242 this(compound.type, compound.values, position);
aoqi@0 243 }
aoqi@0 244 public TypeCompound(Type type,
aoqi@0 245 List<Pair<MethodSymbol, Attribute>> values,
aoqi@0 246 TypeAnnotationPosition position) {
aoqi@0 247 super(type, values);
aoqi@0 248 this.position = position;
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 @Override
aoqi@0 252 public TypeAnnotationPosition getPosition() {
aoqi@0 253 if (hasUnknownPosition()) {
aoqi@0 254 position = super.getPosition();
aoqi@0 255 }
aoqi@0 256 return position;
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 public boolean hasUnknownPosition() {
aoqi@0 260 return position.type == TargetType.UNKNOWN;
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 public boolean isContainerTypeCompound() {
aoqi@0 264 if (isSynthesized() && values.size() == 1)
aoqi@0 265 return getFirstEmbeddedTC() != null;
aoqi@0 266 return false;
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 private TypeCompound getFirstEmbeddedTC() {
aoqi@0 270 if (values.size() == 1) {
aoqi@0 271 Pair<MethodSymbol, Attribute> val = values.get(0);
aoqi@0 272 if (val.fst.getSimpleName().contentEquals("value")
aoqi@0 273 && val.snd instanceof Array) {
aoqi@0 274 Array arr = (Array) val.snd;
aoqi@0 275 if (arr.values.length != 0
aoqi@0 276 && arr.values[0] instanceof Attribute.TypeCompound)
aoqi@0 277 return (Attribute.TypeCompound) arr.values[0];
aoqi@0 278 }
aoqi@0 279 }
aoqi@0 280 return null;
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 public boolean tryFixPosition() {
aoqi@0 284 if (!isContainerTypeCompound())
aoqi@0 285 return false;
aoqi@0 286
aoqi@0 287 TypeCompound from = getFirstEmbeddedTC();
aoqi@0 288 if (from != null && from.position != null &&
aoqi@0 289 from.position.type != TargetType.UNKNOWN) {
aoqi@0 290 position = from.position;
aoqi@0 291 return true;
aoqi@0 292 }
aoqi@0 293 return false;
aoqi@0 294 }
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 /** The value for an annotation element of an array type.
aoqi@0 298 */
aoqi@0 299 public static class Array extends Attribute {
aoqi@0 300 public final Attribute[] values;
aoqi@0 301 public Array(Type type, Attribute[] values) {
aoqi@0 302 super(type);
aoqi@0 303 this.values = values;
aoqi@0 304 }
aoqi@0 305
aoqi@0 306 public Array(Type type, List<Attribute> values) {
aoqi@0 307 super(type);
aoqi@0 308 this.values = values.toArray(new Attribute[values.size()]);
aoqi@0 309 }
aoqi@0 310
aoqi@0 311 public void accept(Visitor v) { v.visitArray(this); }
aoqi@0 312 public String toString() {
aoqi@0 313 StringBuilder buf = new StringBuilder();
aoqi@0 314 buf.append('{');
aoqi@0 315 boolean first = true;
aoqi@0 316 for (Attribute value : values) {
aoqi@0 317 if (!first)
aoqi@0 318 buf.append(", ");
aoqi@0 319 first = false;
aoqi@0 320 buf.append(value);
aoqi@0 321 }
aoqi@0 322 buf.append('}');
aoqi@0 323 return buf.toString();
aoqi@0 324 }
aoqi@0 325 public List<Attribute> getValue() {
aoqi@0 326 return List.from(values);
aoqi@0 327 }
aoqi@0 328 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
aoqi@0 329 return v.visitArray(getValue(), p);
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 @Override
aoqi@0 333 public TypeAnnotationPosition getPosition() {
aoqi@0 334 if (values.length != 0)
aoqi@0 335 return values[0].getPosition();
aoqi@0 336 else
aoqi@0 337 return null;
aoqi@0 338 }
aoqi@0 339 }
aoqi@0 340
aoqi@0 341 /** The value for an annotation element of an enum type.
aoqi@0 342 */
aoqi@0 343 public static class Enum extends Attribute {
aoqi@0 344 public VarSymbol value;
aoqi@0 345 public Enum(Type type, VarSymbol value) {
aoqi@0 346 super(type);
aoqi@0 347 this.value = Assert.checkNonNull(value);
aoqi@0 348 }
aoqi@0 349 public void accept(Visitor v) { v.visitEnum(this); }
aoqi@0 350 public String toString() {
aoqi@0 351 return value.enclClass() + "." + value; // qualified name
aoqi@0 352 }
aoqi@0 353 public VarSymbol getValue() {
aoqi@0 354 return value;
aoqi@0 355 }
aoqi@0 356 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
aoqi@0 357 return v.visitEnumConstant(value, p);
aoqi@0 358 }
aoqi@0 359 }
aoqi@0 360
aoqi@0 361 public static class Error extends Attribute {
aoqi@0 362 public Error(Type type) {
aoqi@0 363 super(type);
aoqi@0 364 }
aoqi@0 365 public void accept(Visitor v) { v.visitError(this); }
aoqi@0 366 public String toString() {
aoqi@0 367 return "<error>";
aoqi@0 368 }
aoqi@0 369 public String getValue() {
aoqi@0 370 return toString();
aoqi@0 371 }
aoqi@0 372 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
aoqi@0 373 return v.visitString(toString(), p);
aoqi@0 374 }
aoqi@0 375 }
aoqi@0 376
aoqi@0 377 public static class UnresolvedClass extends Error {
aoqi@0 378 public Type classType;
aoqi@0 379 public UnresolvedClass(Type type, Type classType) {
aoqi@0 380 super(type);
aoqi@0 381 this.classType = classType;
aoqi@0 382 }
aoqi@0 383 }
aoqi@0 384
aoqi@0 385 /** A visitor type for dynamic dispatch on the kind of attribute value. */
aoqi@0 386 public static interface Visitor {
aoqi@0 387 void visitConstant(Attribute.Constant value);
aoqi@0 388 void visitClass(Attribute.Class clazz);
aoqi@0 389 void visitCompound(Attribute.Compound compound);
aoqi@0 390 void visitArray(Attribute.Array array);
aoqi@0 391 void visitEnum(Attribute.Enum e);
aoqi@0 392 void visitError(Attribute.Error e);
aoqi@0 393 }
aoqi@0 394
aoqi@0 395 /** A mirror of java.lang.annotation.RetentionPolicy. */
aoqi@0 396 public static enum RetentionPolicy {
aoqi@0 397 SOURCE,
aoqi@0 398 CLASS,
aoqi@0 399 RUNTIME
aoqi@0 400 }
aoqi@0 401 }

mercurial