#feature #I581A1 @LiteflowMethod注解 新增新的cmpClass属性,可以定义方法级所生成的nodeComponent类型

This commit is contained in:
sorghum
2022-09-05 19:28:33 +08:00
parent 7f01be26f2
commit e079ae9427
5 changed files with 31 additions and 29 deletions

View File

@@ -1,5 +1,6 @@
package com.yomahub.liteflow.annotation;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.enums.LiteFlowMethodEnum;
import java.lang.annotation.*;
@@ -15,4 +16,10 @@ public @interface LiteflowMethod {
// 节点ID用于区分节点
// 默认为空 则按照Spring模式下BeanName为准。
String nodeId() default "";
/**
* cmp定义
*
*/
Class<? extends NodeComponent> cmpClass() default NodeComponent.class;
}

View File

@@ -54,14 +54,27 @@ public class ComponentProxy {
m -> m.getAnnotation(LiteflowMethod.class) != null
).map(m -> m.getAnnotation(LiteflowMethod.class)).collect(Collectors.groupingBy(LiteflowMethod::nodeId));
return methodListMap.entrySet().stream().map(entry -> {
String activeNodeId = StrUtil.isEmpty(entry.getKey()) ? nodeId : entry.getKey();
boolean isMethodCreate = !StrUtil.isEmpty(entry.getKey());
String activeNodeId = isMethodCreate ? entry.getKey() : nodeId;
List<LiteflowMethod> methodList = entry.getValue();
// 一个节点只能有一个定义NodeCmp类
List<? extends Class<? extends NodeComponent>> classes = methodList.stream().map(LiteflowMethod::cmpClass).distinct().collect(Collectors.toList());
boolean legal = classes.size() == 1;
if (!legal){
throw new LiteFlowException("The cmpClass of the same nodeId must be the same,you declared nodeId:" + activeNodeId + ",cmpClass:" + classes);
}
Class<?> cmpClass;
cmpClass = clazz;
// 判断是否是方法声明的组件
if (isMethodCreate){
cmpClass = methodList.iterator().next().cmpClass();
}
try {
//创建对象
//这里package进行了重设放到了被代理对象的所在目录
//生成的对象也加了上被代理对象拥有的注解
//被拦截的对象也根据被代理对象根据@LiteFlowMethod所标注的进行了动态判断
Object instance = new ByteBuddy().subclass(clazz)
Object instance = new ByteBuddy().subclass(cmpClass)
.name(StrUtil.format("{}.ByteBuddy${}${}",
ClassUtil.getPackage(bean.getClass()),
activeNodeId,

View File

@@ -30,23 +30,6 @@ public class LiteFlowProxyUtil {
//判断一个bean是否是声明式组件
public static boolean isDeclareCmp(Class<?> clazz){
//判断bean是否标记了@LiteflowCmpDefine,@LiteflowCondCmpDefine,LiteflowIfCmpDefine这3个标注之一
boolean flag1 = clazz.getAnnotation(LiteflowCmpDefine.class) != null
|| clazz.getAnnotation(LiteflowSwitchCmpDefine.class) != null
|| clazz.getAnnotation(LiteflowIfCmpDefine.class) != null;
if (!flag1){
return false;
}
//看超类是否是NodeComponent,NodeCondComponent,NodeIfComponent中的一个如果不是则说明满足条件。是的话也不满足
boolean flag2 = !ListUtil.toList(NodeComponent.class, NodeSwitchComponent.class, NodeIfComponent.class)
.contains(clazz.getSuperclass());
if (!flag2){
return false;
}
//查看bean里的method是否有方法标记了@LiteflowMethod标注
//这里的bean有可能是cglib加强过的class所以要先进行个判断
Class<?> targetClass;
@@ -55,11 +38,10 @@ public class LiteFlowProxyUtil {
}else{
targetClass = clazz;
}
boolean flag3 = Arrays.stream(targetClass.getMethods()).anyMatch(
// 判断是否有方法标记了@LiteflowMethod标注
return Arrays.stream(targetClass.getMethods()).anyMatch(
method -> method.getAnnotation(LiteflowMethod.class) != null
);
return flag3;
}
//对一个满足声明式的bean进行代理,生成代理类数组
@@ -84,8 +66,7 @@ public class LiteFlowProxyUtil {
proxy = new ComponentProxy(nodeId, bean, NodeIfComponent.class);
return proxy.getProxyList();
}
throw new RuntimeException();
return new ComponentProxy(nodeId, bean, NodeIfComponent.class).getProxyList();
}catch (Exception e){
String errMsg = StrUtil.format("Error while proxying bean[{}]",bean.getClass().getName());
LOG.error(errMsg);

View File

@@ -1,8 +1,8 @@
package com.yomahub.liteflow.test.cmpMultiNode.cmp;
import com.yomahub.liteflow.annotation.LiteflowCmpDefine;
import com.yomahub.liteflow.annotation.LiteflowMethod;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.core.NodeIfComponent;
import com.yomahub.liteflow.enums.LiteFlowMethodEnum;
import org.springframework.context.annotation.Configuration;
@@ -12,21 +12,20 @@ import org.springframework.context.annotation.Configuration;
* @author sorghum
*/
@Configuration
@LiteflowCmpDefine
public class MultiCmpConfiguration {
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS,nodeId = "a")
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS,nodeId = "a",cmpClass = NodeComponent.class)
public void processA(NodeComponent bindCmp) {
System.out.println("ACmp executed!");
}
@LiteflowMethod(value = LiteFlowMethodEnum.IS_ACCESS,nodeId = "a")
@LiteflowMethod(value = LiteFlowMethodEnum.IS_ACCESS,nodeId = "a",cmpClass = NodeComponent.class)
public boolean isAccessA(NodeComponent bindCmp) {
System.out.println("ACmp isAccessA!");
return true;
}
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS,nodeId = "b")
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS,nodeId = "b",cmpClass = NodeIfComponent.class)
public void processB(NodeComponent bindCmp) {
System.out.println("BCmp executed!");
}

View File

@@ -0,0 +1,2 @@
liteflow.rule-source=cmpMulti/flow.el.xml
liteflow.slot-size=512