Fri, 24 Oct 2014 15:02:28 +0200
8054367: More references for endpoints
Summary: fix also reviewed by Iaroslav.Savytskyi@oracle.com, Alexander.Fomin@oracle.com
Reviewed-by: mullan, skoivu
ohair@286 | 1 | /* |
mkos@384 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.ws.spi.db; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.lang.reflect.Array; |
ohair@286 | 29 | import java.lang.reflect.Field; |
ohair@286 | 30 | import java.lang.reflect.GenericArrayType; |
ohair@286 | 31 | import java.lang.reflect.Method; |
ohair@286 | 32 | import java.lang.reflect.ParameterizedType; |
ohair@286 | 33 | import java.lang.reflect.Type; |
ohair@286 | 34 | import java.security.AccessController; |
ohair@286 | 35 | import java.security.PrivilegedActionException; |
ohair@286 | 36 | import java.security.PrivilegedExceptionAction; |
ohair@286 | 37 | import java.util.ArrayList; |
alanb@368 | 38 | import java.util.Arrays; |
ohair@286 | 39 | import java.util.HashMap; |
ohair@286 | 40 | import java.util.HashSet; |
ohair@286 | 41 | import java.util.List; |
ohair@286 | 42 | |
ohair@286 | 43 | import javax.xml.bind.JAXBElement; |
ohair@286 | 44 | import javax.xml.bind.annotation.XmlElement; |
ohair@286 | 45 | import javax.xml.bind.annotation.XmlElementRef; |
mkos@384 | 46 | import javax.xml.bind.annotation.XmlElementWrapper; |
ohair@286 | 47 | import javax.xml.namespace.QName; |
ohair@286 | 48 | |
ohair@286 | 49 | /** |
ohair@286 | 50 | * JAXBWrapperAccessor |
ohair@286 | 51 | * |
ohair@286 | 52 | * @author shih-chang.chen@oracle.com |
ohair@286 | 53 | */ |
ohair@286 | 54 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
ohair@286 | 55 | public class JAXBWrapperAccessor extends WrapperAccessor { |
ohair@286 | 56 | |
ohair@286 | 57 | protected Class<?> contentClass; |
ohair@286 | 58 | protected HashMap<Object, Class> elementDeclaredTypes; |
ohair@286 | 59 | |
ohair@286 | 60 | public JAXBWrapperAccessor(Class<?> wrapperBean) { |
ohair@286 | 61 | contentClass = (Class<?>) wrapperBean; |
ohair@286 | 62 | |
ohair@286 | 63 | HashMap<Object, PropertySetter> setByQName = new HashMap<Object, PropertySetter>(); |
ohair@286 | 64 | HashMap<Object, PropertySetter> setByLocalpart = new HashMap<Object, PropertySetter>(); |
ohair@286 | 65 | HashMap<String, Method> publicSetters = new HashMap<String, Method>(); |
ohair@286 | 66 | |
ohair@286 | 67 | HashMap<Object, PropertyGetter> getByQName = new HashMap<Object, PropertyGetter>(); |
ohair@286 | 68 | HashMap<Object, PropertyGetter> getByLocalpart = new HashMap<Object, PropertyGetter>(); |
ohair@286 | 69 | HashMap<String, Method> publicGetters = new HashMap<String, Method>(); |
ohair@286 | 70 | |
ohair@286 | 71 | HashMap<Object, Class> elementDeclaredTypesByQName = new HashMap<Object, Class>(); |
ohair@286 | 72 | HashMap<Object, Class> elementDeclaredTypesByLocalpart = new HashMap<Object, Class>(); |
ohair@286 | 73 | |
ohair@286 | 74 | for (Method method : contentClass.getMethods()) { |
ohair@286 | 75 | if (PropertySetterBase.setterPattern(method)) { |
ohair@286 | 76 | String key = method.getName() |
ohair@286 | 77 | .substring(3, method.getName().length()).toLowerCase(); |
ohair@286 | 78 | publicSetters.put(key, method); |
ohair@286 | 79 | } |
ohair@286 | 80 | if (PropertyGetterBase.getterPattern(method)) { |
ohair@286 | 81 | String methodName = method.getName(); |
ohair@286 | 82 | String key = methodName.startsWith("is") ? methodName |
ohair@286 | 83 | .substring(2, method.getName().length()).toLowerCase() |
ohair@286 | 84 | : methodName.substring(3, method.getName().length()) |
ohair@286 | 85 | .toLowerCase(); |
ohair@286 | 86 | publicGetters.put(key, method); |
ohair@286 | 87 | } |
ohair@286 | 88 | } |
ohair@286 | 89 | HashSet<String> elementLocalNames = new HashSet<String>(); |
ohair@286 | 90 | for (Field field : getAllFields(contentClass)) { |
mkos@384 | 91 | XmlElementWrapper xmlElemWrapper = field.getAnnotation(XmlElementWrapper.class); |
ohair@286 | 92 | XmlElement xmlElem = field.getAnnotation(XmlElement.class); |
ohair@286 | 93 | XmlElementRef xmlElemRef = field.getAnnotation(XmlElementRef.class); |
ohair@286 | 94 | String fieldName = field.getName().toLowerCase(); |
ohair@286 | 95 | String namespace = ""; |
ohair@286 | 96 | String localName = field.getName(); |
mkos@384 | 97 | if (xmlElemWrapper != null) { |
mkos@384 | 98 | namespace = xmlElemWrapper.namespace(); |
mkos@384 | 99 | if (xmlElemWrapper.name() != null && !xmlElemWrapper.name().equals("") |
mkos@384 | 100 | && !xmlElemWrapper.name().equals("##default")) { |
mkos@384 | 101 | localName = xmlElemWrapper.name(); |
mkos@384 | 102 | } |
mkos@384 | 103 | }else if (xmlElem != null) { |
ohair@286 | 104 | namespace = xmlElem.namespace(); |
ohair@286 | 105 | if (xmlElem.name() != null && !xmlElem.name().equals("") |
ohair@286 | 106 | && !xmlElem.name().equals("##default")) { |
ohair@286 | 107 | localName = xmlElem.name(); |
ohair@286 | 108 | } |
ohair@286 | 109 | } else if (xmlElemRef != null) { |
ohair@286 | 110 | namespace = xmlElemRef.namespace(); |
ohair@286 | 111 | if (xmlElemRef.name() != null && !xmlElemRef.name().equals("") |
ohair@286 | 112 | && !xmlElemRef.name().equals("##default")) { |
ohair@286 | 113 | localName = xmlElemRef.name(); |
ohair@286 | 114 | } |
ohair@286 | 115 | } |
ohair@286 | 116 | if (elementLocalNames.contains(localName)) { |
ohair@286 | 117 | this.elementLocalNameCollision = true; |
ohair@286 | 118 | } else { |
ohair@286 | 119 | elementLocalNames.add(localName); |
ohair@286 | 120 | } |
ohair@286 | 121 | |
ohair@286 | 122 | QName qname = new QName(namespace, localName); |
ohair@286 | 123 | if (field.getType().equals(JAXBElement.class)) { |
ohair@286 | 124 | if (field.getGenericType() instanceof ParameterizedType) { |
ohair@286 | 125 | Type arg = ((ParameterizedType) field.getGenericType()) |
ohair@286 | 126 | .getActualTypeArguments()[0]; |
ohair@286 | 127 | if (arg instanceof Class) { |
ohair@286 | 128 | elementDeclaredTypesByQName.put(qname, (Class) arg); |
ohair@286 | 129 | elementDeclaredTypesByLocalpart.put(localName, |
ohair@286 | 130 | (Class) arg); |
ohair@286 | 131 | } else if (arg instanceof GenericArrayType) { |
ohair@286 | 132 | Type componentType = ((GenericArrayType) arg) |
ohair@286 | 133 | .getGenericComponentType(); |
ohair@286 | 134 | if (componentType instanceof Class) { |
ohair@286 | 135 | Class arrayClass = Array.newInstance( |
ohair@286 | 136 | (Class) componentType, 0).getClass(); |
ohair@286 | 137 | elementDeclaredTypesByQName.put(qname, arrayClass); |
ohair@286 | 138 | elementDeclaredTypesByLocalpart.put(localName, |
ohair@286 | 139 | arrayClass); |
ohair@286 | 140 | } |
ohair@286 | 141 | } |
ohair@286 | 142 | } |
ohair@286 | 143 | |
ohair@286 | 144 | } |
ohair@286 | 145 | // _return |
ohair@286 | 146 | if (fieldName.startsWith("_") && !localName.startsWith("_")) { |
ohair@286 | 147 | fieldName = fieldName.substring(1); |
ohair@286 | 148 | } |
ohair@286 | 149 | Method setMethod = publicSetters.get(fieldName); |
ohair@286 | 150 | Method getMethod = publicGetters.get(fieldName); |
ohair@286 | 151 | PropertySetter setter = createPropertySetter(field, setMethod); |
ohair@286 | 152 | PropertyGetter getter = createPropertyGetter(field, getMethod); |
ohair@286 | 153 | setByQName.put(qname, setter); |
ohair@286 | 154 | setByLocalpart.put(localName, setter); |
ohair@286 | 155 | getByQName.put(qname, getter); |
ohair@286 | 156 | getByLocalpart.put(localName, getter); |
ohair@286 | 157 | } |
ohair@286 | 158 | if (this.elementLocalNameCollision) { |
ohair@286 | 159 | this.propertySetters = setByQName; |
ohair@286 | 160 | this.propertyGetters = getByQName; |
ohair@286 | 161 | elementDeclaredTypes = elementDeclaredTypesByQName; |
ohair@286 | 162 | } else { |
ohair@286 | 163 | this.propertySetters = setByLocalpart; |
ohair@286 | 164 | this.propertyGetters = getByLocalpart; |
ohair@286 | 165 | elementDeclaredTypes = elementDeclaredTypesByLocalpart; |
ohair@286 | 166 | } |
ohair@286 | 167 | } |
ohair@286 | 168 | |
ohair@286 | 169 | static protected List<Field> getAllFields(Class<?> clz) { |
ohair@286 | 170 | List<Field> list = new ArrayList<Field>(); |
ohair@286 | 171 | while (!Object.class.equals(clz)) { |
alanb@368 | 172 | list.addAll(Arrays.asList(getDeclaredFields(clz))); |
ohair@286 | 173 | clz = clz.getSuperclass(); |
ohair@286 | 174 | } |
ohair@286 | 175 | return list; |
ohair@286 | 176 | } |
ohair@286 | 177 | |
ohair@286 | 178 | static protected Field[] getDeclaredFields(final Class<?> clz) { |
ohair@286 | 179 | try { |
ohair@286 | 180 | return (System.getSecurityManager() == null) ? clz .getDeclaredFields() : |
ohair@286 | 181 | AccessController.doPrivileged(new PrivilegedExceptionAction<Field[]>() { |
alanb@368 | 182 | @Override |
ohair@286 | 183 | public Field[] run() throws IllegalAccessException { |
ohair@286 | 184 | return clz.getDeclaredFields(); |
ohair@286 | 185 | } |
ohair@286 | 186 | }); |
ohair@286 | 187 | } catch (PrivilegedActionException e) { |
ohair@286 | 188 | // TODO Auto-generated catch block |
ohair@286 | 189 | e.printStackTrace(); |
ohair@286 | 190 | return null; |
ohair@286 | 191 | } |
ohair@286 | 192 | } |
ohair@286 | 193 | |
ohair@286 | 194 | static protected PropertyGetter createPropertyGetter(Field field, Method getMethod) { |
ohair@286 | 195 | if (!field.isAccessible()) { |
ohair@286 | 196 | if (getMethod != null) { |
ohair@286 | 197 | MethodGetter methodGetter = new MethodGetter(getMethod); |
alanb@368 | 198 | if (methodGetter.getType().toString().equals(field.getType().toString())) { |
ohair@286 | 199 | return methodGetter; |
ohair@286 | 200 | } |
ohair@286 | 201 | } |
ohair@286 | 202 | } |
ohair@286 | 203 | return new FieldGetter(field); |
ohair@286 | 204 | } |
ohair@286 | 205 | |
ohair@286 | 206 | static protected PropertySetter createPropertySetter(Field field, |
ohair@286 | 207 | Method setter) { |
ohair@286 | 208 | if (!field.isAccessible()) { |
ohair@286 | 209 | if (setter != null) { |
ohair@286 | 210 | MethodSetter injection = new MethodSetter(setter); |
alanb@368 | 211 | if (injection.getType().toString().equals(field.getType().toString())) { |
ohair@286 | 212 | return injection; |
ohair@286 | 213 | } |
ohair@286 | 214 | } |
ohair@286 | 215 | } |
ohair@286 | 216 | return new FieldSetter(field); |
ohair@286 | 217 | } |
ohair@286 | 218 | |
ohair@286 | 219 | private Class getElementDeclaredType(QName name) { |
ohair@286 | 220 | Object key = (this.elementLocalNameCollision) ? name : name |
ohair@286 | 221 | .getLocalPart(); |
ohair@286 | 222 | return elementDeclaredTypes.get(key); |
ohair@286 | 223 | } |
ohair@286 | 224 | |
alanb@368 | 225 | @Override |
ohair@286 | 226 | public PropertyAccessor getPropertyAccessor(String ns, String name) { |
ohair@286 | 227 | final QName n = new QName(ns, name); |
ohair@286 | 228 | final PropertySetter setter = getPropertySetter(n); |
ohair@286 | 229 | final PropertyGetter getter = getPropertyGetter(n); |
ohair@286 | 230 | final boolean isJAXBElement = setter.getType() |
ohair@286 | 231 | .equals(JAXBElement.class); |
ohair@286 | 232 | final boolean isListType = java.util.List.class.isAssignableFrom(setter |
ohair@286 | 233 | .getType()); |
ohair@286 | 234 | final Class elementDeclaredType = isJAXBElement ? getElementDeclaredType(n) |
ohair@286 | 235 | : null; |
ohair@286 | 236 | return new PropertyAccessor() { |
alanb@368 | 237 | @Override |
ohair@286 | 238 | public Object get(Object bean) throws DatabindingException { |
alanb@368 | 239 | Object val; |
ohair@286 | 240 | if (isJAXBElement) { |
ohair@286 | 241 | JAXBElement<Object> jaxbElement = (JAXBElement<Object>) getter.get(bean); |
ohair@286 | 242 | val = (jaxbElement == null) ? null : jaxbElement.getValue(); |
ohair@286 | 243 | } else { |
ohair@286 | 244 | val = getter.get(bean); |
ohair@286 | 245 | } |
ohair@286 | 246 | if (val == null && isListType) { |
ohair@286 | 247 | val = new java.util.ArrayList(); |
ohair@286 | 248 | set(bean, val); |
ohair@286 | 249 | } |
ohair@286 | 250 | return val; |
ohair@286 | 251 | } |
ohair@286 | 252 | |
alanb@368 | 253 | @Override |
ohair@286 | 254 | public void set(Object bean, Object value) throws DatabindingException { |
ohair@286 | 255 | if (isJAXBElement) { |
ohair@286 | 256 | JAXBElement<Object> jaxbElement = new JAXBElement<Object>( |
ohair@286 | 257 | n, elementDeclaredType, contentClass, value); |
ohair@286 | 258 | setter.set(bean, jaxbElement); |
ohair@286 | 259 | } else { |
ohair@286 | 260 | setter.set(bean, value); |
ohair@286 | 261 | } |
ohair@286 | 262 | } |
ohair@286 | 263 | }; |
ohair@286 | 264 | } |
ohair@286 | 265 | } |