Home / Class/ TypeUtilsTests Class — spring-boot Architecture

TypeUtilsTests Class — spring-boot Architecture

Architecture documentation for the TypeUtilsTests class in TypeUtilsTests.java from the spring-boot codebase.

Entity Profile

Relationship Graph

Source Code

configuration-metadata/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TypeUtilsTests.java lines 48–134

class TypeUtilsTests {

	@Test
	void resolveTypeOnConcreteClass() {
		process(SimpleGenericProperties.class, (roundEnv, typeUtils) -> {
			TypeElement typeElement = roundEnv.getRootElement(SimpleGenericProperties.class);
			assertThat(getTypeOfField(typeUtils, typeElement, "name")).hasToString(String.class.getName());
			assertThat(getTypeOfField(typeUtils, typeElement, "mappings"))
				.hasToString(constructMapType(Integer.class, Duration.class));
		});
	}

	@Test
	void resolveTypeOnIntermediateClass() {
		process(AbstractIntermediateGenericProperties.class, (roundEnv, typeUtils) -> {
			TypeElement typeElement = roundEnv.getRootElement(AbstractIntermediateGenericProperties.class);
			assertThat(getTypeOfField(typeUtils, typeElement, "name")).hasToString(String.class.getName());
			assertThat(getTypeOfField(typeUtils, typeElement, "mappings"))
				.hasToString(constructMapType(Integer.class, Object.class));
		});
	}

	@Test
	void resolveTypeWithOnlyGenerics() {
		process(AbstractGenericProperties.class, (roundEnv, typeUtils) -> {
			TypeElement typeElement = roundEnv.getRootElement(AbstractGenericProperties.class);
			assertThat(getTypeOfField(typeUtils, typeElement, "name")).hasToString(Object.class.getName());
			assertThat(getTypeOfField(typeUtils, typeElement, "mappings"))
				.hasToString(constructMapType(Object.class, Object.class));
		});
	}

	@Test
	void resolveTypeWithUnresolvedGenericProperties() {
		process(UnresolvedGenericProperties.class, (roundEnv, typeUtils) -> {
			TypeElement typeElement = roundEnv.getRootElement(UnresolvedGenericProperties.class);
			assertThat(getTypeOfField(typeUtils, typeElement, "name")).hasToString(String.class.getName());
			assertThat(getTypeOfField(typeUtils, typeElement, "mappings"))
				.hasToString(constructMapType(Number.class, Object.class));
		});
	}

	@Test
	void resolvedTypeMixGenericNamePropertiesProperties() {
		process(MixGenericNameProperties.class, (roundEnv, typeUtils) -> {
			TypeElement typeElement = roundEnv.getRootElement(MixGenericNameProperties.class);
			assertThat(getTypeOfField(typeUtils, typeElement, "name")).hasToString(String.class.getName());
			assertThat(getTypeOfField(typeUtils, typeElement, "mappings"))
				.hasToString(constructMapType(Number.class, Object.class));
		});
	}

	private void process(Class<?> target, BiConsumer<RoundEnvironmentTester, TypeUtils> consumer) {
		TestableAnnotationProcessor<TypeUtils> processor = new TestableAnnotationProcessor<>(consumer, TypeUtils::new);
		TestCompiler compiler = TestCompiler.forSystem()
			.withProcessors(processor)
			.withSources(SourceFile.forTestClass(target));
		compiler.compile((compiled) -> {
		});
	}

	private String constructMapType(Class<?> keyType, Class<?> valueType) {
		return "%s<%s,%s>".formatted(Map.class.getName(), keyType.getName(), valueType.getName());
	}

	private String getTypeOfField(TypeUtils typeUtils, TypeElement typeElement, String name) {
		TypeMirror field = findField(typeUtils, typeElement, name);
		if (field == null) {
			throw new IllegalStateException("Unable to find field '" + name + "' in " + typeElement);
		}
		return typeUtils.getType(typeElement, field);
	}

	private TypeMirror findField(TypeUtils typeUtils, TypeElement typeElement, String name) {
		for (VariableElement variableElement : ElementFilter.fieldsIn(typeElement.getEnclosedElements())) {
			if (variableElement.getSimpleName().contentEquals(name)) {
				return variableElement.asType();
			}
		}
		TypeMirror superclass = typeElement.getSuperclass();
		if (superclass != null && !superclass.toString().equals(Object.class.getName())) {
			return findField(typeUtils, (TypeElement) typeUtils.asElement(superclass), name);
		}
		return null;
	}

}

Domain

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free