Skip to content

Commit

Permalink
Add getEnumConstantObjects() (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehutch committed Dec 12, 2021
1 parent 1028925 commit 04223e8
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/main/java/io/github/classgraph/ClassInfo.java
Expand Up @@ -53,6 +53,7 @@
import io.github.classgraph.Classfile.ClassTypeAnnotationDecorator;
import io.github.classgraph.FieldInfoList.FieldInfoFilter;
import nonapi.io.github.classgraph.json.Id;
import nonapi.io.github.classgraph.reflection.ReflectionUtils;
import nonapi.io.github.classgraph.scanspec.ScanSpec;
import nonapi.io.github.classgraph.types.ParseException;
import nonapi.io.github.classgraph.types.Parser;
Expand Down Expand Up @@ -2667,19 +2668,40 @@ public FieldInfoList getFieldInfo() {
return fieldInfoList;
}

/** Get all enum constants of an enum class (as a list of {@link FieldInfo} objects). */
/**
* @return All enum constants of an enum class as a list of {@link FieldInfo} objects (enum constants are stored
* as fields in Java classes).
*/
public FieldInfoList getEnumConstants() {
if (!isEnum()) {
throw new IllegalArgumentException("Class " + getName() + " is not an enum");
}
return getFieldInfo().filter(new FieldInfoFilter() {
@Override
public boolean accept(FieldInfo fieldInfo) {
public boolean accept(final FieldInfo fieldInfo) {
return fieldInfo.isEnum();
}
});
}


/** @return All enum constants of an enum class as a list of objects of the same type as the enum. */
public List<Object> getEnumConstantObjects() {
if (!isEnum()) {
throw new IllegalArgumentException("Class " + getName() + " is not an enum");
}
final Class<?> enumClass = loadClass();
final FieldInfoList consts = getEnumConstants();
final List<Object> constObjs = new ArrayList<>(consts.size());
for (final FieldInfo constFieldInfo : consts) {
final Object constObj = ReflectionUtils.getStaticFieldVal(true, enumClass, constFieldInfo.getName());
if (constObj == null) {
throw new IllegalArgumentException("Could not read enum constant objects");
}
constObjs.add(constObj);
}
return constObjs;
}

/**
* Returns information on the named field declared by the class, but not by its superclasses. See also:
*
Expand Down

0 comments on commit 04223e8

Please sign in to comment.