This commit is contained in:
六如
2024-09-16 21:55:10 +08:00
parent a14ac9e3c9
commit 50e576b9fc
233 changed files with 24635 additions and 7185 deletions

View File

@@ -16,11 +16,15 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 接口注册
*
* @author 六如
*/
public class ApiRegister {
@@ -40,37 +44,56 @@ public class ApiRegister {
for (Object serviceObj : objects) {
Class<?> objClass = serviceObj.getClass();
if (objClass.getAnnotation(DubboService.class) == null) {
continue;
}
checkClass(objClass);
doWithMethod(objClass, (interfaceClass, method, open) ->
this.regApi(appName, interfaceClass, method, open));
}
}
protected void doWithMethod(Class<?> objClass, RegisterCallback callback) {
for (Method method : objClass.getMethods()) {
Class<?> declaringClass = method.getDeclaringClass();
if (declaringClass == Object.class) {
continue;
if (objClass.getAnnotation(DubboService.class) == null) {
return;
}
checkClass(objClass);
// 只找接口中的注解,暂时用不到,先注释
/*for (Class<?> anInterface : objClass.getInterfaces()) {
for (Method method : anInterface.getMethods()) {
Open open = method.getAnnotation(Open.class);
if (open != null) {
callback.callback(anInterface, method, open);
}
}
for (Class<?> anInterface : declaringClass.getInterfaces()) {
}*/
Set<Method> cache = new HashSet<>();
Class<?>[] interfaces = objClass.getInterfaces();
for (Class<?> interfaceClass : interfaces) {
for (Method method : interfaceClass.getMethods()) {
Open open = method.getAnnotation(Open.class);
if (open != null) {
cache.add(method);
callback.callback(interfaceClass, method, open);
}
}
}
// 实现类方法有注解,接口方法没有注解
for (Method method : objClass.getMethods()) {
for (Class<?> interfaceClass : interfaces) {
try {
Method parentMethod = anInterface.getMethod(method.getName(), method.getParameterTypes());
// 接口中方法签名一致的方法,有返回表示找到,找不到抛出NoSuchMethodException
Method parentMethod = interfaceClass.getMethod(method.getName(), method.getParameterTypes());
Open open = method.getAnnotation(Open.class);
if (open == null) {
open = parentMethod.getAnnotation(Open.class);
}
if (open != null) {
callback.callback(anInterface, parentMethod, open);
// 存在且未添加过
if (open != null && !cache.contains(parentMethod)) {
callback.callback(interfaceClass, parentMethod, open);
}
} catch (NoSuchMethodException e) {
// ignore
}
}
}
cache.clear();
}