io.github.kiryu1223.plugin.builder.AotFastCreator Maven / Gradle / Ivy
package io.github.kiryu1223.plugin.builder;
import io.github.kiryu1223.drink.core.builder.DefaultBeanCreator;
import io.github.kiryu1223.drink.core.builder.ISetterCaller;
import io.github.kiryu1223.drink.core.metaData.MetaData;
import io.github.kiryu1223.drink.core.metaData.MetaDataCache;
import org.noear.solon.core.runtime.NativeDetector;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.function.Supplier;
public class AotFastCreator extends DefaultBeanCreator
{
public AotFastCreator(Class target)
{
super(target);
}
@Override
public Supplier initBeanCreator(Class target)
{
if (target.isAnonymousClass())
{
return unsafeCreator(target);
}
else
{
// aot下我们不能使用方法句柄
if (NativeDetector.inNativeImage())
{
MetaData metaData = MetaDataCache.getMetaData(target);
Constructor constructor = (Constructor) metaData.getConstructor();
return () ->
{
try
{
return constructor.newInstance();
}
catch (InstantiationException | IllegalAccessException | InvocationTargetException e)
{
throw new RuntimeException(e);
}
};
}
else
{
return methodHandleCreator(target);
}
}
}
@Override
protected ISetterCaller initBeanSetter(String property)
{
if (target.isAnonymousClass())
{
return methodBeanSetter(property);
}
else
{
// aot下我们不能使用方法句柄
if (NativeDetector.inNativeImage())
{
return methodBeanSetter(property);
}
else
{
return methodHandleBeanSetter(property);
}
}
}
}