Close

Spring Framework - How to find all subclasses in Java?

Spring Framework 

org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider can be used to find all subclass of a super class or an interface.
Note: This doesn't work for Java core internal classes.

package com.logicbig.example;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.type.filter.AssignableTypeFilter;
import java.util.Set;

public class SpringBuiltInConvertersFinder {
public static void main(String[] args) {
ClassPathScanningCandidateComponentProvider provider =
new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AssignableTypeFilter(Converter.class));
Set<BeanDefinition> components = provider.findCandidateComponents("org/springframework");
components.stream()
.map(BeanDefinition::getBeanClassName)
.forEach(System.out::println);
}
}

Output

org.springframework.format.datetime.DateFormatterRegistrar$DateToCalendarConverter
org.springframework.format.datetime.DateFormatterRegistrar$CalendarToLongConverter
org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToCalendarConverter
org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToDateConverter
org.springframework.format.datetime.joda.JodaTimeConverters$LongToReadableInstantConverter
org.springframework.format.datetime.joda.JodaTimeConverters$CalendarToReadableInstantConverter
org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToMutableDateTimeConverter
org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToLocalDateTimeConverter
org.springframework.format.datetime.joda.JodaTimeConverters$LocalDateTimeToLocalTimeConverter
org.springframework.format.datetime.joda.JodaTimeConverters$DateToReadableInstantConverter
org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToInstantConverter
org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToDateMidnightConverter
org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToLocalDateConverter
org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToLongConverter
org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToLocalTimeConverter
org.springframework.format.datetime.joda.JodaTimeConverters$LocalDateTimeToLocalDateConverter
org.springframework.format.datetime.standard.DateTimeConverters$InstantToLongConverter
org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToOffsetDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters$CalendarToOffsetDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters$CalendarToZonedDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToZonedDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalDateConverter
org.springframework.format.datetime.standard.DateTimeConverters$LocalDateTimeToLocalTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToInstantConverter
org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalDateConverter
org.springframework.format.datetime.standard.DateTimeConverters$LocalDateTimeToLocalDateConverter
org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToInstantConverter
org.springframework.format.datetime.standard.DateTimeConverters$CalendarToInstantConverter
org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalDateConverter
org.springframework.format.datetime.standard.DateTimeConverters$LongToInstantConverter
org.springframework.format.datetime.DateFormatterRegistrar$CalendarToDateConverter
org.springframework.format.datetime.DateFormatterRegistrar$DateToLongConverter
org.springframework.format.datetime.DateFormatterRegistrar$LongToDateConverter
org.springframework.format.datetime.DateFormatterRegistrar$LongToCalendarConverter
org.springframework.core.convert.support.PropertiesToStringConverter
org.springframework.core.convert.support.StringToUUIDConverter
org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber
org.springframework.core.convert.support.ZoneIdToTimeZoneConverter
org.springframework.core.convert.support.NumberToNumberConverterFactory$NumberToNumber
org.springframework.core.convert.support.StringToBooleanConverter
org.springframework.core.convert.support.StringToCharacterConverter
org.springframework.core.convert.support.ObjectToStringConverter
org.springframework.core.convert.support.EnumToStringConverter
org.springframework.core.convert.support.StringToCharsetConverter
org.springframework.core.convert.support.EnumToIntegerConverter
org.springframework.core.convert.support.StringToCurrencyConverter
org.springframework.core.convert.support.NumberToCharacterConverter
org.springframework.core.convert.support.ZonedDateTimeToCalendarConverter
org.springframework.core.convert.support.StringToLocaleConverter
org.springframework.core.convert.support.CharacterToNumberFactory$CharacterToNumber
org.springframework.core.convert.support.StringToPropertiesConverter
org.springframework.core.convert.support.StringToTimeZoneConverter
org.springframework.core.convert.converter.ConvertingComparator$ConversionServiceConverter
org.springframework.core.serializer.support.SerializingConverter
org.springframework.core.serializer.support.DeserializingConverter
Original Post




See Also