1 /*
   2  * Copyright (c) 1997, 2013, 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.nav;
  27 
  28 import java.lang.reflect.Type;
  29 import java.util.Collection;
  30 
  31 import com.sun.codemodel.internal.JClass;
  32 import com.sun.xml.internal.bind.v2.model.nav.Navigator;
  33 import com.sun.xml.internal.bind.v2.runtime.Location;
  34 
  35 /**
  36  * {@link Navigator} implementation for XJC.
  37  *
  38  * Most of the Navigator methods are used for parsing the model, which doesn't happen
  39  * in XJC. So Most of the methods aren't really implemented. Implementations should
  40  * be filled in as needed.
  41  *
  42  * @author Kohsuke Kawaguchi
  43  */
  44 public final class NavigatorImpl implements Navigator<NType,NClass,Void,Void> {
  45     public static final NavigatorImpl theInstance = new NavigatorImpl();
  46 
  47     private NavigatorImpl() {
  48     }
  49 
  50     public NClass getSuperClass(NClass nClass) {
  51         throw new UnsupportedOperationException();
  52     }
  53 
  54     public NType getBaseClass(NType nt, NClass base) {
  55         if(nt instanceof EagerNType) {
  56             EagerNType ent = (EagerNType) nt;
  57             if (base instanceof EagerNClass) {
  58                 EagerNClass enc = (EagerNClass) base;
  59                 return create(REFLECTION.getBaseClass(ent.t, enc.c));
  60             }
  61             // lazy class can never be a base type of an eager type
  62             return null;
  63         }
  64         if (nt instanceof NClassByJClass) {
  65             NClassByJClass nnt = (NClassByJClass) nt;
  66             if (base instanceof EagerNClass) {
  67                 EagerNClass enc = (EagerNClass) base;
  68                 return ref(nnt.clazz.getBaseClass(enc.c));
  69             }
  70         }
  71 
  72         throw new UnsupportedOperationException();
  73     }
  74 
  75     public String getClassName(NClass nClass) {
  76         throw new UnsupportedOperationException();
  77     }
  78 
  79     public String getTypeName(NType type) {
  80         return type.fullName();
  81     }
  82 
  83     public String getClassShortName(NClass nClass) {
  84         throw new UnsupportedOperationException();
  85     }
  86 
  87     public Collection<? extends Void> getDeclaredFields(NClass nClass) {
  88         throw new UnsupportedOperationException();
  89     }
  90 
  91     public Void getDeclaredField(NClass clazz, String fieldName) {
  92         throw new UnsupportedOperationException();
  93     }
  94 
  95     public Collection<? extends Void> getDeclaredMethods(NClass nClass) {
  96         throw new UnsupportedOperationException();
  97     }
  98 
  99     public NClass getDeclaringClassForField(Void aVoid) {
 100         throw new UnsupportedOperationException();
 101     }
 102 
 103     public NClass getDeclaringClassForMethod(Void aVoid) {
 104         throw new UnsupportedOperationException();
 105     }
 106 
 107     public NType getFieldType(Void aVoid) {
 108         throw new UnsupportedOperationException();
 109     }
 110 
 111     public String getFieldName(Void aVoid) {
 112         throw new UnsupportedOperationException();
 113     }
 114 
 115     public String getMethodName(Void aVoid) {
 116         throw new UnsupportedOperationException();
 117     }
 118 
 119     public NType getReturnType(Void aVoid) {
 120         throw new UnsupportedOperationException();
 121     }
 122 
 123     public NType[] getMethodParameters(Void aVoid) {
 124         throw new UnsupportedOperationException();
 125     }
 126 
 127     public boolean isStaticMethod(Void aVoid) {
 128         throw new UnsupportedOperationException();
 129     }
 130 
 131     public boolean isFinalMethod(Void aVoid) {
 132         throw new UnsupportedOperationException();
 133     }
 134 
 135     public boolean isSubClassOf(NType sub, NType sup) {
 136         throw new UnsupportedOperationException();
 137     }
 138 
 139     public NClass ref(Class c) {
 140         return create(c);
 141     }
 142 
 143     public NClass ref(JClass c) {
 144         if(c==null)     return null;
 145         return new NClassByJClass(c);
 146     }
 147 
 148     public NType use(NClass nc) {
 149         return nc;
 150     }
 151 
 152     public NClass asDecl(NType nt) {
 153         if(nt instanceof NClass)
 154             return (NClass)nt;
 155         else
 156             return null;
 157     }
 158 
 159     public NClass asDecl(Class c) {
 160         return ref(c);
 161     }
 162 
 163     public boolean isArray(NType nType) {
 164         throw new UnsupportedOperationException();
 165     }
 166 
 167     public boolean isArrayButNotByteArray(NType t) {
 168         throw new UnsupportedOperationException();
 169     }
 170 
 171 
 172     public NType getComponentType(NType nType) {
 173         throw new UnsupportedOperationException();
 174     }
 175 
 176     public NType getTypeArgument(NType nt, int i) {
 177         if (nt instanceof EagerNType) {
 178             EagerNType ent = (EagerNType) nt;
 179             return create(REFLECTION.getTypeArgument(ent.t,i));
 180         }
 181         if (nt instanceof NClassByJClass) {
 182             NClassByJClass nnt = (NClassByJClass) nt;
 183             return ref(nnt.clazz.getTypeParameters().get(i));
 184         }
 185 
 186         throw new UnsupportedOperationException();
 187     }
 188 
 189     public boolean isParameterizedType(NType nt) {
 190         if (nt instanceof EagerNType) {
 191             EagerNType ent = (EagerNType) nt;
 192             return REFLECTION.isParameterizedType(ent.t);
 193         }
 194         if (nt instanceof NClassByJClass) {
 195             NClassByJClass nnt = (NClassByJClass) nt;
 196             return nnt.clazz.isParameterized();
 197         }
 198 
 199         throw new UnsupportedOperationException();
 200     }
 201 
 202     public boolean isPrimitive(NType type) {
 203         throw new UnsupportedOperationException();
 204     }
 205 
 206     public NType getPrimitive(Class primitiveType) {
 207         return create(primitiveType);
 208     }
 209 
 210     @SuppressWarnings("FinalStaticMethod")
 211     public static final NType create(Type t) {
 212         if(t==null)     return null;
 213         if(t instanceof Class)
 214             return create((Class)t);
 215 
 216         return new EagerNType(t);
 217     }
 218 
 219     public static NClass create( Class c ) {
 220         if(c==null)     return null;
 221         return new EagerNClass(c);
 222     }
 223 
 224     /**
 225      * Creates a {@link NType} representation for a parameterized type
 226      * {@code RawType&lt;ParamType1,ParamType2,...> }.
 227      */
 228     public static NType createParameterizedType( NClass rawType, NType... args ) {
 229         return new NParameterizedType(rawType,args);
 230     }
 231 
 232     public static NType createParameterizedType( Class rawType, NType... args ) {
 233         return new NParameterizedType(create(rawType),args);
 234     }
 235 
 236     public Location getClassLocation(final NClass c) {
 237         // not really needed for XJC but doesn't hurt to have one
 238         return new Location() {
 239             @Override
 240             public String toString() {
 241                 return c.fullName();
 242             }
 243         };
 244     }
 245 
 246     public Location getFieldLocation(Void v) {
 247         throw new IllegalStateException();
 248     }
 249 
 250     public Location getMethodLocation(Void v) {
 251         throw new IllegalStateException();
 252     }
 253 
 254     public boolean hasDefaultConstructor(NClass nClass) {
 255         throw new UnsupportedOperationException();
 256     }
 257 
 258     public boolean isStaticField(Void aVoid) {
 259         throw new IllegalStateException();
 260     }
 261 
 262     public boolean isPublicMethod(Void aVoid) {
 263         throw new IllegalStateException();
 264     }
 265 
 266     public boolean isPublicField(Void aVoid) {
 267         throw new IllegalStateException();
 268     }
 269 
 270     public boolean isEnum(NClass c) {
 271         return isSubClassOf(c,create(Enum.class));
 272     }
 273 
 274     public <T> NType erasure(NType type) {
 275         if(type instanceof NParameterizedType) {
 276             NParameterizedType pt = (NParameterizedType) type;
 277             return pt.rawType;
 278         }
 279         return type;
 280     }
 281 
 282     public boolean isAbstract(NClass clazz) {
 283         return clazz.isAbstract();
 284     }
 285 
 286     /**
 287      * @deprecated
 288      *      no class generated by XJC is final.
 289      */
 290     public boolean isFinal(NClass clazz) {
 291         return false;
 292     }
 293 
 294     public Void[] getEnumConstants(NClass clazz) {
 295         throw new UnsupportedOperationException();
 296     }
 297 
 298     public NType getVoidType() {
 299         return ref(void.class);
 300     }
 301 
 302     public String getPackageName(NClass clazz) {
 303         // TODO: implement this method later
 304         throw new UnsupportedOperationException();
 305     }
 306 
 307     public NClass findClass(String className, NClass referencePoint) {
 308         // TODO: implement this method later
 309         throw new UnsupportedOperationException();
 310     }
 311 
 312     public boolean isBridgeMethod(Void method) {
 313         throw new UnsupportedOperationException();
 314     }
 315 
 316     public boolean isOverriding(Void method,NClass clazz) {
 317         throw new UnsupportedOperationException();
 318     }
 319 
 320     public boolean isInterface(NClass clazz) {
 321         throw new UnsupportedOperationException();
 322     }
 323 
 324     public boolean isTransient(Void f) {
 325         throw new UnsupportedOperationException();
 326     }
 327 
 328     public boolean isInnerClass(NClass clazz) {
 329         throw new UnsupportedOperationException();
 330     }
 331 
 332     @Override
 333     public boolean isSameType(NType t1, NType t2) {
 334          throw new UnsupportedOperationException();
 335     }
 336 }