Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2008 Google Inc.
*
* 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 com.google.inject.internal;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Objects;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.inject.Binder;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.InjectionPoint;
import com.google.inject.spi.Message;
import com.google.inject.spi.ModuleAnnotatedMethodScanner;
import com.google.inject.util.Modules;
import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Creates bindings to methods annotated with {@literal @}{@link Provides}. Use the scope and
* binding annotations on the provider method to configure the binding.
*
* @author [email protected] (Bob Lee)
* @author [email protected] (Jesse Wilson)
*/
public final class ProviderMethodsModule implements Module {
private final Object delegate;
private final TypeLiteral> typeLiteral;
private final boolean skipFastClassGeneration;
private final ModuleAnnotatedMethodScanner scanner;
private ProviderMethodsModule(
Object delegate, boolean skipFastClassGeneration, ModuleAnnotatedMethodScanner scanner) {
this.delegate = checkNotNull(delegate, "delegate");
this.typeLiteral = TypeLiteral.get(getDelegateModuleClass());
this.skipFastClassGeneration = skipFastClassGeneration;
this.scanner = scanner;
}
/** Returns a module which creates bindings for provider methods from the given module. */
public static Module forModule(Module module) {
return forObject(module, false, ProvidesMethodScanner.INSTANCE);
}
/** Returns a module which creates bindings methods in the module that match the scanner. */
public static Module forModule(Object module, ModuleAnnotatedMethodScanner scanner) {
return forObject(module, false, scanner);
}
/**
* Returns a module which creates bindings for provider methods from the given object. This is
* useful notably for GIN
*
*
This will skip bytecode generation for provider methods, since it is assumed that callers
* are only interested in Module metadata.
*/
public static Module forObject(Object object) {
return forObject(object, true, ProvidesMethodScanner.INSTANCE);
}
private static Module forObject(
Object object, boolean skipFastClassGeneration, ModuleAnnotatedMethodScanner scanner) {
// avoid infinite recursion, since installing a module always installs itself
if (object instanceof ProviderMethodsModule) {
return Modules.EMPTY_MODULE;
}
return new ProviderMethodsModule(object, skipFastClassGeneration, scanner);
}
public Class> getDelegateModuleClass() {
return isStaticModule() ? (Class>) delegate : delegate.getClass();
}
private boolean isStaticModule() {
return delegate instanceof Class;
}
@Override
public void configure(Binder binder) {
for (ProviderMethod> providerMethod : getProviderMethods(binder)) {
providerMethod.configure(binder);
}
}
public List> getProviderMethods(Binder binder) {
List> result = null;
List methodsAndAnnotations = null;
// The highest class in the type hierarchy that contained a provider method definition.
Class> superMostClass = getDelegateModuleClass();
for (Class> c = superMostClass; c != Object.class && c != null; c = c.getSuperclass()) {
for (Method method : DeclaredMembers.getDeclaredMethods(c)) {
Annotation annotation = getAnnotation(binder, method);
if (annotation != null) {
if (isStaticModule()
&& !Modifier.isStatic(method.getModifiers())
&& !Modifier.isAbstract(method.getModifiers())) {
binder
.skipSources(ProviderMethodsModule.class)
.addError(
"%s is an instance method, but a class literal was passed. Make this method"
+ " static or pass an instance of the module instead.",
method);
continue;
}
if (result == null) {
result = new ArrayList<>();
methodsAndAnnotations = new ArrayList<>();
}
ProviderMethod