io.devcon5.classutils.ClassStreams Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of classutils Show documentation
Show all versions of classutils Show documentation
Utilities for dealing with classes.
package io.devcon5.classutils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
/**
* Utility class for dealing with classes and streams
*/
public final class ClassStreams {
private ClassStreams() {
}
/**
* Creates a stream of classes representing the supertypes (excluding Object.class) of the specified type.
*
* @param type
* the type whose supertypes should be returned as a stream
*
* @return stream of the supertypes of the specified class
*/
public static Stream supertypes(final Class type) {
return selfAndSupertypes(type.getSuperclass());
}
/**
* Creates a stream of classes containing the class itself and all its the supertypes (excluding Object.class) of
* the specified type.
*
* @param type
* the type whose supertypes should be returned as a stream
*
* @return stream of the class itself and its supertypes
*/
public static Stream selfAndSupertypes(final Class type) {
Class current = type;
final List result = new ArrayList<>();
do {
result.add(current);
current = current.getSuperclass();
} while (current != Object.class);
return result.stream();
}
}