src/share/classes/com/sun/tools/javac/model/AnnotationProxyMaker.java

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 506
85242c273d31
child 577
852d8bb356bc
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

duke@1 1 /*
ohair@554 2 * Copyright (c) 2005, 2008, 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.model;
duke@1 27
duke@1 28 import com.sun.tools.javac.util.*;
darcy@506 29 import java.io.ObjectInputStream;
darcy@506 30 import java.io.IOException;
duke@1 31 import java.lang.annotation.*;
duke@1 32 import java.lang.reflect.Array;
duke@1 33 import java.lang.reflect.Method;
duke@1 34 import java.util.LinkedHashMap;
duke@1 35 import java.util.Map;
duke@1 36 import sun.reflect.annotation.*;
duke@1 37
duke@1 38 import javax.lang.model.type.TypeMirror;
duke@1 39 import javax.lang.model.type.MirroredTypeException;
duke@1 40 import javax.lang.model.type.MirroredTypesException;
duke@1 41 import com.sun.tools.javac.code.*;
duke@1 42 import com.sun.tools.javac.code.Symbol.*;
duke@1 43 import com.sun.tools.javac.code.Type.ArrayType;
duke@1 44
duke@1 45
duke@1 46 /**
duke@1 47 * A generator of dynamic proxy implementations of
duke@1 48 * java.lang.annotation.Annotation.
duke@1 49 *
duke@1 50 * <p> The "dynamic proxy return form" of an annotation element value is
duke@1 51 * the form used by sun.reflect.annotation.AnnotationInvocationHandler.
duke@1 52 *
duke@1 53 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 54 * you write code that depends on this, you do so at your own risk.
duke@1 55 * This code and its internal interfaces are subject to change or
duke@1 56 * deletion without notice.</b>
duke@1 57 */
duke@1 58
duke@1 59 public class AnnotationProxyMaker {
duke@1 60
duke@1 61 private final Attribute.Compound anno;
duke@1 62 private final Class<? extends Annotation> annoType;
duke@1 63
duke@1 64
duke@1 65 private AnnotationProxyMaker(Attribute.Compound anno,
duke@1 66 Class<? extends Annotation> annoType) {
duke@1 67 this.anno = anno;
duke@1 68 this.annoType = annoType;
duke@1 69 }
duke@1 70
duke@1 71
duke@1 72 /**
duke@1 73 * Returns a dynamic proxy for an annotation mirror.
duke@1 74 */
duke@1 75 public static <A extends Annotation> A generateAnnotation(
duke@1 76 Attribute.Compound anno, Class<A> annoType) {
duke@1 77 AnnotationProxyMaker apm = new AnnotationProxyMaker(anno, annoType);
duke@1 78 return annoType.cast(apm.generateAnnotation());
duke@1 79 }
duke@1 80
duke@1 81
duke@1 82 /**
duke@1 83 * Returns a dynamic proxy for an annotation mirror.
duke@1 84 */
duke@1 85 private Annotation generateAnnotation() {
duke@1 86 return AnnotationParser.annotationForMap(annoType,
duke@1 87 getAllReflectedValues());
duke@1 88 }
duke@1 89
duke@1 90 /**
duke@1 91 * Returns a map from element names to their values in "dynamic
duke@1 92 * proxy return form". Includes all elements, whether explicit or
duke@1 93 * defaulted.
duke@1 94 */
duke@1 95 private Map<String, Object> getAllReflectedValues() {
duke@1 96 Map<String, Object> res = new LinkedHashMap<String, Object>();
duke@1 97
duke@1 98 for (Map.Entry<MethodSymbol, Attribute> entry :
duke@1 99 getAllValues().entrySet()) {
duke@1 100 MethodSymbol meth = entry.getKey();
duke@1 101 Object value = generateValue(meth, entry.getValue());
duke@1 102 if (value != null) {
duke@1 103 res.put(meth.name.toString(), value);
duke@1 104 } else {
duke@1 105 // Ignore this element. May (properly) lead to
duke@1 106 // IncompleteAnnotationException somewhere down the line.
duke@1 107 }
duke@1 108 }
duke@1 109 return res;
duke@1 110 }
duke@1 111
duke@1 112 /**
duke@1 113 * Returns a map from element symbols to their values.
duke@1 114 * Includes all elements, whether explicit or defaulted.
duke@1 115 */
duke@1 116 private Map<MethodSymbol, Attribute> getAllValues() {
duke@1 117 Map<MethodSymbol, Attribute> res =
duke@1 118 new LinkedHashMap<MethodSymbol, Attribute>();
duke@1 119
duke@1 120 // First find the default values.
duke@1 121 ClassSymbol sym = (ClassSymbol) anno.type.tsym;
duke@1 122 for (Scope.Entry e = sym.members().elems; e != null; e = e.sibling) {
duke@1 123 if (e.sym.kind == Kinds.MTH) {
duke@1 124 MethodSymbol m = (MethodSymbol) e.sym;
duke@1 125 Attribute def = m.getDefaultValue();
duke@1 126 if (def != null)
duke@1 127 res.put(m, def);
duke@1 128 }
duke@1 129 }
duke@1 130 // Next find the explicit values, possibly overriding defaults.
duke@1 131 for (Pair<MethodSymbol, Attribute> p : anno.values)
duke@1 132 res.put(p.fst, p.snd);
duke@1 133 return res;
duke@1 134 }
duke@1 135
duke@1 136 /**
duke@1 137 * Converts an element value to its "dynamic proxy return form".
duke@1 138 * Returns an exception proxy on some errors, but may return null if
duke@1 139 * a useful exception cannot or should not be generated at this point.
duke@1 140 */
duke@1 141 private Object generateValue(MethodSymbol meth, Attribute attr) {
duke@1 142 ValueVisitor vv = new ValueVisitor(meth);
duke@1 143 return vv.getValue(attr);
duke@1 144 }
duke@1 145
duke@1 146
duke@1 147 private class ValueVisitor implements Attribute.Visitor {
duke@1 148
duke@1 149 private MethodSymbol meth; // annotation element being visited
duke@1 150 private Class<?> returnClass; // return type of annotation element
duke@1 151 private Object value; // value in "dynamic proxy return form"
duke@1 152
duke@1 153 ValueVisitor(MethodSymbol meth) {
duke@1 154 this.meth = meth;
duke@1 155 }
duke@1 156
duke@1 157 Object getValue(Attribute attr) {
duke@1 158 Method method; // runtime method of annotation element
duke@1 159 try {
duke@1 160 method = annoType.getMethod(meth.name.toString());
duke@1 161 } catch (NoSuchMethodException e) {
duke@1 162 return null;
duke@1 163 }
duke@1 164 returnClass = method.getReturnType();
duke@1 165 attr.accept(this);
duke@1 166 if (!(value instanceof ExceptionProxy) &&
duke@1 167 !AnnotationType.invocationHandlerReturnType(returnClass)
duke@1 168 .isInstance(value)) {
duke@1 169 typeMismatch(method, attr);
duke@1 170 }
duke@1 171 return value;
duke@1 172 }
duke@1 173
duke@1 174
duke@1 175 public void visitConstant(Attribute.Constant c) {
duke@1 176 value = c.getValue();
duke@1 177 }
duke@1 178
duke@1 179 public void visitClass(Attribute.Class c) {
duke@1 180 value = new MirroredTypeExceptionProxy(c.type);
duke@1 181 }
duke@1 182
duke@1 183 public void visitArray(Attribute.Array a) {
duke@1 184 Name elemName = ((ArrayType) a.type).elemtype.tsym.name;
duke@1 185
jjg@113 186 if (elemName == elemName.table.names.java_lang_Class) { // Class[]
duke@1 187 // Construct a proxy for a MirroredTypesException
duke@1 188 List<TypeMirror> elems = List.nil();
duke@1 189 for (Attribute value : a.values) {
duke@1 190 Type elem = ((Attribute.Class) value).type;
duke@1 191 elems.add(elem);
duke@1 192 }
duke@1 193 value = new MirroredTypesExceptionProxy(elems);
duke@1 194
duke@1 195 } else {
duke@1 196 int len = a.values.length;
duke@1 197 Class<?> returnClassSaved = returnClass;
duke@1 198 returnClass = returnClass.getComponentType();
duke@1 199 try {
duke@1 200 Object res = Array.newInstance(returnClass, len);
duke@1 201 for (int i = 0; i < len; i++) {
duke@1 202 a.values[i].accept(this);
duke@1 203 if (value == null || value instanceof ExceptionProxy) {
duke@1 204 return;
duke@1 205 }
duke@1 206 try {
duke@1 207 Array.set(res, i, value);
duke@1 208 } catch (IllegalArgumentException e) {
duke@1 209 value = null; // indicates a type mismatch
duke@1 210 return;
duke@1 211 }
duke@1 212 }
duke@1 213 value = res;
duke@1 214 } finally {
duke@1 215 returnClass = returnClassSaved;
duke@1 216 }
duke@1 217 }
duke@1 218 }
duke@1 219
mcimadamore@184 220 @SuppressWarnings({"unchecked", "rawtypes"})
duke@1 221 public void visitEnum(Attribute.Enum e) {
duke@1 222 if (returnClass.isEnum()) {
duke@1 223 String constName = e.value.toString();
duke@1 224 try {
mcimadamore@184 225 value = Enum.valueOf((Class)returnClass, constName);
duke@1 226 } catch (IllegalArgumentException ex) {
duke@1 227 value = new EnumConstantNotPresentExceptionProxy(
mcimadamore@184 228 (Class<Enum<?>>) returnClass, constName);
duke@1 229 }
duke@1 230 } else {
duke@1 231 value = null; // indicates a type mismatch
duke@1 232 }
duke@1 233 }
duke@1 234
duke@1 235 public void visitCompound(Attribute.Compound c) {
duke@1 236 try {
duke@1 237 Class<? extends Annotation> nested =
duke@1 238 returnClass.asSubclass(Annotation.class);
duke@1 239 value = generateAnnotation(c, nested);
duke@1 240 } catch (ClassCastException ex) {
duke@1 241 value = null; // indicates a type mismatch
duke@1 242 }
duke@1 243 }
duke@1 244
duke@1 245 public void visitError(Attribute.Error e) {
duke@1 246 value = null; // indicates a type mismatch
duke@1 247 }
duke@1 248
duke@1 249
duke@1 250 /**
duke@1 251 * Sets "value" to an ExceptionProxy indicating a type mismatch.
duke@1 252 */
duke@1 253 private void typeMismatch(final Method method, final Attribute attr) {
duke@1 254 value = new ExceptionProxy() {
duke@1 255 static final long serialVersionUID = 269;
duke@1 256 public String toString() {
duke@1 257 return "<error>"; // eg: @Anno(value=<error>)
duke@1 258 }
duke@1 259 protected RuntimeException generateException() {
duke@1 260 return new AnnotationTypeMismatchException(method,
duke@1 261 attr.type.toString());
duke@1 262 }
duke@1 263 };
duke@1 264 }
duke@1 265 }
duke@1 266
duke@1 267
duke@1 268 /**
duke@1 269 * ExceptionProxy for MirroredTypeException.
duke@1 270 * The toString, hashCode, and equals methods foward to the underlying
duke@1 271 * type.
duke@1 272 */
darcy@506 273 private static final class MirroredTypeExceptionProxy extends ExceptionProxy {
duke@1 274 static final long serialVersionUID = 269;
duke@1 275
darcy@506 276 private transient TypeMirror type;
duke@1 277 private final String typeString;
duke@1 278
duke@1 279 MirroredTypeExceptionProxy(TypeMirror t) {
duke@1 280 type = t;
duke@1 281 typeString = t.toString();
duke@1 282 }
duke@1 283
duke@1 284 public String toString() {
duke@1 285 return typeString;
duke@1 286 }
duke@1 287
duke@1 288 public int hashCode() {
duke@1 289 return (type != null ? type : typeString).hashCode();
duke@1 290 }
duke@1 291
duke@1 292 public boolean equals(Object obj) {
duke@1 293 return type != null &&
duke@1 294 obj instanceof MirroredTypeExceptionProxy &&
duke@1 295 type.equals(((MirroredTypeExceptionProxy) obj).type);
duke@1 296 }
duke@1 297
duke@1 298 protected RuntimeException generateException() {
duke@1 299 return new MirroredTypeException(type);
duke@1 300 }
darcy@506 301
darcy@506 302 // Explicitly set all transient fields.
darcy@506 303 private void readObject(ObjectInputStream s)
darcy@506 304 throws IOException, ClassNotFoundException {
darcy@506 305 s.defaultReadObject();
darcy@506 306 type = null;
darcy@506 307 }
duke@1 308 }
duke@1 309
duke@1 310
duke@1 311 /**
duke@1 312 * ExceptionProxy for MirroredTypesException.
duke@1 313 * The toString, hashCode, and equals methods foward to the underlying
duke@1 314 * types.
duke@1 315 */
darcy@506 316 private static final class MirroredTypesExceptionProxy extends ExceptionProxy {
duke@1 317 static final long serialVersionUID = 269;
duke@1 318
darcy@506 319 private transient List<TypeMirror> types;
duke@1 320 private final String typeStrings;
duke@1 321
duke@1 322 MirroredTypesExceptionProxy(List<TypeMirror> ts) {
duke@1 323 types = ts;
duke@1 324 typeStrings = ts.toString();
duke@1 325 }
duke@1 326
duke@1 327 public String toString() {
duke@1 328 return typeStrings;
duke@1 329 }
duke@1 330
duke@1 331 public int hashCode() {
duke@1 332 return (types != null ? types : typeStrings).hashCode();
duke@1 333 }
duke@1 334
duke@1 335 public boolean equals(Object obj) {
duke@1 336 return types != null &&
duke@1 337 obj instanceof MirroredTypesExceptionProxy &&
duke@1 338 types.equals(
duke@1 339 ((MirroredTypesExceptionProxy) obj).types);
duke@1 340 }
duke@1 341
duke@1 342 protected RuntimeException generateException() {
duke@1 343 return new MirroredTypesException(types);
duke@1 344 }
darcy@506 345
darcy@506 346 // Explicitly set all transient fields.
darcy@506 347 private void readObject(ObjectInputStream s)
darcy@506 348 throws IOException, ClassNotFoundException {
darcy@506 349 s.defaultReadObject();
darcy@506 350 types = null;
darcy@506 351 }
duke@1 352 }
duke@1 353 }

mercurial