ActiveJ学习心得——inject(5)

2021SC@SDUSC

一、代码分析内容

本次博客介绍ActiveJ中inject模块的第五个部分:util包。util包是ActiveJ的实用工具包,其中的类是ActiveJ的inject模块中需要用到的一些方法。这个类也可以帮助我们更好地解读前几次介绍的binding、module包。

二、util包结构


util包中有7个类,分别为Constructors、LocationInfo、ReflectionUtils、ScopedValue、Trie、TypeUtils、Utils。

三、代码解读

1.Constructors类
这是一个构造器类,这些只是DSL使用的一组功能接口。Constructors类中创建了8个接口,分别是Constructor0,Constructor1,Constructor2,Constructor3,Constructor4,Constructor5,Constructor6,ConstructorN。它这里命名的区别就是方法名最后的数就是参数的个数。例如:

	@FunctionalInterface
	public interface Constructor0<R> {
    
    
		R create();
	}
	@FunctionalInterface
	public interface Constructor1<P1, R> {
    
    
		R create(P1 arg1);
	}
	//。。。。。。
	@FunctionalInterface
	public interface ConstructorN<R> {
    
    
		R create(Object... args);
	}

Constructors在Binding类和ModuleBuilder0类中都有所使用,主要就是用来作为构造器创建各种对象。
2.LocationInfo类
从类名中我们就可以看出来,这是一个定位位置信息的类。
LocationInfo是io.activej.inject.binding.binding 绑定中的一个临时字段,DSL尽可能设置该字段,以便错误消息可以显示绑定的位置。
构造方法如下:

	private LocationInfo(Object module, @Nullable Method provider) {
    
    
		this.module = module;
		this.provider = provider;
	}

此类也修改了输出格式:

	@Override
	public String toString() {
    
    
		if (provider == null) {
    
    
			return "module " + module;
		}
		String shortName = ReflectionUtils.getShortName(provider.getDeclaringClass());
		return "object " + module + ", provider method " + shortName + "." + provider.getName() + "(" + shortName + ".java:0)";
	}

3.ReflectionUtils类
这些是DSL使用的各种反射实用程序。
虽然不应该正常使用它们,但它们组织得很好,因此是公开的。
4.ScopedValue类
这是一个模板类,一个简单的通用POJO(或POGJO),用于具有关联作用域路径的某些对象。
它是泛型的,因为它同时用作ScopedValue;Key和ScopedValue;Dependency。
属性和构造方法如下:

	private final Scope[] scope;
	private final T value;

	private ScopedValue(Scope[] scope, T value) {
    
    
		this.scope = scope;
		this.value = value;
	}

用于各种情况的模板类方法:

public static <T> ScopedValue<T> of(@NotNull T value) {
    
    
		return new ScopedValue<>(UNSCOPED, value);
	}

	public static <T> ScopedValue<T> of(@NotNull Scope scope, @NotNull T value) {
    
    
		return new ScopedValue<>(new Scope[]{
    
    scope}, value);
	}

	public static <T> ScopedValue<T> of(Scope[] scope, @NotNull T value) {
    
    
		return new ScopedValue<>(scope.length != 0 ? scope : UNSCOPED, value);
	}

5.Trie类
Tire对象在上一个博客介绍module包时有多次应用。
前缀树(或trie)数据结构的完全通用和抽象的简单Java实现。
属性和构造方法如下:

	private final V payload;
	private final Map<K, Trie<K, V>> children;

	public Trie(V payload, Map<K, Trie<K, V>> children) {
    
    
		this.payload = payload;
		this.children = children;
	}

叶节点它是这样定义的:

	public static <K, V> Trie<K, V> leaf(V value) {
    
    
		return new Trie<>(value, new HashMap<>());
	}

6.TypeUtils类
此类包含用于处理Java类型的反射实用程序。
它主要用于方法io.activej.types.types#parameterizedType types.parameterized。
但是,与ReflectionUtils一样,其他类型的实用程序方法也非常干净,因此它们是公共的。
isInheritedFrom方法:

	public static boolean isInheritedFrom(Type type, Type from) {
    
    
		return isInheritedFrom(type, from, new HashMap<>());
	}

	public static boolean isInheritedFrom(Type type, Type from, Map<Type, Type> dejaVu) {
    
    
		if (from == Object.class) {
    
    
			return true;
		}
		if (matches(type, from, dejaVu) || matches(from, type, dejaVu)) {
    
    
			return true;
		}
		if (!(type instanceof Class || type instanceof ParameterizedType || type instanceof GenericArrayType)) {
    
    
			return false;
		}
		Class<?> rawType = Types.getRawType(type);

		Type superclass = rawType.getGenericSuperclass();
		if (superclass != null && isInheritedFrom(superclass, from, dejaVu)) {
    
    
			return true;
		}
		return Arrays.stream(rawType.getGenericInterfaces()).anyMatch(iface -> isInheritedFrom(iface, from, dejaVu));
	}

四、总结

本次分析的代码是util包中的六个类,这是这些类都是inject注入模块中使用的工具类,有着自己各自的功能,为inject中binding、module等其他包提供支持。

猜你喜欢

转载自blog.csdn.net/zth_idea/article/details/121109799