src/share/jaxws_classes/com/sun/tools/internal/xjc/api/impl/s2j/TypeAndAnnotationImpl.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/api/impl/s2j/TypeAndAnnotationImpl.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,113 @@
     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.api.impl.s2j;
    1.30 +
    1.31 +import javax.xml.bind.annotation.XmlAttachmentRef;
    1.32 +import javax.xml.bind.annotation.XmlList;
    1.33 +
    1.34 +import com.sun.codemodel.internal.JAnnotatable;
    1.35 +import com.sun.codemodel.internal.JPrimitiveType;
    1.36 +import com.sun.codemodel.internal.JType;
    1.37 +import com.sun.tools.internal.xjc.api.TypeAndAnnotation;
    1.38 +import com.sun.tools.internal.xjc.generator.annotation.spec.XmlJavaTypeAdapterWriter;
    1.39 +import com.sun.tools.internal.xjc.model.CAdapter;
    1.40 +import com.sun.tools.internal.xjc.model.TypeUse;
    1.41 +import com.sun.tools.internal.xjc.model.nav.NType;
    1.42 +import static com.sun.tools.internal.xjc.outline.Aspect.EXPOSED;
    1.43 +import com.sun.tools.internal.xjc.outline.Outline;
    1.44 +import com.sun.xml.internal.bind.v2.runtime.SwaRefAdapter;
    1.45 +
    1.46 +/**
    1.47 + * {@link TypeAndAnnotation} implementation.
    1.48 + *
    1.49 + * @author Kohsuke Kawaguchi
    1.50 + */
    1.51 +final class TypeAndAnnotationImpl implements TypeAndAnnotation {
    1.52 +    private final TypeUse typeUse;
    1.53 +    private final Outline outline;
    1.54 +
    1.55 +    public TypeAndAnnotationImpl(Outline outline, TypeUse typeUse) {
    1.56 +        this.typeUse = typeUse;
    1.57 +        this.outline = outline;
    1.58 +    }
    1.59 +
    1.60 +    public JType getTypeClass() {
    1.61 +        CAdapter a = typeUse.getAdapterUse();
    1.62 +        NType nt;
    1.63 +        if(a!=null)
    1.64 +            nt = a.customType;
    1.65 +        else
    1.66 +            nt = typeUse.getInfo().getType();
    1.67 +
    1.68 +        JType jt = nt.toType(outline,EXPOSED);
    1.69 +
    1.70 +        JPrimitiveType prim = jt.boxify().getPrimitiveType();
    1.71 +        if(!typeUse.isCollection() && prim!=null)
    1.72 +            jt = prim;
    1.73 +
    1.74 +        if(typeUse.isCollection())
    1.75 +            jt = jt.array();
    1.76 +
    1.77 +        return jt;
    1.78 +    }
    1.79 +
    1.80 +    public void annotate(JAnnotatable programElement) {
    1.81 +        if(typeUse.getAdapterUse()==null && !typeUse.isCollection())
    1.82 +            return; // nothing
    1.83 +
    1.84 +        CAdapter adapterUse = typeUse.getAdapterUse();
    1.85 +        if(adapterUse!=null) {
    1.86 +            // ugly, ugly hack
    1.87 +            if(adapterUse.getAdapterIfKnown()== SwaRefAdapter.class) {
    1.88 +                programElement.annotate(XmlAttachmentRef.class);
    1.89 +            } else {
    1.90 +                // [RESULT]
    1.91 +                // @XmlJavaTypeAdapter( Foo.class )
    1.92 +                programElement.annotate2(XmlJavaTypeAdapterWriter.class).value(
    1.93 +                    adapterUse.adapterType.toType(outline,EXPOSED));
    1.94 +            }
    1.95 +        }
    1.96 +        if(typeUse.isCollection())
    1.97 +            programElement.annotate(XmlList.class);
    1.98 +    }
    1.99 +
   1.100 +    public String toString() {
   1.101 +        StringBuilder builder = new StringBuilder();
   1.102 +        // TODO: support annotations
   1.103 +        builder.append(getTypeClass());
   1.104 +        return builder.toString();
   1.105 +    }
   1.106 +
   1.107 +    public boolean equals(Object o) {
   1.108 +        if (!(o instanceof TypeAndAnnotationImpl)) return false;
   1.109 +        TypeAndAnnotationImpl that = (TypeAndAnnotationImpl) o;
   1.110 +        return this.typeUse==that.typeUse;
   1.111 +    }
   1.112 +
   1.113 +    public int hashCode() {
   1.114 +        return typeUse.hashCode();
   1.115 +    }
   1.116 +}

mercurial