
com.github.nill14.utils.init.inject.ReflectionUtils Maven / Gradle / Ivy
The newest version!
package com.github.nill14.utils.init.inject;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import javax.inject.Provider;
import com.github.nill14.utils.init.api.BindingKey;
import com.github.nill14.utils.init.binding.Binder;
import com.github.nill14.utils.init.binding.impl.Binding;
import com.github.nill14.utils.init.binding.target.ProvidesMethodBindingTarget;
import com.github.nill14.utils.init.meta.AnnotationScanner;
import com.github.nill14.utils.init.meta.Provides;
import com.github.nill14.utils.init.scope.IScopeStrategy;
import com.github.nill14.utils.init.scope.ScopeStrategies;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
public enum ReflectionUtils {
;
@SuppressWarnings({ "unchecked" })
public static TypeToken getProviderReturnTypeToken(Class extends Provider extends T>> providerClass) {
try {
return (TypeToken) TypeToken.of(providerClass.getMethod("get").getGenericReturnType());
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings({ "unchecked" })
public static TypeToken getProviderReturnTypeToken(Provider extends T> provider) {
return getProviderReturnTypeToken((Class extends Provider extends T>>) provider.getClass());
}
@SuppressWarnings({ "unchecked" })
public static TypeToken getProviderReturnTypeToken(TypeToken extends Provider extends T>> providerType) {
return getProviderReturnTypeToken((Class extends Provider extends T>>)providerType.getRawType());
}
public static boolean isClassPresent(String name) {
try {
Class.forName(name);
return true;
} catch(ClassNotFoundException e) {
return false;
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List> scanProvidesBindings(Binder binder, Object module) {
//avoid confusion with importing the wrong import
boolean isGuicePresent = ReflectionUtils.isClassPresent("com.google.inject.Provides");
List> result = Lists.newArrayList();
Stream stream = ReflectionUtils.getInstanceMethods(module.getClass());
Iterable iterable = stream::iterator;
for (Method m : iterable) {
if (m.isAnnotationPresent(Provides.class) || (isGuicePresent && OptionalGuiceDependency.isGuiceProvidesPresent(m))) {
TypeToken typeToken = TypeToken.of(m.getGenericReturnType());
ProvidesMethodBindingTarget
© 2015 - 2025 Weber Informatics LLC | Privacy Policy