All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.skjolber.stcsv.builder.ByteBuddySetterProjectionHelper Maven / Gradle / Ivy

There is a newer version: 1.0.25
Show newest version
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);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy