Thu, 12 Oct 2017 19:44:07 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.codemodel.internal; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.lang.reflect.InvocationHandler; |
aoqi@0 | 29 | import java.lang.reflect.Method; |
aoqi@0 | 30 | import java.lang.reflect.Proxy; |
aoqi@0 | 31 | import java.lang.reflect.Type; |
aoqi@0 | 32 | import java.lang.reflect.ParameterizedType; |
aoqi@0 | 33 | import java.lang.reflect.InvocationTargetException; |
aoqi@0 | 34 | import java.lang.annotation.Annotation; |
aoqi@0 | 35 | import java.util.Map; |
aoqi@0 | 36 | import java.util.HashMap; |
aoqi@0 | 37 | |
aoqi@0 | 38 | /** |
aoqi@0 | 39 | * Dynamically implements the typed annotation writer interfaces. |
aoqi@0 | 40 | * |
aoqi@0 | 41 | * @author Kohsuke Kawaguchi |
aoqi@0 | 42 | */ |
aoqi@0 | 43 | class TypedAnnotationWriter<A extends Annotation,W extends JAnnotationWriter<A>> |
aoqi@0 | 44 | implements InvocationHandler, JAnnotationWriter<A> { |
aoqi@0 | 45 | /** |
aoqi@0 | 46 | * This is what we are writing to. |
aoqi@0 | 47 | */ |
aoqi@0 | 48 | private final JAnnotationUse use; |
aoqi@0 | 49 | |
aoqi@0 | 50 | /** |
aoqi@0 | 51 | * The annotation that we are writing. |
aoqi@0 | 52 | */ |
aoqi@0 | 53 | private final Class<A> annotation; |
aoqi@0 | 54 | |
aoqi@0 | 55 | /** |
aoqi@0 | 56 | * The type of the writer. |
aoqi@0 | 57 | */ |
aoqi@0 | 58 | private final Class<W> writerType; |
aoqi@0 | 59 | |
aoqi@0 | 60 | /** |
aoqi@0 | 61 | * Keeps track of writers for array members. |
aoqi@0 | 62 | * Lazily created. |
aoqi@0 | 63 | */ |
aoqi@0 | 64 | private Map<String,JAnnotationArrayMember> arrays; |
aoqi@0 | 65 | |
aoqi@0 | 66 | public TypedAnnotationWriter(Class<A> annotation, Class<W> writer, JAnnotationUse use) { |
aoqi@0 | 67 | this.annotation = annotation; |
aoqi@0 | 68 | this.writerType = writer; |
aoqi@0 | 69 | this.use = use; |
aoqi@0 | 70 | } |
aoqi@0 | 71 | |
aoqi@0 | 72 | public JAnnotationUse getAnnotationUse() { |
aoqi@0 | 73 | return use; |
aoqi@0 | 74 | } |
aoqi@0 | 75 | |
aoqi@0 | 76 | public Class<A> getAnnotationType() { |
aoqi@0 | 77 | return annotation; |
aoqi@0 | 78 | } |
aoqi@0 | 79 | |
aoqi@0 | 80 | @SuppressWarnings("unchecked") |
aoqi@0 | 81 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
aoqi@0 | 82 | |
aoqi@0 | 83 | if(method.getDeclaringClass()==JAnnotationWriter.class) { |
aoqi@0 | 84 | try { |
aoqi@0 | 85 | return method.invoke(this,args); |
aoqi@0 | 86 | } catch (InvocationTargetException e) { |
aoqi@0 | 87 | throw e.getTargetException(); |
aoqi@0 | 88 | } |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | String name = method.getName(); |
aoqi@0 | 92 | Object arg=null; |
aoqi@0 | 93 | if(args!=null && args.length>0) |
aoqi@0 | 94 | arg = args[0]; |
aoqi@0 | 95 | |
aoqi@0 | 96 | // check how it's defined on the annotation |
aoqi@0 | 97 | Method m = annotation.getDeclaredMethod(name); |
aoqi@0 | 98 | Class<?> rt = m.getReturnType(); |
aoqi@0 | 99 | |
aoqi@0 | 100 | // array value |
aoqi@0 | 101 | if(rt.isArray()) { |
aoqi@0 | 102 | return addArrayValue(proxy,name,rt.getComponentType(),method.getReturnType(),arg); |
aoqi@0 | 103 | } |
aoqi@0 | 104 | |
aoqi@0 | 105 | // sub annotation |
aoqi@0 | 106 | if(Annotation.class.isAssignableFrom(rt)) { |
aoqi@0 | 107 | Class<? extends Annotation> r = (Class<? extends Annotation>)rt; |
aoqi@0 | 108 | return new TypedAnnotationWriter( |
aoqi@0 | 109 | r,method.getReturnType(),use.annotationParam(name,r)).createProxy(); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | |
aoqi@0 | 112 | // scalar value |
aoqi@0 | 113 | |
aoqi@0 | 114 | if(arg instanceof JType) { |
aoqi@0 | 115 | JType targ = (JType) arg; |
aoqi@0 | 116 | checkType(Class.class,rt); |
aoqi@0 | 117 | if(m.getDefaultValue()!=null) { |
aoqi@0 | 118 | // check the default |
aoqi@0 | 119 | if(targ.equals(targ.owner().ref((Class)m.getDefaultValue()))) |
aoqi@0 | 120 | return proxy; // defaulted |
aoqi@0 | 121 | } |
aoqi@0 | 122 | use.param(name,targ); |
aoqi@0 | 123 | return proxy; |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | // other Java built-in types |
aoqi@0 | 127 | checkType(arg.getClass(),rt); |
aoqi@0 | 128 | if(m.getDefaultValue()!=null && m.getDefaultValue().equals(arg)) |
aoqi@0 | 129 | // defaulted. no need to write out. |
aoqi@0 | 130 | return proxy; |
aoqi@0 | 131 | |
aoqi@0 | 132 | if(arg instanceof String) { |
aoqi@0 | 133 | use.param(name,(String)arg); |
aoqi@0 | 134 | return proxy; |
aoqi@0 | 135 | } |
aoqi@0 | 136 | if(arg instanceof Boolean) { |
aoqi@0 | 137 | use.param(name,(Boolean)arg); |
aoqi@0 | 138 | return proxy; |
aoqi@0 | 139 | } |
aoqi@0 | 140 | if(arg instanceof Integer) { |
aoqi@0 | 141 | use.param(name,(Integer)arg); |
aoqi@0 | 142 | return proxy; |
aoqi@0 | 143 | } |
aoqi@0 | 144 | if(arg instanceof Class) { |
aoqi@0 | 145 | use.param(name,(Class)arg); |
aoqi@0 | 146 | return proxy; |
aoqi@0 | 147 | } |
aoqi@0 | 148 | if(arg instanceof Enum) { |
aoqi@0 | 149 | use.param(name,(Enum)arg); |
aoqi@0 | 150 | return proxy; |
aoqi@0 | 151 | } |
aoqi@0 | 152 | |
aoqi@0 | 153 | throw new IllegalArgumentException("Unable to handle this method call "+method.toString()); |
aoqi@0 | 154 | } |
aoqi@0 | 155 | |
aoqi@0 | 156 | @SuppressWarnings("unchecked") |
aoqi@0 | 157 | private Object addArrayValue(Object proxy,String name, Class itemType, Class expectedReturnType, Object arg) { |
aoqi@0 | 158 | if(arrays==null) |
aoqi@0 | 159 | arrays = new HashMap<String,JAnnotationArrayMember>(); |
aoqi@0 | 160 | JAnnotationArrayMember m = arrays.get(name); |
aoqi@0 | 161 | if(m==null) { |
aoqi@0 | 162 | m = use.paramArray(name); |
aoqi@0 | 163 | arrays.put(name,m); |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | // sub annotation |
aoqi@0 | 167 | if(Annotation.class.isAssignableFrom(itemType)) { |
aoqi@0 | 168 | Class<? extends Annotation> r = (Class<? extends Annotation>)itemType; |
aoqi@0 | 169 | if(!JAnnotationWriter.class.isAssignableFrom(expectedReturnType)) |
aoqi@0 | 170 | throw new IllegalArgumentException("Unexpected return type "+expectedReturnType); |
aoqi@0 | 171 | return new TypedAnnotationWriter(r,expectedReturnType,m.annotate(r)).createProxy(); |
aoqi@0 | 172 | } |
aoqi@0 | 173 | |
aoqi@0 | 174 | // primitive |
aoqi@0 | 175 | if(arg instanceof JType) { |
aoqi@0 | 176 | checkType(Class.class,itemType); |
aoqi@0 | 177 | m.param((JType)arg); |
aoqi@0 | 178 | return proxy; |
aoqi@0 | 179 | } |
aoqi@0 | 180 | checkType(arg.getClass(),itemType); |
aoqi@0 | 181 | if(arg instanceof String) { |
aoqi@0 | 182 | m.param((String)arg); |
aoqi@0 | 183 | return proxy; |
aoqi@0 | 184 | } |
aoqi@0 | 185 | if(arg instanceof Boolean) { |
aoqi@0 | 186 | m.param((Boolean)arg); |
aoqi@0 | 187 | return proxy; |
aoqi@0 | 188 | } |
aoqi@0 | 189 | if(arg instanceof Integer) { |
aoqi@0 | 190 | m.param((Integer)arg); |
aoqi@0 | 191 | return proxy; |
aoqi@0 | 192 | } |
aoqi@0 | 193 | if(arg instanceof Class) { |
aoqi@0 | 194 | m.param((Class)arg); |
aoqi@0 | 195 | return proxy; |
aoqi@0 | 196 | } |
aoqi@0 | 197 | // TODO: enum constant. how should we handle it? |
aoqi@0 | 198 | |
aoqi@0 | 199 | throw new IllegalArgumentException("Unable to handle this method call "); |
aoqi@0 | 200 | } |
aoqi@0 | 201 | |
aoqi@0 | 202 | |
aoqi@0 | 203 | /** |
aoqi@0 | 204 | * Check if the type of the argument matches our expectation. |
aoqi@0 | 205 | * If not, report an error. |
aoqi@0 | 206 | */ |
aoqi@0 | 207 | private void checkType(Class<?> actual, Class<?> expected) { |
aoqi@0 | 208 | if(expected==actual || expected.isAssignableFrom(actual)) |
aoqi@0 | 209 | return; // no problem |
aoqi@0 | 210 | |
aoqi@0 | 211 | if( expected==JCodeModel.boxToPrimitive.get(actual) ) |
aoqi@0 | 212 | return; // no problem |
aoqi@0 | 213 | |
aoqi@0 | 214 | throw new IllegalArgumentException("Expected "+expected+" but found "+actual); |
aoqi@0 | 215 | } |
aoqi@0 | 216 | |
aoqi@0 | 217 | /** |
aoqi@0 | 218 | * Creates a proxy and returns it. |
aoqi@0 | 219 | */ |
aoqi@0 | 220 | @SuppressWarnings("unchecked") |
aoqi@0 | 221 | private W createProxy() { |
aoqi@0 | 222 | return (W)Proxy.newProxyInstance( |
aoqi@0 | 223 | SecureLoader.getClassClassLoader(writerType),new Class[]{writerType},this); |
aoqi@0 | 224 | } |
aoqi@0 | 225 | |
aoqi@0 | 226 | /** |
aoqi@0 | 227 | * Creates a new typed annotation writer. |
aoqi@0 | 228 | */ |
aoqi@0 | 229 | @SuppressWarnings("unchecked") |
aoqi@0 | 230 | static <W extends JAnnotationWriter<?>> W create(Class<W> w, JAnnotatable annotatable) { |
aoqi@0 | 231 | Class<? extends Annotation> a = findAnnotationType(w); |
aoqi@0 | 232 | return (W)new TypedAnnotationWriter(a,w,annotatable.annotate(a)).createProxy(); |
aoqi@0 | 233 | } |
aoqi@0 | 234 | |
aoqi@0 | 235 | private static Class<? extends Annotation> findAnnotationType(Class<?> clazz) { |
aoqi@0 | 236 | for( Type t : clazz.getGenericInterfaces()) { |
aoqi@0 | 237 | if(t instanceof ParameterizedType) { |
aoqi@0 | 238 | ParameterizedType p = (ParameterizedType) t; |
aoqi@0 | 239 | if(p.getRawType()==JAnnotationWriter.class) |
aoqi@0 | 240 | return (Class<? extends Annotation>)p.getActualTypeArguments()[0]; |
aoqi@0 | 241 | } |
aoqi@0 | 242 | if(t instanceof Class<?>) { |
aoqi@0 | 243 | // recursive search |
aoqi@0 | 244 | Class<? extends Annotation> r = findAnnotationType((Class<?>)t); |
aoqi@0 | 245 | if(r!=null) return r; |
aoqi@0 | 246 | } |
aoqi@0 | 247 | } |
aoqi@0 | 248 | return null; |
aoqi@0 | 249 | } |
aoqi@0 | 250 | } |