Skip to content

Commit e6be932

Browse files
committed
[feat] 回公司测试
1 parent ce640e3 commit e6be932

File tree

23 files changed

+125
-343
lines changed

23 files changed

+125
-343
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ captures/
3838

3939
# Keystore files
4040
*.jks
41+
42+
.repo/

.idea/gradle.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AutoRegister/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
apply plugin: 'groovy'
22

33
dependencies {
4-
compile gradleApi()
5-
compile localGroovy()
4+
implementation gradleApi()
5+
implementation localGroovy()
66
}
77

88
repositories {
99
mavenCentral()
1010
}
1111
dependencies {
12-
compile 'com.android.tools.build:gradle:3.4.1'
13-
compile 'commons-io:commons-io:2.4'
14-
compile 'org.javassist:javassist:3.20.0-GA'
12+
implementation 'com.android.tools.build:gradle:3.3.1'
13+
implementation 'commons-io:commons-io:2.4'
14+
implementation 'org.javassist:javassist:3.20.0-GA'
1515
}
1616
apply from: '../upload.gradle'

AutoRegister/src/main/groovy/com/kronos/autoregister/AutoRegisterTransform.groovy

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class AutoRegisterTransform extends Transform {
3636

3737
@Override
3838
boolean isIncremental() {
39-
return true
39+
return false
4040
}
4141

4242
@Override
@@ -45,7 +45,6 @@ class AutoRegisterTransform extends Transform {
4545
def inputs = transformInvocation.getInputs()
4646
def outputProvider = transformInvocation.outputProvider
4747
def context = transformInvocation.context
48-
def jarFile
4948
HashSet<String> items = new HashSet<>()
5049
inputs.each { TransformInput input ->
5150
input.jarInputs.each { JarInput jarInput ->
@@ -61,7 +60,8 @@ class AutoRegisterTransform extends Transform {
6160
directoryInput.file.eachFileRecurse {
6261
dir.traverse(type: FileType.FILES, nameFilter: ~/.*\.class/) {
6362
File classFile ->
64-
String absolutePath = classFile.absolutePath.replace(dir.absolutePath + File.separator, "")
63+
String absolutePath = classFile.absolutePath.replace(dir.absolutePath + File.separator,
64+
"")
6565
String className = path2Classname(absolutePath)
6666
if (checkClassName(className)) {
6767
//key为相对路径
@@ -124,8 +124,7 @@ class AutoRegisterTransform extends Transform {
124124
* @param jarFile
125125
* @return
126126
*/
127-
static boolean addJarInitList(File jarFile, HashSet<String> items) {
128-
boolean modified = false
127+
static void addJarInitList(File jarFile, HashSet<String> items) {
129128
if (jarFile) {
130129
/**
131130
* 读取原jar
@@ -139,18 +138,16 @@ class AutoRegisterTransform extends Transform {
139138
if (entryName.endsWith(".class")) {
140139
className = entryName.replace("/", ".").replace(".class", "")
141140
if (!checkPackage(className)) {
142-
return false
141+
return
143142
}
144143
if (checkClassName(className)) {
145144
items.add(entryName)
146-
modified = true
147145
break
148146
}
149147
}
150148
}
151149
file.close()
152150
}
153-
return modified
154151
}
155152

156153
/**
@@ -174,6 +171,7 @@ class AutoRegisterTransform extends Transform {
174171
className = entryName.replace("/", ".")
175172
.replace(".class", "")
176173
if (!checkPackage(className)) {
174+
file.close()
177175
return false
178176
}
179177
if (checkRouterInitClassName(className)) {

AutoRegister/src/main/groovy/com/kronos/autoregister/helper/ClassFilterVisitor.groovy

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import org.objectweb.asm.ClassVisitor
44
import org.objectweb.asm.MethodVisitor
55
import org.objectweb.asm.Opcodes
66

7+
import java.awt.Label
8+
79
class ClassFilterVisitor extends ClassVisitor {
810
private HashSet<String> classItems
911

@@ -12,29 +14,25 @@ class ClassFilterVisitor extends ClassVisitor {
1214
this.classItems = classItems
1315
}
1416

17+
@Override
18+
void visitEnd() {
19+
super.visitEnd()
20+
}
21+
1522
@Override
1623
MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
17-
if (name == "injectInit") {
24+
Log.info("name:$name desc:$desc ")
25+
if (name == "injectInit" && desc == "()V") {
1826
MethodVisitor methodVisitor = super.visitMethod(access, name, desc, signature, exceptions)
1927
MethodVisitor mv = new MethodVisitor(Opcodes.ASM5, methodVisitor) {
20-
/* @Override
21-
void visitCode() {
22-
super.visitCode()
23-
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false)
24-
methodVisitor.visitVarInsn(Opcodes.LSTORE, 1)
25-
}*/
26-
2728
@Override
28-
void visitInsn(int opcode) {
29-
if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN)
30-
|| opcode == Opcodes.ATHROW) {
31-
classItems.each { String input ->
32-
input = input.replace(".class", "")
33-
Log.info("item:" + input)
34-
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, input, "init", "()V;", false)
35-
}
29+
void visitCode() {
30+
classItems.each { String input ->
31+
input = input.replace(".class", "")
32+
Log.info("item:" + input)
33+
// mv.visitMethodInsn(Opcodes.INVOKESTATIC, input, "init", "()V;", false)
3634
}
37-
methodVisitor.visitInsn(opcode)
35+
super.visitCode()
3836
}
3937
}
4038
return mv

AutoRegister/src/main/groovy/com/kronos/autoregister/helper/InjectHelper.groovy

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ class InjectHelper {
2121
this.classItems = classItems
2222
}
2323

24+
void setClassItems(classItems) {
25+
this.classItems = classItems
26+
}
2427

2528
private byte[] modifyClass(byte[] srcClass) throws IOException {
2629
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS)
2730
ClassVisitor methodFilterCV = new ClassFilterVisitor(classWriter, classItems)
2831
ClassReader cr = new ClassReader(srcClass)
29-
cr.accept(methodFilterCV, ClassReader.EXPAND_FRAMES)
32+
cr.accept(methodFilterCV, 0)
3033
return classWriter.toByteArray()
3134
}
3235

@@ -58,6 +61,7 @@ class InjectHelper {
5861
className = AutoRegisterTransform.path2Classname(entryName)
5962
if (checkRouterInitClassName(className)) {
6063
try {
64+
Log.info("modifyClass $entryName")
6165
modifiedClassBytes = modifyClass(sourceClassBytes)
6266
} catch (Exception e) {
6367
e.printStackTrace()
@@ -67,7 +71,6 @@ class InjectHelper {
6771
if (modifiedClassBytes == null) {
6872
jarOutputStream.write(sourceClassBytes)
6973
} else {
70-
Log.info("modify:RouterLoader")
7174
jarOutputStream.write(modifiedClassBytes)
7275
}
7376
jarOutputStream.closeEntry()
@@ -79,6 +82,6 @@ class InjectHelper {
7982

8083
static boolean checkRouterInitClassName(String className) {
8184
String dexClassName = "com.kronos.router.RouterLoader"
82-
return dexClassName.equals(className)
85+
return dexClassName == className
8386
}
8487
}

RouterLib/src/main/java/com/kronos/router/RouterBind.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,5 @@ package com.kronos.router
66
*/
77

88
object RouterBind {
9-
fun bind(className: String) {
10-
try {
11-
val routerInit = Class.forName(className)
12-
val method = routerInit.getMethod("init")
13-
method.invoke(null)
14-
} catch (e: Exception) {
15-
e.printStackTrace()
16-
}
179

18-
}
1910
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.kronos.router;
2+
3+
public class RouterLoader {
4+
5+
public void attach() {
6+
injectInit();
7+
}
8+
9+
void injectInit() {
10+
11+
}
12+
public boolean isLoadingFinish(){
13+
return true;
14+
}
15+
}

RouterLib/src/main/java/com/kronos/router/RouterLoader.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.

RouterLib/src/main/java/com/kronos/router/interceptor/RouterInterceptor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ private RouterParams getParams(String url, Map<String, HostParams> hosts) throws
5151
Collections.sort(params, new Comparator<RouterParams>() {
5252
@Override
5353
public int compare(RouterParams o1, RouterParams o2) {
54-
return o1.getWeight().compareTo(o2.getWeight());
54+
Integer o1Weight = o1.getWeight();
55+
Integer o2Weight = o2.getWeight();
56+
return o1Weight.compareTo(o2Weight);
5557
}
5658
});
5759
routerParams = params.get(0);

RouterLib/src/main/java/com/kronos/router/model/HostParams.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,20 @@ import java.util.HashMap
66
* Created by zhangyang on 16/7/16.
77
*/
88
class HostParams(val host: String) {
9-
private val _routes = HashMap<String, RouterOptions>()
10-
11-
val routes: Map<String, RouterOptions>
12-
get() = _routes
9+
val routes = HashMap<String, RouterOptions>()
1310

1411
fun setRoute(path: String, options: RouterOptions) {
1512
if (routes.containsKey(path)) {
16-
val oldOptions = _routes[path]
17-
if (oldOptions!!.weight < options.weight) {
18-
_routes.put(path, options)
13+
val oldOptions = routes[path]
14+
if (oldOptions?.weight ?: 0 < options.weight) {
15+
routes[path] = options
1916
}
2017
} else {
21-
_routes.put(path, options)
18+
routes[path] = options
2219
}
2320
}
2421

2522
fun getOptions(path: String): RouterOptions? {
26-
return _routes[path]
23+
return routes[path]
2724
}
2825
}

RouterLib/src/main/java/com/kronos/router/model/RouterOptions.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,32 @@ import com.kronos.router.RouterCallback
99
* Created by zhangyang on 16/7/16.
1010
*/
1111
class RouterOptions {
12+
1213
var openClass: Class<out Activity>? = null
1314
var callback: RouterCallback? = null
14-
private var _defaultParams: Bundle? = null
15+
private val _defaultParams: Bundle by lazy {
16+
Bundle()
17+
}
1518
var weight = 0
1619

1720
var defaultParams: Bundle?
1821
get() = this._defaultParams
1922
set(defaultParams) {
20-
if (_defaultParams == null) {
21-
_defaultParams = Bundle()
22-
}
23-
if (defaultParams != null) {
24-
_defaultParams!!.putAll(defaultParams)
25-
}
23+
24+
_defaultParams.putAll(defaultParams)
25+
2626
}
2727

2828
constructor() {
29-
if (_defaultParams == null) {
30-
_defaultParams = Bundle()
31-
}
32-
}
3329

30+
}
3431

3532
constructor(defaultParams: Bundle) {
3633
this.defaultParams = defaultParams
3734
}
3835

3936

4037
fun putParams(key: String, value: String) {
41-
_defaultParams!!.putString(key, value)
38+
_defaultParams.putString(key, value)
4239
}
4340
}

RouterLib/src/main/java/com/kronos/router/model/RouterParams.kt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,18 @@ package com.kronos.router.model
55
*/
66
class RouterParams {
77
var url: String? = null
8-
private var weight: Int = 0
8+
var weight: Int = 0
99
var routerOptions: RouterOptions? = null
1010
var openParams: Map<String, String>? = null
1111

1212
val realPath: String
1313
get() {
1414
try {
15-
16-
return url!!.subSequence(1, url!!.length).toString()
15+
return url?.let { it.subSequence(1, it.length).toString() } ?: ""
1716
} catch (e: Exception) {
1817
e.printStackTrace()
1918
}
2019
return ""
2120
}
2221

23-
fun getWeight(): Int? {
24-
return weight
25-
}
26-
27-
fun setWeight(weight: Int) {
28-
this.weight = weight
29-
}
3022
}

0 commit comments

Comments
 (0)