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

mercurial