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