添加实体类,利用自定义注解跟反射实现有参构造信息对象

This commit is contained in:
zayac 2023-09-09 01:57:34 +08:00
parent 9306091d3b
commit fe2a9612c1
7 changed files with 178 additions and 1 deletions

View File

@ -0,0 +1,15 @@
package com.zayac.changeurl.annotation;
import java.lang.annotation.*;
/**
* @author zayac
* @since 2023-09-08 23:16
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented // 表示该注解会被文档工具生成文档
public @interface MsgAnnotation {
String startWith() default "";
}

View File

@ -0,0 +1,9 @@
package com.zayac.changeurl.common;
/**
* @author zayac
* @since 2023-09-09 0:06
*/
public class Constant {
public static final String URL_PATTERN = "(https?://[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6})(:[0-9]{1,5})?";
}

View File

@ -0,0 +1,52 @@
package com.zayac.changeurl.entity;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.zayac.changeurl.annotation.MsgAnnotation;
import com.zayac.changeurl.common.Constant;
import lombok.Data;
import java.lang.reflect.Field;
/**
* @author zayac
* @since 2023-09-09 1:05
*/
@Data
public class HthMsgEntity {
@MsgAnnotation(startWith = "web")
private String pc;
@MsgAnnotation(startWith = "h5")
private String h5;
@MsgAnnotation(startWith = "全站APP")
private String app;
@MsgAnnotation(startWith = "体育APP")
private String tyApp;
public HthMsgEntity(String[] array) {
for (Field field : this.getClass().getDeclaredFields()) {
MsgAnnotation msgAnnotation = field.getAnnotation(MsgAnnotation.class);
if (msgAnnotation != null) {
// 获取注解的startWith属性值
String startWith = msgAnnotation.startWith();
String val = "";
for (int i = 0; i < array.length; i++) {
//如果以开头字符开头并且字符长度大于startWith认为在一行
if (array[i].startsWith(startWith) && StrUtil.cleanBlank(array[i]).length() > startWith.length()) {
val = ReUtil.getGroup1(Constant.URL_PATTERN, array[i]);
} else if (array[i].startsWith(startWith)) {
val = ReUtil.getGroup1(Constant.URL_PATTERN, array[i + 1]);
}
}
Assert.notBlank(val);
field.setAccessible(true);
try {
field.set(this, val);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}

View File

@ -0,0 +1,77 @@
package com.zayac.changeurl.entity;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReUtil;
import com.zayac.changeurl.annotation.MsgAnnotation;
import com.zayac.changeurl.common.Constant;
import lombok.Data;
import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.lang.reflect.Field;
import java.util.stream.Collectors;
/**
* @author zayac
* @since 2023-09-08 22:54
*/
@Data
public class KyMsgEntity {
private final String PREFER = "";
@MsgAnnotation(startWith = "WEB")
private List<String> pc;
@MsgAnnotation(startWith = "H5")
private List<String> h5;
@MsgAnnotation(startWith = "全站")
private List<String> app;
@MsgAnnotation(startWith = "体育")
private List<String> ty;
@MsgAnnotation(startWith = "WEB")
private String fljPc;
@MsgAnnotation(startWith = "H5")
private String fljH5;
public KyMsgEntity(String[] array) {
for (Field field : this.getClass().getDeclaredFields()) {
// 获取字段上的MsgAnnotation注解
MsgAnnotation msgAnnotation = field.getAnnotation(MsgAnnotation.class);
// 如果注解不为空说明该字段使用了MsgAnnotation注解
if (msgAnnotation != null) {
// 获取注解的startWith属性值
String startWith = msgAnnotation.startWith();
// 创建一个列表用于存储匹配的字符串
List<String> matched = new ArrayList<>();
// 遍历字符串数组查找以startWith开头的字符串
for (String s : array) {
if (s.startsWith(startWith)) {
// 如果找到匹配的字符串添加到列表中
matched.add(ReUtil.getGroup1(Constant.URL_PATTERN, s));
}
}
//断言 list中不含有null值防止修改数据为空
Assert.noNullElements(ArrayUtil.toArray(matched, String.class));
// 设置字段的访问权限为true以便赋值操作
field.setAccessible(true);
try {
// 判断字段的类型是否是List
if (field.getType().isAssignableFrom(List.class)) {
// 如果是List类型直接将匹配的列表赋值给该字段
matched = matched.stream().sorted(Comparator.comparing(str -> str.contains(PREFER))).collect(Collectors.toList());
field.set(this, matched);
} else {
// 如果不是List类型判断匹配的列表是否为空
if (!matched.isEmpty()) {
// 如果不为空取第一个元素赋值给该字段
field.set(this, ReUtil.getGroup1(Constant.URL_PATTERN, matched.get(0)));
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}

View File

@ -3,6 +3,7 @@ package com.zayac.changeurl.service;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.system.SystemUtil;
import com.zayac.changeurl.entity.Msg2JSEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
@ -90,7 +91,7 @@ public class ChangeURLServiceImpl implements ChangeURLService {
}
//处理传入参数
String[] strings = StrUtil.splitToArray(text, "\n");
String[] strings = StrUtil.splitToArray(text, SystemUtil.LINE_SEPARATOR);
List<String> msgKyWebList = getByStartWith(strings, msgKyWeb, URL_PATTERN, PREFER);
List<String> msgKyH5List = getByStartWith(strings, msgKyH5, URL_PATTERN, PREFER);

View File

@ -0,0 +1,12 @@
package com.zayac.changeurl.service;
/**
* @author zayac
* @since 2023-09-08 23:00
*/
public class KyMsgStrategy implements MsgStrategy{
@Override
public Object transform() {
return null;
}
}

View File

@ -0,0 +1,11 @@
package com.zayac.changeurl.service;
/**
* 定义一个飞机信息转化的接口
*
* @author zayac
* @since 2023-09-08 22:57
*/
public interface MsgStrategy<T> {
public T transform();
}