ohair@286: /* alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.bind.v2.runtime.reflect; ohair@286: ohair@286: import java.lang.ref.WeakReference; ohair@286: import java.lang.reflect.Array; ohair@286: import java.lang.reflect.ParameterizedType; ohair@286: import java.lang.reflect.Type; ohair@286: import java.util.ArrayList; ohair@286: import java.util.Collection; ohair@286: import java.util.Collections; ohair@286: import java.util.HashMap; ohair@286: import java.util.Iterator; ohair@286: import java.util.List; ohair@286: import java.util.Map; ohair@286: import java.util.WeakHashMap; ohair@286: import java.util.LinkedList; ohair@286: import java.util.HashSet; ohair@286: import java.util.TreeSet; ohair@286: import java.util.Stack; ohair@286: import java.util.concurrent.Callable; ohair@286: ohair@286: import javax.xml.bind.JAXBException; ohair@286: ohair@286: import com.sun.istack.internal.SAXException2; ohair@286: import com.sun.xml.internal.bind.api.AccessorException; ohair@286: import com.sun.xml.internal.bind.v2.ClassFactory; ohair@286: import com.sun.xml.internal.bind.v2.TODO; ohair@286: import com.sun.xml.internal.bind.v2.model.core.Adapter; ohair@286: import com.sun.xml.internal.bind.v2.model.core.ID; ohair@286: import com.sun.xml.internal.bind.v2.model.nav.Navigator; ohair@286: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Patcher; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LocatorEx; ohair@286: ohair@286: import org.xml.sax.SAXException; ohair@286: ohair@286: /** ohair@286: * Used to list individual values of a multi-value property, and ohair@286: * to pack individual values into a multi-value property. ohair@286: * ohair@286: * @author Kohsuke Kawaguchi (kk@kohsuke.org) ohair@286: */ ohair@286: public abstract class Lister { ohair@286: ohair@286: protected Lister() {} ohair@286: ohair@286: /** ohair@286: * Iterates values of a multi-value property. ohair@286: * ohair@286: * @param context ohair@286: * This parameter is used to support ID/IDREF handling. ohair@286: */ ohair@286: public abstract ListIterator iterator(PropT multiValueProp, XMLSerializer context); ohair@286: ohair@286: /** ohair@286: * Setting values to a multi-value property starts by creating ohair@286: * a transient object called "pack" from the current field. ohair@286: */ ohair@286: public abstract PackT startPacking(BeanT bean, Accessor acc) throws AccessorException; ohair@286: ohair@286: /** ohair@286: * Once the {@link #startPacking} is called, you can ohair@286: * add values to the pack by using this method. ohair@286: */ ohair@286: public abstract void addToPack( PackT pack, ItemT newValue ) throws AccessorException; ohair@286: ohair@286: /** ohair@286: * Finally, call this method to ohair@286: * wraps up the {@code pack}. This method may update the field of ohair@286: * the given bean. ohair@286: */ ohair@286: public abstract void endPacking( PackT pack, BeanT bean, Accessor acc ) throws AccessorException; ohair@286: ohair@286: /** ohair@286: * Clears the values of the property. ohair@286: */ ohair@286: public abstract void reset(BeanT o,Accessor acc) throws AccessorException; ohair@286: ohair@286: ohair@286: /** ohair@286: * Gets a reference to the appropriate {@link Lister} object ohair@286: * if the field is a multi-value field. Otherwise null. ohair@286: * ohair@286: * @param fieldType ohair@286: * the type of the field that stores the collection ohair@286: * @param idness ohair@286: * ID-ness of the property. ohair@286: * @param adapter ohair@286: * adapter to be used for individual items. can be null. ohair@286: */ ohair@286: public static ohair@286: Lister create(Type fieldType,ID idness, Adapter adapter) { ohair@286: ohair@286: Class rawType = Navigator.REFLECTION.erasure(fieldType); ohair@286: Class itemType; ohair@286: ohair@286: Lister l; ohair@286: if( rawType.isArray() ) { ohair@286: itemType = rawType.getComponentType(); ohair@286: l = getArrayLister(itemType); ohair@286: } else ohair@286: if( Collection.class.isAssignableFrom(rawType) ) { ohair@286: Type bt = Navigator.REFLECTION.getBaseClass(fieldType,Collection.class); ohair@286: if(bt instanceof ParameterizedType) ohair@286: itemType = Navigator.REFLECTION.erasure(((ParameterizedType)bt).getActualTypeArguments()[0]); ohair@286: else ohair@286: itemType = Object.class; ohair@286: l = new CollectionLister(getImplClass(rawType)); ohair@286: } else ohair@286: return null; ohair@286: ohair@286: if(idness==ID.IDREF) ohair@286: l = new IDREFS(l,itemType); ohair@286: ohair@286: if(adapter!=null) ohair@286: l = new AdaptedLister(l,adapter.adapterType); ohair@286: ohair@286: return l; ohair@286: } ohair@286: ohair@286: private static Class getImplClass(Class fieldType) { ohair@286: return ClassFactory.inferImplClass(fieldType,COLLECTION_IMPL_CLASSES); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Cache instances of {@link ArrayLister}s. ohair@286: */ ohair@286: private static final Map> arrayListerCache = ohair@286: Collections.synchronizedMap(new WeakHashMap>()); ohair@286: ohair@286: /** ohair@286: * Creates a lister for array type. ohair@286: */ ohair@286: private static Lister getArrayLister( Class componentType ) { ohair@286: Lister l=null; ohair@286: if(componentType.isPrimitive()) ohair@286: l = primitiveArrayListers.get(componentType); ohair@286: else { ohair@286: WeakReference wr = arrayListerCache.get(componentType); ohair@286: if(wr!=null) ohair@286: l = wr.get(); ohair@286: if(l==null) { ohair@286: l = new ArrayLister(componentType); ohair@286: arrayListerCache.put(componentType,new WeakReference(l)); ohair@286: } ohair@286: } ohair@286: assert l!=null; ohair@286: return l; ohair@286: } ohair@286: ohair@286: /** ohair@286: * {@link Lister} for an array. ohair@286: * ohair@286: *

ohair@286: * Array packing is slower, but we expect this to be used less frequently than ohair@286: * the {@link CollectionLister}. ohair@286: */ ohair@286: private static final class ArrayLister extends Lister> { ohair@286: ohair@286: private final Class itemType; ohair@286: ohair@286: public ArrayLister(Class itemType) { ohair@286: this.itemType = itemType; ohair@286: } ohair@286: ohair@286: public ListIterator iterator(final ItemT[] objects, XMLSerializer context) { ohair@286: return new ListIterator() { ohair@286: int idx=0; ohair@286: public boolean hasNext() { ohair@286: return idx acc) { ohair@286: return new Pack(itemType); ohair@286: } ohair@286: ohair@286: public void addToPack(Pack objects, ItemT o) { ohair@286: objects.add(o); ohair@286: } ohair@286: ohair@286: public void endPacking( Pack pack, BeanT bean, Accessor acc ) throws AccessorException { ohair@286: acc.set(bean,pack.build()); ohair@286: } ohair@286: ohair@286: public void reset(BeanT o,Accessor acc) throws AccessorException { ohair@286: acc.set(o,(ItemT[])Array.newInstance(itemType,0)); ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: public static final class Pack extends ArrayList { ohair@286: private final Class itemType; ohair@286: ohair@286: public Pack(Class itemType) { ohair@286: this.itemType = itemType; ohair@286: } ohair@286: ohair@286: public ItemT[] build() { ohair@286: return super.toArray( (ItemT[])Array.newInstance(itemType,size()) ); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Listers for the primitive type arrays, keyed by their primitive Class object. ohair@286: */ ohair@286: /*package*/ static final Map primitiveArrayListers = new HashMap(); ohair@286: ohair@286: static { ohair@286: // register primitive array listers ohair@286: PrimitiveArrayListerBoolean.register(); ohair@286: PrimitiveArrayListerByte.register(); ohair@286: PrimitiveArrayListerCharacter.register(); ohair@286: PrimitiveArrayListerDouble.register(); ohair@286: PrimitiveArrayListerFloat.register(); ohair@286: PrimitiveArrayListerInteger.register(); ohair@286: PrimitiveArrayListerLong.register(); ohair@286: PrimitiveArrayListerShort.register(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * {@link Lister} for a collection ohair@286: */ ohair@286: public static final class CollectionLister extends Lister { ohair@286: ohair@286: /** ohair@286: * Sometimes we need to create a new instance of a collection. ohair@286: * This is such an implementation class. ohair@286: */ ohair@286: private final Class implClass; ohair@286: ohair@286: public CollectionLister(Class implClass) { ohair@286: this.implClass = implClass; ohair@286: } ohair@286: ohair@286: public ListIterator iterator(T collection, XMLSerializer context) { ohair@286: final Iterator itr = collection.iterator(); ohair@286: return new ListIterator() { ohair@286: public boolean hasNext() { ohair@286: return itr.hasNext(); ohair@286: } ohair@286: public Object next() { ohair@286: return itr.next(); ohair@286: } ohair@286: }; ohair@286: } ohair@286: ohair@286: public T startPacking(BeanT bean, Accessor acc) throws AccessorException { ohair@286: T collection = acc.get(bean); ohair@286: if(collection==null) { ohair@286: collection = ClassFactory.create(implClass); ohair@286: if(!acc.isAdapted()) ohair@286: acc.set(bean,collection); ohair@286: } ohair@286: collection.clear(); ohair@286: return collection; ohair@286: } ohair@286: ohair@286: public void addToPack(T collection, Object o) { ohair@286: collection.add(o); ohair@286: } ohair@286: ohair@286: public void endPacking( T collection, BeanT bean, Accessor acc ) throws AccessorException { ohair@286: // this needs to be done in the endPacking, because ohair@286: // sometimes the accessor uses an adapter, and the adapter needs to see ohair@286: // the whole thing. ohair@286: ohair@286: // but always doing so causes a problem when this collection property ohair@286: // is getter-only ohair@286: ohair@286: // invoke set when possible (see Issue 488) ohair@286: try { alanb@368: if (acc.isAdapted()) { alanb@368: acc.set(bean,collection); alanb@368: } ohair@286: } catch (AccessorException ae) { ohair@286: if(acc.isAdapted()) throw ae; ohair@286: } ohair@286: } ohair@286: ohair@286: public void reset(BeanT bean, Accessor acc) throws AccessorException { ohair@286: T collection = acc.get(bean); ohair@286: if(collection == null) { ohair@286: return; ohair@286: } ohair@286: collection.clear(); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * {@link Lister} for IDREFS. ohair@286: */ ohair@286: private static final class IDREFS extends Lister.Pack> { ohair@286: private final Lister core; ohair@286: /** ohair@286: * Expected type to which IDREF resolves to. ohair@286: */ ohair@286: private final Class itemType; ohair@286: ohair@286: public IDREFS(Lister core, Class itemType) { ohair@286: this.core = core; ohair@286: this.itemType = itemType; ohair@286: } ohair@286: ohair@286: public ListIterator iterator(PropT prop, XMLSerializer context) { ohair@286: final ListIterator i = core.iterator(prop,context); ohair@286: ohair@286: return new IDREFSIterator(i, context); ohair@286: } ohair@286: ohair@286: public Pack startPacking(BeanT bean, Accessor acc) { ohair@286: return new Pack(bean,acc); ohair@286: } ohair@286: ohair@286: public void addToPack(Pack pack, String item) { ohair@286: pack.add(item); ohair@286: } ohair@286: ohair@286: public void endPacking(Pack pack, BeanT bean, Accessor acc) { ohair@286: } ohair@286: ohair@286: public void reset(BeanT bean, Accessor acc) throws AccessorException { ohair@286: core.reset(bean,acc); ohair@286: } ohair@286: ohair@286: /** ohair@286: * PackT for this lister. ohair@286: */ ohair@286: private class Pack implements Patcher { ohair@286: private final BeanT bean; ohair@286: private final List idrefs = new ArrayList(); ohair@286: private final UnmarshallingContext context; ohair@286: private final Accessor acc; ohair@286: private final LocatorEx location; ohair@286: ohair@286: public Pack(BeanT bean, Accessor acc) { ohair@286: this.bean = bean; ohair@286: this.acc = acc; ohair@286: this.context = UnmarshallingContext.getInstance(); ohair@286: this.location = new LocatorEx.Snapshot(context.getLocator()); ohair@286: context.addPatcher(this); ohair@286: } ohair@286: ohair@286: public void add(String item) { ohair@286: idrefs.add(item); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Resolves IDREFS and fill in the actual array. ohair@286: */ ohair@286: public void run() throws SAXException { ohair@286: try { ohair@286: Object pack = core.startPacking(bean,acc); ohair@286: ohair@286: for( String id : idrefs ) { ohair@286: Callable callable = context.getObjectFromId(id,itemType); ohair@286: Object t; ohair@286: ohair@286: try { ohair@286: t = (callable!=null) ? callable.call() : null; ohair@286: } catch (SAXException e) { ohair@286: throw e; ohair@286: } catch (Exception e) { ohair@286: throw new SAXException2(e); ohair@286: } ohair@286: ohair@286: if(t==null) { ohair@286: context.errorUnresolvedIDREF(bean,id,location); ohair@286: } else { ohair@286: TODO.prototype(); // TODO: check if the type of t is proper. ohair@286: core.addToPack(pack,t); ohair@286: } ohair@286: } ohair@286: ohair@286: core.endPacking(pack,bean,acc); ohair@286: } catch (AccessorException e) { ohair@286: context.handleError(e); ohair@286: } ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * {@link Iterator} for IDREFS lister. ohair@286: * ohair@286: *

ohair@286: * Only in ArrayElementProperty we need to get the actual ohair@286: * referenced object. This is a kind of ugly way to make that work. ohair@286: */ ohair@286: public static final class IDREFSIterator implements ListIterator { ohair@286: private final ListIterator i; ohair@286: private final XMLSerializer context; ohair@286: private Object last; ohair@286: ohair@286: private IDREFSIterator(ListIterator i, XMLSerializer context) { ohair@286: this.i = i; ohair@286: this.context = context; ohair@286: } ohair@286: ohair@286: public boolean hasNext() { ohair@286: return i.hasNext(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Returns the last referenced object (not just its ID) ohair@286: */ ohair@286: public Object last() { ohair@286: return last; ohair@286: } ohair@286: ohair@286: public String next() throws SAXException, JAXBException { ohair@286: last = i.next(); ohair@286: String id = context.grammar.getBeanInfo(last,true).getId(last,context); ohair@286: if(id==null) { ohair@286: context.errorMissingId(last); ohair@286: } ohair@286: return id; ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Gets the special {@link Lister} used to recover from an error. ohair@286: */ ohair@286: @SuppressWarnings("unchecked") ohair@286: public static Lister getErrorInstance() { ohair@286: return ERROR; ohair@286: } ohair@286: ohair@286: public static final Lister ERROR = new Lister() { ohair@286: public ListIterator iterator(Object o, XMLSerializer context) { ohair@286: return EMPTY_ITERATOR; ohair@286: } ohair@286: ohair@286: public Object startPacking(Object o, Accessor accessor) { ohair@286: return null; ohair@286: } ohair@286: ohair@286: public void addToPack(Object o, Object o1) { ohair@286: } ohair@286: ohair@286: public void endPacking(Object o, Object o1, Accessor accessor) { ohair@286: } ohair@286: ohair@286: public void reset(Object o, Accessor accessor) { ohair@286: } ohair@286: }; ohair@286: ohair@286: private static final ListIterator EMPTY_ITERATOR = new ListIterator() { ohair@286: public boolean hasNext() { ohair@286: return false; ohair@286: } ohair@286: ohair@286: public Object next() { ohair@286: throw new IllegalStateException(); ohair@286: } ohair@286: }; ohair@286: ohair@286: private static final Class[] COLLECTION_IMPL_CLASSES = new Class[] { ohair@286: ArrayList.class, ohair@286: LinkedList.class, ohair@286: HashSet.class, ohair@286: TreeSet.class, ohair@286: Stack.class ohair@286: }; ohair@286: }