Skip to content

Commit

Permalink
Review: We have to always ensure the whole component type chain is pr…
Browse files Browse the repository at this point in the history
…esent. Background is, that if those component types are not referenced in another place (e.g. the two-dim component type is not also a field type somewhere, etc.), we will otherwise create a "simple" class stub here on the fly, and that simple stub will not get processed anymore and thus will not get any component type. In other words, the component type chain would then be broken.

Issue: #187
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
  • Loading branch information
codecholeric committed Nov 3, 2019
1 parent ac545fb commit b10dde6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ public interface ImportContext {

Set<ThrowsDeclaration<JavaConstructor>> getConstructorThrowsDeclarationsOfType(JavaClass javaClass);

Optional<JavaClass> resolveComponentType(JavaType type);
JavaClass resolveClass(String fullyQualifiedClassName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ public Map<String, JavaAnnotation> get() {
}

CompletionProcess completeFrom(ImportContext context) {
componentType = context.resolveComponentType(javaType);
completeComponentType(context);
enclosingClass = context.createEnclosingClass(this);
memberDependenciesOnClass = new MemberDependenciesOnClass(
context.getFieldsOfType(this),
Expand All @@ -859,6 +859,15 @@ CompletionProcess completeFrom(ImportContext context) {
return new CompletionProcess();
}

private void completeComponentType(ImportContext context) {
JavaClass current = this;
while (current.isArray() && !current.componentType.isPresent()) {
JavaClass componentType = context.resolveClass(current.javaType.tryGetComponentType().get().getName());
current.componentType = Optional.of(componentType);
current = componentType;
}
}

@Override
public String toString() {
return "JavaClass{name='" + javaType.getName() + "\'}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.core.domain.JavaMethodCall;
import com.tngtech.archunit.core.domain.JavaStaticInitializer;
import com.tngtech.archunit.core.domain.JavaType;
import com.tngtech.archunit.core.domain.ThrowsDeclaration;
import com.tngtech.archunit.core.importer.AccessRecord.FieldAccessRecord;
import com.tngtech.archunit.core.importer.DomainBuilders.JavaConstructorCallBuilder;
Expand Down Expand Up @@ -277,13 +276,8 @@ public Optional<JavaClass> createEnclosingClass(JavaClass owner) {
}

@Override
public Optional<JavaClass> resolveComponentType(JavaType array) {
return array.tryGetComponentType().transform(new Function<JavaType, JavaClass>() {
@Override
public JavaClass apply(JavaType input) {
return classes.getOrResolve(input.getName());
}
});
public JavaClass resolveClass(String fullyQualifiedClassName) {
return classes.getOrResolve(fullyQualifiedClassName);
}

private static class MemberDependenciesByTarget {
Expand Down Expand Up @@ -347,4 +341,4 @@ Set<ThrowsDeclaration<JavaConstructor>> getConstructorThrowsDeclarationsOfType(J
return constructorThrowsDeclarationDependencies.get(javaClass);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ public void finds_multidimensional_array_type() {
assertThat(oneDimArray.tryGetComponentType().get()).isEqualTo(type);
assertThat(twoDimArray.isArray()).isTrue();
assertThat(twoDimArray.tryGetComponentType().get()).isEqualTo(oneDimArray);
assertThat(twoDimArray.tryGetComponentType().get().tryGetComponentType().get()).isEqualTo(type);
}

@Test
public void finds_component_type_chain_of_otherwise_unreferenced_component_type() {
class OnlyReferencingMultiDimArray {
OnlyReferencingMultiDimArray[][][] field;
}

JavaClass javaClass = importClassesWithContext(OnlyReferencingMultiDimArray.class)
.get(OnlyReferencingMultiDimArray.class);

JavaClass arrayType = javaClass.getField("field").getRawType();
JavaClass twoDim = arrayType.tryGetComponentType().get();
assertThat(twoDim.getName()).isEqualTo(OnlyReferencingMultiDimArray[][].class.getName());

JavaClass oneDim = twoDim.tryGetComponentType().get();
assertThat(oneDim.getName()).isEqualTo(OnlyReferencingMultiDimArray[].class.getName());

JavaClass original = oneDim.tryGetComponentType().get();
assertThat(original).isEqualTo(javaClass);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ public Set<ThrowsDeclaration<JavaConstructor>> getConstructorThrowsDeclarationsO
}

@Override
public Optional<JavaClass> resolveComponentType(JavaType type) {
return Optional.absent();
public JavaClass resolveClass(String fullyQualifiedClassName) {
throw new UnsupportedOperationException("Override me where necessary");
}
}
}
}

0 comments on commit b10dde6

Please sign in to comment.