Skip to content

Refactor #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.microsphere.mybatis.plugin;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.PluginException;
import org.apache.ibatis.plugin.Signature;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static io.microsphere.collection.ListUtils.ofList;
import static io.microsphere.text.FormatUtils.format;
import static java.lang.reflect.Proxy.getInvocationHandler;
import static org.apache.ibatis.util.MapUtil.computeIfAbsent;

/**
* The utilities class of {@link Plugin}
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/>
* @see Plugin
* @see Intercepts
* @see Signature
* @since 1.0.0
*/
public abstract class Plugins {

/**
* The valid target classes for {@link Interceptor}
*/
public static final List<Class<?>> TARGET_CLASSES = ofList(Executor.class, ParameterHandler.class,
ResultSetHandler.class, StatementHandler.class);

/**
* The size of {@link #TARGET_CLASSES}
*/
public static final int TARGET_CLASSES_SIZE = TARGET_CLASSES.size();

/**
* Get the {@link Signature} map from the specified {@link Interceptor} that was annotated {@link Intercepts}
*
* @param interceptor the {@link Interceptor} that was annotated {@link Intercepts}
* @return
* @throws PluginException if No {@link Intercepts @Intercepts} annotation was found in interceptor or
* no {@link Signature @Signature} was found in the {@link Intercepts @Intercepts}
* @see Plugin#getSignatureMap(Interceptor)
*/
public static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) throws PluginException {
return getSignatureMap(interceptor.getClass());
}

/**
* Get the {@link Signature} map from the specified {@link Interceptor} that was annotated {@link Intercepts}
*
* @param interceptorClass the {@link Interceptor} that was annotated {@link Intercepts}
* @return
* @throws PluginException if No {@link Intercepts @Intercepts} annotation was found in interceptorClass or
* no {@link Signature @Signature} was found in the {@link Intercepts @Intercepts}
* @see Plugin#getSignatureMap(Interceptor)
*/
public static Map<Class<?>, Set<Method>> getSignatureMap(Class<? extends Interceptor> interceptorClass) throws PluginException {
Intercepts interceptsAnnotation = interceptorClass.getAnnotation(Intercepts.class);
// issue #251
if (interceptsAnnotation == null) {
throw newPluginException("No @Intercepts annotation was found in interceptorClass : {}", interceptorClass.getName());
}
Signature[] sigs = interceptsAnnotation.value();
// @Intercepts usually specifies one or two targets, so HashMap can be optimized
Map<Class<?>, Set<Method>> signatureMap = new HashMap<>(TARGET_CLASSES_SIZE / 2, 1.0f);
for (Signature sig : sigs) {
Class<?> targetType = sig.type();
if (!TARGET_CLASSES.contains(targetType)) {
throw newPluginException("The @Intercepts#type() = {} must be one of the target classes : {}", targetType.getName(), TARGET_CLASSES);
}
Set<Method> methods = computeIfAbsent(signatureMap, targetType, k -> new HashSet<>());
String methodName = sig.method();
try {
Method method = targetType.getMethod(methodName, sig.args());
methods.add(method);
} catch (NoSuchMethodException e) {
throw new PluginException(format("Could not find method on {} named {}", targetType, methodName), e);
}
}
return signatureMap;
}

/**
* Get the {@link Plugin} from the specified proxy
*
* @param proxy the specified proxy
* @return <code>null</code> if the proxy is not a {@link Plugin}
*/
public static Plugin getPlugin(Object proxy) {
if (proxy instanceof Proxy) {
InvocationHandler invocationHandler = getInvocationHandler(proxy);
if (invocationHandler instanceof Plugin) {
return (Plugin) invocationHandler;
}
}
return null;
}

