|
|
@@ -13,6 +13,7 @@ import javassist.bytecode.Bytecode;
|
|
|
import javassist.bytecode.CodeAttribute;
|
|
|
import javassist.bytecode.CodeIterator;
|
|
|
import javassist.bytecode.ExceptionTable;
|
|
|
+import javassist.bytecode.annotation.Annotation;
|
|
|
import javassist.compiler.CompileError;
|
|
|
import javassist.compiler.Javac;
|
|
|
import winter.com.ideaaedi.classwinter.author.JustryDeng;
|
|
|
@@ -20,6 +21,7 @@ import winter.com.ideaaedi.classwinter.exception.ClassWinterException;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -59,9 +61,11 @@ public final class JavassistUtil {
|
|
|
boolean cleanOverCLass = encryptClassArgs.isCleanAnnotationOverClazz();
|
|
|
boolean cleanOverMethod = encryptClassArgs.isCleanAnnotationOverMethod();
|
|
|
boolean cleanOverField = encryptClassArgs.isCleanAnnotationOverField();
|
|
|
+ String toCleanAnnotationPrefix = encryptClassArgs.getToCleanAnnotationPrefix();
|
|
|
+
|
|
|
if (cleanOverCLass || cleanOverMethod || cleanOverField) {
|
|
|
try {
|
|
|
- clearAnnotation(ctClass, cleanOverCLass, cleanOverMethod, cleanOverField);
|
|
|
+ clearAnnotation(ctClass, cleanOverCLass, cleanOverMethod, cleanOverField, toCleanAnnotationPrefix);
|
|
|
} catch (Exception ignore) {
|
|
|
Logger.warn(JavassistUtil.class, "clean className [" + className + "] annotation fail. ignore.");
|
|
|
// ignore
|
|
|
@@ -113,23 +117,23 @@ public final class JavassistUtil {
|
|
|
* @param cleanOverMethod 是否清空方法上的注解
|
|
|
* @param cleanOverField 是否清空字段上的注解
|
|
|
*/
|
|
|
- private static void clearAnnotation(CtClass ctClass, boolean cleanOverCLass, boolean cleanOverMethod, boolean cleanOverField) {
|
|
|
+ private static void clearAnnotation(CtClass ctClass, boolean cleanOverCLass, boolean cleanOverMethod, boolean cleanOverField, String toCleanAnnotationPrefix) {
|
|
|
// 清空类上的注解
|
|
|
if (cleanOverCLass) {
|
|
|
- removeAnnotationsAttribute(ctClass.getClassFile().getAttributes());
|
|
|
+ removeAnnotationsAttribute(ctClass.getClassFile().getAttributes(), toCleanAnnotationPrefix);
|
|
|
}
|
|
|
// 清空方法上的注解
|
|
|
if (cleanOverMethod) {
|
|
|
CtMethod[] methods = ctClass.getDeclaredMethods();
|
|
|
for (CtMethod ctMethod : methods) {
|
|
|
- removeAnnotationsAttribute(ctMethod.getMethodInfo().getAttributes());
|
|
|
+ removeAnnotationsAttribute(ctMethod.getMethodInfo().getAttributes(), toCleanAnnotationPrefix);
|
|
|
}
|
|
|
}
|
|
|
// 清空字段上的注解
|
|
|
if (cleanOverField) {
|
|
|
CtField[] fields = ctClass.getDeclaredFields();
|
|
|
for (CtField declaredField : fields) {
|
|
|
- removeAnnotationsAttribute(declaredField.getFieldInfo().getAttributes());
|
|
|
+ removeAnnotationsAttribute(declaredField.getFieldInfo().getAttributes(), toCleanAnnotationPrefix);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -141,17 +145,36 @@ public final class JavassistUtil {
|
|
|
* @return 操作是否成功
|
|
|
*/
|
|
|
@SuppressWarnings("UnusedReturnValue")
|
|
|
- private static boolean removeAnnotationsAttribute(List<AttributeInfo> attributeInfoList) {
|
|
|
+ private static boolean removeAnnotationsAttribute(List<AttributeInfo> attributeInfoList, String toCleanAnnotationPrefix) {
|
|
|
if (attributeInfoList == null || attributeInfoList.size() == 0) {
|
|
|
return true;
|
|
|
}
|
|
|
- return attributeInfoList.removeIf(x -> {
|
|
|
+ // 匹配注解全部删除
|
|
|
+ if (StrUtil.isBlank(toCleanAnnotationPrefix)) {
|
|
|
+ return attributeInfoList.removeIf(x -> {
|
|
|
+ try {
|
|
|
+ return x instanceof AnnotationsAttribute;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ // 删除指定注解
|
|
|
+ StrUtil.strToSet(toCleanAnnotationPrefix, "\\|").forEach(x -> attributeInfoList.forEach(y -> {
|
|
|
try {
|
|
|
- return x instanceof AnnotationsAttribute;
|
|
|
+ if (y instanceof AnnotationsAttribute) {
|
|
|
+ AnnotationsAttribute annotationsAttribute = (AnnotationsAttribute) y;
|
|
|
+ Annotation[] annotations = annotationsAttribute.getAnnotations();
|
|
|
+ Annotation[] newAnnotations = Arrays.stream(annotations)
|
|
|
+ .filter(annotation -> !annotation.getTypeName().startsWith(x))
|
|
|
+ .toArray(Annotation[]::new);
|
|
|
+ annotationsAttribute.setAnnotations(newAnnotations);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- return false;
|
|
|
+ throw e;
|
|
|
}
|
|
|
- });
|
|
|
+ }));
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|