src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/reflect/Accessor.java

Sun, 31 Aug 2014 16:14:36 +0400

author
aefimov
date
Sun, 31 Aug 2014 16:14:36 +0400
changeset 707
31893650acaf
parent 650
121e938cb9c3
child 760
e530533619ec
permissions
-rw-r--r--

8036981: JAXB not preserving formatting for xsd:any Mixed content
Reviewed-by: lancea, mkos

ohair@286 1 /*
aefimov@650 2 * Copyright (c) 1997, 2014, 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.bind.v2.runtime.reflect;
ohair@286 27
ohair@286 28 import java.lang.reflect.Field;
ohair@286 29 import java.lang.reflect.InvocationTargetException;
ohair@286 30 import java.lang.reflect.Method;
ohair@286 31 import java.lang.reflect.Modifier;
ohair@286 32 import java.lang.reflect.Type;
ohair@286 33 import java.util.Arrays;
ohair@286 34 import java.util.HashMap;
ohair@286 35 import java.util.List;
ohair@286 36 import java.util.Map;
ohair@286 37 import java.util.logging.Level;
ohair@286 38 import java.util.logging.Logger;
ohair@286 39
ohair@286 40 import javax.xml.bind.JAXBElement;
ohair@286 41 import javax.xml.bind.annotation.adapters.XmlAdapter;
ohair@286 42
ohair@286 43 import com.sun.istack.internal.Nullable;
ohair@286 44 import com.sun.xml.internal.bind.Util;
ohair@286 45 import com.sun.xml.internal.bind.api.AccessorException;
ohair@286 46 import com.sun.xml.internal.bind.api.JAXBRIContext;
ohair@286 47 import com.sun.xml.internal.bind.v2.model.core.Adapter;
ohair@286 48 import com.sun.xml.internal.bind.v2.model.impl.RuntimeModelBuilder;
ohair@286 49 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
ohair@286 50 import com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory;
ohair@286 51 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
ohair@286 52 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver;
ohair@286 53 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
ohair@286 54
ohair@286 55 import org.xml.sax.SAXException;
ohair@286 56
ohair@286 57 /**
ohair@286 58 * Accesses a particular property of a bean.
ohair@286 59 * <p/>
ohair@286 60 * <p/>
ohair@286 61 * This interface encapsulates the access to the actual data store.
ohair@286 62 * The intention is to generate implementations for a particular bean
ohair@286 63 * and a property to improve the performance.
ohair@286 64 * <p/>
ohair@286 65 * <p/>
ohair@286 66 * Accessor can be used as a receiver. Upon receiving an object
ohair@286 67 * it sets that to the field.
ohair@286 68 *
ohair@286 69 * @author Kohsuke Kawaguchi (kk@kohsuke.org)
ohair@286 70 * @see Accessor.FieldReflection
ohair@286 71 * @see TransducedAccessor
ohair@286 72 */
ohair@286 73 public abstract class Accessor<BeanT, ValueT> implements Receiver {
ohair@286 74
ohair@286 75 public final Class<ValueT> valueType;
ohair@286 76
ohair@286 77 public Class<ValueT> getValueType() {
ohair@286 78 return valueType;
ohair@286 79 }
ohair@286 80
ohair@286 81 protected Accessor(Class<ValueT> valueType) {
ohair@286 82 this.valueType = valueType;
ohair@286 83 }
ohair@286 84
ohair@286 85 /**
ohair@286 86 * Returns the optimized version of the same accessor.
ohair@286 87 *
ohair@286 88 * @param context The {@link JAXBContextImpl} that owns the whole thing.
ohair@286 89 * (See {@link RuntimeModelBuilder#context}.)
ohair@286 90 * @return At least the implementation can return <tt>this</tt>.
ohair@286 91 */
ohair@286 92 public Accessor<BeanT, ValueT> optimize(@Nullable JAXBContextImpl context) {
ohair@286 93 return this;
ohair@286 94 }
ohair@286 95
ohair@286 96
ohair@286 97 /**
ohair@286 98 * Gets the value of the property of the given bean object.
ohair@286 99 *
ohair@286 100 * @param bean must not be null.
ohair@286 101 * @throws AccessorException if failed to set a value. For example, the getter method
ohair@286 102 * may throw an exception.
ohair@286 103 * @since 2.0 EA1
ohair@286 104 */
ohair@286 105 public abstract ValueT get(BeanT bean) throws AccessorException;
ohair@286 106
ohair@286 107 /**
ohair@286 108 * Sets the value of the property of the given bean object.
ohair@286 109 *
ohair@286 110 * @param bean must not be null.
ohair@286 111 * @param value the value to be set. Setting value to null means resetting
ohair@286 112 * to the VM default value (even for primitive properties.)
ohair@286 113 * @throws AccessorException if failed to set a value. For example, the setter method
ohair@286 114 * may throw an exception.
ohair@286 115 * @since 2.0 EA1
ohair@286 116 */
ohair@286 117 public abstract void set(BeanT bean, ValueT value) throws AccessorException;
ohair@286 118
ohair@286 119
ohair@286 120 /**
ohair@286 121 * Sets the value without adapting the value.
ohair@286 122 * <p/>
ohair@286 123 * This ugly entry point is only used by JAX-WS.
ohair@286 124 * See {@link JAXBRIContext#getElementPropertyAccessor}
ohair@286 125 */
ohair@286 126 public Object getUnadapted(BeanT bean) throws AccessorException {
ohair@286 127 return get(bean);
ohair@286 128 }
ohair@286 129
ohair@286 130 /**
ohair@286 131 * Returns true if this accessor wraps an adapter.
ohair@286 132 * <p/>
ohair@286 133 * This method needs to be used with care, but it helps some optimization.
ohair@286 134 */
ohair@286 135 public boolean isAdapted() {
ohair@286 136 return false;
ohair@286 137 }
ohair@286 138
ohair@286 139 /**
ohair@286 140 * Sets the value without adapting the value.
ohair@286 141 * <p/>
ohair@286 142 * This ugly entry point is only used by JAX-WS.
ohair@286 143 * See {@link JAXBRIContext#getElementPropertyAccessor}
ohair@286 144 */
ohair@286 145 public void setUnadapted(BeanT bean, Object value) throws AccessorException {
ohair@286 146 set(bean, (ValueT) value);
ohair@286 147 }
ohair@286 148
ohair@286 149 public void receive(UnmarshallingContext.State state, Object o) throws SAXException {
ohair@286 150 try {
aefimov@650 151 set((BeanT) state.getTarget(), (ValueT) o);
ohair@286 152 } catch (AccessorException e) {
ohair@286 153 Loader.handleGenericException(e, true);
ohair@286 154 } catch (IllegalAccessError iae) {
ohair@286 155 // throw UnmarshalException instead IllegalAccesssError | Issue 475
ohair@286 156 Loader.handleGenericError(iae);
ohair@286 157 }
ohair@286 158 }
ohair@286 159
ohair@286 160 private static List<Class> nonAbstractableClasses = Arrays.asList(new Class[]{
ohair@286 161 Object.class,
ohair@286 162 java.util.Calendar.class,
ohair@286 163 javax.xml.datatype.Duration.class,
ohair@286 164 javax.xml.datatype.XMLGregorianCalendar.class,
ohair@286 165 java.awt.Image.class,
ohair@286 166 javax.activation.DataHandler.class,
ohair@286 167 javax.xml.transform.Source.class,
ohair@286 168 java.util.Date.class,
ohair@286 169 java.io.File.class,
ohair@286 170 java.net.URI.class,
ohair@286 171 java.net.URL.class,
ohair@286 172 Class.class,
ohair@286 173 String.class,
ohair@286 174 javax.xml.transform.Source.class}
ohair@286 175 );
ohair@286 176
ohair@286 177 public boolean isValueTypeAbstractable() {
ohair@286 178 return !nonAbstractableClasses.contains(getValueType());
ohair@286 179 }
ohair@286 180
ohair@286 181 /**
alanb@368 182 * Checks if it is not builtin jaxb class
alanb@368 183 * @param clazz to be checked
alanb@368 184 * @return true if it is NOT builtin class
alanb@368 185 */
alanb@368 186 public boolean isAbstractable(Class clazz) {
alanb@368 187 return !nonAbstractableClasses.contains(clazz);
alanb@368 188 }
alanb@368 189
alanb@368 190 /**
ohair@286 191 * Wraps this {@link Accessor} into another {@link Accessor}
ohair@286 192 * and performs the type adaption as necessary.
ohair@286 193 */
ohair@286 194 public final <T> Accessor<BeanT, T> adapt(Class<T> targetType, final Class<? extends XmlAdapter<T, ValueT>> adapter) {
ohair@286 195 return new AdaptedAccessor<BeanT, ValueT, T>(targetType, this, adapter);
ohair@286 196 }
ohair@286 197
ohair@286 198 public final <T> Accessor<BeanT, T> adapt(Adapter<Type, Class> adapter) {
ohair@286 199 return new AdaptedAccessor<BeanT, ValueT, T>(
mkos@450 200 (Class<T>) Utils.REFLECTION_NAVIGATOR.erasure(adapter.defaultType),
ohair@286 201 this,
ohair@286 202 adapter.adapterType);
ohair@286 203 }
ohair@286 204
ohair@286 205 /**
ohair@286 206 * Flag that will be set to true after issueing a warning
ohair@286 207 * about the lack of permission to access non-public fields.
ohair@286 208 */
ohair@286 209 private static boolean accessWarned = false;
ohair@286 210
ohair@286 211
ohair@286 212 /**
ohair@286 213 * {@link Accessor} that uses Java reflection to access a field.
ohair@286 214 */
ohair@286 215 public static class FieldReflection<BeanT, ValueT> extends Accessor<BeanT, ValueT> {
ohair@286 216 public final Field f;
ohair@286 217
ohair@286 218 private static final Logger logger = Util.getClassLogger();
ohair@286 219
ohair@286 220 public FieldReflection(Field f) {
ohair@286 221 this(f, false);
ohair@286 222 }
ohair@286 223
ohair@286 224 public FieldReflection(Field f, boolean supressAccessorWarnings) {
ohair@286 225 super((Class<ValueT>) f.getType());
ohair@286 226 this.f = f;
ohair@286 227
ohair@286 228 int mod = f.getModifiers();
ohair@286 229 if (!Modifier.isPublic(mod) || Modifier.isFinal(mod) || !Modifier.isPublic(f.getDeclaringClass().getModifiers())) {
ohair@286 230 try {
ohair@286 231 // attempt to make it accessible, but do so in the security context of the calling application.
ohair@286 232 // don't do this in the doPrivilege block, as that would create a security hole for anyone
ohair@286 233 // to make any field accessible.
ohair@286 234 f.setAccessible(true);
ohair@286 235 } catch (SecurityException e) {
ohair@286 236 if ((!accessWarned) && (!supressAccessorWarnings)) {
ohair@286 237 // this happens when we don't have enough permission.
ohair@286 238 logger.log(Level.WARNING, Messages.UNABLE_TO_ACCESS_NON_PUBLIC_FIELD.format(
ohair@286 239 f.getDeclaringClass().getName(),
ohair@286 240 f.getName()),
ohair@286 241 e);
ohair@286 242 }
ohair@286 243 accessWarned = true;
ohair@286 244 }
ohair@286 245 }
ohair@286 246 }
ohair@286 247
ohair@286 248 public ValueT get(BeanT bean) {
ohair@286 249 try {
ohair@286 250 return (ValueT) f.get(bean);
ohair@286 251 } catch (IllegalAccessException e) {
ohair@286 252 throw new IllegalAccessError(e.getMessage());
ohair@286 253 }
ohair@286 254 }
ohair@286 255
ohair@286 256 public void set(BeanT bean, ValueT value) {
ohair@286 257 try {
ohair@286 258 if (value == null)
ohair@286 259 value = (ValueT) uninitializedValues.get(valueType);
ohair@286 260 f.set(bean, value);
ohair@286 261 } catch (IllegalAccessException e) {
ohair@286 262 throw new IllegalAccessError(e.getMessage());
ohair@286 263 }
ohair@286 264 }
ohair@286 265
ohair@286 266 @Override
ohair@286 267 public Accessor<BeanT, ValueT> optimize(JAXBContextImpl context) {
ohair@286 268 if (context != null && context.fastBoot)
ohair@286 269 // let's not waste time on doing this for the sake of faster boot.
ohair@286 270 return this;
ohair@286 271 Accessor<BeanT, ValueT> acc = OptimizedAccessorFactory.get(f);
ohair@286 272 if (acc != null)
ohair@286 273 return acc;
ohair@286 274 else
ohair@286 275 return this;
ohair@286 276 }
ohair@286 277 }
ohair@286 278
ohair@286 279 /**
ohair@286 280 * Read-only access to {@link Field}. Used to handle a static field.
ohair@286 281 */
ohair@286 282 public static final class ReadOnlyFieldReflection<BeanT, ValueT> extends FieldReflection<BeanT, ValueT> {
ohair@286 283 public ReadOnlyFieldReflection(Field f, boolean supressAccessorWarnings) {
ohair@286 284 super(f, supressAccessorWarnings);
ohair@286 285 }
ohair@286 286 public ReadOnlyFieldReflection(Field f) {
ohair@286 287 super(f);
ohair@286 288 }
ohair@286 289
ohair@286 290 @Override
ohair@286 291 public void set(BeanT bean, ValueT value) {
ohair@286 292 // noop
ohair@286 293 }
ohair@286 294
ohair@286 295 @Override
ohair@286 296 public Accessor<BeanT, ValueT> optimize(JAXBContextImpl context) {
ohair@286 297 return this;
ohair@286 298 }
ohair@286 299 }
ohair@286 300
ohair@286 301
ohair@286 302 /**
ohair@286 303 * {@link Accessor} that uses Java reflection to access a getter and a setter.
ohair@286 304 */
ohair@286 305 public static class GetterSetterReflection<BeanT, ValueT> extends Accessor<BeanT, ValueT> {
ohair@286 306 public final Method getter;
ohair@286 307 public final Method setter;
ohair@286 308
ohair@286 309 private static final Logger logger = Util.getClassLogger();
ohair@286 310
ohair@286 311 public GetterSetterReflection(Method getter, Method setter) {
ohair@286 312 super(
ohair@286 313 (Class<ValueT>) (getter != null ?
ohair@286 314 getter.getReturnType() :
ohair@286 315 setter.getParameterTypes()[0]));
ohair@286 316 this.getter = getter;
ohair@286 317 this.setter = setter;
ohair@286 318
ohair@286 319 if (getter != null)
ohair@286 320 makeAccessible(getter);
ohair@286 321 if (setter != null)
ohair@286 322 makeAccessible(setter);
ohair@286 323 }
ohair@286 324
ohair@286 325 private void makeAccessible(Method m) {
ohair@286 326 if (!Modifier.isPublic(m.getModifiers()) || !Modifier.isPublic(m.getDeclaringClass().getModifiers())) {
ohair@286 327 try {
ohair@286 328 m.setAccessible(true);
ohair@286 329 } catch (SecurityException e) {
ohair@286 330 if (!accessWarned)
ohair@286 331 // this happens when we don't have enough permission.
ohair@286 332 logger.log(Level.WARNING, Messages.UNABLE_TO_ACCESS_NON_PUBLIC_FIELD.format(
ohair@286 333 m.getDeclaringClass().getName(),
ohair@286 334 m.getName()),
ohair@286 335 e);
ohair@286 336 accessWarned = true;
ohair@286 337 }
ohair@286 338 }
ohair@286 339 }
ohair@286 340
ohair@286 341 public ValueT get(BeanT bean) throws AccessorException {
ohair@286 342 try {
ohair@286 343 return (ValueT) getter.invoke(bean);
ohair@286 344 } catch (IllegalAccessException e) {
ohair@286 345 throw new IllegalAccessError(e.getMessage());
ohair@286 346 } catch (InvocationTargetException e) {
ohair@286 347 throw handleInvocationTargetException(e);
ohair@286 348 }
ohair@286 349 }
ohair@286 350
ohair@286 351 public void set(BeanT bean, ValueT value) throws AccessorException {
ohair@286 352 try {
ohair@286 353 if (value == null)
ohair@286 354 value = (ValueT) uninitializedValues.get(valueType);
ohair@286 355 setter.invoke(bean, value);
ohair@286 356 } catch (IllegalAccessException e) {
ohair@286 357 throw new IllegalAccessError(e.getMessage());
ohair@286 358 } catch (InvocationTargetException e) {
ohair@286 359 throw handleInvocationTargetException(e);
ohair@286 360 }
ohair@286 361 }
ohair@286 362
ohair@286 363 private AccessorException handleInvocationTargetException(InvocationTargetException e) {
ohair@286 364 // don't block a problem in the user code
ohair@286 365 Throwable t = e.getTargetException();
ohair@286 366 if (t instanceof RuntimeException)
ohair@286 367 throw (RuntimeException) t;
ohair@286 368 if (t instanceof Error)
ohair@286 369 throw (Error) t;
ohair@286 370
ohair@286 371 // otherwise it's a checked exception.
ohair@286 372 // I'm not sure how to handle this.
ohair@286 373 // we can throw a checked exception from here,
ohair@286 374 // but because get/set would be called from so many different places,
ohair@286 375 // the handling would be tedious.
ohair@286 376 return new AccessorException(t);
ohair@286 377 }
ohair@286 378
ohair@286 379 @Override
ohair@286 380 public Accessor<BeanT, ValueT> optimize(JAXBContextImpl context) {
ohair@286 381 if (getter == null || setter == null)
ohair@286 382 // if we aren't complete, OptimizedAccessor won't always work
ohair@286 383 return this;
ohair@286 384 if (context != null && context.fastBoot)
ohair@286 385 // let's not waste time on doing this for the sake of faster boot.
ohair@286 386 return this;
ohair@286 387
ohair@286 388 Accessor<BeanT, ValueT> acc = OptimizedAccessorFactory.get(getter, setter);
ohair@286 389 if (acc != null)
ohair@286 390 return acc;
ohair@286 391 else
ohair@286 392 return this;
ohair@286 393 }
ohair@286 394 }
ohair@286 395
ohair@286 396 /**
ohair@286 397 * A version of {@link GetterSetterReflection} that doesn't have any setter.
ohair@286 398 * <p/>
ohair@286 399 * <p/>
ohair@286 400 * This provides a user-friendly error message.
ohair@286 401 */
ohair@286 402 public static class GetterOnlyReflection<BeanT, ValueT> extends GetterSetterReflection<BeanT, ValueT> {
ohair@286 403 public GetterOnlyReflection(Method getter) {
ohair@286 404 super(getter, null);
ohair@286 405 }
ohair@286 406
ohair@286 407 @Override
ohair@286 408 public void set(BeanT bean, ValueT value) throws AccessorException {
ohair@286 409 throw new AccessorException(Messages.NO_SETTER.format(getter.toString()));
ohair@286 410 }
ohair@286 411 }
ohair@286 412
ohair@286 413 /**
ohair@286 414 * A version of {@link GetterSetterReflection} thaat doesn't have any getter.
ohair@286 415 * <p/>
ohair@286 416 * <p/>
ohair@286 417 * This provides a user-friendly error message.
ohair@286 418 */
ohair@286 419 public static class SetterOnlyReflection<BeanT, ValueT> extends GetterSetterReflection<BeanT, ValueT> {
ohair@286 420 public SetterOnlyReflection(Method setter) {
ohair@286 421 super(null, setter);
ohair@286 422 }
ohair@286 423
ohair@286 424 @Override
ohair@286 425 public ValueT get(BeanT bean) throws AccessorException {
ohair@286 426 throw new AccessorException(Messages.NO_GETTER.format(setter.toString()));
ohair@286 427 }
ohair@286 428 }
ohair@286 429
ohair@286 430 /**
ohair@286 431 * Gets the special {@link Accessor} used to recover from errors.
ohair@286 432 */
ohair@286 433 @SuppressWarnings("unchecked")
ohair@286 434 public static <A, B> Accessor<A, B> getErrorInstance() {
ohair@286 435 return ERROR;
ohair@286 436 }
ohair@286 437
ohair@286 438 private static final Accessor ERROR = new Accessor<Object, Object>(Object.class) {
ohair@286 439 public Object get(Object o) {
ohair@286 440 return null;
ohair@286 441 }
ohair@286 442
ohair@286 443 public void set(Object o, Object o1) {
ohair@286 444 }
ohair@286 445 };
ohair@286 446
ohair@286 447 /**
ohair@286 448 * {@link Accessor} for {@link JAXBElement#getValue()}.
ohair@286 449 */
ohair@286 450 public static final Accessor<JAXBElement, Object> JAXB_ELEMENT_VALUE = new Accessor<JAXBElement, Object>(Object.class) {
ohair@286 451 public Object get(JAXBElement jaxbElement) {
ohair@286 452 return jaxbElement.getValue();
ohair@286 453 }
ohair@286 454
ohair@286 455 public void set(JAXBElement jaxbElement, Object o) {
ohair@286 456 jaxbElement.setValue(o);
ohair@286 457 }
ohair@286 458 };
ohair@286 459
ohair@286 460 /**
ohair@286 461 * Uninitialized map keyed by their classes.
ohair@286 462 */
ohair@286 463 private static final Map<Class, Object> uninitializedValues = new HashMap<Class, Object>();
ohair@286 464
ohair@286 465 static {
ohair@286 466 /*
ohair@286 467 static byte default_value_byte = 0;
ohair@286 468 static boolean default_value_boolean = false;
ohair@286 469 static char default_value_char = 0;
ohair@286 470 static float default_value_float = 0;
ohair@286 471 static double default_value_double = 0;
ohair@286 472 static int default_value_int = 0;
ohair@286 473 static long default_value_long = 0;
ohair@286 474 static short default_value_short = 0;
ohair@286 475 */
ohair@286 476 uninitializedValues.put(byte.class, Byte.valueOf((byte) 0));
ohair@286 477 uninitializedValues.put(boolean.class, false);
ohair@286 478 uninitializedValues.put(char.class, Character.valueOf((char) 0));
ohair@286 479 uninitializedValues.put(float.class, Float.valueOf(0));
ohair@286 480 uninitializedValues.put(double.class, Double.valueOf(0));
ohair@286 481 uninitializedValues.put(int.class, Integer.valueOf(0));
ohair@286 482 uninitializedValues.put(long.class, Long.valueOf(0));
ohair@286 483 uninitializedValues.put(short.class, Short.valueOf((short) 0));
ohair@286 484 }
ohair@286 485
ohair@286 486 }

mercurial