Skip to content

Commit

Permalink
add possibility to get enum constants from JavaClass
Browse files Browse the repository at this point in the history
It might prove useful to check for the existence of a certain enum constant within an enum type.

Signed-off-by: Peter Gafert <[email protected]>
  • Loading branch information
codecholeric authored and Alen Kosanović committed Jan 9, 2020
1 parent b284e32 commit edbff5a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.tngtech.archunit.base.ArchUnitException.InvalidSyntaxUsageException;
import com.tngtech.archunit.base.ChainableFunction;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.base.HasDescription;
import com.tngtech.archunit.base.Optional;
import com.tngtech.archunit.base.PackageMatcher;
import com.tngtech.archunit.core.MayResolveTypesViaReflection;
Expand All @@ -45,6 +44,7 @@
import com.tngtech.archunit.core.domain.properties.HasSourceCodeLocation;
import com.tngtech.archunit.core.importer.DomainBuilders.JavaClassBuilder;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Sets.union;
Expand All @@ -54,10 +54,12 @@
import static com.tngtech.archunit.base.DescribedPredicate.not;
import static com.tngtech.archunit.core.domain.JavaClass.Functions.GET_SIMPLE_NAME;
import static com.tngtech.archunit.core.domain.JavaConstructor.CONSTRUCTOR_NAME;
import static com.tngtech.archunit.core.domain.JavaModifier.ENUM;
import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Utils.toAnnotationOfType;
import static com.tngtech.archunit.core.domain.properties.HasName.Functions.GET_NAME;
import static com.tngtech.archunit.core.domain.properties.HasType.Functions.GET_RAW_TYPE;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;

public class JavaClass implements HasName.AndFullName, HasAnnotations<JavaClass>, HasModifiers, HasSourceCodeLocation {
private final Optional<Source> source;
Expand All @@ -68,11 +70,11 @@ public class JavaClass implements HasName.AndFullName, HasAnnotations<JavaClass>
private final boolean isEnum;
private final Set<JavaModifier> modifiers;
private final Supplier<Class<?>> reflectSupplier;
private Set<JavaField> fields = new HashSet<>();
private Set<JavaCodeUnit> codeUnits = new HashSet<>();
private Set<JavaMethod> methods = new HashSet<>();
private Set<JavaMember> members = new HashSet<>();
private Set<JavaConstructor> constructors = new HashSet<>();
private Set<JavaField> fields = emptySet();
private Set<JavaCodeUnit> codeUnits = emptySet();
private Set<JavaMethod> methods = emptySet();
private Set<JavaMember> members = emptySet();
private Set<JavaConstructor> constructors = emptySet();
private Optional<JavaStaticInitializer> staticInitializer = Optional.absent();
private Optional<JavaClass> superClass = Optional.absent();
private final Set<JavaClass> interfaces = new HashSet<>();
Expand Down Expand Up @@ -178,6 +180,33 @@ public boolean isEnum() {
return isEnum;
}

@PublicAPI(usage = ACCESS)
public Optional<JavaEnumConstant> tryGetEnumConstant(String name) {
Optional<JavaField> field = tryGetField(name);
if (!field.isPresent() || !field.get().getModifiers().contains(ENUM)) {
return Optional.absent();
}
return Optional.of(new JavaEnumConstant(this, field.get().getName()));
}

@PublicAPI(usage = ACCESS)
public JavaEnumConstant getEnumConstant(String name) {
Optional<JavaEnumConstant> enumConstant = tryGetEnumConstant(name);
checkArgument(enumConstant.isPresent(), "There exists no enum constant with name '%s' in class %s", name, getName());
return enumConstant.get();
}

@PublicAPI(usage = ACCESS)
public Set<JavaEnumConstant> getEnumConstants() {
ImmutableSet.Builder<JavaEnumConstant> result = ImmutableSet.builder();
for (JavaField field : fields) {
if (field.getModifiers().contains(ENUM)) {
result.add(new JavaEnumConstant(this, field.getName()));
}
}
return result.build();
}

@PublicAPI(usage = ACCESS)
public boolean isArray() {
return javaType.isArray();
Expand Down Expand Up @@ -899,7 +928,7 @@ private void completeComponentType(ImportContext context) {

@Override
public String toString() {
return "JavaClass{name='" + javaType.getName() + "\'}";
return "JavaClass{name='" + javaType.getName() + "'}";
}

@PublicAPI(usage = ACCESS)
Expand All @@ -921,7 +950,6 @@ public boolean isAnonymous() {
return getSimpleName().isEmpty();
}


public static final class Functions {
private Functions() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@
import com.tngtech.archunit.PublicAPI;
import com.tngtech.archunit.core.importer.DomainBuilders.JavaEnumConstantBuilder;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;

public final class JavaEnumConstant {
private final JavaClass declaringClass;
private final String name;

JavaEnumConstant(JavaEnumConstantBuilder builder) {
this.declaringClass = builder.getDeclaringClass();
this.name = builder.getName();
this(builder.getDeclaringClass(), builder.getName());
}

JavaEnumConstant(JavaClass declaringClass, String name) {
this.declaringClass = checkNotNull(declaringClass);
this.name = checkNotNull(name);
}

@PublicAPI(usage = ACCESS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public enum JavaModifier {
@PublicAPI(usage = ACCESS)
TRANSIENT(EnumSet.of(ApplicableType.FIELD), Opcodes.ACC_TRANSIENT),
@PublicAPI(usage = ACCESS)
ENUM(EnumSet.of(ApplicableType.FIELD), Opcodes.ACC_ENUM),
@PublicAPI(usage = ACCESS)
ABSTRACT(EnumSet.of(ApplicableType.CLASS, ApplicableType.METHOD), Opcodes.ACC_ABSTRACT),
@PublicAPI(usage = ACCESS)
SYNCHRONIZED(EnumSet.of(ApplicableType.METHOD), Opcodes.ACC_SYNCHRONIZED),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ public void imports_simple_enum() throws Exception {
assertThatClasses(javaClass.getAllInterfaces()).matchInAnyOrder(Enum.class.getInterfaces());
assertThat(javaClass.isInterface()).as("is interface").isFalse();
assertThat(javaClass.isEnum()).as("is enum").isTrue();

JavaEnumConstant constant = javaClass.getEnumConstant(EnumToImport.FIRST.name());
assertThat(constant.getDeclaringClass()).as("declaring class").isEqualTo(javaClass);
assertThat(constant.name()).isEqualTo(EnumToImport.FIRST.name());
assertThat(javaClass.getEnumConstants()).extractingResultOf("name").as("enum constant names")
.containsOnly(EnumToImport.FIRST.name(), EnumToImport.SECOND.name());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package com.tngtech.archunit.core.importer.testexamples.simpleimport;

@SuppressWarnings("unused")
public enum EnumToImport {
FIRST, SECOND;

public static EnumToImport someStaticField;

EnumToImport instantField;
}

0 comments on commit edbff5a

Please sign in to comment.