Wed, 20 Jun 2012 13:23:26 -0700
7174143: encapsulate doc comment table
Reviewed-by: ksrini, mcimadamore
duke@1 | 1 | /* |
ohair@798 | 2 | * Copyright (c) 2005, 2010, 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 | * |
jjg@581 | 53 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 54 | * If 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) { |
darcy@577 | 184 | Name elemName = ((ArrayType) a.type).elemtype.tsym.getQualifiedName(); |
duke@1 | 185 | |
darcy@577 | 186 | if (elemName.equals(elemName.table.names.java_lang_Class)) { // Class[] |
duke@1 | 187 | // Construct a proxy for a MirroredTypesException |
darcy@577 | 188 | ListBuffer<TypeMirror> elems = new ListBuffer<TypeMirror>(); |
duke@1 | 189 | for (Attribute value : a.values) { |
duke@1 | 190 | Type elem = ((Attribute.Class) value).type; |
darcy@577 | 191 | elems.append(elem); |
duke@1 | 192 | } |
darcy@577 | 193 | value = new MirroredTypesExceptionProxy(elems.toList()); |
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 | */ |
jjg@713 | 253 | private void typeMismatch(Method method, final Attribute attr) { |
jjg@713 | 254 | class AnnotationTypeMismatchExceptionProxy extends ExceptionProxy { |
duke@1 | 255 | static final long serialVersionUID = 269; |
jjg@713 | 256 | transient final Method method; |
jjg@713 | 257 | AnnotationTypeMismatchExceptionProxy(Method method) { |
jjg@713 | 258 | this.method = method; |
jjg@713 | 259 | } |
duke@1 | 260 | public String toString() { |
duke@1 | 261 | return "<error>"; // eg: @Anno(value=<error>) |
duke@1 | 262 | } |
duke@1 | 263 | protected RuntimeException generateException() { |
duke@1 | 264 | return new AnnotationTypeMismatchException(method, |
duke@1 | 265 | attr.type.toString()); |
duke@1 | 266 | } |
jjg@713 | 267 | } |
jjg@713 | 268 | value = new AnnotationTypeMismatchExceptionProxy(method); |
duke@1 | 269 | } |
duke@1 | 270 | } |
duke@1 | 271 | |
duke@1 | 272 | |
duke@1 | 273 | /** |
duke@1 | 274 | * ExceptionProxy for MirroredTypeException. |
duke@1 | 275 | * The toString, hashCode, and equals methods foward to the underlying |
duke@1 | 276 | * type. |
duke@1 | 277 | */ |
darcy@506 | 278 | private static final class MirroredTypeExceptionProxy extends ExceptionProxy { |
duke@1 | 279 | static final long serialVersionUID = 269; |
duke@1 | 280 | |
darcy@506 | 281 | private transient TypeMirror type; |
duke@1 | 282 | private final String typeString; |
duke@1 | 283 | |
duke@1 | 284 | MirroredTypeExceptionProxy(TypeMirror t) { |
duke@1 | 285 | type = t; |
duke@1 | 286 | typeString = t.toString(); |
duke@1 | 287 | } |
duke@1 | 288 | |
duke@1 | 289 | public String toString() { |
duke@1 | 290 | return typeString; |
duke@1 | 291 | } |
duke@1 | 292 | |
duke@1 | 293 | public int hashCode() { |
duke@1 | 294 | return (type != null ? type : typeString).hashCode(); |
duke@1 | 295 | } |
duke@1 | 296 | |
duke@1 | 297 | public boolean equals(Object obj) { |
duke@1 | 298 | return type != null && |
duke@1 | 299 | obj instanceof MirroredTypeExceptionProxy && |
duke@1 | 300 | type.equals(((MirroredTypeExceptionProxy) obj).type); |
duke@1 | 301 | } |
duke@1 | 302 | |
duke@1 | 303 | protected RuntimeException generateException() { |
duke@1 | 304 | return new MirroredTypeException(type); |
duke@1 | 305 | } |
darcy@506 | 306 | |
darcy@506 | 307 | // Explicitly set all transient fields. |
darcy@506 | 308 | private void readObject(ObjectInputStream s) |
darcy@506 | 309 | throws IOException, ClassNotFoundException { |
darcy@506 | 310 | s.defaultReadObject(); |
darcy@506 | 311 | type = null; |
darcy@506 | 312 | } |
duke@1 | 313 | } |
duke@1 | 314 | |
duke@1 | 315 | |
duke@1 | 316 | /** |
duke@1 | 317 | * ExceptionProxy for MirroredTypesException. |
duke@1 | 318 | * The toString, hashCode, and equals methods foward to the underlying |
duke@1 | 319 | * types. |
duke@1 | 320 | */ |
darcy@506 | 321 | private static final class MirroredTypesExceptionProxy extends ExceptionProxy { |
duke@1 | 322 | static final long serialVersionUID = 269; |
duke@1 | 323 | |
darcy@506 | 324 | private transient List<TypeMirror> types; |
duke@1 | 325 | private final String typeStrings; |
duke@1 | 326 | |
duke@1 | 327 | MirroredTypesExceptionProxy(List<TypeMirror> ts) { |
duke@1 | 328 | types = ts; |
duke@1 | 329 | typeStrings = ts.toString(); |
duke@1 | 330 | } |
duke@1 | 331 | |
duke@1 | 332 | public String toString() { |
duke@1 | 333 | return typeStrings; |
duke@1 | 334 | } |
duke@1 | 335 | |
duke@1 | 336 | public int hashCode() { |
duke@1 | 337 | return (types != null ? types : typeStrings).hashCode(); |
duke@1 | 338 | } |
duke@1 | 339 | |
duke@1 | 340 | public boolean equals(Object obj) { |
duke@1 | 341 | return types != null && |
duke@1 | 342 | obj instanceof MirroredTypesExceptionProxy && |
duke@1 | 343 | types.equals( |
duke@1 | 344 | ((MirroredTypesExceptionProxy) obj).types); |
duke@1 | 345 | } |
duke@1 | 346 | |
duke@1 | 347 | protected RuntimeException generateException() { |
duke@1 | 348 | return new MirroredTypesException(types); |
duke@1 | 349 | } |
darcy@506 | 350 | |
darcy@506 | 351 | // Explicitly set all transient fields. |
darcy@506 | 352 | private void readObject(ObjectInputStream s) |
darcy@506 | 353 | throws IOException, ClassNotFoundException { |
darcy@506 | 354 | s.defaultReadObject(); |
darcy@506 | 355 | types = null; |
darcy@506 | 356 | } |
duke@1 | 357 | } |
duke@1 | 358 | } |