1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/model/TypeUseImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,129 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.xjc.model; 1.30 + 1.31 +import javax.activation.MimeType; 1.32 +import javax.xml.bind.annotation.adapters.XmlAdapter; 1.33 + 1.34 +import com.sun.codemodel.internal.JExpr; 1.35 +import com.sun.codemodel.internal.JExpression; 1.36 +import com.sun.codemodel.internal.JStringLiteral; 1.37 +import com.sun.tools.internal.xjc.outline.Outline; 1.38 +import com.sun.xml.internal.bind.v2.ClassFactory; 1.39 +import com.sun.xml.internal.bind.v2.model.core.ID; 1.40 +import com.sun.xml.internal.xsom.XmlString; 1.41 + 1.42 + 1.43 +/** 1.44 + * General-purpose {@link TypeUse} implementation. 1.45 + * 1.46 + * @author Kohsuke Kawaguchi 1.47 + */ 1.48 +final class TypeUseImpl implements TypeUse { 1.49 + private final CNonElement coreType; 1.50 + private final boolean collection; 1.51 + private final CAdapter adapter; 1.52 + private final ID id; 1.53 + private final MimeType expectedMimeType; 1.54 + 1.55 + 1.56 + public TypeUseImpl(CNonElement itemType, boolean collection, ID id, MimeType expectedMimeType, CAdapter adapter) { 1.57 + this.coreType = itemType; 1.58 + this.collection = collection; 1.59 + this.id = id; 1.60 + this.expectedMimeType = expectedMimeType; 1.61 + this.adapter = adapter; 1.62 + } 1.63 + 1.64 + public boolean isCollection() { 1.65 + return collection; 1.66 + } 1.67 + 1.68 + public CNonElement getInfo() { 1.69 + return coreType; 1.70 + } 1.71 + 1.72 + public CAdapter getAdapterUse() { 1.73 + return adapter; 1.74 + } 1.75 + 1.76 + public ID idUse() { 1.77 + return id; 1.78 + } 1.79 + 1.80 + public MimeType getExpectedMimeType() { 1.81 + return expectedMimeType; 1.82 + } 1.83 + 1.84 + public boolean equals(Object o) { 1.85 + if (this == o) return true; 1.86 + if (!(o instanceof TypeUseImpl)) return false; 1.87 + 1.88 + final TypeUseImpl that = (TypeUseImpl) o; 1.89 + 1.90 + if (collection != that.collection) return false; 1.91 + if (this.id != that.id ) return false; 1.92 + if (adapter != null ? !adapter.equals(that.adapter) : that.adapter != null) return false; 1.93 + if (coreType != null ? !coreType.equals(that.coreType) : that.coreType != null) return false; 1.94 + 1.95 + return true; 1.96 + } 1.97 + 1.98 + public int hashCode() { 1.99 + int result; 1.100 + result = (coreType != null ? coreType.hashCode() : 0); 1.101 + result = 29 * result + (collection ? 1 : 0); 1.102 + result = 29 * result + (adapter != null ? adapter.hashCode() : 0); 1.103 + return result; 1.104 + } 1.105 + 1.106 + 1.107 + public JExpression createConstant(Outline outline, XmlString lexical) { 1.108 + if(isCollection()) return null; 1.109 + 1.110 + if(adapter==null) return coreType.createConstant(outline, lexical); 1.111 + 1.112 + // [RESULT] new Adapter().unmarshal(CONSTANT); 1.113 + JExpression cons = coreType.createConstant(outline, lexical); 1.114 + Class<? extends XmlAdapter> atype = adapter.getAdapterIfKnown(); 1.115 + 1.116 + // try to run the adapter now rather than later. 1.117 + if(cons instanceof JStringLiteral && atype!=null) { 1.118 + JStringLiteral scons = (JStringLiteral) cons; 1.119 + XmlAdapter a = ClassFactory.create(atype); 1.120 + try { 1.121 + Object value = a.unmarshal(scons.str); 1.122 + if(value instanceof String) { 1.123 + return JExpr.lit((String)value); 1.124 + } 1.125 + } catch (Exception e) { 1.126 + // assume that we can't eagerly bind this 1.127 + } 1.128 + } 1.129 + 1.130 + return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal").arg(cons); 1.131 + } 1.132 +}