Vanco / SequencePlugin

SequencePlugin for IntelliJ IDEA
Other
686 stars 196 forks source link

There is no return information in the generated sequence diagram. #170

Closed YangAoLib closed 3 months ago

YangAoLib commented 8 months ago

Describe the bug I gnerated a sequence diagram, it has call the method arrow that actor to metod, but don't has return when the method return end.

To Reproduce Steps to reproduce the behavior:

  1. Open the bottom Java code.
  2. generate the sequence diagram.
  3. the diagram isn't that I want.

Expected behavior The diagram has the return action, and the alt that has nothing will not show. image

Screenshots image

Plugin & IDE info:

Additional context

import cn.hutool.core.util.RandomUtil; import edu.yangao.hedgehog.anno.Hedgehog; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component;

import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map;

@Aspect @Component @Slf4j public class HedgehogAspect {

private static final Map<String, HedgehogModel> SERVICE_MAP = new HashMap<>();

@Around("@annotation(edu.yangao.hedgehog.anno.Hedgehog)")
public Object hedgehogAround(ProceedingJoinPoint joinPoint) throws Throwable {
    // 获取被环绕的方法的反射信息 - 获取方法上面的注解
    Signature signature = joinPoint.getSignature();
    Method method = signature.getDeclaringType().getMethod(signature.getName());
    Hedgehog hedgehog = method.getAnnotation(Hedgehog.class);
    // 访问注解的属性
    String serviceName = hedgehog.serviceName();
    // 获取对应熔断器
    HedgehogModel hedgehogModel = SERVICE_MAP.merge(serviceName, new HedgehogModel(), (oldValue, newValue) -> oldValue);
    // 根据熔断器状态 进行操作
    switch (hedgehogModel.getStatus()) {
        case OPEN: // 熔断器开启
            // 直接返回备用数据
            return "刺猬熔断器: 租车失败";
        case CLOSE: // 熔断器关闭
            // 执行正常操作
            try {
                return joinPoint.proceed();
            } catch (Throwable e) {
                // 如果出现错误, 熔断器记数
                hedgehogModel.addFailCount();
                // 返回备用数据
                return "刺猬熔断器: 租车失败";
            }
        case HALF_OPEN: // 熔断器半开状态
            // 只选取部分(20%)流量进行请求 其余部分直接返回备用数据
            if (RandomUtil.randomInt(5) == 0) {
                log.info("执行部分请求");
                // 20% 概率进入此块代码
                // 执行正常操作
                try {
                    Object result = joinPoint.proceed();
                    // 如果正常执行了 那么将熔断器关闭
                    hedgehogModel.close();
                    return result;
                } catch (Throwable e) {
                    // 保持半开状态
                    // 返回备用数据
                    return "刺猬熔断器: 租车失败";
                }
            } else {
                // 直接返回备用数据
                return "刺猬熔断器: 租车失败";
            }
        default:
            // 直接返回备用数据
            return "刺猬熔断器: 租车失败";
    }
}

}

Vanco commented 6 months ago

Currently only method call are generated. other statement, such as return, variable definition, asignment will not generate. e.g.

int a = 2;
String s = "";
if (a > 0) {
s = "A is a positive number";
} else {
s = "A is a negative number or 0 ";
}

the code block will generate nothing. but, when there is a method call in it, e.g.

int a = 2;
String s = "";
if (a > 0) {
s = "A is a positive number";
System.out.println(s);
} else {
s = "A is a negative number or 0 ";
}

the alt (than branch) block will generate, because it has println() call in it. the else branch will ignored.

Another example: return 1; will not generate. but return sum(2); will generate sum() instead.

YangAoLib commented 6 months ago

This OPEN area was included when I exported the puml, but the RETURN was added by me. So why is the generated plot displayed with one less alt region than the exported puml?

Vanco commented 6 months ago

In fact, every branch has been generated, OPEN, CLOSE, HALF_OPEN, and default. But the drawing strategy is different from PlantUML, there is no call statement, and it is not drawn.