• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Dagger Authors.
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 
17 package dagger.internal.codegen.binding;
18 
19 import static dagger.internal.codegen.binding.SourceFiles.simpleVariableName;
20 import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
21 
22 import com.google.common.base.Ascii;
23 import com.google.common.base.CaseFormat;
24 import dagger.internal.codegen.model.DependencyRequest;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 
28 /**
29  * Picks a reasonable name for what we think is being provided from the variable name associated
30  * with the {@link DependencyRequest}. I.e. strips out words like "lazy" and "provider" if we
31  * believe that those refer to {@link dagger.Lazy} and {@link javax.inject.Provider} rather than the
32  * type being provided.
33  */
34 // TODO(gak): develop the heuristics to get better names
35 final class DependencyVariableNamer {
36   private static final Pattern LAZY_PROVIDER_PATTERN = Pattern.compile("lazy(\\w+)Provider");
37 
name(DependencyRequest dependency)38   static String name(DependencyRequest dependency) {
39     if (!dependency.requestElement().isPresent()) {
40       return simpleVariableName(dependency.key().type().xprocessing().getTypeElement());
41     }
42 
43     String variableName = getSimpleName(dependency.requestElement().get().xprocessing());
44     if (Ascii.isUpperCase(variableName.charAt(0))) {
45       variableName = toLowerCamel(variableName);
46     }
47     switch (dependency.kind()) {
48       case INSTANCE:
49         return variableName;
50       case LAZY:
51         return variableName.startsWith("lazy") && !variableName.equals("lazy")
52             ? toLowerCamel(variableName.substring(4))
53             : variableName;
54       case PROVIDER_OF_LAZY:
55         Matcher matcher = LAZY_PROVIDER_PATTERN.matcher(variableName);
56         if (matcher.matches()) {
57           return toLowerCamel(matcher.group(1));
58         }
59         // fall through
60       case PROVIDER:
61         return variableName.endsWith("Provider") && !variableName.equals("Provider")
62             ? variableName.substring(0, variableName.length() - 8)
63             : variableName;
64       case PRODUCED:
65         return variableName.startsWith("produced") && !variableName.equals("produced")
66             ? toLowerCamel(variableName.substring(8))
67             : variableName;
68       case PRODUCER:
69         return variableName.endsWith("Producer") && !variableName.equals("Producer")
70             ? variableName.substring(0, variableName.length() - 8)
71             : variableName;
72       default:
73         throw new AssertionError();
74     }
75   }
76 
toLowerCamel(String name)77   private static String toLowerCamel(String name) {
78     return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, name);
79   }
80 }
81