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