protected static PluginException newPluginException(String messagePattern, Object... args) {
return new PluginException(format(messagePattern, args));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import io.microsphere.mybatis.executor.LogggingExecutorInterceptor;
import io.microsphere.mybatis.executor.LoggingExecutorFilter;
import io.microsphere.mybatis.executor.TestExecutorFilter;
import io.microsphere.mybatis.test.AbstractMyBatisTest;
import io.microsphere.mybatis.test.DefaultMapperTest;
import org.apache.ibatis.session.Configuration;

import java.util.Properties;
Expand All @@ -33,7 +33,7 @@
* @see InterceptingExecutorInterceptor
* @since 1.0.0
*/
public class InterceptingExecutorInterceptorTest extends AbstractMyBatisTest {
public class InterceptingExecutorInterceptorTest extends DefaultMapperTest {

public static final String TEST_PROPERTY_KEY = "test.class";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.microsphere.mybatis.plugin;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Invocation;

/**
* No-Operation {@link Interceptor}
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/>
* @see Interceptor
* @since 1.0.0
*/
public class NoOpInterceptor implements Interceptor {

@Override
public Object intercept(Invocation invocation) throws Throwable {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.microsphere.mybatis.plugin;

import io.microsphere.mybatis.test.executor.LoggingExecutor;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.PluginException;
import org.apache.ibatis.plugin.Signature;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.Map;
import java.util.Set;

import static io.microsphere.mybatis.plugin.Plugins.TARGET_CLASSES;
import static io.microsphere.mybatis.plugin.Plugins.TARGET_CLASSES_SIZE;
import static io.microsphere.mybatis.plugin.Plugins.getPlugin;
import static io.microsphere.mybatis.plugin.Plugins.getSignatureMap;
import static org.apache.ibatis.plugin.Plugin.wrap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* {@link Plugins} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/>
* @see Plugins
* @since 1.0.0
*/
public class PluginsTest {

@Test
public void testTargetClasses() {
assertEquals(4, TARGET_CLASSES.size());
assertEquals(TARGET_CLASSES_SIZE, TARGET_CLASSES.size());
assertTrue(TARGET_CLASSES.contains(Executor.class));
assertTrue(TARGET_CLASSES.contains(ParameterHandler.class));
assertTrue(TARGET_CLASSES.contains(ResultSetHandler.class));
assertTrue(TARGET_CLASSES.contains(StatementHandler.class));
}

@Test
public void testGetSignatureMap() {
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(new TestAnnotatedExecutorInterceptor());
assertEquals(1, signatureMap.size());
assertTrue(signatureMap.containsKey(Executor.class));
assertFalse(signatureMap.containsKey(ParameterHandler.class));
assertFalse(signatureMap.containsKey(ResultSetHandler.class));
assertFalse(signatureMap.containsKey(StatementHandler.class));
Set<Method> methods = signatureMap.get(Executor.class);
assertEquals(3, methods.size());
}

@Test
public void testGetSignatureMapOnFailed() {
assertThrows(PluginException.class, () -> getSignatureMap(new NoOpInterceptor()));
assertThrows(PluginException.class, () -> getSignatureMap(new WrongTargetSignatureInterceptor()));
assertThrows(PluginException.class, () -> getSignatureMap(new WrongMethodSignatureInterceptor()));
}


@Test
public void testGetPlugin() throws SQLException {
Executor executor = newExecutor();
Object proxy = wrap(executor, new TestAnnotatedExecutorInterceptor());
assertTrue(proxy instanceof Executor);

Plugin plugin = getPlugin(proxy);
assertNotNull(plugin);

Executor proxyExecutor = (Executor) proxy;
assertEquals(0, proxyExecutor.update(null, null));

// No Proxy
proxy = wrap(executor, new SignatureInterceptor());
assertSame(executor, proxy);
plugin = getPlugin(proxy);
assertNull(plugin);
}

private Executor newExecutor() {
return new LoggingExecutor();
}

@Intercepts(
value = {}
)
static class SignatureInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
return invocation.proceed();
}
}


@Intercepts(
@Signature(
type = Object.class,
method = "equals",
args = {Object.class}
)
)
static class WrongTargetSignatureInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
return invocation.proceed();
}
}

@Intercepts(
@Signature(
type = Executor.class,
method = "methodNotFound",
args = {}
)
)
static class WrongMethodSignatureInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
return invocation.proceed();
}
}

}
Loading