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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1357
c75be5bc5283
child 1645
97f6839673d6
permissions
-rw-r--r--

Merge

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

mercurial