[dev_arch_opt_3.0] 代码逻辑优化

This commit is contained in:
renwj
2023-03-14 23:12:09 +08:00
parent d60a7c3429
commit 4fdbe43cae
6 changed files with 36 additions and 49 deletions

View File

@@ -64,10 +64,7 @@ if (!isAndroidTestBuild()) {
memory_leak {
enable true
}
object_hashcode {
enable true
}
TextView_setTextProxy {
textview_opt {
enable true
}
}

View File

@@ -1,38 +0,0 @@
package com.mogo.launcher.lancet;
import androidx.annotation.Keep;
import com.knightboost.lancet.api.Scope;
import com.knightboost.lancet.api.annotations.Group;
import com.knightboost.lancet.api.annotations.ImplementedInterface;
import com.knightboost.lancet.api.annotations.NameRegex;
import com.knightboost.lancet.api.annotations.ReplaceInvoke;
import com.knightboost.lancet.api.annotations.TargetClass;
import com.knightboost.lancet.api.annotations.TargetMethod;
import com.knightboost.lancet.api.annotations.Weaver;
import java.util.concurrent.ConcurrentHashMap;
@Weaver
@Group("object_hashcode")
@Keep
public class ObjectHashCodeProxy {
public static final ConcurrentHashMap<String, Integer> hashCodes = new ConcurrentHashMap<>();
@NameRegex("(com\\.mogo\\.eagle\\.core\\.utilcode\\.util\\.ThreadUtils|com\\.mogo\\.eagle\\.core\\.utilcode\\.util\\.UiThreadHandler)")
@TargetClass(value = "java.lang.Object",scope = Scope.SELF)
@TargetMethod(methodName = "hashCode")
@ReplaceInvoke
public static int hashCodeProxy(Object r) {
Class<?> clazz = r.getClass();
String className = clazz.getName();
if (hashCodes.containsKey(className)) {
Integer value = hashCodes.get(className);
if (value != null) {
return value;
}
}
int hashCode = System.identityHashCode(r);
hashCodes.put(className, hashCode);
return hashCode;
}
}

View File

@@ -3,7 +3,6 @@ package com.mogo.launcher.lancet;
import android.os.Looper;
import android.util.Log;
import android.widget.TextView;
import com.knightboost.lancet.api.Scope;
import com.knightboost.lancet.api.annotations.Group;
import com.knightboost.lancet.api.annotations.NameRegex;
@@ -13,7 +12,7 @@ import com.knightboost.lancet.api.annotations.TargetMethod;
import com.knightboost.lancet.api.annotations.Weaver;
@Weaver
@Group("TextView_setTextProxy")
@Group("textview_opt")
public class TextViewSetTextOpt {
@NameRegex("(com\\.mogo|com\\.zhidao|com\\.elegant|com\\.zhidaoauto|com\\.zhjt).*")
@@ -31,6 +30,7 @@ public class TextViewSetTextOpt {
sb.setLength(sb.length() - 1);
}
Log.e("ALERT", sb.toString());
return;
}
CharSequence old = view.getText();
if (old != null && !old.equals(text)) {

View File

@@ -0,0 +1,28 @@
package com.mogo.eagle.core.utilcode.util;
import java.util.concurrent.ConcurrentHashMap;
/**
* 根据对象的类名返回一致的hashCode
* 如果缓存中不存在则创建hashCode并存入缓存
* 如果缓存中存在,直接用缓存数据
*/
public class ObjectHashCodeUtils {
private static final ConcurrentHashMap<String, Integer> hashCodes = new ConcurrentHashMap<>();
public static int getHashCodeIfNeed(Object obj) {
if (obj == null) {
return -1;
}
String name = obj.getClass().getName();
Integer value = hashCodes.get(name);
if (value != null) {
return value;
}
int hashCode = System.identityHashCode(obj);
hashCodes.put(name, hashCode);
return hashCode;
}
}

View File

@@ -66,7 +66,7 @@ public final class ThreadUtils {
if (Looper.myLooper() == Looper.getMainLooper()) {
r.run();
} else {
int what = r.hashCode();
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
HANDLER.removeMessages(what);
Message msg = Message.obtain(HANDLER, r);
msg.what = what;
@@ -75,7 +75,7 @@ public final class ThreadUtils {
}
public static void runOnUiThreadDelayed(final Runnable r, long delayMillis) {
int what = r.hashCode();
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
HANDLER.removeMessages(what);
Message msg = Message.obtain(HANDLER, r);
msg.what = what;

View File

@@ -11,7 +11,7 @@ public class UiThreadHandler {
private UiThreadHandler() { }
public static boolean post( Runnable r ) {
int what = r.hashCode();
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
sUiHandler.removeMessages(what);
Message msg = Message.obtain(sUiHandler, r);
msg.what = what;
@@ -19,7 +19,7 @@ public class UiThreadHandler {
}
public static boolean postDelayed( Runnable r, long delayMillis ) {
int what = r.hashCode();
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
sUiHandler.removeMessages(what);
Message msg = Message.obtain(sUiHandler, r);
msg.what = what;
@@ -27,7 +27,7 @@ public class UiThreadHandler {
}
public static void removeCallbacks( Runnable r ) {
int what = r.hashCode();
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
sUiHandler.removeMessages(what);
}
}