-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from aboutbits/ab-237-type-ids
Ab 237 type ids
- Loading branch information
Showing
79 changed files
with
3,199 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...tbits/springboot/toolbox/autoconfiguration/persistence/AbstractCustomTypeContributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package it.aboutbits.springboot.toolbox.autoconfiguration.persistence; | ||
|
||
import it.aboutbits.springboot.toolbox.reflection.util.ClassScannerUtil; | ||
import lombok.SneakyThrows; | ||
import org.hibernate.boot.model.TypeContributions; | ||
import org.hibernate.boot.model.TypeContributor; | ||
import org.hibernate.service.ServiceRegistry; | ||
import org.hibernate.type.descriptor.java.JavaType; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Set; | ||
|
||
public abstract class AbstractCustomTypeContributor implements TypeContributor { | ||
@SuppressWarnings("rawtypes") | ||
private final Set<Class<? extends AutoRegisteredJavaType>> relevantTypes; | ||
|
||
protected AbstractCustomTypeContributor(String... packageNames) { | ||
var classScanner = ClassScannerUtil.getScannerForPackages(packageNames); | ||
|
||
this.relevantTypes = findAllRelevantTypes(classScanner); | ||
} | ||
|
||
@SneakyThrows({InstantiationException.class, IllegalAccessException.class, InvocationTargetException.class}) | ||
@Override | ||
public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) { | ||
for (var type : relevantTypes) { | ||
typeContributions.contributeJavaType( | ||
(JavaType<?>) type.getConstructors()[0].newInstance() | ||
); | ||
} | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
private static Set<Class<? extends AutoRegisteredJavaType>> findAllRelevantTypes(ClassScannerUtil.ClassScanner classScanner) { | ||
return classScanner.getSubTypesOf(AutoRegisteredJavaType.class); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...it/aboutbits/springboot/toolbox/autoconfiguration/persistence/AutoRegisteredJavaType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package it.aboutbits.springboot.toolbox.autoconfiguration.persistence; | ||
|
||
import org.hibernate.type.descriptor.java.JavaType; | ||
|
||
public interface AutoRegisteredJavaType<T> extends JavaType<T> { | ||
} |
7 changes: 7 additions & 0 deletions
7
.../it/aboutbits/springboot/toolbox/autoconfiguration/persistence/CustomTypeContributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package it.aboutbits.springboot.toolbox.autoconfiguration.persistence; | ||
|
||
public final class CustomTypeContributor extends AbstractCustomTypeContributor { | ||
public CustomTypeContributor() { | ||
super("it.aboutbits.springboot.toolbox"); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...boutbits/springboot/toolbox/autoconfiguration/swagger/RegisterCustomTypesWithSwagger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package it.aboutbits.springboot.toolbox.autoconfiguration.swagger; | ||
|
||
import it.aboutbits.springboot.toolbox.swagger.CustomTypeModelConverter; | ||
import it.aboutbits.springboot.toolbox.swagger.CustomTypePropertyCustomizer; | ||
import org.springframework.context.annotation.Import; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Import({ | ||
CustomTypeModelConverter.class, | ||
CustomTypePropertyCustomizer.class | ||
}) | ||
public @interface RegisterCustomTypesWithSwagger { | ||
} |
50 changes: 50 additions & 0 deletions
50
...n/java/it/aboutbits/springboot/toolbox/autoconfiguration/web/CustomTypeConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package it.aboutbits.springboot.toolbox.autoconfiguration.web; | ||
|
||
import it.aboutbits.springboot.toolbox.jackson.CustomTypeDeserializer; | ||
import it.aboutbits.springboot.toolbox.jackson.CustomTypeSerializer; | ||
import it.aboutbits.springboot.toolbox.mvc.CustomTypePropertyEditor; | ||
import it.aboutbits.springboot.toolbox.type.CustomType; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.bind.WebDataBinder; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.InitBinder; | ||
|
||
import java.util.Set; | ||
|
||
@Slf4j | ||
@Configuration | ||
public class CustomTypeConfiguration { | ||
@ControllerAdvice | ||
public static class CustomTypePropertyBinder { | ||
@SuppressWarnings("rawtypes") | ||
private final Set<Class<? extends CustomType>> types; | ||
|
||
public CustomTypePropertyBinder(CustomTypeScanner configuration) { | ||
this.types = configuration.getRelevantTypes(); | ||
} | ||
|
||
@InitBinder | ||
public void initBinder(WebDataBinder binder) { | ||
for (var clazz : types) { | ||
binder.registerCustomEditor(clazz, new CustomTypePropertyEditor<>(clazz)); | ||
} | ||
} | ||
} | ||
|
||
@Bean | ||
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer(CustomTypeScanner configuration) { | ||
var types = configuration.getRelevantTypes(); | ||
|
||
var deserializers = types.stream() | ||
.map(CustomTypeDeserializer::new) | ||
.toList() | ||
.toArray(new CustomTypeDeserializer[types.size()]); | ||
|
||
return builder -> builder | ||
.serializers(new CustomTypeSerializer()) | ||
.deserializers(deserializers); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/it/aboutbits/springboot/toolbox/autoconfiguration/web/CustomTypeScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package it.aboutbits.springboot.toolbox.autoconfiguration.web; | ||
|
||
import it.aboutbits.springboot.toolbox.reflection.util.ClassScannerUtil; | ||
import it.aboutbits.springboot.toolbox.type.CustomType; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Getter | ||
public class CustomTypeScanner { | ||
private static final String LIBRARY_BASE_PACKAGE_NAME = "it.aboutbits.springboot.toolbox"; | ||
|
||
@SuppressWarnings("rawtypes") | ||
private Set<Class<? extends CustomType>> relevantTypes = new HashSet<>(); | ||
|
||
public void setAdditionalTypePackages(String[] additionalTypePackages) { | ||
var tmp = new ArrayList<String>(); | ||
tmp.add(LIBRARY_BASE_PACKAGE_NAME); | ||
tmp.addAll(Arrays.stream(additionalTypePackages) | ||
.filter(item -> !item.isBlank()) | ||
.collect(Collectors.toSet())); | ||
|
||
var packageNamesToScan = tmp.toArray(new String[0]); | ||
|
||
log.info("CustomTypeConfiguration enabled. Scanning: {}", Arrays.toString(packageNamesToScan)); | ||
var classScanner = ClassScannerUtil.getScannerForPackages(packageNamesToScan); | ||
|
||
this.relevantTypes = findAllCustomTypeRecords(classScanner); | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
public static Set<Class<? extends CustomType>> findAllCustomTypeRecords(ClassScannerUtil.ClassScanner classScanner) { | ||
return classScanner.getSubTypesOf(CustomType.class).stream() | ||
.filter(Record.class::isAssignableFrom) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ava/it/aboutbits/springboot/toolbox/autoconfiguration/web/CustomTypeScannerRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package it.aboutbits.springboot.toolbox.autoconfiguration.web; | ||
|
||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||
import org.springframework.core.annotation.AnnotationAttributes; | ||
import org.springframework.core.type.AnnotationMetadata; | ||
|
||
import java.util.Objects; | ||
|
||
public class CustomTypeScannerRegistrar implements ImportBeanDefinitionRegistrar { | ||
|
||
/** | ||
* We use this to register a new bean with actual configuration parameters coming from an annotation. | ||
* That way we can use @RegisterCustomTypesWithJacksonAndMvc and take the parameter "additionalTypePackages" to | ||
* scan packages. | ||
*/ | ||
@Override | ||
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { | ||
var attributes = new AnnotationAttributes( | ||
Objects.requireNonNull( | ||
metadata.getAnnotationAttributes( | ||
RegisterCustomTypesWithJacksonAndMvc.class.getName() | ||
) | ||
) | ||
); | ||
var value = attributes.getStringArray("additionalTypePackages"); | ||
|
||
var builder = BeanDefinitionBuilder.genericBeanDefinition(CustomTypeScanner.class); | ||
builder.addPropertyValue("additionalTypePackages", value); | ||
registry.registerBeanDefinition("CustomTypeScanner", builder.getBeanDefinition()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...utbits/springboot/toolbox/autoconfiguration/web/RegisterCustomTypesWithJacksonAndMvc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package it.aboutbits.springboot.toolbox.autoconfiguration.web; | ||
|
||
import org.springframework.context.annotation.Import; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Import({CustomTypeScannerRegistrar.class, CustomTypeConfiguration.class}) | ||
public @interface RegisterCustomTypesWithJacksonAndMvc { | ||
String[] additionalTypePackages() default ""; | ||
} |
Oops, something went wrong.