Home | History | Annotate | Download | only in writer
      1 /*
      2  * Copyright (C) 2014 Google, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package dagger.internal.codegen.writer;
     17 
     18 import com.google.common.base.Function;
     19 import com.google.common.collect.FluentIterable;
     20 import javax.lang.model.element.TypeElement;
     21 import javax.lang.model.type.ArrayType;
     22 import javax.lang.model.type.DeclaredType;
     23 import javax.lang.model.type.NoType;
     24 import javax.lang.model.type.NullType;
     25 import javax.lang.model.type.PrimitiveType;
     26 import javax.lang.model.type.TypeMirror;
     27 import javax.lang.model.type.TypeVariable;
     28 import javax.lang.model.type.WildcardType;
     29 import javax.lang.model.util.SimpleTypeVisitor6;
     30 
     31 public final class TypeNames {
     32   static final Function<TypeMirror, TypeName> FOR_TYPE_MIRROR =
     33       new Function<TypeMirror, TypeName>() {
     34         @Override public TypeName apply(TypeMirror input) {
     35           return forTypeMirror(input);
     36         }
     37       };
     38 
     39   public static TypeName forClass(Class<?> clazz) {
     40     if (clazz.isPrimitive()) {
     41       return PrimitiveName.forClass(clazz);
     42     } else if (void.class.equals(clazz)) {
     43       return VoidName.VOID;
     44     } else if (clazz.isArray()) {
     45       return new ArrayTypeName(forClass(clazz.getComponentType()));
     46     } else {
     47       return ClassName.fromClass(clazz);
     48     }
     49   }
     50 
     51   public static TypeName forTypeMirror(TypeMirror mirror) {
     52     return mirror.accept(new SimpleTypeVisitor6<TypeName, Void>() {
     53       @Override
     54       protected TypeName defaultAction(TypeMirror e, Void p) {
     55         throw new IllegalArgumentException(e.toString());
     56       }
     57 
     58       @Override
     59       public TypeName visitTypeVariable(TypeVariable t, Void p) {
     60         return TypeVariableName.fromTypeVariable(t);
     61       }
     62 
     63       @Override
     64       public ArrayTypeName visitArray(ArrayType t, Void p) {
     65         return new ArrayTypeName(t.getComponentType().accept(this, null));
     66       }
     67 
     68       @Override
     69       public TypeName visitDeclared(DeclaredType t, Void p) {
     70         return t.getTypeArguments().isEmpty()
     71             ? ClassName.fromTypeElement((TypeElement) t.asElement())
     72             : new ParameterizedTypeName(
     73                 ClassName.fromTypeElement((TypeElement) t.asElement()),
     74                 FluentIterable.from(t.getTypeArguments()).transform(FOR_TYPE_MIRROR));
     75       }
     76 
     77       @Override
     78       public PrimitiveName visitPrimitive(PrimitiveType t, Void p) {
     79         return PrimitiveName.forTypeMirror(t);
     80       }
     81 
     82       @Override
     83       public WildcardName visitWildcard(WildcardType t, Void p) {
     84         return WildcardName.forTypeMirror(t);
     85       }
     86 
     87       @Override
     88       public NullName visitNull(NullType t, Void p) {
     89         return NullName.NULL;
     90       }
     91 
     92       @Override
     93       public TypeName visitNoType(NoType t, Void p) {
     94         switch (t.getKind()) {
     95           case VOID:
     96             return VoidName.VOID;
     97           case PACKAGE:
     98             throw new IllegalArgumentException();
     99           default:
    100             throw new IllegalStateException();
    101         }
    102       }
    103     }, null);
    104   }
    105 
    106   private TypeNames() {
    107   }
    108 }
    109