src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/reflect/Lister.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 /*
alanb@368 2 * Copyright (c) 1997, 2013, 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.ref.WeakReference;
ohair@286 29 import java.lang.reflect.Array;
ohair@286 30 import java.lang.reflect.ParameterizedType;
ohair@286 31 import java.lang.reflect.Type;
ohair@286 32 import java.util.ArrayList;
ohair@286 33 import java.util.Collection;
ohair@286 34 import java.util.Collections;
ohair@286 35 import java.util.HashMap;
ohair@286 36 import java.util.Iterator;
ohair@286 37 import java.util.List;
ohair@286 38 import java.util.Map;
ohair@286 39 import java.util.WeakHashMap;
ohair@286 40 import java.util.LinkedList;
ohair@286 41 import java.util.HashSet;
ohair@286 42 import java.util.TreeSet;
ohair@286 43 import java.util.Stack;
ohair@286 44 import java.util.concurrent.Callable;
ohair@286 45
ohair@286 46 import javax.xml.bind.JAXBException;
ohair@286 47
ohair@286 48 import com.sun.istack.internal.SAXException2;
ohair@286 49 import com.sun.xml.internal.bind.api.AccessorException;
ohair@286 50 import com.sun.xml.internal.bind.v2.ClassFactory;
ohair@286 51 import com.sun.xml.internal.bind.v2.TODO;
ohair@286 52 import com.sun.xml.internal.bind.v2.model.core.Adapter;
ohair@286 53 import com.sun.xml.internal.bind.v2.model.core.ID;
ohair@286 54 import com.sun.xml.internal.bind.v2.model.nav.Navigator;
ohair@286 55 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
ohair@286 56 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Patcher;
ohair@286 57 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
ohair@286 58 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LocatorEx;
ohair@286 59
ohair@286 60 import org.xml.sax.SAXException;
ohair@286 61
ohair@286 62 /**
ohair@286 63 * Used to list individual values of a multi-value property, and
ohair@286 64 * to pack individual values into a multi-value property.
ohair@286 65 *
ohair@286 66 * @author Kohsuke Kawaguchi (kk@kohsuke.org)
ohair@286 67 */
ohair@286 68 public abstract class Lister<BeanT,PropT,ItemT,PackT> {
ohair@286 69
ohair@286 70 protected Lister() {}
ohair@286 71
ohair@286 72 /**
ohair@286 73 * Iterates values of a multi-value property.
ohair@286 74 *
ohair@286 75 * @param context
ohair@286 76 * This parameter is used to support ID/IDREF handling.
ohair@286 77 */
ohair@286 78 public abstract ListIterator<ItemT> iterator(PropT multiValueProp, XMLSerializer context);
ohair@286 79
ohair@286 80 /**
ohair@286 81 * Setting values to a multi-value property starts by creating
ohair@286 82 * a transient object called "pack" from the current field.
ohair@286 83 */
ohair@286 84 public abstract PackT startPacking(BeanT bean, Accessor<BeanT, PropT> acc) throws AccessorException;
ohair@286 85
ohair@286 86 /**
ohair@286 87 * Once the {@link #startPacking} is called, you can
ohair@286 88 * add values to the pack by using this method.
ohair@286 89 */
ohair@286 90 public abstract void addToPack( PackT pack, ItemT newValue ) throws AccessorException;
ohair@286 91
ohair@286 92 /**
ohair@286 93 * Finally, call this method to
ohair@286 94 * wraps up the {@code pack}. This method may update the field of
ohair@286 95 * the given bean.
ohair@286 96 */
ohair@286 97 public abstract void endPacking( PackT pack, BeanT bean, Accessor<BeanT,PropT> acc ) throws AccessorException;
ohair@286 98
ohair@286 99 /**
ohair@286 100 * Clears the values of the property.
ohair@286 101 */
ohair@286 102 public abstract void reset(BeanT o,Accessor<BeanT,PropT> acc) throws AccessorException;
ohair@286 103
ohair@286 104
ohair@286 105 /**
ohair@286 106 * Gets a reference to the appropriate {@link Lister} object
ohair@286 107 * if the field is a multi-value field. Otherwise null.
ohair@286 108 *
ohair@286 109 * @param fieldType
ohair@286 110 * the type of the field that stores the collection
ohair@286 111 * @param idness
ohair@286 112 * ID-ness of the property.
ohair@286 113 * @param adapter
ohair@286 114 * adapter to be used for individual items. can be null.
ohair@286 115 */
ohair@286 116 public static <BeanT,PropT,ItemT,PackT>
ohair@286 117 Lister<BeanT,PropT,ItemT,PackT> create(Type fieldType,ID idness, Adapter<Type,Class> adapter) {
ohair@286 118
ohair@286 119 Class rawType = Navigator.REFLECTION.erasure(fieldType);
ohair@286 120 Class itemType;
ohair@286 121
ohair@286 122 Lister l;
ohair@286 123 if( rawType.isArray() ) {
ohair@286 124 itemType = rawType.getComponentType();
ohair@286 125 l = getArrayLister(itemType);
ohair@286 126 } else
ohair@286 127 if( Collection.class.isAssignableFrom(rawType) ) {
ohair@286 128 Type bt = Navigator.REFLECTION.getBaseClass(fieldType,Collection.class);
ohair@286 129 if(bt instanceof ParameterizedType)
ohair@286 130 itemType = Navigator.REFLECTION.erasure(((ParameterizedType)bt).getActualTypeArguments()[0]);
ohair@286 131 else
ohair@286 132 itemType = Object.class;
ohair@286 133 l = new CollectionLister(getImplClass(rawType));
ohair@286 134 } else
ohair@286 135 return null;
ohair@286 136
ohair@286 137 if(idness==ID.IDREF)
ohair@286 138 l = new IDREFS(l,itemType);
ohair@286 139
ohair@286 140 if(adapter!=null)
ohair@286 141 l = new AdaptedLister(l,adapter.adapterType);
ohair@286 142
ohair@286 143 return l;
ohair@286 144 }
ohair@286 145
ohair@286 146 private static Class getImplClass(Class<?> fieldType) {
ohair@286 147 return ClassFactory.inferImplClass(fieldType,COLLECTION_IMPL_CLASSES);
ohair@286 148 }
ohair@286 149
ohair@286 150 /**
ohair@286 151 * Cache instances of {@link ArrayLister}s.
ohair@286 152 */
ohair@286 153 private static final Map<Class,WeakReference<Lister>> arrayListerCache =
ohair@286 154 Collections.synchronizedMap(new WeakHashMap<Class,WeakReference<Lister>>());
ohair@286 155
ohair@286 156 /**
ohair@286 157 * Creates a lister for array type.
ohair@286 158 */
ohair@286 159 private static Lister getArrayLister( Class componentType ) {
ohair@286 160 Lister l=null;
ohair@286 161 if(componentType.isPrimitive())
ohair@286 162 l = primitiveArrayListers.get(componentType);
ohair@286 163 else {
ohair@286 164 WeakReference<Lister> wr = arrayListerCache.get(componentType);
ohair@286 165 if(wr!=null)
ohair@286 166 l = wr.get();
ohair@286 167 if(l==null) {
ohair@286 168 l = new ArrayLister(componentType);
ohair@286 169 arrayListerCache.put(componentType,new WeakReference<Lister>(l));
ohair@286 170 }
ohair@286 171 }
ohair@286 172 assert l!=null;
ohair@286 173 return l;
ohair@286 174 }
ohair@286 175
ohair@286 176 /**
ohair@286 177 * {@link Lister} for an array.
ohair@286 178 *
ohair@286 179 * <p>
ohair@286 180 * Array packing is slower, but we expect this to be used less frequently than
ohair@286 181 * the {@link CollectionLister}.
ohair@286 182 */
ohair@286 183 private static final class ArrayLister<BeanT,ItemT> extends Lister<BeanT,ItemT[],ItemT,Pack<ItemT>> {
ohair@286 184
ohair@286 185 private final Class<ItemT> itemType;
ohair@286 186
ohair@286 187 public ArrayLister(Class<ItemT> itemType) {
ohair@286 188 this.itemType = itemType;
ohair@286 189 }
ohair@286 190
ohair@286 191 public ListIterator<ItemT> iterator(final ItemT[] objects, XMLSerializer context) {
ohair@286 192 return new ListIterator<ItemT>() {
ohair@286 193 int idx=0;
ohair@286 194 public boolean hasNext() {
ohair@286 195 return idx<objects.length;
ohair@286 196 }
ohair@286 197
ohair@286 198 public ItemT next() {
ohair@286 199 return objects[idx++];
ohair@286 200 }
ohair@286 201 };
ohair@286 202 }
ohair@286 203
ohair@286 204 public Pack startPacking(BeanT current, Accessor<BeanT, ItemT[]> acc) {
ohair@286 205 return new Pack<ItemT>(itemType);
ohair@286 206 }
ohair@286 207
ohair@286 208 public void addToPack(Pack<ItemT> objects, ItemT o) {
ohair@286 209 objects.add(o);
ohair@286 210 }
ohair@286 211
ohair@286 212 public void endPacking( Pack<ItemT> pack, BeanT bean, Accessor<BeanT,ItemT[]> acc ) throws AccessorException {
ohair@286 213 acc.set(bean,pack.build());
ohair@286 214 }
ohair@286 215
ohair@286 216 public void reset(BeanT o,Accessor<BeanT,ItemT[]> acc) throws AccessorException {
ohair@286 217 acc.set(o,(ItemT[])Array.newInstance(itemType,0));
ohair@286 218 }
ohair@286 219
ohair@286 220 }
ohair@286 221
ohair@286 222 public static final class Pack<ItemT> extends ArrayList<ItemT> {
ohair@286 223 private final Class<ItemT> itemType;
ohair@286 224
ohair@286 225 public Pack(Class<ItemT> itemType) {
ohair@286 226 this.itemType = itemType;
ohair@286 227 }
ohair@286 228
ohair@286 229 public ItemT[] build() {
ohair@286 230 return super.toArray( (ItemT[])Array.newInstance(itemType,size()) );
ohair@286 231 }
ohair@286 232 }
ohair@286 233
ohair@286 234 /**
ohair@286 235 * Listers for the primitive type arrays, keyed by their primitive Class object.
ohair@286 236 */
ohair@286 237 /*package*/ static final Map<Class,Lister> primitiveArrayListers = new HashMap<Class,Lister>();
ohair@286 238
ohair@286 239 static {
ohair@286 240 // register primitive array listers
ohair@286 241 PrimitiveArrayListerBoolean.register();
ohair@286 242 PrimitiveArrayListerByte.register();
ohair@286 243 PrimitiveArrayListerCharacter.register();
ohair@286 244 PrimitiveArrayListerDouble.register();
ohair@286 245 PrimitiveArrayListerFloat.register();
ohair@286 246 PrimitiveArrayListerInteger.register();
ohair@286 247 PrimitiveArrayListerLong.register();
ohair@286 248 PrimitiveArrayListerShort.register();
ohair@286 249 }
ohair@286 250
ohair@286 251 /**
ohair@286 252 * {@link Lister} for a collection
ohair@286 253 */
ohair@286 254 public static final class CollectionLister<BeanT,T extends Collection> extends Lister<BeanT,T,Object,T> {
ohair@286 255
ohair@286 256 /**
ohair@286 257 * Sometimes we need to create a new instance of a collection.
ohair@286 258 * This is such an implementation class.
ohair@286 259 */
ohair@286 260 private final Class<? extends T> implClass;
ohair@286 261
ohair@286 262 public CollectionLister(Class<? extends T> implClass) {
ohair@286 263 this.implClass = implClass;
ohair@286 264 }
ohair@286 265
ohair@286 266 public ListIterator iterator(T collection, XMLSerializer context) {
ohair@286 267 final Iterator itr = collection.iterator();
ohair@286 268 return new ListIterator() {
ohair@286 269 public boolean hasNext() {
ohair@286 270 return itr.hasNext();
ohair@286 271 }
ohair@286 272 public Object next() {
ohair@286 273 return itr.next();
ohair@286 274 }
ohair@286 275 };
ohair@286 276 }
ohair@286 277
ohair@286 278 public T startPacking(BeanT bean, Accessor<BeanT, T> acc) throws AccessorException {
ohair@286 279 T collection = acc.get(bean);
ohair@286 280 if(collection==null) {
ohair@286 281 collection = ClassFactory.create(implClass);
ohair@286 282 if(!acc.isAdapted())
ohair@286 283 acc.set(bean,collection);
ohair@286 284 }
ohair@286 285 collection.clear();
ohair@286 286 return collection;
ohair@286 287 }
ohair@286 288
ohair@286 289 public void addToPack(T collection, Object o) {
ohair@286 290 collection.add(o);
ohair@286 291 }
ohair@286 292
ohair@286 293 public void endPacking( T collection, BeanT bean, Accessor<BeanT,T> acc ) throws AccessorException {
ohair@286 294 // this needs to be done in the endPacking, because
ohair@286 295 // sometimes the accessor uses an adapter, and the adapter needs to see
ohair@286 296 // the whole thing.
ohair@286 297
ohair@286 298 // but always doing so causes a problem when this collection property
ohair@286 299 // is getter-only
ohair@286 300
ohair@286 301 // invoke set when possible (see Issue 488)
ohair@286 302 try {
alanb@368 303 if (acc.isAdapted()) {
alanb@368 304 acc.set(bean,collection);
alanb@368 305 }
ohair@286 306 } catch (AccessorException ae) {
ohair@286 307 if(acc.isAdapted()) throw ae;
ohair@286 308 }
ohair@286 309 }
ohair@286 310
ohair@286 311 public void reset(BeanT bean, Accessor<BeanT, T> acc) throws AccessorException {
ohair@286 312 T collection = acc.get(bean);
ohair@286 313 if(collection == null) {
ohair@286 314 return;
ohair@286 315 }
ohair@286 316 collection.clear();
ohair@286 317 }
ohair@286 318 }
ohair@286 319
ohair@286 320 /**
ohair@286 321 * {@link Lister} for IDREFS.
ohair@286 322 */
ohair@286 323 private static final class IDREFS<BeanT,PropT> extends Lister<BeanT,PropT,String,IDREFS<BeanT,PropT>.Pack> {
ohair@286 324 private final Lister<BeanT,PropT,Object,Object> core;
ohair@286 325 /**
ohair@286 326 * Expected type to which IDREF resolves to.
ohair@286 327 */
ohair@286 328 private final Class itemType;
ohair@286 329
ohair@286 330 public IDREFS(Lister core, Class itemType) {
ohair@286 331 this.core = core;
ohair@286 332 this.itemType = itemType;
ohair@286 333 }
ohair@286 334
ohair@286 335 public ListIterator<String> iterator(PropT prop, XMLSerializer context) {
ohair@286 336 final ListIterator i = core.iterator(prop,context);
ohair@286 337
ohair@286 338 return new IDREFSIterator(i, context);
ohair@286 339 }
ohair@286 340
ohair@286 341 public Pack startPacking(BeanT bean, Accessor<BeanT, PropT> acc) {
ohair@286 342 return new Pack(bean,acc);
ohair@286 343 }
ohair@286 344
ohair@286 345 public void addToPack(Pack pack, String item) {
ohair@286 346 pack.add(item);
ohair@286 347 }
ohair@286 348
ohair@286 349 public void endPacking(Pack pack, BeanT bean, Accessor<BeanT, PropT> acc) {
ohair@286 350 }
ohair@286 351
ohair@286 352 public void reset(BeanT bean, Accessor<BeanT, PropT> acc) throws AccessorException {
ohair@286 353 core.reset(bean,acc);
ohair@286 354 }
ohair@286 355
ohair@286 356 /**
ohair@286 357 * PackT for this lister.
ohair@286 358 */
ohair@286 359 private class Pack implements Patcher {
ohair@286 360 private final BeanT bean;
ohair@286 361 private final List<String> idrefs = new ArrayList<String>();
ohair@286 362 private final UnmarshallingContext context;
ohair@286 363 private final Accessor<BeanT,PropT> acc;
ohair@286 364 private final LocatorEx location;
ohair@286 365
ohair@286 366 public Pack(BeanT bean, Accessor<BeanT,PropT> acc) {
ohair@286 367 this.bean = bean;
ohair@286 368 this.acc = acc;
ohair@286 369 this.context = UnmarshallingContext.getInstance();
ohair@286 370 this.location = new LocatorEx.Snapshot(context.getLocator());
ohair@286 371 context.addPatcher(this);
ohair@286 372 }
ohair@286 373
ohair@286 374 public void add(String item) {
ohair@286 375 idrefs.add(item);
ohair@286 376 }
ohair@286 377
ohair@286 378 /**
ohair@286 379 * Resolves IDREFS and fill in the actual array.
ohair@286 380 */
ohair@286 381 public void run() throws SAXException {
ohair@286 382 try {
ohair@286 383 Object pack = core.startPacking(bean,acc);
ohair@286 384
ohair@286 385 for( String id : idrefs ) {
ohair@286 386 Callable callable = context.getObjectFromId(id,itemType);
ohair@286 387 Object t;
ohair@286 388
ohair@286 389 try {
ohair@286 390 t = (callable!=null) ? callable.call() : null;
ohair@286 391 } catch (SAXException e) {
ohair@286 392 throw e;
ohair@286 393 } catch (Exception e) {
ohair@286 394 throw new SAXException2(e);
ohair@286 395 }
ohair@286 396
ohair@286 397 if(t==null) {
ohair@286 398 context.errorUnresolvedIDREF(bean,id,location);
ohair@286 399 } else {
ohair@286 400 TODO.prototype(); // TODO: check if the type of t is proper.
ohair@286 401 core.addToPack(pack,t);
ohair@286 402 }
ohair@286 403 }
ohair@286 404
ohair@286 405 core.endPacking(pack,bean,acc);
ohair@286 406 } catch (AccessorException e) {
ohair@286 407 context.handleError(e);
ohair@286 408 }
ohair@286 409 }
ohair@286 410 }
ohair@286 411 }
ohair@286 412
ohair@286 413 /**
ohair@286 414 * {@link Iterator} for IDREFS lister.
ohair@286 415 *
ohair@286 416 * <p>
ohair@286 417 * Only in ArrayElementProperty we need to get the actual
ohair@286 418 * referenced object. This is a kind of ugly way to make that work.
ohair@286 419 */
ohair@286 420 public static final class IDREFSIterator implements ListIterator<String> {
ohair@286 421 private final ListIterator i;
ohair@286 422 private final XMLSerializer context;
ohair@286 423 private Object last;
ohair@286 424
ohair@286 425 private IDREFSIterator(ListIterator i, XMLSerializer context) {
ohair@286 426 this.i = i;
ohair@286 427 this.context = context;
ohair@286 428 }
ohair@286 429
ohair@286 430 public boolean hasNext() {
ohair@286 431 return i.hasNext();
ohair@286 432 }
ohair@286 433
ohair@286 434 /**
ohair@286 435 * Returns the last referenced object (not just its ID)
ohair@286 436 */
ohair@286 437 public Object last() {
ohair@286 438 return last;
ohair@286 439 }
ohair@286 440
ohair@286 441 public String next() throws SAXException, JAXBException {
ohair@286 442 last = i.next();
ohair@286 443 String id = context.grammar.getBeanInfo(last,true).getId(last,context);
ohair@286 444 if(id==null) {
ohair@286 445 context.errorMissingId(last);
ohair@286 446 }
ohair@286 447 return id;
ohair@286 448 }
ohair@286 449 }
ohair@286 450
ohair@286 451 /**
ohair@286 452 * Gets the special {@link Lister} used to recover from an error.
ohair@286 453 */
ohair@286 454 @SuppressWarnings("unchecked")
ohair@286 455 public static <A,B,C,D> Lister<A,B,C,D> getErrorInstance() {
ohair@286 456 return ERROR;
ohair@286 457 }
ohair@286 458
ohair@286 459 public static final Lister ERROR = new Lister() {
ohair@286 460 public ListIterator iterator(Object o, XMLSerializer context) {
ohair@286 461 return EMPTY_ITERATOR;
ohair@286 462 }
ohair@286 463
ohair@286 464 public Object startPacking(Object o, Accessor accessor) {
ohair@286 465 return null;
ohair@286 466 }
ohair@286 467
ohair@286 468 public void addToPack(Object o, Object o1) {
ohair@286 469 }
ohair@286 470
ohair@286 471 public void endPacking(Object o, Object o1, Accessor accessor) {
ohair@286 472 }
ohair@286 473
ohair@286 474 public void reset(Object o, Accessor accessor) {
ohair@286 475 }
ohair@286 476 };
ohair@286 477
ohair@286 478 private static final ListIterator EMPTY_ITERATOR = new ListIterator() {
ohair@286 479 public boolean hasNext() {
ohair@286 480 return false;
ohair@286 481 }
ohair@286 482
ohair@286 483 public Object next() {
ohair@286 484 throw new IllegalStateException();
ohair@286 485 }
ohair@286 486 };
ohair@286 487
ohair@286 488 private static final Class[] COLLECTION_IMPL_CLASSES = new Class[] {
ohair@286 489 ArrayList.class,
ohair@286 490 LinkedList.class,
ohair@286 491 HashSet.class,
ohair@286 492 TreeSet.class,
ohair@286 493 Stack.class
ohair@286 494 };
ohair@286 495 }

mercurial