src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/RawTypeSet.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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.tools.internal.xjc.reader;
aoqi@0 27
aoqi@0 28 import java.util.HashSet;
aoqi@0 29 import java.util.List;
aoqi@0 30 import java.util.Set;
aoqi@0 31
aoqi@0 32 import javax.activation.MimeType;
aoqi@0 33
aoqi@0 34 import com.sun.tools.internal.xjc.model.CElementPropertyInfo;
aoqi@0 35 import static com.sun.tools.internal.xjc.model.CElementPropertyInfo.CollectionMode.*;
aoqi@0 36 import com.sun.tools.internal.xjc.model.CReferencePropertyInfo;
aoqi@0 37 import com.sun.tools.internal.xjc.model.CTypeRef;
aoqi@0 38 import com.sun.tools.internal.xjc.model.Multiplicity;
aoqi@0 39 import com.sun.tools.internal.xjc.model.nav.NType;
aoqi@0 40 import com.sun.xml.internal.bind.v2.model.core.Element;
aoqi@0 41 import com.sun.xml.internal.bind.v2.model.core.ID;
aoqi@0 42 import java.math.BigInteger;
aoqi@0 43
aoqi@0 44 /**
aoqi@0 45 * Set of {@link Ref}.
aoqi@0 46 *
aoqi@0 47 * @author Kohsuke Kawaguchi
aoqi@0 48 */
aoqi@0 49 public final class RawTypeSet {
aoqi@0 50
aoqi@0 51
aoqi@0 52 public final Set<Ref> refs;
aoqi@0 53
aoqi@0 54 /**
aoqi@0 55 * True if this type set can form references to types.
aoqi@0 56 */
aoqi@0 57 public final Mode canBeTypeRefs;
aoqi@0 58
aoqi@0 59 /**
aoqi@0 60 * The occurence of the whole references.
aoqi@0 61 */
aoqi@0 62 public final Multiplicity mul;
aoqi@0 63
aoqi@0 64 // computed inside canBeTypeRefs()
aoqi@0 65 private CElementPropertyInfo.CollectionMode collectionMode;
aoqi@0 66
aoqi@0 67 /**
aoqi@0 68 * Should be called from one of the raw type set builders.
aoqi@0 69 */
aoqi@0 70 public RawTypeSet( Set<Ref> refs, Multiplicity m ) {
aoqi@0 71 this.refs = refs;
aoqi@0 72 mul = m;
aoqi@0 73 canBeTypeRefs = canBeTypeRefs();
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 public CElementPropertyInfo.CollectionMode getCollectionMode() {
aoqi@0 77 return collectionMode;
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 public boolean isRequired() {
aoqi@0 81 return mul.min.compareTo(BigInteger.ZERO) == 1;
aoqi@0 82 }
aoqi@0 83
aoqi@0 84
aoqi@0 85 /**
aoqi@0 86 * Represents the possible binding option for this {@link RawTypeSet}.
aoqi@0 87 */
aoqi@0 88 public enum Mode {
aoqi@0 89 /**
aoqi@0 90 * This {@link RawTypeSet} can be either an reference property or
aoqi@0 91 * an element property, and XJC recommends element property.
aoqi@0 92 */
aoqi@0 93 SHOULD_BE_TYPEREF(0),
aoqi@0 94 /**
aoqi@0 95 * This {@link RawTypeSet} can be either an reference property or
aoqi@0 96 * an element property, and XJC recommends reference property.
aoqi@0 97 */
aoqi@0 98 CAN_BE_TYPEREF(1),
aoqi@0 99 /**
aoqi@0 100 * This {@link RawTypeSet} can be only bound to a reference property.
aoqi@0 101 */
aoqi@0 102 MUST_BE_REFERENCE(2);
aoqi@0 103
aoqi@0 104 private final int rank;
aoqi@0 105
aoqi@0 106 Mode(int rank) {
aoqi@0 107 this.rank = rank;
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 Mode or(Mode that) {
aoqi@0 111 switch(Math.max(this.rank,that.rank)) {
aoqi@0 112 case 0: return SHOULD_BE_TYPEREF;
aoqi@0 113 case 1: return CAN_BE_TYPEREF;
aoqi@0 114 case 2: return MUST_BE_REFERENCE;
aoqi@0 115 }
aoqi@0 116 throw new AssertionError();
aoqi@0 117 }
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 /**
aoqi@0 121 * Returns true if {@link #refs} can form refs of types.
aoqi@0 122 *
aoqi@0 123 * If there are multiple {@link Ref}s with the same type,
aoqi@0 124 * we cannot make them into type refs. Or if any of the {@link Ref}
aoqi@0 125 * says they cannot be in type refs, we cannot do that either.
aoqi@0 126 *
aoqi@0 127 * TODO: just checking if the refs are the same is not suffice.
aoqi@0 128 * If two refs derive from each other, they cannot form a list of refs
aoqi@0 129 * (because of a possible ambiguity).
aoqi@0 130 */
aoqi@0 131 private Mode canBeTypeRefs() {
aoqi@0 132 Set<NType> types = new HashSet<NType>();
aoqi@0 133
aoqi@0 134 collectionMode = mul.isAtMostOnce()?NOT_REPEATED:REPEATED_ELEMENT;
aoqi@0 135
aoqi@0 136 // the way we compute this is that we start from the most optimistic value,
aoqi@0 137 // and then gradually degrade as we find something problematic.
aoqi@0 138 Mode mode = Mode.SHOULD_BE_TYPEREF;
aoqi@0 139
aoqi@0 140 for( Ref r : refs ) {
aoqi@0 141 mode = mode.or(r.canBeType(this));
aoqi@0 142 if(mode== Mode.MUST_BE_REFERENCE)
aoqi@0 143 return mode; // no need to continue the processing
aoqi@0 144
aoqi@0 145 if(!types.add(r.toTypeRef(null).getTarget().getType()))
aoqi@0 146 return Mode.MUST_BE_REFERENCE; // collision
aoqi@0 147 if(r.isListOfValues()) {
aoqi@0 148 if(refs.size()>1 || !mul.isAtMostOnce())
aoqi@0 149 return Mode.MUST_BE_REFERENCE; // restriction on @XmlList
aoqi@0 150 collectionMode = REPEATED_VALUE;
aoqi@0 151 }
aoqi@0 152 }
aoqi@0 153 return mode;
aoqi@0 154 }
aoqi@0 155
aoqi@0 156
aoqi@0 157
aoqi@0 158
aoqi@0 159 public void addTo(CElementPropertyInfo prop) {
aoqi@0 160 assert canBeTypeRefs!= Mode.MUST_BE_REFERENCE;
aoqi@0 161 if(mul.isZero())
aoqi@0 162 return; // the property can't have any value
aoqi@0 163
aoqi@0 164 List<CTypeRef> dst = prop.getTypes();
aoqi@0 165 for( Ref t : refs )
aoqi@0 166 dst.add(t.toTypeRef(prop));
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 public void addTo(CReferencePropertyInfo prop) {
aoqi@0 170 if(mul.isZero())
aoqi@0 171 return; // the property can't have any value
aoqi@0 172 for( Ref t : refs )
aoqi@0 173 t.toElementRef(prop);
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 public ID id() {
aoqi@0 177 for( Ref t : refs ) {
aoqi@0 178 ID id = t.id();
aoqi@0 179 if(id!=ID.NONE) return id;
aoqi@0 180 }
aoqi@0 181 return ID.NONE;
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 public MimeType getExpectedMimeType() {
aoqi@0 185 for( Ref t : refs ) {
aoqi@0 186 MimeType mt = t.getExpectedMimeType();
aoqi@0 187 if(mt!=null) return mt;
aoqi@0 188 }
aoqi@0 189 return null;
aoqi@0 190 }
aoqi@0 191
aoqi@0 192
aoqi@0 193 /**
aoqi@0 194 * A reference to something.
aoqi@0 195 *
aoqi@0 196 * <p>
aoqi@0 197 * A {@link Ref} can be either turned into {@link CTypeRef} to form
aoqi@0 198 * an element property, or {@link Element} to form a reference property.
aoqi@0 199 */
aoqi@0 200 public static abstract class Ref {
aoqi@0 201 /**
aoqi@0 202 * @param ep
aoqi@0 203 * the property to which the returned {@link CTypeRef} will be
aoqi@0 204 * added to.
aoqi@0 205 */
aoqi@0 206 protected abstract CTypeRef toTypeRef(CElementPropertyInfo ep);
aoqi@0 207 protected abstract void toElementRef(CReferencePropertyInfo prop);
aoqi@0 208 /**
aoqi@0 209 * Can this {@link Ref} be a type ref?
aoqi@0 210 * @return false to veto.
aoqi@0 211 * @param parent
aoqi@0 212 */
aoqi@0 213 protected abstract Mode canBeType(RawTypeSet parent);
aoqi@0 214 protected abstract boolean isListOfValues();
aoqi@0 215 /**
aoqi@0 216 * When this {@link RawTypeSet} binds to a {@link CElementPropertyInfo},
aoqi@0 217 * this method is used to determine if the property is ID or not.
aoqi@0 218 */
aoqi@0 219 protected abstract ID id();
aoqi@0 220
aoqi@0 221 /**
aoqi@0 222 * When this {@link RawTypeSet} binds to a {@link CElementPropertyInfo},
aoqi@0 223 * this method is used to determine if the property has an associated expected MIME type or not.
aoqi@0 224 */
aoqi@0 225 protected MimeType getExpectedMimeType() { return null; }
aoqi@0 226 }
aoqi@0 227 }

mercurial