com.github.skjolber.stcsv.builder.ByteBuddySetterProjectionHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of databinder Show documentation
Show all versions of databinder Show documentation
High-performance CSV databinding library
package com.github.skjolber.stcsv.builder;
import java.lang.reflect.Method;
public class ByteBuddySetterProjectionHelper extends SetterProjectionHelper {
public ByteBuddySetterProjectionHelper(Class target) {
super(target);
}
protected T generateProxy() throws Exception {
return (T) new net.bytebuddy.ByteBuddy()
.subclass(target)
.method(net.bytebuddy.matcher.ElementMatchers.any())
.intercept(net.bytebuddy.implementation.InvocationHandlerAdapter.of(this))
.make()
.load(target.getClassLoader()).getLoaded().getDeclaredConstructor().newInstance();
}
protected Method invokeSetter(AbstractCsvFieldMapperBuilder abstractCsvFieldMapperBuilder) throws CsvBuilderException {
if(proxy == null) {
try {
proxy = generateProxy();
} catch (Exception e) {
throw new CsvBuilderException(e);
}
}
abstractCsvFieldMapperBuilder.invokeSetter(proxy);
return this.method;
}
public Method toMethod(AbstractCsvFieldMapperBuilder abstractCsvFieldMapperBuilder) throws CsvBuilderException {
if(abstractCsvFieldMapperBuilder.hasSetter()) {
// detect setter using proxy class
return invokeSetter(abstractCsvFieldMapperBuilder); // populates the 'method' field
}
// detect setter using reflection, based on the name
return super.toMethod(abstractCsvFieldMapperBuilder);
}
}