From d63d927de9b137c278a1b165c3057154288e7bd8 Mon Sep 17 00:00:00 2001 From: Peter Gafert Date: Sun, 3 Nov 2019 15:32:36 +0100 Subject: [PATCH] Review: Add convenience method for cases where we know this type is an array (or we've already checked javaClass.isArray()) Issue: #187 Signed-off-by: Peter Gafert --- .../archunit/core/domain/JavaClass.java | 19 +++++++++++++++++++ .../archunit/core/domain/JavaClassTest.java | 6 +++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClass.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClass.java index 764827453e..bad96fb931 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClass.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClass.java @@ -187,6 +187,25 @@ public boolean isArray() { return javaType.isArray(); } + /** + * This is a convenience method for {@link #tryGetComponentType()} in cases where + * clients know that this type is certainly an array type and thus the component type present. + * @throws IllegalArgumentException if this class is no array + * @return The result of {@link #tryGetComponentType()} + */ + @PublicAPI(usage = ACCESS) + public JavaClass getComponentType() { + return tryGetComponentType().getOrThrow(new IllegalArgumentException( + String.format("Type %s is no array", getSimpleName()))); + } + + /** + * Returns the component type of this class, if this class is an array, otherwise + * {@link Optional#absent()}. The component type is the type of the elements of an array type. + * Consider {@code String[]}, then the component type would be {@code String}. + * Likewise for {@code String[][]} the component type would be {@code String[]}. + * @return The component type, if this type is an array, otherwise {@link Optional#absent()} + */ @PublicAPI(usage = ACCESS) Optional tryGetComponentType() { return componentType; diff --git a/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaClassTest.java b/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaClassTest.java index b54552cb6f..5c3faa9e8d 100644 --- a/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaClassTest.java +++ b/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaClassTest.java @@ -118,13 +118,13 @@ class OnlyReferencingMultiDimArray { .get(OnlyReferencingMultiDimArray.class); JavaClass arrayType = javaClass.getField("field").getRawType(); - JavaClass twoDim = arrayType.tryGetComponentType().get(); + JavaClass twoDim = arrayType.getComponentType(); assertThat(twoDim.getName()).isEqualTo(OnlyReferencingMultiDimArray[][].class.getName()); - JavaClass oneDim = twoDim.tryGetComponentType().get(); + JavaClass oneDim = twoDim.getComponentType(); assertThat(oneDim.getName()).isEqualTo(OnlyReferencingMultiDimArray[].class.getName()); - JavaClass original = oneDim.tryGetComponentType().get(); + JavaClass original = oneDim.getComponentType(); assertThat(original).isEqualTo(javaClass); }