Java tutorial
/* * Copyright (C) 2014 The Dagger Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package dagger.internal.codegen; import static com.google.common.base.Preconditions.checkArgument; import com.google.common.base.Ascii; import com.google.common.base.CaseFormat; import com.google.common.base.Function; import dagger.Lazy; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.inject.Provider; /** * Picks a reasonable name for what we think is being provided from the variable name associated * with the {@link DependencyRequest}. I.e. strips out words like "lazy" and "provider" if we * believe that those refer to {@link Lazy} and {@link Provider} rather than the type being * provided. * * @author Gregory Kick * @since 2.0 */ //TODO(gak): develop the heuristics to get better names final class DependencyVariableNamer implements Function<DependencyRequest, String> { private static final Pattern LAZY_PROVIDER_PATTERN = Pattern.compile("lazy(\\w+)Provider"); @Override public String apply(DependencyRequest dependency) { if (dependency.overriddenVariableName().isPresent()) { return dependency.overriddenVariableName().get(); } checkArgument(dependency.requestElement().isPresent()); String variableName = dependency.requestElement().get().getSimpleName().toString(); if (Ascii.isUpperCase(variableName.charAt(0))) { variableName = toLowerCamel(variableName); } switch (dependency.kind()) { case INSTANCE: return variableName; case LAZY: return variableName.startsWith("lazy") && !variableName.equals("lazy") ? toLowerCamel(variableName.substring(4)) : variableName; case PROVIDER_OF_LAZY: Matcher matcher = LAZY_PROVIDER_PATTERN.matcher(variableName); if (matcher.matches()) { return toLowerCamel(matcher.group(1)); } // fall through case PROVIDER: return variableName.endsWith("Provider") && !variableName.equals("Provider") ? variableName.substring(0, variableName.length() - 8) : variableName; case MEMBERS_INJECTOR: return variableName.endsWith("MembersInjector") && !variableName.equals("MembersInjector") ? variableName.substring(0, variableName.length() - 15) : variableName; case PRODUCED: return variableName.startsWith("produced") && !variableName.equals("produced") ? toLowerCamel(variableName.substring(8)) : variableName; case PRODUCER: return variableName.endsWith("Producer") && !variableName.equals("Producer") ? variableName.substring(0, variableName.length() - 8) : variableName; default: throw new AssertionError(); } } private String toLowerCamel(String name) { return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, name); } }