1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/spi/db/JAXBWrapperAccessor.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,257 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.spi.db; 1.30 + 1.31 +import java.lang.reflect.Array; 1.32 +import java.lang.reflect.Field; 1.33 +import java.lang.reflect.GenericArrayType; 1.34 +import java.lang.reflect.Method; 1.35 +import java.lang.reflect.ParameterizedType; 1.36 +import java.lang.reflect.Type; 1.37 +import java.security.AccessController; 1.38 +import java.security.PrivilegedActionException; 1.39 +import java.security.PrivilegedExceptionAction; 1.40 +import java.util.ArrayList; 1.41 +import java.util.HashMap; 1.42 +import java.util.HashSet; 1.43 +import java.util.List; 1.44 + 1.45 +import javax.xml.bind.JAXBElement; 1.46 +import javax.xml.bind.annotation.XmlElement; 1.47 +import javax.xml.bind.annotation.XmlElementRef; 1.48 +import javax.xml.namespace.QName; 1.49 + 1.50 +/** 1.51 + * JAXBWrapperAccessor 1.52 + * 1.53 + * @author shih-chang.chen@oracle.com 1.54 + */ 1.55 +@SuppressWarnings({ "unchecked", "rawtypes" }) 1.56 +public class JAXBWrapperAccessor extends WrapperAccessor { 1.57 + 1.58 + protected Class<?> contentClass; 1.59 + protected HashMap<Object, Class> elementDeclaredTypes; 1.60 + 1.61 + public JAXBWrapperAccessor(Class<?> wrapperBean) { 1.62 + contentClass = (Class<?>) wrapperBean; 1.63 + 1.64 + HashMap<Object, PropertySetter> setByQName = new HashMap<Object, PropertySetter>(); 1.65 + HashMap<Object, PropertySetter> setByLocalpart = new HashMap<Object, PropertySetter>(); 1.66 + HashMap<String, Method> publicSetters = new HashMap<String, Method>(); 1.67 + 1.68 + HashMap<Object, PropertyGetter> getByQName = new HashMap<Object, PropertyGetter>(); 1.69 + HashMap<Object, PropertyGetter> getByLocalpart = new HashMap<Object, PropertyGetter>(); 1.70 + HashMap<String, Method> publicGetters = new HashMap<String, Method>(); 1.71 + 1.72 + HashMap<Object, Class> elementDeclaredTypesByQName = new HashMap<Object, Class>(); 1.73 + HashMap<Object, Class> elementDeclaredTypesByLocalpart = new HashMap<Object, Class>(); 1.74 + 1.75 + for (Method method : contentClass.getMethods()) { 1.76 + if (PropertySetterBase.setterPattern(method)) { 1.77 + String key = method.getName() 1.78 + .substring(3, method.getName().length()).toLowerCase(); 1.79 + publicSetters.put(key, method); 1.80 + } 1.81 + if (PropertyGetterBase.getterPattern(method)) { 1.82 + String methodName = method.getName(); 1.83 + String key = methodName.startsWith("is") ? methodName 1.84 + .substring(2, method.getName().length()).toLowerCase() 1.85 + : methodName.substring(3, method.getName().length()) 1.86 + .toLowerCase(); 1.87 + publicGetters.put(key, method); 1.88 + } 1.89 + } 1.90 + HashSet<String> elementLocalNames = new HashSet<String>(); 1.91 + for (Field field : getAllFields(contentClass)) { 1.92 + XmlElement xmlElem = field.getAnnotation(XmlElement.class); 1.93 + XmlElementRef xmlElemRef = field.getAnnotation(XmlElementRef.class); 1.94 + String fieldName = field.getName().toLowerCase(); 1.95 + String namespace = ""; 1.96 + String localName = field.getName(); 1.97 + if (xmlElem != null) { 1.98 + namespace = xmlElem.namespace(); 1.99 + if (xmlElem.name() != null && !xmlElem.name().equals("") 1.100 + && !xmlElem.name().equals("##default")) { 1.101 + localName = xmlElem.name(); 1.102 + } 1.103 + } else if (xmlElemRef != null) { 1.104 + namespace = xmlElemRef.namespace(); 1.105 + if (xmlElemRef.name() != null && !xmlElemRef.name().equals("") 1.106 + && !xmlElemRef.name().equals("##default")) { 1.107 + localName = xmlElemRef.name(); 1.108 + } 1.109 + } 1.110 + if (elementLocalNames.contains(localName)) { 1.111 + this.elementLocalNameCollision = true; 1.112 + } else { 1.113 + elementLocalNames.add(localName); 1.114 + } 1.115 + 1.116 + QName qname = new QName(namespace, localName); 1.117 + if (field.getType().equals(JAXBElement.class)) { 1.118 + Class elementDeclaredType = Object.class; 1.119 + if (field.getGenericType() instanceof ParameterizedType) { 1.120 + Type arg = ((ParameterizedType) field.getGenericType()) 1.121 + .getActualTypeArguments()[0]; 1.122 + if (arg instanceof Class) { 1.123 + elementDeclaredTypesByQName.put(qname, (Class) arg); 1.124 + elementDeclaredTypesByLocalpart.put(localName, 1.125 + (Class) arg); 1.126 + } else if (arg instanceof GenericArrayType) { 1.127 + Type componentType = ((GenericArrayType) arg) 1.128 + .getGenericComponentType(); 1.129 + if (componentType instanceof Class) { 1.130 + Class arrayClass = Array.newInstance( 1.131 + (Class) componentType, 0).getClass(); 1.132 + elementDeclaredTypesByQName.put(qname, arrayClass); 1.133 + elementDeclaredTypesByLocalpart.put(localName, 1.134 + arrayClass); 1.135 + } 1.136 + } 1.137 + } 1.138 + 1.139 + } 1.140 + // _return 1.141 + if (fieldName.startsWith("_") && !localName.startsWith("_")) { 1.142 + fieldName = fieldName.substring(1); 1.143 + } 1.144 + Method setMethod = publicSetters.get(fieldName); 1.145 + Method getMethod = publicGetters.get(fieldName); 1.146 + PropertySetter setter = createPropertySetter(field, setMethod); 1.147 + PropertyGetter getter = createPropertyGetter(field, getMethod); 1.148 + setByQName.put(qname, setter); 1.149 + setByLocalpart.put(localName, setter); 1.150 + getByQName.put(qname, getter); 1.151 + getByLocalpart.put(localName, getter); 1.152 + } 1.153 + if (this.elementLocalNameCollision) { 1.154 + this.propertySetters = setByQName; 1.155 + this.propertyGetters = getByQName; 1.156 + elementDeclaredTypes = elementDeclaredTypesByQName; 1.157 + } else { 1.158 + this.propertySetters = setByLocalpart; 1.159 + this.propertyGetters = getByLocalpart; 1.160 + elementDeclaredTypes = elementDeclaredTypesByLocalpart; 1.161 + } 1.162 + } 1.163 + 1.164 + static protected List<Field> getAllFields(Class<?> clz) { 1.165 + List<Field> list = new ArrayList<Field>(); 1.166 + while (!Object.class.equals(clz)) { 1.167 + for (Field f : getDeclaredFields(clz)) list.add(f); 1.168 + clz = clz.getSuperclass(); 1.169 + } 1.170 + return list; 1.171 + } 1.172 + 1.173 + static protected Field[] getDeclaredFields(final Class<?> clz) { 1.174 + try { 1.175 + return (System.getSecurityManager() == null) ? clz .getDeclaredFields() : 1.176 + AccessController.doPrivileged(new PrivilegedExceptionAction<Field[]>() { 1.177 + public Field[] run() throws IllegalAccessException { 1.178 + return clz.getDeclaredFields(); 1.179 + } 1.180 + }); 1.181 + } catch (PrivilegedActionException e) { 1.182 + // TODO Auto-generated catch block 1.183 + e.printStackTrace(); 1.184 + return null; 1.185 + } 1.186 + } 1.187 + 1.188 + static protected PropertyGetter createPropertyGetter(Field field, Method getMethod) { 1.189 + if (!field.isAccessible()) { 1.190 + if (getMethod != null) { 1.191 + MethodGetter methodGetter = new MethodGetter(getMethod); 1.192 + if (!methodGetter.getType().toString().equals(field.getType().toString())) { 1.193 + methodGetter = null; 1.194 + } else { 1.195 + return methodGetter; 1.196 + } 1.197 + } 1.198 + } 1.199 + return new FieldGetter(field); 1.200 + } 1.201 + 1.202 + static protected PropertySetter createPropertySetter(Field field, 1.203 + Method setter) { 1.204 + if (!field.isAccessible()) { 1.205 + if (setter != null) { 1.206 + MethodSetter injection = new MethodSetter(setter); 1.207 + if (!injection.getType().toString().equals(field.getType().toString())) { 1.208 + injection = null; 1.209 + } else { 1.210 + return injection; 1.211 + } 1.212 + } 1.213 + } 1.214 + return new FieldSetter(field); 1.215 + } 1.216 + 1.217 + private Class getElementDeclaredType(QName name) { 1.218 + Object key = (this.elementLocalNameCollision) ? name : name 1.219 + .getLocalPart(); 1.220 + return elementDeclaredTypes.get(key); 1.221 + } 1.222 + 1.223 + public PropertyAccessor getPropertyAccessor(String ns, String name) { 1.224 + final QName n = new QName(ns, name); 1.225 + final PropertySetter setter = getPropertySetter(n); 1.226 + final PropertyGetter getter = getPropertyGetter(n); 1.227 + final boolean isJAXBElement = setter.getType() 1.228 + .equals(JAXBElement.class); 1.229 + final boolean isListType = java.util.List.class.isAssignableFrom(setter 1.230 + .getType()); 1.231 + final Class elementDeclaredType = isJAXBElement ? getElementDeclaredType(n) 1.232 + : null; 1.233 + return new PropertyAccessor() { 1.234 + public Object get(Object bean) throws DatabindingException { 1.235 + Object val = null; 1.236 + if (isJAXBElement) { 1.237 + JAXBElement<Object> jaxbElement = (JAXBElement<Object>) getter.get(bean); 1.238 + val = (jaxbElement == null) ? null : jaxbElement.getValue(); 1.239 + } else { 1.240 + val = getter.get(bean); 1.241 + } 1.242 + if (val == null && isListType) { 1.243 + val = new java.util.ArrayList(); 1.244 + set(bean, val); 1.245 + } 1.246 + return val; 1.247 + } 1.248 + 1.249 + public void set(Object bean, Object value) throws DatabindingException { 1.250 + if (isJAXBElement) { 1.251 + JAXBElement<Object> jaxbElement = new JAXBElement<Object>( 1.252 + n, elementDeclaredType, contentClass, value); 1.253 + setter.set(bean, jaxbElement); 1.254 + } else { 1.255 + setter.set(bean, value); 1.256 + } 1.257 + } 1.258 + }; 1.259 + } 1.260 +}