src/share/jaxws_classes/com/sun/xml/internal/ws/spi/db/JAXBWrapperAccessor.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 384
8f2986ff0235
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial