sharding-jdbc中WrapperAdapter的作用是什么
本篇文章为大家展示了sharding-jdbc中WrapperAdapter的作用是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
Wrapper
jdk-12.jdk/Contents/Home/lib/src.zip!/java.sql/java/sql/Wrapper.java
public interface Wrapper {T unwrap(java.lang.Class iface) throws java.sql.SQLException; boolean isWrapperFor(java.lang.Class> iface) throws java.sql.SQLException;}
Wrapper接口定义了unwrap、isWrapperFor方法
WrapperAdapter
incubator-shardingsphere-4.0.0-RC1/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/WrapperAdapter.java
public abstract class WrapperAdapter implements Wrapper { private final CollectionjdbcMethodInvocations = new ArrayList<>(); @SuppressWarnings("unchecked") @Override public final T unwrap(final Class iface) throws SQLException { if (isWrapperFor(iface)) { return (T) this; } throw new SQLException(String.format("[%s] cannot be unwrapped as [%s]", getClass().getName(), iface.getName())); } @Override public final boolean isWrapperFor(final Class> iface) { return iface.isInstance(this); } /** * record method invocation. * * @param targetClass target class * @param methodName method name * @param argumentTypes argument types * @param arguments arguments */ @SneakyThrows public final void recordMethodInvocation(final Class> targetClass, final String methodName, final Class>[] argumentTypes, final Object[] arguments) { jdbcMethodInvocations.add(new JdbcMethodInvocation(targetClass.getMethod(methodName, argumentTypes), arguments)); } /** * Replay methods invocation. * * @param target target object */ public final void replayMethodsInvocation(final Object target) { for (JdbcMethodInvocation each : jdbcMethodInvocations) { each.invoke(target); } }}
WrapperAdapter声明实现java.sql.Wrapper接口,它定义了JdbcMethodInvocation集合;recordMethodInvocation方法会往jdbcMethodInvocations添加JdbcMethodInvocation;replayMethodsInvocation方法则会挨个执行JdbcMethodInvocation的invoke方法
JdbcMethodInvocation
incubator-shardingsphere-4.0.0-RC1/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/invocation/JdbcMethodInvocation.java
@RequiredArgsConstructorpublic class JdbcMethodInvocation { @Getter private final Method method; @Getter private final Object[] arguments; /** * Invoke JDBC method. * * @param target target object */ @SneakyThrows public void invoke(final Object target) { method.invoke(target, arguments); }}
JdbcMethodInvocation的invoke方法执行的是method.invoke
小结
WrapperAdapter声明实现java.sql.Wrapper接口,它定义了JdbcMethodInvocation集合;recordMethodInvocation方法会往jdbcMethodInvocations添加JdbcMethodInvocation;replayMethodsInvocation方法则会挨个执行JdbcMethodInvocation的invoke方法
上述内容就是sharding-jdbc中WrapperAdapter的作用是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。