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

Sun, 18 Jun 2017 23:18:45 +0100

author
aefimov
date
Sun, 18 Jun 2017 23:18:45 +0100
changeset 1491
d5c5a205d7fb
parent 450
b0c2840e2513
child 637
9c07ef4934dd
permissions
-rw-r--r--

8172297: In java 8, the marshalling with JAX-WS does not escape carriage return
Reviewed-by: lancea

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

mercurial