huang
6 天以前 9571229a2013472dc701ecf5767f2873b36d8f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.mes.s7.enhanced;
 
import com.github.xingshuangs.iot.protocol.s7.serializer.S7Variable;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;
 
/**
 * @Author : zhoush
 * @Date: 2025/7/31 9:19
 * @Description:
 */
@Component
public class S7AnnotationProcessor implements BeanPostProcessor {
    @Autowired
    private S7AddressProperties addressProperties;
 
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Class<?> clazz = bean.getClass();
        // 处理类上的注解
        processClassAnnotations(clazz);
        // 处理字段上的注解
        for (Field field : clazz.getDeclaredFields()) {
            processFieldAnnotations(field);
        }
        return bean;
    }
 
    private void processClassAnnotations(Class<?> clazz) {
        if (clazz.isAnnotationPresent(S7Variable.class)) {
            S7Variable annotation = clazz.getAnnotation(S7Variable.class);
            updateAnnotationValue(annotation, determineAddress(annotation.address()));
        }
    }
 
    private void processFieldAnnotations(Field field) {
        if (field.isAnnotationPresent(S7Variable.class)) {
            S7Variable annotation = field.getAnnotation(S7Variable.class);
            updateAnnotationValue(annotation, determineAddress(annotation.address()));
        }
    }
 
    private String determineAddress(String addressKey) {
        // 如果addressKey是配置中的key,则从配置获取
        if (addressProperties.getAddress().containsKey(addressKey)) {
            return addressProperties.getAddress().get(addressKey);
        }
 
        // 尝试直接解析为数字
        try {
            return addressKey;
        } catch (NumberFormatException e) {
            // 返回默认地址
            return "";
        }
    }
 
    @SuppressWarnings("unchecked")
    private void updateAnnotationValue(S7Variable annotation, String newAddress) {
        try {
            InvocationHandler handler = Proxy.getInvocationHandler(annotation);
            Field memberValuesField = handler.getClass().getDeclaredField("memberValues");
            memberValuesField.setAccessible(true);
            Map<String, Object> memberValues = (Map<String, Object>) memberValuesField.get(handler);
            memberValues.put("address", newAddress);
        } catch (Exception e) {
            throw new RuntimeException("Failed to update S7Variable annotation", e);
        }
    }
}