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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 450
b0c2840e2513
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.bind.v2.runtime.reflect;
aoqi@0 27
aoqi@0 28 import java.io.IOException;
aoqi@0 29 import java.util.concurrent.Callable;
aoqi@0 30
aoqi@0 31 import javax.xml.bind.JAXBException;
aoqi@0 32 import javax.xml.bind.annotation.XmlValue;
aoqi@0 33 import javax.xml.stream.XMLStreamException;
aoqi@0 34
aoqi@0 35 import com.sun.istack.internal.NotNull;
aoqi@0 36 import com.sun.istack.internal.Nullable;
aoqi@0 37 import com.sun.istack.internal.SAXException2;
aoqi@0 38 import com.sun.xml.internal.bind.WhiteSpaceProcessor;
aoqi@0 39 import com.sun.xml.internal.bind.api.AccessorException;
aoqi@0 40 import com.sun.xml.internal.bind.v2.model.core.ID;
aoqi@0 41 import com.sun.xml.internal.bind.v2.model.impl.RuntimeModelBuilder;
aoqi@0 42 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeNonElementRef;
aoqi@0 43 import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
aoqi@0 44 import com.sun.xml.internal.bind.v2.runtime.Name;
aoqi@0 45 import com.sun.xml.internal.bind.v2.runtime.Transducer;
aoqi@0 46 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
aoqi@0 47 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
aoqi@0 48 import com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedTransducedAccessorFactory;
aoqi@0 49 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Patcher;
aoqi@0 50 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
aoqi@0 51 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LocatorEx;
aoqi@0 52
aoqi@0 53 import org.xml.sax.SAXException;
aoqi@0 54
aoqi@0 55 /**
aoqi@0 56 * {@link Accessor} and {@link Transducer} combined into one object.
aoqi@0 57 *
aoqi@0 58 * <p>
aoqi@0 59 * This allows efficient conversions between primitive values and
aoqi@0 60 * String without using boxing.
aoqi@0 61 *
aoqi@0 62 * <p>
aoqi@0 63 * This abstraction only works for a single-value property.
aoqi@0 64 *
aoqi@0 65 * <p>
aoqi@0 66 * An instance of {@link TransducedAccessor} implicitly holds a
aoqi@0 67 * field of the {@code BeanT} that the accessors access.
aoqi@0 68 *
aoqi@0 69 * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
aoqi@0 70 */
aoqi@0 71 public abstract class TransducedAccessor<BeanT> {
aoqi@0 72
aoqi@0 73 /**
aoqi@0 74 * @see Transducer#useNamespace()
aoqi@0 75 */
aoqi@0 76 public boolean useNamespace() {
aoqi@0 77 return false;
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 /**
aoqi@0 81 * Obtain the value of the field and declares the namespace URIs used in
aoqi@0 82 * the value.
aoqi@0 83 *
aoqi@0 84 * @see Transducer#declareNamespace(Object, XMLSerializer)
aoqi@0 85 */
aoqi@0 86 public void declareNamespace( BeanT o, XMLSerializer w ) throws AccessorException, SAXException {
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 /**
aoqi@0 90 * Prints the responsible field of the given bean to the writer.
aoqi@0 91 *
aoqi@0 92 * <p>
aoqi@0 93 * Use {@link XMLSerializer#getInstance()} to access to the namespace bindings
aoqi@0 94 *
aoqi@0 95 * @return
aoqi@0 96 * if the accessor didn't yield a value, return null.
aoqi@0 97 */
aoqi@0 98 public abstract @Nullable CharSequence print(@NotNull BeanT o) throws AccessorException, SAXException;
aoqi@0 99
aoqi@0 100 /**
aoqi@0 101 * Parses the text value into the responsible field of the given bean.
aoqi@0 102 *
aoqi@0 103 * <p>
aoqi@0 104 * Use {@link UnmarshallingContext#getInstance()} to access to the namespace bindings
aoqi@0 105 *
aoqi@0 106 * @throws AccessorException
aoqi@0 107 * if the transducer is used to parse an user bean that uses {@link XmlValue},
aoqi@0 108 * then this exception may occur when it tries to set the leaf value to the bean.
aoqi@0 109 * @throws RuntimeException
aoqi@0 110 * if the lexical form is incorrect. The method may throw a RuntimeException,
aoqi@0 111 * but it shouldn't cause the entire unmarshalling to fail.
aoqi@0 112 * @throws SAXException
aoqi@0 113 * if the parse method found an error, the error is reported, and then
aoqi@0 114 * the processing is aborted.
aoqi@0 115 */
aoqi@0 116 public abstract void parse(BeanT o, CharSequence lexical) throws AccessorException, SAXException;
aoqi@0 117
aoqi@0 118 /**
aoqi@0 119 * Checks if the field has a value.
aoqi@0 120 */
aoqi@0 121 public abstract boolean hasValue(BeanT o) throws AccessorException;
aoqi@0 122
aoqi@0 123
aoqi@0 124
aoqi@0 125
aoqi@0 126
aoqi@0 127
aoqi@0 128
aoqi@0 129
aoqi@0 130
aoqi@0 131
aoqi@0 132
aoqi@0 133 /**
aoqi@0 134 * Gets the {@link TransducedAccessor} appropriately configured for
aoqi@0 135 * the given property.
aoqi@0 136 *
aoqi@0 137 * <p>
aoqi@0 138 * This allows the implementation to use an optimized code.
aoqi@0 139 */
aoqi@0 140 public static <T> TransducedAccessor<T> get(JAXBContextImpl context, RuntimeNonElementRef ref) {
aoqi@0 141 Transducer xducer = RuntimeModelBuilder.createTransducer(ref);
aoqi@0 142 RuntimePropertyInfo prop = ref.getSource();
aoqi@0 143
aoqi@0 144 if(prop.isCollection()) {
aoqi@0 145 return new ListTransducedAccessorImpl(xducer,prop.getAccessor(),
aoqi@0 146 Lister.create(Utils.REFLECTION_NAVIGATOR.erasure(prop.getRawType()), prop.id(), prop.getAdapter()));
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 if(prop.id()==ID.IDREF)
aoqi@0 150 return new IDREFTransducedAccessorImpl(prop.getAccessor());
aoqi@0 151
aoqi@0 152 if(xducer.isDefault() && context != null && !context.fastBoot) {
aoqi@0 153 TransducedAccessor xa = OptimizedTransducedAccessorFactory.get(prop);
aoqi@0 154 if(xa!=null) return xa;
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 if(xducer.useNamespace())
aoqi@0 158 return new CompositeContextDependentTransducedAccessorImpl( context, xducer, prop.getAccessor() );
aoqi@0 159 else
aoqi@0 160 return new CompositeTransducedAccessorImpl( context, xducer, prop.getAccessor() );
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 /**
aoqi@0 164 * Convenience method to write the value as a text inside an element
aoqi@0 165 * without any attributes.
aoqi@0 166 * Can be overridden for improved performance.
aoqi@0 167 *
aoqi@0 168 * <p>
aoqi@0 169 * The callee assumes that there's an associated value in the field.
aoqi@0 170 * No @xsi:type handling is expected.
aoqi@0 171 */
aoqi@0 172 public abstract void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws SAXException, AccessorException, IOException, XMLStreamException;
aoqi@0 173
aoqi@0 174 /**
aoqi@0 175 * Invokes one of the {@link XMLSerializer#text(String, String)} method
aoqi@0 176 * with the representation of data bested suited for this transduced accessor.
aoqi@0 177 */
aoqi@0 178 public abstract void writeText(XMLSerializer w, BeanT o, String fieldName) throws AccessorException, SAXException, IOException, XMLStreamException;
aoqi@0 179
aoqi@0 180 static class CompositeContextDependentTransducedAccessorImpl<BeanT,ValueT> extends CompositeTransducedAccessorImpl<BeanT,ValueT> {
aoqi@0 181 public CompositeContextDependentTransducedAccessorImpl(JAXBContextImpl context,Transducer<ValueT> xducer, Accessor<BeanT,ValueT> acc) {
aoqi@0 182 super(context,xducer,acc);
aoqi@0 183 assert xducer.useNamespace();
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 @Override
aoqi@0 187 public boolean useNamespace() {
aoqi@0 188 return true;
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 @Override
aoqi@0 192 public void declareNamespace(BeanT bean, XMLSerializer w) throws AccessorException {
aoqi@0 193 ValueT o = acc.get(bean);
aoqi@0 194 if(o!=null)
aoqi@0 195 xducer.declareNamespace(o,w);
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 @Override
aoqi@0 199 public void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws SAXException, AccessorException, IOException, XMLStreamException {
aoqi@0 200 w.startElement(tagName,null);
aoqi@0 201 declareNamespace(o,w);
aoqi@0 202 w.endNamespaceDecls(null);
aoqi@0 203 w.endAttributes();
aoqi@0 204 xducer.writeText(w,acc.get(o),fieldName);
aoqi@0 205 w.endElement();
aoqi@0 206 }
aoqi@0 207 }
aoqi@0 208
aoqi@0 209
aoqi@0 210 /**
aoqi@0 211 * Implementation of {@link TransducedAccessor} that
aoqi@0 212 * simply combines a {@link Transducer} and {@link Accessor}.
aoqi@0 213 */
aoqi@0 214 public static class CompositeTransducedAccessorImpl<BeanT,ValueT> extends TransducedAccessor<BeanT> {
aoqi@0 215 protected final Transducer<ValueT> xducer;
aoqi@0 216 protected final Accessor<BeanT,ValueT> acc;
aoqi@0 217
aoqi@0 218 public CompositeTransducedAccessorImpl(JAXBContextImpl context, Transducer<ValueT> xducer, Accessor<BeanT,ValueT> acc) {
aoqi@0 219 this.xducer = xducer;
aoqi@0 220 this.acc = acc.optimize(context);
aoqi@0 221 }
aoqi@0 222
aoqi@0 223 public CharSequence print(BeanT bean) throws AccessorException {
aoqi@0 224 ValueT o = acc.get(bean);
aoqi@0 225 if(o==null) return null;
aoqi@0 226 return xducer.print(o);
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 public void parse(BeanT bean, CharSequence lexical) throws AccessorException, SAXException {
aoqi@0 230 acc.set(bean,xducer.parse(lexical));
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 public boolean hasValue(BeanT bean) throws AccessorException {
aoqi@0 234 return acc.getUnadapted(bean)!=null;
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 @Override
aoqi@0 238 public void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws SAXException, AccessorException, IOException, XMLStreamException {
aoqi@0 239 xducer.writeLeafElement(w,tagName,acc.get(o),fieldName);
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 @Override
aoqi@0 243 public void writeText(XMLSerializer w, BeanT o, String fieldName) throws AccessorException, SAXException, IOException, XMLStreamException {
aoqi@0 244 xducer.writeText(w,acc.get(o),fieldName);
aoqi@0 245 }
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 /**
aoqi@0 249 * {@link TransducedAccessor} for IDREF.
aoqi@0 250 *
aoqi@0 251 * BeanT: the type of the bean that contains this the IDREF field.
aoqi@0 252 * TargetT: the type of the bean pointed by IDREF.
aoqi@0 253 */
aoqi@0 254 private static final class IDREFTransducedAccessorImpl<BeanT,TargetT> extends DefaultTransducedAccessor<BeanT> {
aoqi@0 255 private final Accessor<BeanT,TargetT> acc;
aoqi@0 256 /**
aoqi@0 257 * The object that an IDREF resolves to should be
aoqi@0 258 * assignable to this type.
aoqi@0 259 */
aoqi@0 260 private final Class<TargetT> targetType;
aoqi@0 261
aoqi@0 262 public IDREFTransducedAccessorImpl(Accessor<BeanT, TargetT> acc) {
aoqi@0 263 this.acc = acc;
aoqi@0 264 this.targetType = acc.getValueType();
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 public String print(BeanT bean) throws AccessorException, SAXException {
aoqi@0 268 TargetT target = acc.get(bean);
aoqi@0 269 if(target==null) return null;
aoqi@0 270
aoqi@0 271 XMLSerializer w = XMLSerializer.getInstance();
aoqi@0 272 try {
aoqi@0 273 String id = w.grammar.getBeanInfo(target,true).getId(target,w);
aoqi@0 274 if(id==null)
aoqi@0 275 w.errorMissingId(target);
aoqi@0 276 return id;
aoqi@0 277 } catch (JAXBException e) {
aoqi@0 278 w.reportError(null,e);
aoqi@0 279 return null;
aoqi@0 280 }
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 private void assign( BeanT bean, TargetT t, UnmarshallingContext context ) throws AccessorException {
aoqi@0 284 if(!targetType.isInstance(t))
aoqi@0 285 context.handleError(Messages.UNASSIGNABLE_TYPE.format(targetType,t.getClass()));
aoqi@0 286 else
aoqi@0 287 acc.set(bean,t);
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 public void parse(final BeanT bean, CharSequence lexical) throws AccessorException, SAXException {
aoqi@0 291 final String idref = WhiteSpaceProcessor.trim(lexical).toString();
aoqi@0 292 final UnmarshallingContext context = UnmarshallingContext.getInstance();
aoqi@0 293
aoqi@0 294 final Callable callable = context.getObjectFromId(idref,acc.valueType);
aoqi@0 295 if(callable==null) {
aoqi@0 296 // the IDResolver decided to abort it now
aoqi@0 297 context.errorUnresolvedIDREF(bean,idref,context.getLocator());
aoqi@0 298 return;
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 TargetT t;
aoqi@0 302 try {
aoqi@0 303 t = (TargetT)callable.call();
aoqi@0 304 } catch (SAXException e) {// from callable.call
aoqi@0 305 throw e;
aoqi@0 306 } catch (RuntimeException e) {// from callable.call
aoqi@0 307 throw e;
aoqi@0 308 } catch (Exception e) {// from callable.call
aoqi@0 309 throw new SAXException2(e);
aoqi@0 310 }
aoqi@0 311 if(t!=null) {
aoqi@0 312 assign(bean,t,context);
aoqi@0 313 } else {
aoqi@0 314 // try again later
aoqi@0 315 final LocatorEx loc = new LocatorEx.Snapshot(context.getLocator());
aoqi@0 316 context.addPatcher(new Patcher() {
aoqi@0 317 public void run() throws SAXException {
aoqi@0 318 try {
aoqi@0 319 TargetT t = (TargetT)callable.call();
aoqi@0 320 if(t==null) {
aoqi@0 321 context.errorUnresolvedIDREF(bean,idref,loc);
aoqi@0 322 } else {
aoqi@0 323 assign(bean,t,context);
aoqi@0 324 }
aoqi@0 325 } catch (AccessorException e) {
aoqi@0 326 context.handleError(e);
aoqi@0 327 } catch (SAXException e) {// from callable.call
aoqi@0 328 throw e;
aoqi@0 329 } catch (RuntimeException e) {// from callable.call
aoqi@0 330 throw e;
aoqi@0 331 } catch (Exception e) {// from callable.call
aoqi@0 332 throw new SAXException2(e);
aoqi@0 333 }
aoqi@0 334 }
aoqi@0 335 });
aoqi@0 336 }
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 public boolean hasValue(BeanT bean) throws AccessorException {
aoqi@0 340 return acc.get(bean)!=null;
aoqi@0 341 }
aoqi@0 342 }
aoqi@0 343 }

mercurial