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

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 0
373ffda63c9a
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

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.property;
aoqi@0 27
aoqi@0 28 import java.lang.reflect.Constructor;
aoqi@0 29 import java.lang.reflect.InvocationTargetException;
aoqi@0 30 import java.util.Collection;
aoqi@0 31
aoqi@0 32 import com.sun.xml.internal.bind.v2.model.core.ID;
aoqi@0 33 import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
aoqi@0 34 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeAttributePropertyInfo;
aoqi@0 35 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementPropertyInfo;
aoqi@0 36 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeNonElement;
aoqi@0 37 import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
aoqi@0 38 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfo;
aoqi@0 39 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeValuePropertyInfo;
aoqi@0 40 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
aoqi@0 41
aoqi@0 42 /**
aoqi@0 43 * Create {@link Property} objects.
aoqi@0 44 *
aoqi@0 45 * @author Kohsuke Kawaguchi (kk@kohsuke.org)
aoqi@0 46 */
aoqi@0 47 public abstract class PropertyFactory {
aoqi@0 48 private PropertyFactory() {}
aoqi@0 49
aoqi@0 50
aoqi@0 51 /**
aoqi@0 52 * Constructors of the {@link Property} implementation.
aoqi@0 53 */
aoqi@0 54 private static final Constructor<? extends Property>[] propImpls;
aoqi@0 55
aoqi@0 56 static {
aoqi@0 57 Class<? extends Property>[] implClasses = new Class[] {
aoqi@0 58 SingleElementLeafProperty.class,
aoqi@0 59 null, // single reference leaf --- but there's no such thing as "reference leaf"
aoqi@0 60 null, // no such thing as "map leaf"
aoqi@0 61
aoqi@0 62 ArrayElementLeafProperty.class,
aoqi@0 63 null, // array reference leaf --- but there's no such thing as "reference leaf"
aoqi@0 64 null, // no such thing as "map leaf"
aoqi@0 65
aoqi@0 66 SingleElementNodeProperty.class,
aoqi@0 67 SingleReferenceNodeProperty.class,
aoqi@0 68 SingleMapNodeProperty.class,
aoqi@0 69
aoqi@0 70 ArrayElementNodeProperty.class,
aoqi@0 71 ArrayReferenceNodeProperty.class,
aoqi@0 72 null, // map is always a single property (Map doesn't implement Collection)
aoqi@0 73 };
aoqi@0 74
aoqi@0 75 propImpls = new Constructor[implClasses.length];
aoqi@0 76 for( int i=0; i<propImpls.length; i++ ) {
aoqi@0 77 if(implClasses[i]!=null)
aoqi@0 78 // this pointless casting necessary for Mustang
aoqi@0 79 propImpls[i] = (Constructor)implClasses[i].getConstructors()[0];
aoqi@0 80 }
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 /**
aoqi@0 84 * Creates/obtains a properly configured {@link Property}
aoqi@0 85 * object from the given description.
aoqi@0 86 */
aoqi@0 87 public static Property create( JAXBContextImpl grammar, RuntimePropertyInfo info ) {
aoqi@0 88
aoqi@0 89 PropertyKind kind = info.kind();
aoqi@0 90
aoqi@0 91 switch(kind) {
aoqi@0 92 case ATTRIBUTE:
aoqi@0 93 return new AttributeProperty(grammar,(RuntimeAttributePropertyInfo)info);
aoqi@0 94 case VALUE:
aoqi@0 95 return new ValueProperty(grammar,(RuntimeValuePropertyInfo)info);
aoqi@0 96 case ELEMENT:
aoqi@0 97 if(((RuntimeElementPropertyInfo)info).isValueList())
aoqi@0 98 return new ListElementProperty(grammar,(RuntimeElementPropertyInfo) info);
aoqi@0 99 break;
aoqi@0 100 case REFERENCE:
aoqi@0 101 case MAP:
aoqi@0 102 break;
aoqi@0 103 default:
aoqi@0 104 assert false;
aoqi@0 105 }
aoqi@0 106
aoqi@0 107
aoqi@0 108 boolean isCollection = info.isCollection();
aoqi@0 109 boolean isLeaf = isLeaf(info);
aoqi@0 110
aoqi@0 111 Constructor<? extends Property> c = propImpls[(isLeaf?0:6)+(isCollection?3:0)+kind.propertyIndex];
aoqi@0 112 try {
aoqi@0 113 return c.newInstance( grammar, info );
aoqi@0 114 } catch (InstantiationException e) {
aoqi@0 115 throw new InstantiationError(e.getMessage());
aoqi@0 116 } catch (IllegalAccessException e) {
aoqi@0 117 throw new IllegalAccessError(e.getMessage());
aoqi@0 118 } catch (InvocationTargetException e) {
aoqi@0 119 Throwable t = e.getCause();
aoqi@0 120 if(t instanceof Error)
aoqi@0 121 throw (Error)t;
aoqi@0 122 if(t instanceof RuntimeException)
aoqi@0 123 throw (RuntimeException)t;
aoqi@0 124
aoqi@0 125 throw new AssertionError(t);
aoqi@0 126 }
aoqi@0 127 }
aoqi@0 128
aoqi@0 129 /**
aoqi@0 130 * Look for the case that can be optimized as a leaf,
aoqi@0 131 * which is a kind of type whose XML representation is just PCDATA.
aoqi@0 132 */
aoqi@0 133 static boolean isLeaf(RuntimePropertyInfo info) {
aoqi@0 134 Collection<? extends RuntimeTypeInfo> types = info.ref();
aoqi@0 135 if(types.size()!=1) return false;
aoqi@0 136
aoqi@0 137 RuntimeTypeInfo rti = types.iterator().next();
aoqi@0 138 if(!(rti instanceof RuntimeNonElement)) return false;
aoqi@0 139
aoqi@0 140 if(info.id()==ID.IDREF)
aoqi@0 141 // IDREF is always handled as leaf -- Transducer maps IDREF String back to an object
aoqi@0 142 return true;
aoqi@0 143
aoqi@0 144 if(((RuntimeNonElement)rti).getTransducer()==null)
aoqi@0 145 // Transducer!=null means definitely binds to PCDATA.
aoqi@0 146 // even if transducer==null, a referene might be IDREF,
aoqi@0 147 // in which case it will still produce PCDATA in this reference.
aoqi@0 148 return false;
aoqi@0 149
aoqi@0 150 if(!info.getIndividualType().equals(rti.getType()))
aoqi@0 151 return false;
aoqi@0 152
aoqi@0 153 return true;
aoqi@0 154 }
aoqi@0 155 }

mercurial