Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, 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.xml.internal.ws.spi.db; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.lang.reflect.Array; |
aoqi@0 | 29 | import java.lang.reflect.Field; |
aoqi@0 | 30 | import java.lang.reflect.GenericArrayType; |
aoqi@0 | 31 | import java.lang.reflect.Method; |
aoqi@0 | 32 | import java.lang.reflect.ParameterizedType; |
aoqi@0 | 33 | import java.lang.reflect.Type; |
aoqi@0 | 34 | import java.security.AccessController; |
aoqi@0 | 35 | import java.security.PrivilegedActionException; |
aoqi@0 | 36 | import java.security.PrivilegedExceptionAction; |
aoqi@0 | 37 | import java.util.ArrayList; |
aoqi@0 | 38 | import java.util.Arrays; |
aoqi@0 | 39 | import java.util.HashMap; |
aoqi@0 | 40 | import java.util.HashSet; |
aoqi@0 | 41 | import java.util.List; |
aoqi@0 | 42 | |
aoqi@0 | 43 | import javax.xml.bind.JAXBElement; |
aoqi@0 | 44 | import javax.xml.bind.annotation.XmlElement; |
aoqi@0 | 45 | import javax.xml.bind.annotation.XmlElementRef; |
aoqi@0 | 46 | import javax.xml.bind.annotation.XmlElementWrapper; |
aoqi@0 | 47 | import javax.xml.namespace.QName; |
aoqi@0 | 48 | |
aoqi@0 | 49 | /** |
aoqi@0 | 50 | * JAXBWrapperAccessor |
aoqi@0 | 51 | * |
aoqi@0 | 52 | * @author shih-chang.chen@oracle.com |
aoqi@0 | 53 | */ |
aoqi@0 | 54 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
aoqi@0 | 55 | public class JAXBWrapperAccessor extends WrapperAccessor { |
aoqi@0 | 56 | |
aoqi@0 | 57 | protected Class<?> contentClass; |
aoqi@0 | 58 | protected HashMap<Object, Class> elementDeclaredTypes; |
aoqi@0 | 59 | |
aoqi@0 | 60 | public JAXBWrapperAccessor(Class<?> wrapperBean) { |
aoqi@0 | 61 | contentClass = (Class<?>) wrapperBean; |
aoqi@0 | 62 | |
aoqi@0 | 63 | HashMap<Object, PropertySetter> setByQName = new HashMap<Object, PropertySetter>(); |
aoqi@0 | 64 | HashMap<Object, PropertySetter> setByLocalpart = new HashMap<Object, PropertySetter>(); |
aoqi@0 | 65 | HashMap<String, Method> publicSetters = new HashMap<String, Method>(); |
aoqi@0 | 66 | |
aoqi@0 | 67 | HashMap<Object, PropertyGetter> getByQName = new HashMap<Object, PropertyGetter>(); |
aoqi@0 | 68 | HashMap<Object, PropertyGetter> getByLocalpart = new HashMap<Object, PropertyGetter>(); |
aoqi@0 | 69 | HashMap<String, Method> publicGetters = new HashMap<String, Method>(); |
aoqi@0 | 70 | |
aoqi@0 | 71 | HashMap<Object, Class> elementDeclaredTypesByQName = new HashMap<Object, Class>(); |
aoqi@0 | 72 | HashMap<Object, Class> elementDeclaredTypesByLocalpart = new HashMap<Object, Class>(); |
aoqi@0 | 73 | |
aoqi@0 | 74 | for (Method method : contentClass.getMethods()) { |
aoqi@0 | 75 | if (PropertySetterBase.setterPattern(method)) { |
aoqi@0 | 76 | String key = method.getName() |
aoqi@0 | 77 | .substring(3, method.getName().length()).toLowerCase(); |
aoqi@0 | 78 | publicSetters.put(key, method); |
aoqi@0 | 79 | } |
aoqi@0 | 80 | if (PropertyGetterBase.getterPattern(method)) { |
aoqi@0 | 81 | String methodName = method.getName(); |
aoqi@0 | 82 | String key = methodName.startsWith("is") ? methodName |
aoqi@0 | 83 | .substring(2, method.getName().length()).toLowerCase() |
aoqi@0 | 84 | : methodName.substring(3, method.getName().length()) |
aoqi@0 | 85 | .toLowerCase(); |
aoqi@0 | 86 | publicGetters.put(key, method); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | } |
aoqi@0 | 89 | HashSet<String> elementLocalNames = new HashSet<String>(); |
aoqi@0 | 90 | for (Field field : getAllFields(contentClass)) { |
aoqi@0 | 91 | XmlElementWrapper xmlElemWrapper = field.getAnnotation(XmlElementWrapper.class); |
aoqi@0 | 92 | XmlElement xmlElem = field.getAnnotation(XmlElement.class); |
aoqi@0 | 93 | XmlElementRef xmlElemRef = field.getAnnotation(XmlElementRef.class); |
aoqi@0 | 94 | String fieldName = field.getName().toLowerCase(); |
aoqi@0 | 95 | String namespace = ""; |
aoqi@0 | 96 | String localName = field.getName(); |
aoqi@0 | 97 | if (xmlElemWrapper != null) { |
aoqi@0 | 98 | namespace = xmlElemWrapper.namespace(); |
aoqi@0 | 99 | if (xmlElemWrapper.name() != null && !xmlElemWrapper.name().equals("") |
aoqi@0 | 100 | && !xmlElemWrapper.name().equals("##default")) { |
aoqi@0 | 101 | localName = xmlElemWrapper.name(); |
aoqi@0 | 102 | } |
aoqi@0 | 103 | }else if (xmlElem != null) { |
aoqi@0 | 104 | namespace = xmlElem.namespace(); |
aoqi@0 | 105 | if (xmlElem.name() != null && !xmlElem.name().equals("") |
aoqi@0 | 106 | && !xmlElem.name().equals("##default")) { |
aoqi@0 | 107 | localName = xmlElem.name(); |
aoqi@0 | 108 | } |
aoqi@0 | 109 | } else if (xmlElemRef != null) { |
aoqi@0 | 110 | namespace = xmlElemRef.namespace(); |
aoqi@0 | 111 | if (xmlElemRef.name() != null && !xmlElemRef.name().equals("") |
aoqi@0 | 112 | && !xmlElemRef.name().equals("##default")) { |
aoqi@0 | 113 | localName = xmlElemRef.name(); |
aoqi@0 | 114 | } |
aoqi@0 | 115 | } |
aoqi@0 | 116 | if (elementLocalNames.contains(localName)) { |
aoqi@0 | 117 | this.elementLocalNameCollision = true; |
aoqi@0 | 118 | } else { |
aoqi@0 | 119 | elementLocalNames.add(localName); |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | QName qname = new QName(namespace, localName); |
aoqi@0 | 123 | if (field.getType().equals(JAXBElement.class)) { |
aoqi@0 | 124 | if (field.getGenericType() instanceof ParameterizedType) { |
aoqi@0 | 125 | Type arg = ((ParameterizedType) field.getGenericType()) |
aoqi@0 | 126 | .getActualTypeArguments()[0]; |
aoqi@0 | 127 | if (arg instanceof Class) { |
aoqi@0 | 128 | elementDeclaredTypesByQName.put(qname, (Class) arg); |
aoqi@0 | 129 | elementDeclaredTypesByLocalpart.put(localName, |
aoqi@0 | 130 | (Class) arg); |
aoqi@0 | 131 | } else if (arg instanceof GenericArrayType) { |
aoqi@0 | 132 | Type componentType = ((GenericArrayType) arg) |
aoqi@0 | 133 | .getGenericComponentType(); |
aoqi@0 | 134 | if (componentType instanceof Class) { |
aoqi@0 | 135 | Class arrayClass = Array.newInstance( |
aoqi@0 | 136 | (Class) componentType, 0).getClass(); |
aoqi@0 | 137 | elementDeclaredTypesByQName.put(qname, arrayClass); |
aoqi@0 | 138 | elementDeclaredTypesByLocalpart.put(localName, |
aoqi@0 | 139 | arrayClass); |
aoqi@0 | 140 | } |
aoqi@0 | 141 | } |
aoqi@0 | 142 | } |
aoqi@0 | 143 | |
aoqi@0 | 144 | } |
aoqi@0 | 145 | // _return |
aoqi@0 | 146 | if (fieldName.startsWith("_") && !localName.startsWith("_")) { |
aoqi@0 | 147 | fieldName = fieldName.substring(1); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | Method setMethod = publicSetters.get(fieldName); |
aoqi@0 | 150 | Method getMethod = publicGetters.get(fieldName); |
aoqi@0 | 151 | PropertySetter setter = createPropertySetter(field, setMethod); |
aoqi@0 | 152 | PropertyGetter getter = createPropertyGetter(field, getMethod); |
aoqi@0 | 153 | setByQName.put(qname, setter); |
aoqi@0 | 154 | setByLocalpart.put(localName, setter); |
aoqi@0 | 155 | getByQName.put(qname, getter); |
aoqi@0 | 156 | getByLocalpart.put(localName, getter); |
aoqi@0 | 157 | } |
aoqi@0 | 158 | if (this.elementLocalNameCollision) { |
aoqi@0 | 159 | this.propertySetters = setByQName; |
aoqi@0 | 160 | this.propertyGetters = getByQName; |
aoqi@0 | 161 | elementDeclaredTypes = elementDeclaredTypesByQName; |
aoqi@0 | 162 | } else { |
aoqi@0 | 163 | this.propertySetters = setByLocalpart; |
aoqi@0 | 164 | this.propertyGetters = getByLocalpart; |
aoqi@0 | 165 | elementDeclaredTypes = elementDeclaredTypesByLocalpart; |
aoqi@0 | 166 | } |
aoqi@0 | 167 | } |
aoqi@0 | 168 | |
aoqi@0 | 169 | static protected List<Field> getAllFields(Class<?> clz) { |
aoqi@0 | 170 | List<Field> list = new ArrayList<Field>(); |
aoqi@0 | 171 | while (!Object.class.equals(clz)) { |
aoqi@0 | 172 | list.addAll(Arrays.asList(getDeclaredFields(clz))); |
aoqi@0 | 173 | clz = clz.getSuperclass(); |
aoqi@0 | 174 | } |
aoqi@0 | 175 | return list; |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | static protected Field[] getDeclaredFields(final Class<?> clz) { |
aoqi@0 | 179 | try { |
aoqi@0 | 180 | return (System.getSecurityManager() == null) ? clz .getDeclaredFields() : |
aoqi@0 | 181 | AccessController.doPrivileged(new PrivilegedExceptionAction<Field[]>() { |
aoqi@0 | 182 | @Override |
aoqi@0 | 183 | public Field[] run() throws IllegalAccessException { |
aoqi@0 | 184 | return clz.getDeclaredFields(); |
aoqi@0 | 185 | } |
aoqi@0 | 186 | }); |
aoqi@0 | 187 | } catch (PrivilegedActionException e) { |
aoqi@0 | 188 | // TODO Auto-generated catch block |
aoqi@0 | 189 | e.printStackTrace(); |
aoqi@0 | 190 | return null; |
aoqi@0 | 191 | } |
aoqi@0 | 192 | } |
aoqi@0 | 193 | |
aoqi@0 | 194 | static protected PropertyGetter createPropertyGetter(Field field, Method getMethod) { |
aoqi@0 | 195 | if (!field.isAccessible()) { |
aoqi@0 | 196 | if (getMethod != null) { |
aoqi@0 | 197 | MethodGetter methodGetter = new MethodGetter(getMethod); |
aoqi@0 | 198 | if (methodGetter.getType().toString().equals(field.getType().toString())) { |
aoqi@0 | 199 | return methodGetter; |
aoqi@0 | 200 | } |
aoqi@0 | 201 | } |
aoqi@0 | 202 | } |
aoqi@0 | 203 | return new FieldGetter(field); |
aoqi@0 | 204 | } |
aoqi@0 | 205 | |
aoqi@0 | 206 | static protected PropertySetter createPropertySetter(Field field, |
aoqi@0 | 207 | Method setter) { |
aoqi@0 | 208 | if (!field.isAccessible()) { |
aoqi@0 | 209 | if (setter != null) { |
aoqi@0 | 210 | MethodSetter injection = new MethodSetter(setter); |
aoqi@0 | 211 | if (injection.getType().toString().equals(field.getType().toString())) { |
aoqi@0 | 212 | return injection; |
aoqi@0 | 213 | } |
aoqi@0 | 214 | } |
aoqi@0 | 215 | } |
aoqi@0 | 216 | return new FieldSetter(field); |
aoqi@0 | 217 | } |
aoqi@0 | 218 | |
aoqi@0 | 219 | private Class getElementDeclaredType(QName name) { |
aoqi@0 | 220 | Object key = (this.elementLocalNameCollision) ? name : name |
aoqi@0 | 221 | .getLocalPart(); |
aoqi@0 | 222 | return elementDeclaredTypes.get(key); |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | @Override |
aoqi@0 | 226 | public PropertyAccessor getPropertyAccessor(String ns, String name) { |
aoqi@0 | 227 | final QName n = new QName(ns, name); |
aoqi@0 | 228 | final PropertySetter setter = getPropertySetter(n); |
aoqi@0 | 229 | final PropertyGetter getter = getPropertyGetter(n); |
aoqi@0 | 230 | final boolean isJAXBElement = setter.getType() |
aoqi@0 | 231 | .equals(JAXBElement.class); |
aoqi@0 | 232 | final boolean isListType = java.util.List.class.isAssignableFrom(setter |
aoqi@0 | 233 | .getType()); |
aoqi@0 | 234 | final Class elementDeclaredType = isJAXBElement ? getElementDeclaredType(n) |
aoqi@0 | 235 | : null; |
aoqi@0 | 236 | return new PropertyAccessor() { |
aoqi@0 | 237 | @Override |
aoqi@0 | 238 | public Object get(Object bean) throws DatabindingException { |
aoqi@0 | 239 | Object val; |
aoqi@0 | 240 | if (isJAXBElement) { |
aoqi@0 | 241 | JAXBElement<Object> jaxbElement = (JAXBElement<Object>) getter.get(bean); |
aoqi@0 | 242 | val = (jaxbElement == null) ? null : jaxbElement.getValue(); |
aoqi@0 | 243 | } else { |
aoqi@0 | 244 | val = getter.get(bean); |
aoqi@0 | 245 | } |
aoqi@0 | 246 | if (val == null && isListType) { |
aoqi@0 | 247 | val = new java.util.ArrayList(); |
aoqi@0 | 248 | set(bean, val); |
aoqi@0 | 249 | } |
aoqi@0 | 250 | return val; |
aoqi@0 | 251 | } |
aoqi@0 | 252 | |
aoqi@0 | 253 | @Override |
aoqi@0 | 254 | public void set(Object bean, Object value) throws DatabindingException { |
aoqi@0 | 255 | if (isJAXBElement) { |
aoqi@0 | 256 | JAXBElement<Object> jaxbElement = new JAXBElement<Object>( |
aoqi@0 | 257 | n, elementDeclaredType, contentClass, value); |
aoqi@0 | 258 | setter.set(bean, jaxbElement); |
aoqi@0 | 259 | } else { |
aoqi@0 | 260 | setter.set(bean, value); |
aoqi@0 | 261 | } |
aoqi@0 | 262 | } |
aoqi@0 | 263 | }; |
aoqi@0 | 264 | } |
aoqi@0 | 265 | } |