1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.internal.xjc.api.impl.s2j; 27 28 import javax.xml.bind.annotation.XmlAttachmentRef; 29 import javax.xml.bind.annotation.XmlList; 30 31 import com.sun.codemodel.internal.JAnnotatable; 32 import com.sun.codemodel.internal.JPrimitiveType; 33 import com.sun.codemodel.internal.JType; 34 import com.sun.tools.internal.xjc.api.TypeAndAnnotation; 35 import com.sun.tools.internal.xjc.generator.annotation.spec.XmlJavaTypeAdapterWriter; 36 import com.sun.tools.internal.xjc.model.CAdapter; 37 import com.sun.tools.internal.xjc.model.TypeUse; 38 import com.sun.tools.internal.xjc.model.nav.NType; 39 import static com.sun.tools.internal.xjc.model.Aspect.EXPOSED; 40 import com.sun.tools.internal.xjc.outline.Outline; 41 import com.sun.xml.internal.bind.v2.runtime.SwaRefAdapterMarker; 42 43 /** 44 * {@link TypeAndAnnotation} implementation. 45 * 46 * @author Kohsuke Kawaguchi 47 */ 48 final class TypeAndAnnotationImpl implements TypeAndAnnotation { 49 private final TypeUse typeUse; 50 private final Outline outline; 51 52 public TypeAndAnnotationImpl(Outline outline, TypeUse typeUse) { 53 this.typeUse = typeUse; 54 this.outline = outline; 55 } 56 57 public JType getTypeClass() { 58 CAdapter a = typeUse.getAdapterUse(); 59 NType nt; 60 if(a!=null) 61 nt = a.customType; 62 else 63 nt = typeUse.getInfo().getType(); 64 65 JType jt = nt.toType(outline,EXPOSED); 66 67 JPrimitiveType prim = jt.boxify().getPrimitiveType(); 68 if(!typeUse.isCollection() && prim!=null) 69 jt = prim; 70 71 if(typeUse.isCollection()) 72 jt = jt.array(); 73 74 return jt; 75 } 76 77 public void annotate(JAnnotatable programElement) { 78 if(typeUse.getAdapterUse()==null && !typeUse.isCollection()) 79 return; // nothing 80 81 CAdapter adapterUse = typeUse.getAdapterUse(); 82 if(adapterUse!=null) { 83 // ugly, ugly hack 84 if(adapterUse.getAdapterIfKnown() == SwaRefAdapterMarker.class) { 85 programElement.annotate(XmlAttachmentRef.class); 86 } else { 87 // [RESULT] 88 // @XmlJavaTypeAdapter( Foo.class ) 89 programElement.annotate2(XmlJavaTypeAdapterWriter.class).value( 90 adapterUse.adapterType.toType(outline,EXPOSED)); 91 } 92 } 93 if(typeUse.isCollection()) 94 programElement.annotate(XmlList.class); 95 } 96 97 public String toString() { 98 StringBuilder builder = new StringBuilder(); 99 // TODO: support annotations 100 builder.append(getTypeClass()); 101 return builder.toString(); 102 } 103 104 public boolean equals(Object o) { 105 if (!(o instanceof TypeAndAnnotationImpl)) return false; 106 TypeAndAnnotationImpl that = (TypeAndAnnotationImpl) o; 107 return this.typeUse==that.typeUse; 108 } 109 110 public int hashCode() { 111 return typeUse.hashCode(); 112 } 113 }