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

Fri, 23 Aug 2013 09:57:21 +0100

author
mkos
date
Fri, 23 Aug 2013 09:57:21 +0100
changeset 397
b99d7e355d4b
parent 286
f50545b5e2f1
child 450
b0c2840e2513
permissions
-rw-r--r--

8022885: Update JAX-WS RI integration to 2.2.9-b14140
8013016: Rebase 8009009 against the latest jdk8/jaxws
Reviewed-by: alanb, chegar

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;
ohair@286 27
ohair@286 28 import java.io.IOException;
ohair@286 29 import java.lang.reflect.Constructor;
ohair@286 30 import java.lang.reflect.InvocationTargetException;
ohair@286 31
ohair@286 32 import javax.xml.bind.JAXBElement;
ohair@286 33 import javax.xml.bind.JAXBException;
ohair@286 34 import javax.xml.namespace.QName;
ohair@286 35 import javax.xml.stream.XMLStreamException;
ohair@286 36
ohair@286 37 import com.sun.xml.internal.bind.api.AccessorException;
ohair@286 38 import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
ohair@286 39 import com.sun.xml.internal.bind.v2.model.nav.Navigator;
ohair@286 40 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementInfo;
ohair@286 41 import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
ohair@286 42 import com.sun.xml.internal.bind.v2.runtime.property.Property;
ohair@286 43 import com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory;
ohair@286 44 import com.sun.xml.internal.bind.v2.runtime.property.UnmarshallerChain;
ohair@286 45 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
ohair@286 46 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
ohair@286 47 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Discarder;
ohair@286 48 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Intercepter;
ohair@286 49 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
ohair@286 50 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName;
ohair@286 51 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
ohair@286 52 import com.sun.xml.internal.bind.v2.util.QNameMap;
ohair@286 53
ohair@286 54 import org.xml.sax.SAXException;
ohair@286 55
ohair@286 56 /**
ohair@286 57 * {@link JaxBeanInfo} implementation for {@link RuntimeElementInfo}.
ohair@286 58 *
ohair@286 59 * @author Kohsuke Kawaguchi
ohair@286 60 */
ohair@286 61 public final class ElementBeanInfoImpl extends JaxBeanInfo<JAXBElement> {
ohair@286 62
ohair@286 63 private Loader loader;
ohair@286 64
ohair@286 65 private final Property property;
ohair@286 66
ohair@286 67 // used to create new instances of JAXBElement.
ohair@286 68 private final QName tagName;
ohair@286 69 public final Class expectedType;
ohair@286 70 private final Class scope;
ohair@286 71
ohair@286 72 /**
ohair@286 73 * If non-null, use this to create an instance.
ohair@286 74 * It takes one value.
ohair@286 75 */
ohair@286 76 private final Constructor<? extends JAXBElement> constructor;
ohair@286 77
ohair@286 78 ElementBeanInfoImpl(JAXBContextImpl grammar, RuntimeElementInfo rei) {
ohair@286 79 super(grammar,rei,(Class<JAXBElement>)rei.getType(),true,false,true);
ohair@286 80
ohair@286 81 this.property = PropertyFactory.create(grammar,rei.getProperty());
ohair@286 82
ohair@286 83 tagName = rei.getElementName();
ohair@286 84 expectedType = Navigator.REFLECTION.erasure(rei.getContentInMemoryType());
ohair@286 85 scope = rei.getScope()==null ? JAXBElement.GlobalScope.class : rei.getScope().getClazz();
ohair@286 86
ohair@286 87 Class type = Navigator.REFLECTION.erasure(rei.getType());
ohair@286 88 if(type==JAXBElement.class)
ohair@286 89 constructor = null;
ohair@286 90 else {
ohair@286 91 try {
ohair@286 92 constructor = type.getConstructor(expectedType);
ohair@286 93 } catch (NoSuchMethodException e) {
ohair@286 94 NoSuchMethodError x = new NoSuchMethodError("Failed to find the constructor for " + type + " with " + expectedType);
ohair@286 95 x.initCause(e);
ohair@286 96 throw x;
ohair@286 97 }
ohair@286 98 }
ohair@286 99 }
ohair@286 100
ohair@286 101 /**
ohair@286 102 * The constructor for the sole instanceof {@link JaxBeanInfo} for
ohair@286 103 * handling user-created {@link JAXBElement}.
ohair@286 104 *
ohair@286 105 * Such {@link JaxBeanInfo} is used only for marshalling.
ohair@286 106 *
ohair@286 107 * This is a hack.
ohair@286 108 */
ohair@286 109 protected ElementBeanInfoImpl(final JAXBContextImpl grammar) {
ohair@286 110 super(grammar,null,JAXBElement.class,true,false,true);
ohair@286 111 tagName = null;
ohair@286 112 expectedType = null;
ohair@286 113 scope = null;
ohair@286 114 constructor = null;
ohair@286 115
ohair@286 116 this.property = new Property<JAXBElement>() {
ohair@286 117 public void reset(JAXBElement o) {
ohair@286 118 throw new UnsupportedOperationException();
ohair@286 119 }
ohair@286 120
ohair@286 121 public void serializeBody(JAXBElement e, XMLSerializer target, Object outerPeer) throws SAXException, IOException, XMLStreamException {
ohair@286 122 Class scope = e.getScope();
ohair@286 123 if(e.isGlobalScope()) scope = null;
ohair@286 124 QName n = e.getName();
ohair@286 125 ElementBeanInfoImpl bi = grammar.getElement(scope,n);
ohair@286 126 if(bi==null) {
ohair@286 127 // infer what to do from the type
ohair@286 128 JaxBeanInfo tbi;
ohair@286 129 try {
ohair@286 130 tbi = grammar.getBeanInfo(e.getDeclaredType(),true);
ohair@286 131 } catch (JAXBException x) {
ohair@286 132 // if e.getDeclaredType() isn't known to this JAXBContext
ohair@286 133 target.reportError(null,x);
ohair@286 134 return;
ohair@286 135 }
ohair@286 136 Object value = e.getValue();
ohair@286 137 target.startElement(n.getNamespaceURI(),n.getLocalPart(),n.getPrefix(),null);
ohair@286 138 if(value==null) {
ohair@286 139 target.writeXsiNilTrue();
ohair@286 140 } else {
ohair@286 141 target.childAsXsiType(value,"value",tbi, false);
ohair@286 142 }
ohair@286 143 target.endElement();
ohair@286 144 } else {
ohair@286 145 try {
ohair@286 146 bi.property.serializeBody(e,target,e);
ohair@286 147 } catch (AccessorException x) {
ohair@286 148 target.reportError(null,x);
ohair@286 149 }
ohair@286 150 }
ohair@286 151 }
ohair@286 152
ohair@286 153 public void serializeURIs(JAXBElement o, XMLSerializer target) {
ohair@286 154 }
ohair@286 155
ohair@286 156 public boolean hasSerializeURIAction() {
ohair@286 157 return false;
ohair@286 158 }
ohair@286 159
ohair@286 160 public String getIdValue(JAXBElement o) {
ohair@286 161 return null;
ohair@286 162 }
ohair@286 163
ohair@286 164 public PropertyKind getKind() {
ohair@286 165 return PropertyKind.ELEMENT;
ohair@286 166 }
ohair@286 167
ohair@286 168 public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) {
ohair@286 169 }
ohair@286 170
ohair@286 171 public Accessor getElementPropertyAccessor(String nsUri, String localName) {
ohair@286 172 throw new UnsupportedOperationException();
ohair@286 173 }
ohair@286 174
ohair@286 175 public void wrapUp() {
ohair@286 176 }
ohair@286 177
ohair@286 178 public RuntimePropertyInfo getInfo() {
ohair@286 179 return property.getInfo();
ohair@286 180 }
ohair@286 181
ohair@286 182 public boolean isHiddenByOverride() {
ohair@286 183 return false;
ohair@286 184 }
ohair@286 185
ohair@286 186 public void setHiddenByOverride(boolean hidden) {
ohair@286 187 throw new UnsupportedOperationException("Not supported on jaxbelements.");
ohair@286 188 }
ohair@286 189
ohair@286 190 public String getFieldName() {
ohair@286 191 return null;
ohair@286 192 }
ohair@286 193
ohair@286 194 };
ohair@286 195 }
ohair@286 196
ohair@286 197 /**
ohair@286 198 * Use the previous {@link UnmarshallingContext.State}'s target to store
ohair@286 199 * {@link JAXBElement} object to be unmarshalled. This allows the property {@link Loader}
ohair@286 200 * to correctly find the parent object.
ohair@286 201 * This is a hack.
ohair@286 202 */
ohair@286 203 private final class IntercepterLoader extends Loader implements Intercepter {
ohair@286 204 private final Loader core;
ohair@286 205
ohair@286 206 public IntercepterLoader(Loader core) {
ohair@286 207 this.core = core;
ohair@286 208 }
ohair@286 209
ohair@286 210 @Override
ohair@286 211 public final void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
ohair@286 212 state.loader = core;
ohair@286 213 state.intercepter = this;
ohair@286 214
ohair@286 215 // TODO: make sure there aren't too many duplicate of this code
ohair@286 216 // create the object to unmarshal
ohair@286 217 Object child;
ohair@286 218 UnmarshallingContext context = state.getContext();
ohair@286 219
ohair@286 220 // let's see if we can reuse the existing peer object
ohair@286 221 child = context.getOuterPeer();
ohair@286 222
ohair@286 223 if(child!=null && jaxbType!=child.getClass())
ohair@286 224 child = null; // unexpected type.
ohair@286 225
ohair@286 226 if(child!=null)
ohair@286 227 reset((JAXBElement)child,context);
ohair@286 228
ohair@286 229 if(child==null)
ohair@286 230 child = context.createInstance(ElementBeanInfoImpl.this);
ohair@286 231
ohair@286 232 fireBeforeUnmarshal(ElementBeanInfoImpl.this, child, state);
ohair@286 233
ohair@286 234 context.recordOuterPeer(child);
ohair@286 235 UnmarshallingContext.State p = state.prev;
ohair@286 236 p.backup = p.target;
ohair@286 237 p.target = child;
ohair@286 238
ohair@286 239 core.startElement(state,ea);
ohair@286 240 }
ohair@286 241
ohair@286 242 public Object intercept(UnmarshallingContext.State state, Object o) throws SAXException {
ohair@286 243 JAXBElement e = (JAXBElement)state.target;
ohair@286 244 state.target = state.backup;
ohair@286 245 state.backup = null;
ohair@286 246
ohair@286 247 if (state.nil) {
ohair@286 248 e.setNil(true);
ohair@286 249 state.nil = false;
ohair@286 250 }
ohair@286 251
ohair@286 252 if(o!=null)
ohair@286 253 // if the value is a leaf type, it's often already set to the element
ohair@286 254 // through Accessor.
ohair@286 255 e.setValue(o);
ohair@286 256
ohair@286 257 fireAfterUnmarshal(ElementBeanInfoImpl.this, e, state);
ohair@286 258
ohair@286 259 return e;
ohair@286 260 }
ohair@286 261 }
ohair@286 262
ohair@286 263 public String getElementNamespaceURI(JAXBElement e) {
ohair@286 264 return e.getName().getNamespaceURI();
ohair@286 265 }
ohair@286 266
ohair@286 267 public String getElementLocalName(JAXBElement e) {
ohair@286 268 return e.getName().getLocalPart();
ohair@286 269 }
ohair@286 270
ohair@286 271 public Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) {
ohair@286 272 if(loader==null) {
ohair@286 273 // this has to be done lazily to avoid cyclic reference issue
ohair@286 274 UnmarshallerChain c = new UnmarshallerChain(context);
ohair@286 275 QNameMap<ChildLoader> result = new QNameMap<ChildLoader>();
ohair@286 276 property.buildChildElementUnmarshallers(c,result);
ohair@286 277 if(result.size()==1)
ohair@286 278 // for ElementBeanInfoImpl created from RuntimeElementInfo
ohair@286 279 this.loader = new IntercepterLoader(result.getOne().getValue().loader);
ohair@286 280 else
ohair@286 281 // for special ElementBeanInfoImpl only used for marshalling
ohair@286 282 this.loader = Discarder.INSTANCE;
ohair@286 283 }
ohair@286 284 return loader;
ohair@286 285 }
ohair@286 286
ohair@286 287 public final JAXBElement createInstance(UnmarshallingContext context) throws IllegalAccessException, InvocationTargetException, InstantiationException {
ohair@286 288 return createInstanceFromValue(null);
ohair@286 289 }
ohair@286 290
ohair@286 291 public final JAXBElement createInstanceFromValue(Object o) throws IllegalAccessException, InvocationTargetException, InstantiationException {
ohair@286 292 if(constructor==null)
ohair@286 293 return new JAXBElement(tagName,expectedType,scope,o);
ohair@286 294 else
ohair@286 295 return constructor.newInstance(o);
ohair@286 296 }
ohair@286 297
ohair@286 298 public boolean reset(JAXBElement e, UnmarshallingContext context) {
ohair@286 299 e.setValue(null);
ohair@286 300 return true;
ohair@286 301 }
ohair@286 302
ohair@286 303 public String getId(JAXBElement e, XMLSerializer target) {
ohair@286 304 // TODO: is this OK? Should we be returning the ID value of the type property?
ohair@286 305 /*
ohair@286 306 There's one case where we JAXBElement needs to be designated as ID,
ohair@286 307 and that is when there's a global element whose type is ID.
ohair@286 308 */
ohair@286 309 Object o = e.getValue();
ohair@286 310 if(o instanceof String)
ohair@286 311 return (String)o;
ohair@286 312 else
ohair@286 313 return null;
ohair@286 314 }
ohair@286 315
ohair@286 316 public void serializeBody(JAXBElement element, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
ohair@286 317 try {
ohair@286 318 property.serializeBody(element,target,null);
ohair@286 319 } catch (AccessorException x) {
ohair@286 320 target.reportError(null,x);
ohair@286 321 }
ohair@286 322 }
ohair@286 323
ohair@286 324 public void serializeRoot(JAXBElement e, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
ohair@286 325 serializeBody(e,target);
ohair@286 326 }
ohair@286 327
ohair@286 328 public void serializeAttributes(JAXBElement e, XMLSerializer target) {
ohair@286 329 // noop
ohair@286 330 }
ohair@286 331
ohair@286 332 public void serializeURIs(JAXBElement e, XMLSerializer target) {
ohair@286 333 // noop
ohair@286 334 }
ohair@286 335
ohair@286 336 public final Transducer<JAXBElement> getTransducer() {
ohair@286 337 return null;
ohair@286 338 }
ohair@286 339
ohair@286 340 @Override
ohair@286 341 public void wrapUp() {
ohair@286 342 super.wrapUp();
ohair@286 343 property.wrapUp();
ohair@286 344 }
ohair@286 345
ohair@286 346 @Override
ohair@286 347 public void link(JAXBContextImpl grammar) {
ohair@286 348 super.link(grammar);
ohair@286 349 getLoader(grammar,true); // make sure to build them, if we hadn't done so
ohair@286 350 }
ohair@286 351 }

mercurial