Skip to content

Commit

Permalink
plugin fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JuneLeo committed Jan 3, 2019
1 parent 9b89511 commit 5bc89ea
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// classpath 'com.mock.internel:MockPlugin:1.0' //依赖本地仓库
classpath 'com.github.JuneLeo.mock-method:mock-method-plugin:1.1.4' //依赖jitpack
classpath 'com.github.JuneLeo.mock-method:mock-method-plugin:1.1.5' //依赖jitpack
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
}
Expand All @@ -30,4 +30,4 @@ allprojects { project ->



apply from:'debug.gradle'
//apply from:'debug.gradle'
44 changes: 22 additions & 22 deletions mock-method-plugin/src/main/groovy/com/mock/internel/Inject.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,35 @@ import org.gradle.api.Project
class Inject {
private static ClassPool pool = ClassPool.getDefault()
private static List<MockMethodModel> generatorMockMap = new ArrayList<>();

static void addPoolPath(String path) {
pool.appendClassPath(path)
}


static void generatorMockMap(String path, Project project){
static void generatorMockMap(String path, Project project) {
File jarFile = new File(path)
String jarZipDir = JarUtils.getExtractJarPath(jarFile)
File unJar = new File(jarZipDir)
List<String> strings = JarUtils.unJar(jarFile, unJar)
addPoolPath(unJar.path)
//todo inject代码
if (strings.contains('com.mock.generator.MockMethodGenerator.class')){
if (strings.contains('com.mock.generator.MockMethodGenerator.class')) {
CtClass ctClass = pool.getCtClass('com.mock.generator.MockMethodGenerator')
try {
CtMethod ctMethod = ctClass.getDeclaredMethod('initMackMock')
CtMethod ctMethod = ctClass.getDeclaredMethod('initMackMock')

StringBuilder builder = new StringBuilder()
generatorMockMap.each {
builder.append(getMockMapDes(it.className,it.methodName,it.values,it.defaultValue)+"\n")
}
println '方法' + builder.toString()
ctMethod.insertBefore(builder.toString())
ctClass.writeFile(unJar.path)
ctClass.detach()
}catch (Exception e){
StringBuilder builder = new StringBuilder()
generatorMockMap.each {
builder.append(getMockMapDes(it.className, it.methodName, it.values, it.defaultValue) + "\n")
}
println '方法' + builder.toString()
ctMethod.insertBefore(builder.toString())
ctClass.writeFile(unJar.path)
ctClass.detach()
} catch (Exception e) {
e.printStackTrace()
} finally {
generatorMockMap.clear()
}
}
println '解压路径:' + new File(jarZipDir).path
Expand All @@ -45,12 +47,13 @@ class Inject {
JarUtils.jar(jarFile, unJar)
}

private static String getMockMapDes(String className, String methodName, String values,String defaultValue) {
private
static String getMockMapDes(String className, String methodName, String values, String defaultValue) {
return String.format("mockMethodModels.add(getMockMap(\"%s\",\"%s\",\"%s\",\"%s\"));", className, methodName, values, defaultValue)
}

static void injectMockMap(String className,String methodName,String values,String defaultValue){
MockMethodModel mockMethodModel= new MockMethodModel()
static void injectMockMap(String className, String methodName, String values, String defaultValue) {
MockMethodModel mockMethodModel = new MockMethodModel()
mockMethodModel.values = values
mockMethodModel.defaultValue = defaultValue
mockMethodModel.methodName = methodName
Expand Down Expand Up @@ -98,10 +101,10 @@ class Inject {
project.extensions[MockExtension.plugin].packages.each { String pkg ->
if (className.contains(pkg)) {
className = className.substring(0, className.length() - 6)
println 'className:'+className
println 'className:' + className
try {
injectClass(path, className)
}catch(Exception e){
} catch (Exception e) {
e.printStackTrace()
}
}
Expand Down Expand Up @@ -133,7 +136,7 @@ class Inject {
if (!isAllow(method.returnType.name)) {
return
}
injectMockMap(className,method.name,mockMethod.values(),mockMethod.defaultValue())
injectMockMap(className, method.name, mockMethod.values(), mockMethod.defaultValue())
println("methodDes:" + getMethodDes(className, method.name, method.returnType.name))

method.insertBefore(getMethodDes(className, method.name, method.returnType.name))
Expand Down Expand Up @@ -208,7 +211,4 @@ class Inject {
}





}
4 changes: 2 additions & 2 deletions mock-method-sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

// implementation 'com.github.JuneLeo:mock-method-annotation:1.0.8'
implementation 'com.github.JuneLeo.mock-method:mock-method-android:1.1.4'
implementation project(':mock-method-android')
implementation 'com.github.JuneLeo.mock-method:mock-method-android:1.1.5'
// implementation project(':mock-method-android')
implementation project(':mock-method-lib-1')

// annotationProcessor project(':compiler')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void onClick(View v) {
});
}

@MockMethod(defaultValue = "修改为a", values = "修改为a,修改为b")
// @MockMethod(defaultValue = "修改为a", values = "修改为a,修改为b")
public String a() {
return "我是a方法的返回值 - 设置后点击我改变值";
}
Expand Down

0 comments on commit 5bc89ea

Please sign in to comment.