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; 17 18 import com.google.common.base.Function; 19 import java.util.Iterator; 20 import javax.lang.model.type.DeclaredType; 21 import javax.lang.model.type.TypeMirror; 22 import javax.lang.model.util.SimpleTypeVisitor6; 23 24 import static com.google.common.base.CaseFormat.LOWER_CAMEL; 25 import static com.google.common.base.CaseFormat.UPPER_CAMEL; 26 27 /** 28 * Suggests a variable name for a type based on a {@link Key}. Prefer 29 * {@link DependencyVariableNamer} for cases where a specific {@link DependencyRequest} is present. 30 * 31 * @author Gregory Kick 32 * @since 2.0 33 */ 34 enum KeyVariableNamer implements Function<Key, String> { 35 INSTANCE; 36 37 @Override apply(Key key)38 public String apply(Key key) { 39 StringBuilder builder = new StringBuilder(); 40 41 if (key.qualifier().isPresent()) { 42 // TODO(gak): Use a better name for fields with qualifiers with members. 43 builder.append(key.qualifier().get().getAnnotationType().asElement().getSimpleName()); 44 } 45 46 key.type().accept(new SimpleTypeVisitor6<Void, StringBuilder>() { 47 @Override 48 public Void visitDeclared(DeclaredType t, StringBuilder builder) { 49 builder.append(t.asElement().getSimpleName()); 50 Iterator<? extends TypeMirror> argumentIterator = t.getTypeArguments().iterator(); 51 if (argumentIterator.hasNext()) { 52 builder.append("Of"); 53 TypeMirror first = argumentIterator.next(); 54 first.accept(this, builder); 55 while (argumentIterator.hasNext()) { 56 builder.append("And"); 57 argumentIterator.next().accept(this, builder); 58 } 59 } 60 return null; 61 } 62 }, builder); 63 64 return UPPER_CAMEL.to(LOWER_CAMEL, builder.toString()); 65 } 66 } 67