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

Mon, 12 Aug 2013 17:25:07 +0100

author
mcimadamore
date
Mon, 12 Aug 2013 17:25:07 +0100
changeset 1945
f7f271bd74a2
parent 1853
831467c4c6a7
child 1960
e811fb09a1dc
permissions
-rw-r--r--

6537020: JCK tests: a compile-time error should be given in case of ambiguously imported fields (types, methods)
Summary: Hiding check does not support interface multiple inheritance
Reviewed-by: jjg

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;
vromero@1853 86 switch (type.getTag()) {
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 }
vromero@1853 94 switch (type.getTag()) {
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@1755 233 public boolean hasUnknownPosition() {
jjg@1755 234 return position == null || position.type == TargetType.UNKNOWN;
jjg@1755 235 }
jjg@1755 236
jjg@1755 237 public boolean isContainerTypeCompound() {
jjg@1755 238 if (isSynthesized() && values.size() == 1)
jjg@1755 239 return getFirstEmbeddedTC() != null;
jjg@1755 240 return false;
jjg@1755 241 }
jjg@1755 242
jjg@1755 243 private TypeCompound getFirstEmbeddedTC() {
jjg@1755 244 if (values.size() == 1) {
jjg@1755 245 Pair<MethodSymbol, Attribute> val = values.get(0);
jjg@1755 246 if (val.fst.getSimpleName().contentEquals("value")
jjg@1755 247 && val.snd instanceof Array) {
jjg@1755 248 Array arr = (Array) val.snd;
jjg@1755 249 if (arr.values.length != 0
jjg@1755 250 && arr.values[0] instanceof Attribute.TypeCompound)
jjg@1755 251 return (Attribute.TypeCompound) arr.values[0];
jjg@1755 252 }
jjg@1755 253 }
jjg@1755 254 return null;
jjg@1755 255 }
jjg@1755 256
jjg@1755 257 public boolean tryFixPosition() {
jjg@1755 258 if (!isContainerTypeCompound())
jjg@1755 259 return false;
jjg@1755 260
jjg@1755 261 TypeCompound from = getFirstEmbeddedTC();
jjg@1755 262 if (from != null && from.position != null &&
jjg@1755 263 from.position.type != TargetType.UNKNOWN) {
jjg@1755 264 position = from.position;
jjg@1755 265 return true;
jjg@1755 266 }
jjg@1755 267 return false;
jjg@1755 268 }
jjg@1521 269 }
jjg@1521 270
duke@1 271 /** The value for an annotation element of an array type.
duke@1 272 */
duke@1 273 public static class Array extends Attribute {
duke@1 274 public final Attribute[] values;
duke@1 275 public Array(Type type, Attribute[] values) {
duke@1 276 super(type);
duke@1 277 this.values = values;
duke@1 278 }
jfranck@1313 279
jfranck@1313 280 public Array(Type type, List<Attribute> values) {
jfranck@1313 281 super(type);
jfranck@1313 282 this.values = values.toArray(new Attribute[values.size()]);
jfranck@1313 283 }
jfranck@1313 284
duke@1 285 public void accept(Visitor v) { v.visitArray(this); }
duke@1 286 public String toString() {
duke@1 287 StringBuilder buf = new StringBuilder();
duke@1 288 buf.append('{');
duke@1 289 boolean first = true;
duke@1 290 for (Attribute value : values) {
duke@1 291 if (!first)
duke@1 292 buf.append(", ");
duke@1 293 first = false;
duke@1 294 buf.append(value);
duke@1 295 }
duke@1 296 buf.append('}');
duke@1 297 return buf.toString();
duke@1 298 }
duke@1 299 public List<Attribute> getValue() {
duke@1 300 return List.from(values);
duke@1 301 }
duke@1 302 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 303 return v.visitArray(getValue(), p);
duke@1 304 }
duke@1 305 }
duke@1 306
duke@1 307 /** The value for an annotation element of an enum type.
duke@1 308 */
duke@1 309 public static class Enum extends Attribute {
duke@1 310 public VarSymbol value;
duke@1 311 public Enum(Type type, VarSymbol value) {
duke@1 312 super(type);
jjg@816 313 this.value = Assert.checkNonNull(value);
duke@1 314 }
duke@1 315 public void accept(Visitor v) { v.visitEnum(this); }
duke@1 316 public String toString() {
duke@1 317 return value.enclClass() + "." + value; // qualified name
duke@1 318 }
duke@1 319 public VarSymbol getValue() {
duke@1 320 return value;
duke@1 321 }
duke@1 322 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 323 return v.visitEnumConstant(value, p);
duke@1 324 }
duke@1 325 }
duke@1 326
duke@1 327 public static class Error extends Attribute {
duke@1 328 public Error(Type type) {
duke@1 329 super(type);
duke@1 330 }
duke@1 331 public void accept(Visitor v) { v.visitError(this); }
duke@1 332 public String toString() {
duke@1 333 return "<error>";
duke@1 334 }
duke@1 335 public String getValue() {
duke@1 336 return toString();
duke@1 337 }
duke@1 338 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 339 return v.visitString(toString(), p);
duke@1 340 }
duke@1 341 }
duke@1 342
duke@1 343 /** A visitor type for dynamic dispatch on the kind of attribute value. */
duke@1 344 public static interface Visitor {
duke@1 345 void visitConstant(Attribute.Constant value);
duke@1 346 void visitClass(Attribute.Class clazz);
duke@1 347 void visitCompound(Attribute.Compound compound);
duke@1 348 void visitArray(Attribute.Array array);
duke@1 349 void visitEnum(Attribute.Enum e);
duke@1 350 void visitError(Attribute.Error e);
duke@1 351 }
jjg@657 352
jjg@657 353 /** A mirror of java.lang.annotation.RetentionPolicy. */
jjg@657 354 public static enum RetentionPolicy {
jjg@657 355 SOURCE,
jjg@657 356 CLASS,
jjg@657 357 RUNTIME
jjg@657 358 }
duke@1 359 }

mercurial