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