插件支撑正则表达式
This commit is contained in:
xpc 2025-02-01 19:53:27 +08:00
parent ae6b377c7c
commit 1531775207
4 changed files with 88 additions and 11 deletions

View File

@ -39,4 +39,10 @@ public @interface Signature {
*/
Class<?>[] args();
/**
* use regexp
*
* @return java method name is use regexp
*/
boolean useRegexp() default false;
}

View File

@ -3,6 +3,8 @@ package org.dromara.easyes.extension.plugins;
import org.dromara.easyes.annotation.Intercepts;
import org.dromara.easyes.annotation.Signature;
import org.dromara.easyes.common.exception.EasyEsException;
import org.dromara.easyes.common.utils.CollectionUtils;
import org.dromara.easyes.common.utils.ExceptionUtils;
import org.dromara.easyes.extension.context.Interceptor;
import org.dromara.easyes.extension.context.Invocation;
@ -10,10 +12,9 @@ import org.dromara.easyes.extension.context.Invocation;
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.Map;
import java.util.Set;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* <p>
@ -76,14 +77,26 @@ public class Plugin implements InvocationHandler {
// 检查被@Signature标记的方法是否存在
for (Signature sig : sigs) {
Set<Method> methods = signatureMap.computeIfAbsent(sig.type(), k -> new HashSet<>());
try {
Method method = sig.type().getMethod(sig.method(), sig.args());
methods.add(method);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
if (sig.useRegexp()) {
Pattern pattern = Pattern.compile(sig.method());
Set<Method> methodSet = Arrays.stream(sig.type().getMethods())
.filter(item -> pattern.matcher(item.getName()).matches())
.collect(Collectors.toSet());
if (CollectionUtils.isEmpty(methodSet)) {
throw new EasyEsException("This regular expression does not match any methods:" + sig.type() + " named " + sig.method());
}
methods.addAll(methodSet);
} else {
try {
Method method = sig.type().getMethod(sig.method(), sig.args());
methods.add(method);
} catch (NoSuchMethodException e) {
throw new EasyEsException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
}
}
}
return signatureMap;
}
}

View File

@ -0,0 +1,58 @@
package org.dromara.easyes.test.interceptor;
import org.dromara.easyes.annotation.Intercepts;
import org.dromara.easyes.annotation.Signature;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.dromara.easyes.core.kernel.BaseEsMapper;
import org.dromara.easyes.extension.context.Interceptor;
import org.dromara.easyes.extension.context.Invocation;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.RequestOptions;
import org.springframework.stereotype.Component;
/**
* 文件描述
*
* @ProductName: Hundsun HEP
* @ProjectName: easy-es
* @Package: org.dromara.easyes.test.interceptor
* @Description: note
* @Author: xingpc37977
* @Date: 2025/2/1 19:35
* @UpdateUser: xingpc37977
* @UpdateDate: 2025/2/1 19:35
* @UpdateRemark: The modified content
* @Version: 1.0
* <p>
* Copyright © 2025 Hundsun Technologies Inc. All Rights Reserved
**/
/**
* 测试方法使用正则表达式
*
* @author huangjy
*/
@Intercepts(
{
@Signature(type = BaseEsMapper.class, method = "select.*", args = {LambdaEsQueryWrapper.class}, useRegexp = true),
@Signature(type = BaseEsMapper.class, method = "search", args = {SearchRequest.class, RequestOptions.class}),
@Signature(type = BaseEsMapper.class, method = "insert|update", args = {Object.class}, useRegexp = true),
@Signature(type = BaseEsMapper.class, method = ".*ById", args = {Object.class}, useRegexp = true),
}
)
@Component
public class TenantLineInnerInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
// TODO 这里可以写你自己的拦截处理逻辑,此处仅打印
// Object[] args = invocation.getArgs();
// Object arg = args[0];
// if (arg instanceof LambdaEsQueryWrapper) {
// LambdaEsQueryWrapper wrapper = ((LambdaEsQueryWrapper) args[0]);
// wrapper.eq("tenantId", "1");
// return invocation.proceed();
// }
System.out.println("增则拦截方法");
return invocation.proceed();
}
}

View File

@ -15,11 +15,11 @@ import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.xcontent.XContentType;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;