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.model; 27 28 import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; 29 import javax.xml.bind.annotation.adapters.NormalizedStringAdapter; 30 import javax.xml.bind.annotation.adapters.XmlAdapter; 31 32 import com.sun.codemodel.internal.JClass; 33 import com.sun.tools.internal.xjc.model.nav.EagerNClass; 34 import com.sun.tools.internal.xjc.model.nav.NClass; 35 import com.sun.tools.internal.xjc.model.nav.NType; 36 import com.sun.tools.internal.xjc.model.nav.NavigatorImpl; 37 import com.sun.tools.internal.xjc.outline.Outline; 38 import com.sun.xml.internal.bind.v2.model.core.Adapter; 39 40 /** 41 * Extended {@link Adapter} for use within XJC. 42 * 43 * @author Kohsuke Kawaguchi 44 */ 45 public final class CAdapter extends Adapter<NType,NClass> { 46 47 /** 48 * If non-null, the same as {@link #adapterType} but more conveniently typed. 49 */ 50 private JClass adapterClass1; 51 52 /** 53 * If non-null, the same as {@link #adapterType} but more conveniently typed. 54 */ 55 private Class<? extends XmlAdapter> adapterClass2; 56 57 /** 58 * When the adapter class is statically known to us. 59 * 60 * @param copy 61 * true to copy the adapter class into the user package, 62 * or otherwise just refer to the class specified via the 63 * adapter parameter. 64 */ 65 public CAdapter(Class<? extends XmlAdapter> adapter, boolean copy) { 66 super(getRef(adapter,copy),NavigatorImpl.theInstance); 67 this.adapterClass1 = null; 68 this.adapterClass2 = adapter; 69 } 70 71 static NClass getRef( final Class<? extends XmlAdapter> adapter, boolean copy ) { 72 if(copy) { 73 // TODO: this is a hack. the code generation should be defered until 74 // the backend. (right now constant generation happens in the front-end) 75 return new EagerNClass(adapter) { 76 @Override 77 public JClass toType(Outline o, Aspect aspect) { 78 return o.addRuntime(adapter); 79 } 80 public String fullName() { 81 // TODO: implement this method later 82 throw new UnsupportedOperationException(); 83 } 84 }; 85 } else { 86 return NavigatorImpl.theInstance.ref(adapter); 87 } 88 } 89 90 public CAdapter(JClass adapter) { 91 super( NavigatorImpl.theInstance.ref(adapter), NavigatorImpl.theInstance); 92 this.adapterClass1 = adapter; 93 this.adapterClass2 = null; 94 } 95 96 public JClass getAdapterClass(Outline o) { 97 if(adapterClass1==null) 98 adapterClass1 = o.getCodeModel().ref(adapterClass2); 99 return adapterType.toType(o, Aspect.EXPOSED); 100 } 101 102 /** 103 * Returns true if the adapter is for whitespace normalization. 104 * Such an adapter can be ignored when producing a list. 105 */ 106 public boolean isWhitespaceAdapter() { 107 return adapterClass2==CollapsedStringAdapter.class || adapterClass2==NormalizedStringAdapter.class; 108 } 109 110 /** 111 * Returns the adapter class if the adapter type is statically known to XJC. 112 * <p> 113 * This method is mostly for enabling certain optimized code generation. 114 */ 115 public Class<? extends XmlAdapter> getAdapterIfKnown() { 116 return adapterClass2; 117 } 118 }