aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.xjc.model; aoqi@0: aoqi@0: import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; aoqi@0: import javax.xml.bind.annotation.adapters.NormalizedStringAdapter; aoqi@0: import javax.xml.bind.annotation.adapters.XmlAdapter; aoqi@0: aoqi@0: import com.sun.codemodel.internal.JClass; aoqi@0: import com.sun.tools.internal.xjc.model.nav.EagerNClass; aoqi@0: import com.sun.tools.internal.xjc.model.nav.NClass; aoqi@0: import com.sun.tools.internal.xjc.model.nav.NType; aoqi@0: import com.sun.tools.internal.xjc.model.nav.NavigatorImpl; aoqi@0: import com.sun.tools.internal.xjc.outline.Aspect; aoqi@0: import com.sun.tools.internal.xjc.outline.Outline; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.Adapter; aoqi@0: aoqi@0: /** aoqi@0: * Extended {@link Adapter} for use within XJC. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public final class CAdapter extends Adapter { aoqi@0: aoqi@0: /** aoqi@0: * If non-null, the same as {@link #adapterType} but more conveniently typed. aoqi@0: */ aoqi@0: private JClass adapterClass1; aoqi@0: aoqi@0: /** aoqi@0: * If non-null, the same as {@link #adapterType} but more conveniently typed. aoqi@0: */ aoqi@0: private Class adapterClass2; aoqi@0: aoqi@0: /** aoqi@0: * When the adapter class is statically known to us. aoqi@0: * aoqi@0: * @param copy aoqi@0: * true to copy the adapter class into the user package, aoqi@0: * or otherwise just refer to the class specified via the aoqi@0: * adapter parameter. aoqi@0: */ aoqi@0: public CAdapter(Class adapter, boolean copy) { aoqi@0: super(getRef(adapter,copy),NavigatorImpl.theInstance); aoqi@0: this.adapterClass1 = null; aoqi@0: this.adapterClass2 = adapter; aoqi@0: } aoqi@0: aoqi@0: static NClass getRef( final Class adapter, boolean copy ) { aoqi@0: if(copy) { aoqi@0: // TODO: this is a hack. the code generation should be defered until aoqi@0: // the backend. (right now constant generation happens in the front-end) aoqi@0: return new EagerNClass(adapter) { aoqi@0: @Override aoqi@0: public JClass toType(Outline o, Aspect aspect) { aoqi@0: return o.addRuntime(adapter); aoqi@0: } aoqi@0: public String fullName() { aoqi@0: // TODO: implement this method later aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: }; aoqi@0: } else { aoqi@0: return NavigatorImpl.theInstance.ref(adapter); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public CAdapter(JClass adapter) { aoqi@0: super( NavigatorImpl.theInstance.ref(adapter), NavigatorImpl.theInstance); aoqi@0: this.adapterClass1 = adapter; aoqi@0: this.adapterClass2 = null; aoqi@0: } aoqi@0: aoqi@0: public JClass getAdapterClass(Outline o) { aoqi@0: if(adapterClass1==null) aoqi@0: adapterClass1 = o.getCodeModel().ref(adapterClass2); aoqi@0: return adapterType.toType(o, Aspect.EXPOSED); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns true if the adapter is for whitespace normalization. aoqi@0: * Such an adapter can be ignored when producing a list. aoqi@0: */ aoqi@0: public boolean isWhitespaceAdapter() { aoqi@0: return adapterClass2==CollapsedStringAdapter.class || adapterClass2==NormalizedStringAdapter.class; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the adapter class if the adapter type is statically known to XJC. aoqi@0: *

aoqi@0: * This method is mostly for enabling certain optimized code generation. aoqi@0: */ aoqi@0: public Class getAdapterIfKnown() { aoqi@0: return adapterClass2; aoqi@0: } aoqi@0: }