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

Wed, 19 Mar 2014 16:44:49 +0000

author
vromero
date
Wed, 19 Mar 2014 16:44:49 +0000
changeset 2301
27a3026256cd
parent 2134
b0c086cd4520
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8034924: Incorrect inheritance of inaccessible static method
Reviewed-by: jjg, jlahoda

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
emc@2103 67 public TypeAnnotationPosition getPosition() { return null; };
emc@2103 68
duke@1 69 /** The value for an annotation element of primitive type or String. */
duke@1 70 public static class Constant extends Attribute {
duke@1 71 public final Object value;
duke@1 72 public void accept(Visitor v) { v.visitConstant(this); }
duke@1 73 public Constant(Type type, Object value) {
duke@1 74 super(type);
duke@1 75 this.value = value;
duke@1 76 }
duke@1 77 public String toString() {
duke@1 78 return Constants.format(value, type);
duke@1 79 }
duke@1 80 public Object getValue() {
duke@1 81 return Constants.decode(value, type);
duke@1 82 }
duke@1 83 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 84 if (value instanceof String)
duke@1 85 return v.visitString((String) value, p);
duke@1 86 if (value instanceof Integer) {
duke@1 87 int i = (Integer) value;
vromero@1853 88 switch (type.getTag()) {
duke@1 89 case BOOLEAN: return v.visitBoolean(i != 0, p);
duke@1 90 case CHAR: return v.visitChar((char) i, p);
duke@1 91 case BYTE: return v.visitByte((byte) i, p);
duke@1 92 case SHORT: return v.visitShort((short) i, p);
duke@1 93 case INT: return v.visitInt(i, p);
duke@1 94 }
duke@1 95 }
vromero@1853 96 switch (type.getTag()) {
duke@1 97 case LONG: return v.visitLong((Long) value, p);
duke@1 98 case FLOAT: return v.visitFloat((Float) value, p);
duke@1 99 case DOUBLE: return v.visitDouble((Double) value, p);
duke@1 100 }
duke@1 101 throw new AssertionError("Bad annotation element value: " + value);
duke@1 102 }
duke@1 103 }
duke@1 104
duke@1 105 /** The value for an annotation element of type java.lang.Class,
duke@1 106 * represented as a ClassSymbol.
duke@1 107 */
duke@1 108 public static class Class extends Attribute {
jfranck@1313 109 public final Type classType;
duke@1 110 public void accept(Visitor v) { v.visitClass(this); }
duke@1 111 public Class(Types types, Type type) {
duke@1 112 super(makeClassType(types, type));
jfranck@1313 113 this.classType = type;
duke@1 114 }
duke@1 115 static Type makeClassType(Types types, Type type) {
duke@1 116 Type arg = type.isPrimitive()
duke@1 117 ? types.boxedClass(type).type
duke@1 118 : types.erasure(type);
duke@1 119 return new Type.ClassType(types.syms.classType.getEnclosingType(),
duke@1 120 List.of(arg),
duke@1 121 types.syms.classType.tsym);
duke@1 122 }
duke@1 123 public String toString() {
jfranck@1313 124 return classType + ".class";
duke@1 125 }
duke@1 126 public Type getValue() {
jfranck@1313 127 return classType;
duke@1 128 }
duke@1 129 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
jfranck@1313 130 return v.visitType(classType, p);
duke@1 131 }
duke@1 132 }
duke@1 133
duke@1 134 /** A compound annotation element value, the type of which is an
duke@1 135 * attribute interface.
duke@1 136 */
duke@1 137 public static class Compound extends Attribute implements AnnotationMirror {
duke@1 138 /** The attributes values, as pairs. Each pair contains a
duke@1 139 * reference to the accessing method in the attribute interface
duke@1 140 * and the value to be returned when that method is called to
duke@1 141 * access this attribute.
duke@1 142 */
duke@1 143 public final List<Pair<MethodSymbol,Attribute>> values;
jfranck@1464 144
jfranck@1464 145 private boolean synthesized = false;
jfranck@1464 146
jfranck@1464 147 @Override
jfranck@1464 148 public boolean isSynthesized() {
jfranck@1464 149 return synthesized;
jfranck@1464 150 }
jfranck@1464 151
jfranck@1464 152 public void setSynthesized(boolean synthesized) {
jfranck@1464 153 this.synthesized = synthesized;
jfranck@1464 154 }
jfranck@1464 155
duke@1 156 public Compound(Type type,
duke@1 157 List<Pair<MethodSymbol,Attribute>> values) {
duke@1 158 super(type);
duke@1 159 this.values = values;
duke@1 160 }
duke@1 161 public void accept(Visitor v) { v.visitCompound(this); }
duke@1 162
duke@1 163 /**
duke@1 164 * Returns a string representation of this annotation.
duke@1 165 * String is of one of the forms:
duke@1 166 * @com.example.foo(name1=val1, name2=val2)
duke@1 167 * @com.example.foo(val)
duke@1 168 * @com.example.foo
duke@1 169 * Omit parens for marker annotations, and omit "value=" when allowed.
duke@1 170 */
duke@1 171 public String toString() {
duke@1 172 StringBuilder buf = new StringBuilder();
duke@1 173 buf.append("@");
duke@1 174 buf.append(type);
duke@1 175 int len = values.length();
duke@1 176 if (len > 0) {
duke@1 177 buf.append('(');
duke@1 178 boolean first = true;
duke@1 179 for (Pair<MethodSymbol, Attribute> value : values) {
duke@1 180 if (!first) buf.append(", ");
duke@1 181 first = false;
duke@1 182
duke@1 183 Name name = value.fst.name;
jjg@113 184 if (len > 1 || name != name.table.names.value) {
duke@1 185 buf.append(name);
duke@1 186 buf.append('=');
duke@1 187 }
duke@1 188 buf.append(value.snd);
duke@1 189 }
duke@1 190 buf.append(')');
duke@1 191 }
duke@1 192 return buf.toString();
duke@1 193 }
duke@1 194
duke@1 195 public Attribute member(Name member) {
emc@2103 196 Pair<MethodSymbol,Attribute> res = getElemPair(member);
emc@2103 197 return res == null ? null : res.snd;
emc@2103 198 }
emc@2103 199
emc@2103 200 private Pair<MethodSymbol, Attribute> getElemPair(Name member) {
duke@1 201 for (Pair<MethodSymbol,Attribute> pair : values)
emc@2103 202 if (pair.fst.name == member) return pair;
duke@1 203 return null;
duke@1 204 }
duke@1 205
duke@1 206 public Attribute.Compound getValue() {
duke@1 207 return this;
duke@1 208 }
duke@1 209
duke@1 210 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 211 return v.visitAnnotation(this, p);
duke@1 212 }
duke@1 213
duke@1 214 public DeclaredType getAnnotationType() {
duke@1 215 return (DeclaredType) type;
duke@1 216 }
duke@1 217
emc@2103 218 @Override
emc@2103 219 public TypeAnnotationPosition getPosition() {
emc@2103 220 if (values.size() != 0) {
emc@2103 221 Name valueName = values.head.fst.name.table.names.value;
emc@2103 222 Pair<MethodSymbol, Attribute> res = getElemPair(valueName);
emc@2103 223 return res == null ? null : res.snd.getPosition();
emc@2103 224 }
emc@2103 225 return null;
emc@2103 226 }
emc@2103 227
duke@1 228 public Map<MethodSymbol, Attribute> getElementValues() {
duke@1 229 Map<MethodSymbol, Attribute> valmap =
duke@1 230 new LinkedHashMap<MethodSymbol, Attribute>();
duke@1 231 for (Pair<MethodSymbol, Attribute> value : values)
duke@1 232 valmap.put(value.fst, value.snd);
duke@1 233 return valmap;
duke@1 234 }
duke@1 235 }
duke@1 236
jjg@1521 237 public static class TypeCompound extends Compound {
jjg@1521 238 public TypeAnnotationPosition position;
jjg@2134 239
jjg@1521 240 public TypeCompound(Compound compound,
jjg@1521 241 TypeAnnotationPosition position) {
jjg@1521 242 this(compound.type, compound.values, position);
jjg@1521 243 }
jjg@1521 244 public TypeCompound(Type type,
jjg@1521 245 List<Pair<MethodSymbol, Attribute>> values,
jjg@1521 246 TypeAnnotationPosition position) {
jjg@1521 247 super(type, values);
jjg@1521 248 this.position = position;
jjg@1521 249 }
jjg@1521 250
emc@2103 251 @Override
emc@2103 252 public TypeAnnotationPosition getPosition() {
emc@2103 253 if (hasUnknownPosition()) {
emc@2103 254 position = super.getPosition();
emc@2103 255 }
emc@2103 256 return position;
emc@2103 257 }
emc@2103 258
jjg@1755 259 public boolean hasUnknownPosition() {
jjg@2134 260 return position.type == TargetType.UNKNOWN;
jjg@1755 261 }
jjg@1755 262
jjg@1755 263 public boolean isContainerTypeCompound() {
jjg@1755 264 if (isSynthesized() && values.size() == 1)
jjg@1755 265 return getFirstEmbeddedTC() != null;
jjg@1755 266 return false;
jjg@1755 267 }
jjg@1755 268
jjg@1755 269 private TypeCompound getFirstEmbeddedTC() {
jjg@1755 270 if (values.size() == 1) {
jjg@1755 271 Pair<MethodSymbol, Attribute> val = values.get(0);
jjg@1755 272 if (val.fst.getSimpleName().contentEquals("value")
jjg@1755 273 && val.snd instanceof Array) {
jjg@1755 274 Array arr = (Array) val.snd;
jjg@1755 275 if (arr.values.length != 0
jjg@1755 276 && arr.values[0] instanceof Attribute.TypeCompound)
jjg@1755 277 return (Attribute.TypeCompound) arr.values[0];
jjg@1755 278 }
jjg@1755 279 }
jjg@1755 280 return null;
jjg@1755 281 }
jjg@1755 282
jjg@1755 283 public boolean tryFixPosition() {
jjg@1755 284 if (!isContainerTypeCompound())
jjg@1755 285 return false;
jjg@1755 286
jjg@1755 287 TypeCompound from = getFirstEmbeddedTC();
jjg@1755 288 if (from != null && from.position != null &&
jjg@1755 289 from.position.type != TargetType.UNKNOWN) {
jjg@1755 290 position = from.position;
jjg@1755 291 return true;
jjg@1755 292 }
jjg@1755 293 return false;
jjg@1755 294 }
jjg@1521 295 }
jjg@1521 296
duke@1 297 /** The value for an annotation element of an array type.
duke@1 298 */
duke@1 299 public static class Array extends Attribute {
duke@1 300 public final Attribute[] values;
duke@1 301 public Array(Type type, Attribute[] values) {
duke@1 302 super(type);
duke@1 303 this.values = values;
duke@1 304 }
jfranck@1313 305
jfranck@1313 306 public Array(Type type, List<Attribute> values) {
jfranck@1313 307 super(type);
jfranck@1313 308 this.values = values.toArray(new Attribute[values.size()]);
jfranck@1313 309 }
jfranck@1313 310
duke@1 311 public void accept(Visitor v) { v.visitArray(this); }
duke@1 312 public String toString() {
duke@1 313 StringBuilder buf = new StringBuilder();
duke@1 314 buf.append('{');
duke@1 315 boolean first = true;
duke@1 316 for (Attribute value : values) {
duke@1 317 if (!first)
duke@1 318 buf.append(", ");
duke@1 319 first = false;
duke@1 320 buf.append(value);
duke@1 321 }
duke@1 322 buf.append('}');
duke@1 323 return buf.toString();
duke@1 324 }
duke@1 325 public List<Attribute> getValue() {
duke@1 326 return List.from(values);
duke@1 327 }
duke@1 328 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 329 return v.visitArray(getValue(), p);
duke@1 330 }
emc@2103 331
emc@2103 332 @Override
emc@2103 333 public TypeAnnotationPosition getPosition() {
emc@2103 334 if (values.length != 0)
emc@2103 335 return values[0].getPosition();
emc@2103 336 else
emc@2103 337 return null;
emc@2103 338 }
duke@1 339 }
duke@1 340
duke@1 341 /** The value for an annotation element of an enum type.
duke@1 342 */
duke@1 343 public static class Enum extends Attribute {
duke@1 344 public VarSymbol value;
duke@1 345 public Enum(Type type, VarSymbol value) {
duke@1 346 super(type);
jjg@816 347 this.value = Assert.checkNonNull(value);
duke@1 348 }
duke@1 349 public void accept(Visitor v) { v.visitEnum(this); }
duke@1 350 public String toString() {
duke@1 351 return value.enclClass() + "." + value; // qualified name
duke@1 352 }
duke@1 353 public VarSymbol getValue() {
duke@1 354 return value;
duke@1 355 }
duke@1 356 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 357 return v.visitEnumConstant(value, p);
duke@1 358 }
duke@1 359 }
duke@1 360
duke@1 361 public static class Error extends Attribute {
duke@1 362 public Error(Type type) {
duke@1 363 super(type);
duke@1 364 }
duke@1 365 public void accept(Visitor v) { v.visitError(this); }
duke@1 366 public String toString() {
duke@1 367 return "<error>";
duke@1 368 }
duke@1 369 public String getValue() {
duke@1 370 return toString();
duke@1 371 }
duke@1 372 public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
duke@1 373 return v.visitString(toString(), p);
duke@1 374 }
duke@1 375 }
duke@1 376
jfranck@1960 377 public static class UnresolvedClass extends Error {
jfranck@1960 378 public Type classType;
jfranck@1960 379 public UnresolvedClass(Type type, Type classType) {
jfranck@1960 380 super(type);
jfranck@1960 381 this.classType = classType;
jfranck@1960 382 }
jfranck@1960 383 }
jfranck@1960 384
duke@1 385 /** A visitor type for dynamic dispatch on the kind of attribute value. */
duke@1 386 public static interface Visitor {
duke@1 387 void visitConstant(Attribute.Constant value);
duke@1 388 void visitClass(Attribute.Class clazz);
duke@1 389 void visitCompound(Attribute.Compound compound);
duke@1 390 void visitArray(Attribute.Array array);
duke@1 391 void visitEnum(Attribute.Enum e);
duke@1 392 void visitError(Attribute.Error e);
duke@1 393 }
jjg@657 394
jjg@657 395 /** A mirror of java.lang.annotation.RetentionPolicy. */
jjg@657 396 public static enum RetentionPolicy {
jjg@657 397 SOURCE,
jjg@657 398 CLASS,
jjg@657 399 RUNTIME
jjg@657 400 }
duke@1 401 }

mercurial