Skip to content

Commit

Permalink
1、完善新增的切面类型
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyJingFish committed May 10, 2024
1 parent 9e35953 commit e32b21a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeSpec
import java.lang.annotation.ElementType
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.util.Locale

class AndroidAopSymbolProcessor(private val codeGenerator: CodeGenerator,
Expand Down Expand Up @@ -441,6 +443,7 @@ class AndroidAopSymbolProcessor(private val codeGenerator: CodeGenerator,
clazzName = parent.toString()
parent = parent?.parent
}

if (symbol.parameters.isEmpty()){
throw IllegalArgumentException("注意:函数$className${symbol} 必须设置您想收集的类作为参数")
}else if (symbol.parameters.size != 1){
Expand Down Expand Up @@ -482,7 +485,7 @@ class AndroidAopSymbolProcessor(private val codeGenerator: CodeGenerator,
// logger.error("invokeClassName=$invokeClassName")
val parameter = symbol.parameters[0]
val collectClassName = "${parameter.type.resolve().declaration.packageName.asString()}.${parameter.type}"

clazzName += computeMD5("$symbol($collectClassName)")
val fileName = "${clazzName}\$\$AndroidAopClass";
val typeBuilder = TypeSpec.classBuilder(
fileName
Expand Down Expand Up @@ -551,4 +554,25 @@ class AndroidAopSymbolProcessor(private val codeGenerator: CodeGenerator,
private fun whatsMyName(name: String): FunSpec.Builder {
return FunSpec.builder(name).addModifiers(KModifier.FINAL)
}

private fun computeMD5(string: String): String? {
return try {
val messageDigest = MessageDigest.getInstance("MD5")
val digestBytes = messageDigest.digest(string.toByteArray())
bytesToHex(digestBytes)
} catch (var3: NoSuchAlgorithmException) {
throw IllegalStateException(var3)
}
}
private fun bytesToHex(bytes: ByteArray): String? {
val hexString = StringBuilder()
for (b in bytes) {
val hex = Integer.toHexString(0xff and b.toInt())
if (hex.length == 1) {
hexString.append('0')
}
hexString.append(hex)
}
return hexString.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ data class AopCollectCut(
val collectClassName: String,
val invokeClassName: String,
val invokeMethod: String,
)
){
fun getKey():String{
return invokeClassName + invokeMethod + collectClassName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ object WovenInfoUtils {
}

fun addCollectConfig(aopCollectCut: AopCollectCut){
aopCollectInfoMap[aopCollectCut.invokeClassName] = aopCollectCut
aopCollectInfoMap[aopCollectCut.getKey()] = aopCollectCut
}

fun addCollectClass(aopCollectCut: AopCollectClass){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -385,8 +387,13 @@ private void processCollectMethod(Set<? extends TypeElement> set, RoundEnvironme
throw new IllegalArgumentException("注意:函数"+element.getEnclosingElement()+"."+name1+" 参数必须设置一个");
}
VariableElement variableElement = executableElement.getParameters().get(0);

TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(element.getEnclosingElement().getSimpleName()+"$$AndroidAopClass")
String clazzName;
try {
clazzName = element.getEnclosingElement().getSimpleName()+computeMD5(name1+"("+variableElement.asType()+")");
} catch (NoSuchAlgorithmException e) {
clazzName = element.getEnclosingElement().getSimpleName().toString();
}
TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(clazzName+"$$AndroidAopClass")
.addAnnotation(AopClass.class)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
MethodSpec.Builder whatsMyName1 = whatsMyName(AOP_METHOD_NAME)
Expand All @@ -410,6 +417,23 @@ private void processCollectMethod(Set<? extends TypeElement> set, RoundEnvironme
}
}
}
private static String computeMD5(String message) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(message.getBytes());
return bytesToHex(digest);
}

private static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
private static MethodSpec.Builder whatsMyName(String name) {
return MethodSpec.methodBuilder(name)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ object InitCollect {
Collects.add(sub)
}

@AndroidAopCollectMethod
@JvmStatic
fun collect2(sub: SubApplication){
Collects.add(sub)
}

@MyAnno
fun init(application: Application){
Log.e("InitCollect","----init----");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public static void collect(SubApplication2 sub){
collects.add(sub);
}

@AndroidAopCollectMethod
public static void collect2(SubApplication2 sub){
collects.add(sub);
}

@MyAnno
public static void init(Application application){
Log.e("InitCollect2","----init----");
Expand Down

0 comments on commit e32b21a

Please sign in to comment.