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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2011, 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;
ohair@286 38 import java.util.HashMap;
ohair@286 39 import java.util.HashSet;
ohair@286 40 import java.util.List;
ohair@286 41
ohair@286 42 import javax.xml.bind.JAXBElement;
ohair@286 43 import javax.xml.bind.annotation.XmlElement;
ohair@286 44 import javax.xml.bind.annotation.XmlElementRef;
ohair@286 45 import javax.xml.namespace.QName;
ohair@286 46
ohair@286 47 /**
ohair@286 48 * JAXBWrapperAccessor
ohair@286 49 *
ohair@286 50 * @author shih-chang.chen@oracle.com
ohair@286 51 */
ohair@286 52 @SuppressWarnings({ "unchecked", "rawtypes" })
ohair@286 53 public class JAXBWrapperAccessor extends WrapperAccessor {
ohair@286 54
ohair@286 55 protected Class<?> contentClass;
ohair@286 56 protected HashMap<Object, Class> elementDeclaredTypes;
ohair@286 57
ohair@286 58 public JAXBWrapperAccessor(Class<?> wrapperBean) {
ohair@286 59 contentClass = (Class<?>) wrapperBean;
ohair@286 60
ohair@286 61 HashMap<Object, PropertySetter> setByQName = new HashMap<Object, PropertySetter>();
ohair@286 62 HashMap<Object, PropertySetter> setByLocalpart = new HashMap<Object, PropertySetter>();
ohair@286 63 HashMap<String, Method> publicSetters = new HashMap<String, Method>();
ohair@286 64
ohair@286 65 HashMap<Object, PropertyGetter> getByQName = new HashMap<Object, PropertyGetter>();
ohair@286 66 HashMap<Object, PropertyGetter> getByLocalpart = new HashMap<Object, PropertyGetter>();
ohair@286 67 HashMap<String, Method> publicGetters = new HashMap<String, Method>();
ohair@286 68
ohair@286 69 HashMap<Object, Class> elementDeclaredTypesByQName = new HashMap<Object, Class>();
ohair@286 70 HashMap<Object, Class> elementDeclaredTypesByLocalpart = new HashMap<Object, Class>();
ohair@286 71
ohair@286 72 for (Method method : contentClass.getMethods()) {
ohair@286 73 if (PropertySetterBase.setterPattern(method)) {
ohair@286 74 String key = method.getName()
ohair@286 75 .substring(3, method.getName().length()).toLowerCase();
ohair@286 76 publicSetters.put(key, method);
ohair@286 77 }
ohair@286 78 if (PropertyGetterBase.getterPattern(method)) {
ohair@286 79 String methodName = method.getName();
ohair@286 80 String key = methodName.startsWith("is") ? methodName
ohair@286 81 .substring(2, method.getName().length()).toLowerCase()
ohair@286 82 : methodName.substring(3, method.getName().length())
ohair@286 83 .toLowerCase();
ohair@286 84 publicGetters.put(key, method);
ohair@286 85 }
ohair@286 86 }
ohair@286 87 HashSet<String> elementLocalNames = new HashSet<String>();
ohair@286 88 for (Field field : getAllFields(contentClass)) {
ohair@286 89 XmlElement xmlElem = field.getAnnotation(XmlElement.class);
ohair@286 90 XmlElementRef xmlElemRef = field.getAnnotation(XmlElementRef.class);
ohair@286 91 String fieldName = field.getName().toLowerCase();
ohair@286 92 String namespace = "";
ohair@286 93 String localName = field.getName();
ohair@286 94 if (xmlElem != null) {
ohair@286 95 namespace = xmlElem.namespace();
ohair@286 96 if (xmlElem.name() != null && !xmlElem.name().equals("")
ohair@286 97 && !xmlElem.name().equals("##default")) {
ohair@286 98 localName = xmlElem.name();
ohair@286 99 }
ohair@286 100 } else if (xmlElemRef != null) {
ohair@286 101 namespace = xmlElemRef.namespace();
ohair@286 102 if (xmlElemRef.name() != null && !xmlElemRef.name().equals("")
ohair@286 103 && !xmlElemRef.name().equals("##default")) {
ohair@286 104 localName = xmlElemRef.name();
ohair@286 105 }
ohair@286 106 }
ohair@286 107 if (elementLocalNames.contains(localName)) {
ohair@286 108 this.elementLocalNameCollision = true;
ohair@286 109 } else {
ohair@286 110 elementLocalNames.add(localName);
ohair@286 111 }
ohair@286 112
ohair@286 113 QName qname = new QName(namespace, localName);
ohair@286 114 if (field.getType().equals(JAXBElement.class)) {
ohair@286 115 Class elementDeclaredType = Object.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)) {
ohair@286 164 for (Field f : getDeclaredFields(clz)) list.add(f);
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[]>() {
ohair@286 174 public Field[] run() throws IllegalAccessException {
ohair@286 175 return clz.getDeclaredFields();
ohair@286 176 }
ohair@286 177 });
ohair@286 178 } catch (PrivilegedActionException e) {
ohair@286 179 // TODO Auto-generated catch block
ohair@286 180 e.printStackTrace();
ohair@286 181 return null;
ohair@286 182 }
ohair@286 183 }
ohair@286 184
ohair@286 185 static protected PropertyGetter createPropertyGetter(Field field, Method getMethod) {
ohair@286 186 if (!field.isAccessible()) {
ohair@286 187 if (getMethod != null) {
ohair@286 188 MethodGetter methodGetter = new MethodGetter(getMethod);
ohair@286 189 if (!methodGetter.getType().toString().equals(field.getType().toString())) {
ohair@286 190 methodGetter = null;
ohair@286 191 } else {
ohair@286 192 return methodGetter;
ohair@286 193 }
ohair@286 194 }
ohair@286 195 }
ohair@286 196 return new FieldGetter(field);
ohair@286 197 }
ohair@286 198
ohair@286 199 static protected PropertySetter createPropertySetter(Field field,
ohair@286 200 Method setter) {
ohair@286 201 if (!field.isAccessible()) {
ohair@286 202 if (setter != null) {
ohair@286 203 MethodSetter injection = new MethodSetter(setter);
ohair@286 204 if (!injection.getType().toString().equals(field.getType().toString())) {
ohair@286 205 injection = null;
ohair@286 206 } else {
ohair@286 207 return injection;
ohair@286 208 }
ohair@286 209 }
ohair@286 210 }
ohair@286 211 return new FieldSetter(field);
ohair@286 212 }
ohair@286 213
ohair@286 214 private Class getElementDeclaredType(QName name) {
ohair@286 215 Object key = (this.elementLocalNameCollision) ? name : name
ohair@286 216 .getLocalPart();
ohair@286 217 return elementDeclaredTypes.get(key);
ohair@286 218 }
ohair@286 219
ohair@286 220 public PropertyAccessor getPropertyAccessor(String ns, String name) {
ohair@286 221 final QName n = new QName(ns, name);
ohair@286 222 final PropertySetter setter = getPropertySetter(n);
ohair@286 223 final PropertyGetter getter = getPropertyGetter(n);
ohair@286 224 final boolean isJAXBElement = setter.getType()
ohair@286 225 .equals(JAXBElement.class);
ohair@286 226 final boolean isListType = java.util.List.class.isAssignableFrom(setter
ohair@286 227 .getType());
ohair@286 228 final Class elementDeclaredType = isJAXBElement ? getElementDeclaredType(n)
ohair@286 229 : null;
ohair@286 230 return new PropertyAccessor() {
ohair@286 231 public Object get(Object bean) throws DatabindingException {
ohair@286 232 Object val = null;
ohair@286 233 if (isJAXBElement) {
ohair@286 234 JAXBElement<Object> jaxbElement = (JAXBElement<Object>) getter.get(bean);
ohair@286 235 val = (jaxbElement == null) ? null : jaxbElement.getValue();
ohair@286 236 } else {
ohair@286 237 val = getter.get(bean);
ohair@286 238 }
ohair@286 239 if (val == null && isListType) {
ohair@286 240 val = new java.util.ArrayList();
ohair@286 241 set(bean, val);
ohair@286 242 }
ohair@286 243 return val;
ohair@286 244 }
ohair@286 245
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