patternjavaMinor
Determine the class hierarchy of a class
Viewed 0 times
hierarchytheclassdetermine
Problem
My only concern with the following class is the line
Since this class is completely finished and tested, I also look for any other helpful comments. The target version is 1.8.
```
/** The class hierarchy of a class. */
public class ClassHierarchy {
private final List> hierarchy;
/**
* Creates a ClassHierarchy instance for the given class {@literal clazz}.
*
* @param clazz
* Class for which the class hierarchy should be determined. Must not be null and must refer to a class
* type.
*
* @throws IllegalArgumentException
* if {@literal clazz} does not refer to a class type
* @throws NullPointerException
* if {@literal clazz} is null
*/
public ClassHierarchy(Class clazz) {
validateConstructorArgument(clazz);
hierarchy = Collections.unmodifiableList(determineHierarchy(clazz));
}
/**
* Returns the class hierarchy as an immutable list.
*
*
* The classes are ordered, starting with the class for which the instance was created and ending with
* {@code Object.class}.
*
* @return the class hierarchy as an immutable list.
*/
public List> getHierarchy() {
return hierarchy;
}
private static void validateConstructorArgument(Class clazz) {
Objects.requireNonNull(clazz, "clazz must not be null");
if (clazz.isEnum() || clazz.isAnnotation() || clazz.isInterface() || clazz.isArray() || clazz.isPrimitive()) {
throw new IllegalArgumentException("clazz must refer to a class type");
}
}
private static ArrayList> determineHierarchy(Class clazz) {
ArrayList> hierarchy = new ArrayList<>()
hierarchy.trimToSize() in determineHierarchy, because it is not necessary for determining the class hierarchy. However, putting it in the constructor clutters the code more than my current solution. Any recommendations or should I just live with it?Since this class is completely finished and tested, I also look for any other helpful comments. The target version is 1.8.
```
/** The class hierarchy of a class. */
public class ClassHierarchy {
private final List> hierarchy;
/**
* Creates a ClassHierarchy instance for the given class {@literal clazz}.
*
* @param clazz
* Class for which the class hierarchy should be determined. Must not be null and must refer to a class
* type.
*
* @throws IllegalArgumentException
* if {@literal clazz} does not refer to a class type
* @throws NullPointerException
* if {@literal clazz} is null
*/
public ClassHierarchy(Class clazz) {
validateConstructorArgument(clazz);
hierarchy = Collections.unmodifiableList(determineHierarchy(clazz));
}
/**
* Returns the class hierarchy as an immutable list.
*
*
* The classes are ordered, starting with the class for which the instance was created and ending with
* {@code Object.class}.
*
* @return the class hierarchy as an immutable list.
*/
public List> getHierarchy() {
return hierarchy;
}
private static void validateConstructorArgument(Class clazz) {
Objects.requireNonNull(clazz, "clazz must not be null");
if (clazz.isEnum() || clazz.isAnnotation() || clazz.isInterface() || clazz.isArray() || clazz.isPrimitive()) {
throw new IllegalArgumentException("clazz must refer to a class type");
}
}
private static ArrayList> determineHierarchy(Class clazz) {
ArrayList> hierarchy = new ArrayList<>()
Solution
The one comment I have is that I find your
I would at least put that into a small helper to throw that exception:
if statement to be really hard to read:if (clazz.isEnum() || clazz.isAnnotation() || clazz.isInterface() || clazz.isArray() || clazz.isPrimitive())I would at least put that into a small helper to throw that exception:
private void checkClassRefersToClassType()Code Snippets
if (clazz.isEnum() || clazz.isAnnotation() || clazz.isInterface() || clazz.isArray() || clazz.isPrimitive())private void checkClassRefersToClassType()Context
StackExchange Code Review Q#140546, answer score: 4
Revisions (0)
No revisions yet.