forked from halo-dev/halo
-
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.
Automate attribute converter (halo-dev#1325)
* Deprecate AbstractConverter * Remove unused enum and attribute converter * Add AttributeConverterApplyTest * Add JpaConfiguration * Add AttributeConverterAutoGenerator * Integrate automate-attribute-converter * Rename JpaConfiguration * Remove useless attribute converters * Exclude property enums for auto-generating * Refine JournalType definition * Fix an error about existing injected type
- Loading branch information
Showing
22 changed files
with
307 additions
and
184 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
20 changes: 20 additions & 0 deletions
20
...a/run/halo/app/config/attributeconverter/AttributeConverterAutoGenerateConfiguration.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,20 @@ | ||
package run.halo.app.config.attributeconverter; | ||
|
||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilderCustomizer; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* Jpa configuration. | ||
* | ||
* @author johnniang | ||
*/ | ||
public class AttributeConverterAutoGenerateConfiguration { | ||
|
||
@Bean | ||
EntityManagerFactoryBuilderCustomizer entityManagerFactoryBuilderCustomizer( | ||
ConfigurableListableBeanFactory factory) { | ||
return builder -> builder.setPersistenceUnitPostProcessors( | ||
new AutoGenerateConverterPersistenceUnitPostProcessor(factory)); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/run/halo/app/config/attributeconverter/AttributeConverterAutoGenerator.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,71 @@ | ||
package run.halo.app.config.attributeconverter; | ||
|
||
import static net.bytebuddy.description.annotation.AnnotationDescription.Builder.ofType; | ||
import static net.bytebuddy.description.type.TypeDescription.Generic.Builder.parameterizedType; | ||
import static net.bytebuddy.implementation.FieldAccessor.ofField; | ||
import static net.bytebuddy.implementation.MethodDelegation.to; | ||
import static net.bytebuddy.matcher.ElementMatchers.isDefaultConstructor; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import java.lang.reflect.Modifier; | ||
import javax.persistence.AttributeConverter; | ||
import javax.persistence.Converter; | ||
import net.bytebuddy.ByteBuddy; | ||
import net.bytebuddy.NamingStrategy; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; | ||
import net.bytebuddy.implementation.MethodCall; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** | ||
* Attribute converter auto generator. | ||
* | ||
* @author johnniang | ||
*/ | ||
class AttributeConverterAutoGenerator { | ||
|
||
/** | ||
* Auto generation suffix. | ||
*/ | ||
public static final String AUTO_GENERATION_SUFFIX = "$AttributeConverterGeneratedByByteBuddy"; | ||
|
||
private final ClassLoader classLoader; | ||
|
||
public AttributeConverterAutoGenerator(ClassLoader classLoader) { | ||
this.classLoader = classLoader; | ||
} | ||
|
||
public <T> Class<?> generate(Class<T> clazz) { | ||
try { | ||
return new ByteBuddy() | ||
.with(new NamingStrategy.AbstractBase() { | ||
@Override | ||
protected String name(TypeDescription superClass) { | ||
return clazz.getName() + AUTO_GENERATION_SUFFIX; | ||
} | ||
}) | ||
.subclass( | ||
parameterizedType(AttributeConverter.class, clazz, Integer.class).build()) | ||
.annotateType(ofType(Converter.class).define("autoApply", true).build()) | ||
.constructor(isDefaultConstructor()) | ||
.intercept(MethodCall.invoke(Object.class.getDeclaredConstructor()) | ||
.andThen(ofField("enumType").setsValue(clazz))) | ||
.defineField("enumType", Class.class, Modifier.PRIVATE | Modifier.FINAL) | ||
.method(named("convertToDatabaseColumn")) | ||
.intercept(to(AttributeConverterInterceptor.class)) | ||
.method(named("convertToEntityAttribute")) | ||
.intercept(to(AttributeConverterInterceptor.class)) | ||
.make() | ||
.load(this.classLoader, ClassLoadingStrategy.Default.INJECTION.allowExistingTypes()) | ||
.getLoaded(); | ||
} catch (NoSuchMethodException e) { | ||
// should never happen | ||
throw new RuntimeException("Failed to get declared constructor.", e); | ||
} | ||
} | ||
|
||
public static boolean isGeneratedByByteBuddy(String className) { | ||
return StringUtils.endsWith(className, AUTO_GENERATION_SUFFIX); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/run/halo/app/config/attributeconverter/AttributeConverterInterceptor.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,27 @@ | ||
package run.halo.app.config.attributeconverter; | ||
|
||
import net.bytebuddy.implementation.bind.annotation.FieldValue; | ||
import net.bytebuddy.implementation.bind.annotation.RuntimeType; | ||
import run.halo.app.model.enums.ValueEnum; | ||
|
||
/** | ||
* Attribute Converter Interceptor. | ||
* | ||
* @author johnniang | ||
*/ | ||
public class AttributeConverterInterceptor { | ||
|
||
private AttributeConverterInterceptor() { | ||
} | ||
|
||
@RuntimeType | ||
public static <T extends Enum<T> & ValueEnum<V>, V> V convertToDatabaseColumn(T attribute) { | ||
return attribute == null ? null : attribute.getValue(); | ||
} | ||
|
||
@RuntimeType | ||
public static <T extends Enum<T> & ValueEnum<V>, V> T convertToEntityAttribute(V dbData, | ||
@FieldValue("enumType") Class<T> enumType) { | ||
return dbData == null ? null : ValueEnum.valueToEnum(enumType, dbData); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...halo/app/config/attributeconverter/AutoGenerateConverterPersistenceUnitPostProcessor.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,55 @@ | ||
package run.halo.app.config.attributeconverter; | ||
|
||
import static java.util.stream.Collectors.toUnmodifiableSet; | ||
|
||
import java.util.Set; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; | ||
import org.springframework.core.type.filter.AssignableTypeFilter; | ||
import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo; | ||
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor; | ||
import org.springframework.util.ClassUtils; | ||
import run.halo.app.model.enums.ValueEnum; | ||
import run.halo.app.model.properties.PropertyEnum; | ||
|
||
/** | ||
* Attribute converter persistence unit post processor. | ||
* | ||
* @author johnniang | ||
*/ | ||
class AutoGenerateConverterPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor { | ||
|
||
private static final String PACKAGE_TO_SCAN = "run.halo.app"; | ||
|
||
private final ConfigurableListableBeanFactory factory; | ||
|
||
public AutoGenerateConverterPersistenceUnitPostProcessor( | ||
ConfigurableListableBeanFactory factory) { | ||
this.factory = factory; | ||
} | ||
|
||
@Override | ||
public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) { | ||
var generator = new AttributeConverterAutoGenerator(factory.getBeanClassLoader()); | ||
|
||
findValueEnumClasses() | ||
.stream() | ||
.map(generator::generate) | ||
.map(Class::getName) | ||
.forEach(pui::addManagedClassName); | ||
} | ||
|
||
private Set<Class<?>> findValueEnumClasses() { | ||
var scanner = new ClassPathScanningCandidateComponentProvider(false); | ||
// include ValueEnum class | ||
scanner.addIncludeFilter(new AssignableTypeFilter(ValueEnum.class)); | ||
// exclude PropertyEnum class | ||
scanner.addExcludeFilter(new AssignableTypeFilter(PropertyEnum.class)); | ||
|
||
return scanner.findCandidateComponents(PACKAGE_TO_SCAN) | ||
.stream() | ||
.filter(bd -> bd.getBeanClassName() != null) | ||
.map(bd -> ClassUtils.resolveClassName(bd.getBeanClassName(), null)) | ||
.collect(toUnmodifiableSet()); | ||
} | ||
} |
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
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
This file was deleted.
Oops, something went wrong.
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
40 changes: 0 additions & 40 deletions
40
src/main/java/run/halo/app/model/enums/converter/AbstractConverter.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/main/java/run/halo/app/model/enums/converter/AttachmentTypeConverter.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/main/java/run/halo/app/model/enums/converter/CommentStatusConverter.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/main/java/run/halo/app/model/enums/converter/DataTypeConverter.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.