1 /* |
1 /* |
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
33 import java.lang.reflect.Type; |
33 import java.lang.reflect.Type; |
34 import java.security.AccessController; |
34 import java.security.AccessController; |
35 import java.security.PrivilegedActionException; |
35 import java.security.PrivilegedActionException; |
36 import java.security.PrivilegedExceptionAction; |
36 import java.security.PrivilegedExceptionAction; |
37 import java.util.ArrayList; |
37 import java.util.ArrayList; |
|
38 import java.util.Arrays; |
38 import java.util.HashMap; |
39 import java.util.HashMap; |
39 import java.util.HashSet; |
40 import java.util.HashSet; |
40 import java.util.List; |
41 import java.util.List; |
41 |
42 |
42 import javax.xml.bind.JAXBElement; |
43 import javax.xml.bind.JAXBElement; |
110 elementLocalNames.add(localName); |
111 elementLocalNames.add(localName); |
111 } |
112 } |
112 |
113 |
113 QName qname = new QName(namespace, localName); |
114 QName qname = new QName(namespace, localName); |
114 if (field.getType().equals(JAXBElement.class)) { |
115 if (field.getType().equals(JAXBElement.class)) { |
115 Class elementDeclaredType = Object.class; |
|
116 if (field.getGenericType() instanceof ParameterizedType) { |
116 if (field.getGenericType() instanceof ParameterizedType) { |
117 Type arg = ((ParameterizedType) field.getGenericType()) |
117 Type arg = ((ParameterizedType) field.getGenericType()) |
118 .getActualTypeArguments()[0]; |
118 .getActualTypeArguments()[0]; |
119 if (arg instanceof Class) { |
119 if (arg instanceof Class) { |
120 elementDeclaredTypesByQName.put(qname, (Class) arg); |
120 elementDeclaredTypesByQName.put(qname, (Class) arg); |
159 } |
159 } |
160 |
160 |
161 static protected List<Field> getAllFields(Class<?> clz) { |
161 static protected List<Field> getAllFields(Class<?> clz) { |
162 List<Field> list = new ArrayList<Field>(); |
162 List<Field> list = new ArrayList<Field>(); |
163 while (!Object.class.equals(clz)) { |
163 while (!Object.class.equals(clz)) { |
164 for (Field f : getDeclaredFields(clz)) list.add(f); |
164 list.addAll(Arrays.asList(getDeclaredFields(clz))); |
165 clz = clz.getSuperclass(); |
165 clz = clz.getSuperclass(); |
166 } |
166 } |
167 return list; |
167 return list; |
168 } |
168 } |
169 |
169 |
170 static protected Field[] getDeclaredFields(final Class<?> clz) { |
170 static protected Field[] getDeclaredFields(final Class<?> clz) { |
171 try { |
171 try { |
172 return (System.getSecurityManager() == null) ? clz .getDeclaredFields() : |
172 return (System.getSecurityManager() == null) ? clz .getDeclaredFields() : |
173 AccessController.doPrivileged(new PrivilegedExceptionAction<Field[]>() { |
173 AccessController.doPrivileged(new PrivilegedExceptionAction<Field[]>() { |
|
174 @Override |
174 public Field[] run() throws IllegalAccessException { |
175 public Field[] run() throws IllegalAccessException { |
175 return clz.getDeclaredFields(); |
176 return clz.getDeclaredFields(); |
176 } |
177 } |
177 }); |
178 }); |
178 } catch (PrivilegedActionException e) { |
179 } catch (PrivilegedActionException e) { |
184 |
185 |
185 static protected PropertyGetter createPropertyGetter(Field field, Method getMethod) { |
186 static protected PropertyGetter createPropertyGetter(Field field, Method getMethod) { |
186 if (!field.isAccessible()) { |
187 if (!field.isAccessible()) { |
187 if (getMethod != null) { |
188 if (getMethod != null) { |
188 MethodGetter methodGetter = new MethodGetter(getMethod); |
189 MethodGetter methodGetter = new MethodGetter(getMethod); |
189 if (!methodGetter.getType().toString().equals(field.getType().toString())) { |
190 if (methodGetter.getType().toString().equals(field.getType().toString())) { |
190 methodGetter = null; |
|
191 } else { |
|
192 return methodGetter; |
191 return methodGetter; |
193 } |
192 } |
194 } |
193 } |
195 } |
194 } |
196 return new FieldGetter(field); |
195 return new FieldGetter(field); |
199 static protected PropertySetter createPropertySetter(Field field, |
198 static protected PropertySetter createPropertySetter(Field field, |
200 Method setter) { |
199 Method setter) { |
201 if (!field.isAccessible()) { |
200 if (!field.isAccessible()) { |
202 if (setter != null) { |
201 if (setter != null) { |
203 MethodSetter injection = new MethodSetter(setter); |
202 MethodSetter injection = new MethodSetter(setter); |
204 if (!injection.getType().toString().equals(field.getType().toString())) { |
203 if (injection.getType().toString().equals(field.getType().toString())) { |
205 injection = null; |
|
206 } else { |
|
207 return injection; |
204 return injection; |
208 } |
205 } |
209 } |
206 } |
210 } |
207 } |
211 return new FieldSetter(field); |
208 return new FieldSetter(field); |
215 Object key = (this.elementLocalNameCollision) ? name : name |
212 Object key = (this.elementLocalNameCollision) ? name : name |
216 .getLocalPart(); |
213 .getLocalPart(); |
217 return elementDeclaredTypes.get(key); |
214 return elementDeclaredTypes.get(key); |
218 } |
215 } |
219 |
216 |
|
217 @Override |
220 public PropertyAccessor getPropertyAccessor(String ns, String name) { |
218 public PropertyAccessor getPropertyAccessor(String ns, String name) { |
221 final QName n = new QName(ns, name); |
219 final QName n = new QName(ns, name); |
222 final PropertySetter setter = getPropertySetter(n); |
220 final PropertySetter setter = getPropertySetter(n); |
223 final PropertyGetter getter = getPropertyGetter(n); |
221 final PropertyGetter getter = getPropertyGetter(n); |
224 final boolean isJAXBElement = setter.getType() |
222 final boolean isJAXBElement = setter.getType() |
226 final boolean isListType = java.util.List.class.isAssignableFrom(setter |
224 final boolean isListType = java.util.List.class.isAssignableFrom(setter |
227 .getType()); |
225 .getType()); |
228 final Class elementDeclaredType = isJAXBElement ? getElementDeclaredType(n) |
226 final Class elementDeclaredType = isJAXBElement ? getElementDeclaredType(n) |
229 : null; |
227 : null; |
230 return new PropertyAccessor() { |
228 return new PropertyAccessor() { |
|
229 @Override |
231 public Object get(Object bean) throws DatabindingException { |
230 public Object get(Object bean) throws DatabindingException { |
232 Object val = null; |
231 Object val; |
233 if (isJAXBElement) { |
232 if (isJAXBElement) { |
234 JAXBElement<Object> jaxbElement = (JAXBElement<Object>) getter.get(bean); |
233 JAXBElement<Object> jaxbElement = (JAXBElement<Object>) getter.get(bean); |
235 val = (jaxbElement == null) ? null : jaxbElement.getValue(); |
234 val = (jaxbElement == null) ? null : jaxbElement.getValue(); |
236 } else { |
235 } else { |
237 val = getter.get(bean); |
236 val = getter.get(bean); |
241 set(bean, val); |
240 set(bean, val); |
242 } |
241 } |
243 return val; |
242 return val; |
244 } |
243 } |
245 |
244 |
|
245 @Override |
246 public void set(Object bean, Object value) throws DatabindingException { |
246 public void set(Object bean, Object value) throws DatabindingException { |
247 if (isJAXBElement) { |
247 if (isJAXBElement) { |
248 JAXBElement<Object> jaxbElement = new JAXBElement<Object>( |
248 JAXBElement<Object> jaxbElement = new JAXBElement<Object>( |
249 n, elementDeclaredType, contentClass, value); |
249 n, elementDeclaredType, contentClass, value); |
250 setter.set(bean, jaxbElement); |
250 setter.set(bean, jaxbElement); |