├── README.md ├── mock-method-android ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── values │ │ │ ├── strings.xml │ │ │ └── style.xml │ │ └── layout │ │ │ ├── activity_setting.xml │ │ │ ├── item_abtest_spinner.xml │ │ │ └── item_mock.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── com │ │ └── mock │ │ └── generator │ │ ├── IMockMethodMap.java │ │ ├── config │ │ ├── IMockConfig.java │ │ └── DefaultMockConfig.java │ │ ├── MockMethodModel.java │ │ ├── MockMethodGenerator.java │ │ ├── MockMethodTransform.java │ │ ├── MockManager.java │ │ └── ui │ │ └── MockSettingActivity.java ├── build.gradle └── proguard-rules.pro ├── mock-method-plugin ├── .gitignore ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── gradle-plugins │ │ │ └── MockPlugin.properties │ │ ├── java │ │ └── com │ │ │ └── mock │ │ │ └── internel │ │ │ ├── Util.java │ │ │ ├── MockMethodModel.java │ │ │ └── JarUtils.java │ │ └── groovy │ │ └── com │ │ └── mock │ │ └── internel │ │ ├── MockExtension.groovy │ │ ├── DependencyListener.groovy │ │ ├── MockPlugin.groovy │ │ ├── MockTransform.groovy │ │ └── Inject.groovy ├── .idea │ ├── modules.xml │ ├── gradle.xml │ ├── runConfigurations.xml │ ├── codeStyles │ │ └── Project.xml │ ├── misc.xml │ └── workspace.xml ├── upload.gradle ├── local.properties └── build.gradle ├── mock-method-sample ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── layout │ │ │ │ └── activity_main.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── mock │ │ │ └── sample │ │ │ └── ui │ │ │ └── MainActivity.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── mock │ │ │ └── mock │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── mock │ │ └── mock │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── mock-lib └── mock-method-lib-1 │ ├── .gitignore │ ├── src │ └── main │ │ ├── res │ │ └── values │ │ │ └── strings.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── com │ │ └── mock │ │ └── lib1 │ │ └── MockMethodTest.java │ ├── build.gradle │ └── proguard-rules.pro ├── screenshot ├── ic_mock_class.png ├── ic_mock_plugin.png ├── ic_mock_source.png ├── ic_screen_demo.gif ├── ic_upload_jar.jpeg └── ic_mock_annotation.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── .gitmodules ├── settings.gradle ├── gradle.properties ├── debug.gradle ├── gradlew.bat └── gradlew /README.md: -------------------------------------------------------------------------------- 1 | # mock-method-sample 2 | -------------------------------------------------------------------------------- /mock-method-android/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /mock-method-plugin/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /mock-method-sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /mock-lib/mock-method-lib-1/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /screenshot/ic_mock_class.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/screenshot/ic_mock_class.png -------------------------------------------------------------------------------- /screenshot/ic_mock_plugin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/screenshot/ic_mock_plugin.png -------------------------------------------------------------------------------- /screenshot/ic_mock_source.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/screenshot/ic_mock_source.png -------------------------------------------------------------------------------- /screenshot/ic_screen_demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/screenshot/ic_screen_demo.gif -------------------------------------------------------------------------------- /screenshot/ic_upload_jar.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/screenshot/ic_upload_jar.jpeg -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /screenshot/ic_mock_annotation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/screenshot/ic_mock_annotation.png -------------------------------------------------------------------------------- /mock-method-android/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | library 3 | 4 | -------------------------------------------------------------------------------- /mock-method-plugin/src/main/resources/META-INF/gradle-plugins/MockPlugin.properties: -------------------------------------------------------------------------------- 1 | implementation-class=com.mock.internel.MockPlugin -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Mock 3 | 4 | -------------------------------------------------------------------------------- /mock-lib/mock-method-lib-1/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | mock1 3 | 4 | -------------------------------------------------------------------------------- /mock-lib/mock-method-lib-1/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /mock-method-android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/mock-method-sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/mock-method-sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .DS_Store 5 | /build 6 | /captures 7 | .externalNativeBuild 8 | /.idea/* 9 | /local-repos/* 10 | task.txt 11 | -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/mock-method-sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/mock-method-sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /mock-method-plugin/src/main/java/com/mock/internel/Util.java: -------------------------------------------------------------------------------- 1 | package com.mock.internel; 2 | 3 | /** 4 | * Created by spf on 2018/11/21. 5 | */ 6 | public class Util { 7 | } 8 | -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/mock-method-sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/mock-method-sample/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/mock-method-sample/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/mock-method-sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/mock-method-sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JuneLeo/mock-method/HEAD/mock-method-sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Jan 03 10:07:29 CST 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip 7 | -------------------------------------------------------------------------------- /mock-method-android/src/main/res/layout/activity_setting.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "mock-method-compiler"] 2 | path = mock-method-compiler 3 | url = git@github.com:JuneLeo/mock-method-compiler.git 4 | branch = master 5 | [submodule "mock-method-annotation"] 6 | path = mock-method-annotation 7 | url = git@github.com:JuneLeo/mock-method-annotation.git 8 | branch = master 9 | -------------------------------------------------------------------------------- /mock-method-plugin/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /mock-method-plugin/src/main/groovy/com/mock/internel/MockExtension.groovy: -------------------------------------------------------------------------------- 1 | package com.mock.internel 2 | 3 | class MockExtension { 4 | boolean isEnable = true 5 | static String plugin = "mock" 6 | def packages 7 | boolean dependencyEnable = true 8 | 9 | List subprojects 10 | 11 | } -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /mock-method-android/src/main/java/com/mock/generator/IMockMethodMap.java: -------------------------------------------------------------------------------- 1 | package com.mock.generator; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by spf on 2018/12/24. 8 | */ 9 | public interface IMockMethodMap { 10 | void initMackMock(List> mockMethodModels); 11 | } 12 | -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /mock-method-plugin/upload.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'groovy' 2 | apply plugin: 'maven' 3 | 4 | uploadArchives { 5 | 6 | repositories.mavenDeployer { 7 | repository(url: uri('../local-repos')) 8 | pom.groupId = 'com.mock.internel' // 组名 9 | pom.artifactId = 'MockPlugin' // 插件名 10 | pom.version = '1.0' // 版本号 11 | } 12 | } -------------------------------------------------------------------------------- /mock-method-plugin/local.properties: -------------------------------------------------------------------------------- 1 | ## This file must *NOT* be checked into Version Control Systems, 2 | # as it contains information specific to your local configuration. 3 | # 4 | # Location of the SDK. This is only used by Gradle. 5 | # For customization when using a Version Control System, please read the 6 | # header note. 7 | #Sun Dec 16 17:07:42 CST 2018 8 | sdk.dir=/Users/bighero/Library/Android/sdk 9 | -------------------------------------------------------------------------------- /mock-method-plugin/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':mock-method-sample', ':mock-method-plugin', ':mock-method-android' 2 | include':mock-method-lib-1' 3 | project(':mock-method-lib-1').projectDir = new File('mock-lib/mock-method-lib-1') 4 | 5 | 6 | //compiler 自动生成类 7 | include ':compiler' 8 | project(':compiler').projectDir = new File('mock-method-compiler/library') 9 | //注解 10 | include ':annotation' 11 | project(':annotation').projectDir = new File('mock-method-annotation/library') -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /mock-lib/mock-method-lib-1/src/main/java/com/mock/lib1/MockMethodTest.java: -------------------------------------------------------------------------------- 1 | package com.mock.lib1; 2 | 3 | import com.mock.annotation.MockMethod; 4 | 5 | /** 6 | * Created by spf on 2019/1/3. 7 | */ 8 | public class MockMethodTest { 9 | @MockMethod(defaultValue = "1", values = "1,2") 10 | public int test() { 11 | return 1; 12 | } 13 | 14 | @MockMethod(defaultValue = "true",values = "false,true") 15 | public boolean isEnable(){ 16 | return false; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /mock-method-sample/src/test/java/com/mock/mock/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.mock.mock; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /mock-method-android/src/main/java/com/mock/generator/config/IMockConfig.java: -------------------------------------------------------------------------------- 1 | package com.mock.generator.config; 2 | 3 | import com.mock.generator.MockMethodModel; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * Created by spf on 2019/1/2. 10 | */ 11 | public interface IMockConfig { 12 | 13 | boolean isEnable(); 14 | 15 | Map> getMockMap(); 16 | 17 | void save(); 18 | 19 | void saveModel(MockMethodModel mockMethodModel); 20 | } 21 | -------------------------------------------------------------------------------- /mock-method-android/src/main/res/values/style.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 11 | -------------------------------------------------------------------------------- /mock-method-android/src/main/res/layout/item_abtest_spinner.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /mock-method-plugin/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /mock-method-plugin/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'groovy' 2 | //apply from:file('upload.gradle') 3 | apply plugin: 'com.github.dcendents.android-maven' 4 | group = 'com.github.JuneLeo' 5 | 6 | dependencies { 7 | implementation fileTree(dir: 'libs', include: ['*.jar']) 8 | compile gradleApi() 9 | compile localGroovy() 10 | implementation 'com.android.tools.build:gradle:3.2.1' //build tools 11 | implementation 'com.android.tools.build:transform-api:1.5.0' //transform 12 | implementation 'org.javassist:javassist:3.23.0-GA' //javassist 13 | implementation 'com.github.JuneLeo:mock-method-annotation:1.0.8' 14 | } 15 | 16 | sourceCompatibility = "7" 17 | targetCompatibility = "7" 18 | -------------------------------------------------------------------------------- /mock-lib/mock-method-lib-1/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 26 5 | 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 14 10 | targetSdkVersion 26 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | } 25 | 26 | dependencies { 27 | implementation fileTree(dir: 'libs', include: ['*.jar']) 28 | implementation 'com.android.support:appcompat-v7:26.1.0' 29 | // implementation 'com.github.JuneLeo:mock-method-annotation:1.0.8' 30 | 31 | } 32 | -------------------------------------------------------------------------------- /mock-method-android/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | 4 | group = 'com.github.JuneLeo' 5 | android { 6 | compileSdkVersion 26 7 | defaultConfig { 8 | minSdkVersion 14 9 | targetSdkVersion 26 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | 21 | } 22 | 23 | dependencies { 24 | implementation fileTree(dir: 'libs', include: ['*.jar']) 25 | implementation 'com.android.support:appcompat-v7:26.1.0' 26 | implementation 'com.google.code.gson:gson:2.8.5' 27 | } 28 | -------------------------------------------------------------------------------- /mock-method-plugin/src/main/java/com/mock/internel/MockMethodModel.java: -------------------------------------------------------------------------------- 1 | package com.mock.internel; 2 | 3 | /** 4 | * Created by spf on 2018/12/24. 5 | */ 6 | public class MockMethodModel { 7 | public String className; 8 | public String methodName; 9 | public String returnType; 10 | public String values; 11 | public String defaultValue; 12 | public boolean switcher; 13 | 14 | public MockMethodModel(String className, String methodName, String returnType, String values, String defaultValue) { 15 | this.className = className; 16 | this.methodName = methodName; 17 | this.returnType = returnType; 18 | this.values = values; 19 | this.defaultValue = defaultValue; 20 | 21 | } 22 | 23 | public MockMethodModel() { 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /mock-method-android/src/main/java/com/mock/generator/MockMethodModel.java: -------------------------------------------------------------------------------- 1 | package com.mock.generator; 2 | 3 | /** 4 | * Created by spf on 2018/12/24. 5 | */ 6 | public class MockMethodModel { 7 | public String className; 8 | public String methodName; 9 | public String returnType; 10 | public String values; 11 | public String defaultValue; 12 | public boolean switcher; 13 | 14 | public MockMethodModel(String className, String methodName, String returnType, String values, String defaultValue) { 15 | this.className = className; 16 | this.methodName = methodName; 17 | this.returnType = returnType; 18 | this.values = values; 19 | this.defaultValue = defaultValue; 20 | 21 | } 22 | 23 | public MockMethodModel() { 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /mock-method-android/src/main/java/com/mock/generator/MockMethodGenerator.java: -------------------------------------------------------------------------------- 1 | package com.mock.generator; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | 6 | public class MockMethodGenerator implements IMockMethodMap { 7 | @Override 8 | public void initMackMock(List> mockMethodModels) { 9 | // mockMethodModels.add(getMockMap("com.mock.sample.ui.MainActivity","a","修改为a,修改为b","修改为a")); 10 | //todo plugin会注入代码 11 | } 12 | 13 | private HashMap getMockMap(String className, String methodName, String values, 14 | String defaultValue) { 15 | HashMap map = new HashMap<>(); 16 | map.put("className",className); 17 | map.put("methodName",methodName); 18 | map.put("values",values); 19 | map.put("defaultValue",defaultValue); 20 | return map; 21 | } 22 | } -------------------------------------------------------------------------------- /mock-method-android/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /mock-method-sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /mock-lib/mock-method-lib-1/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /mock-method-sample/src/androidTest/java/com/mock/mock/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.mock.mock; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.leo.mock", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | #org.gradle.daemon=true 11 | #org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 12 | # When configured, Gradle will run in incubating parallel mode. 13 | # This option should only be used with decoupled projects. More details, visit 14 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 15 | # org.gradle.parallel=true 16 | 17 | 18 | -------------------------------------------------------------------------------- /mock-method-sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 13 | 23 | 24 | -------------------------------------------------------------------------------- /mock-method-plugin/src/main/groovy/com/mock/internel/DependencyListener.groovy: -------------------------------------------------------------------------------- 1 | package com.mock.internel 2 | 3 | import org.gradle.api.Project 4 | import org.gradle.api.artifacts.DependencyResolutionListener 5 | import org.gradle.api.artifacts.ResolvableDependencies 6 | 7 | class DependencyListener implements DependencyResolutionListener { 8 | private Project mProject 9 | 10 | DependencyListener(Project project) { 11 | mProject = project 12 | } 13 | 14 | @Override 15 | void beforeResolve(ResolvableDependencies resolvableDependencies) { 16 | if (mProject.extensions[MockExtension.plugin].dependencyEnable) { 17 | println '内部依赖使用' 18 | mProject.rootProject.repositories.maven { 19 | url "https://jitpack.io" 20 | } 21 | mProject.rootProject.subprojects { 22 | println 'project name:' + it.name 23 | it.configurations.implementation.dependencies.add( 24 | it.dependencies.create('com.github.JuneLeo:mock-method-annotation:1.0.8')) 25 | } 26 | // mProject.configurations.annotationProcessor.dependencies.add( 27 | // mProject.dependencies.create('com.github.JuneLeo:mock-method-compiler:1.0.8')) 28 | } 29 | mProject.gradle.removeListener(this) 30 | } 31 | 32 | @Override 33 | void afterResolve(ResolvableDependencies resolvableDependencies) { 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /mock-method-sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'MockPlugin' 3 | mock { 4 | //加快编译速度 5 | packages = [ 6 | 'com.mock.sample','com.mock.lib1' 7 | ] 8 | dependencyEnable = true 9 | } 10 | android { 11 | compileSdkVersion 26 12 | defaultConfig { 13 | applicationId "com.mock.sample" 14 | minSdkVersion 14 15 | targetSdkVersion 26 16 | versionCode 1 17 | versionName "1.0" 18 | } 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | debug { 25 | minifyEnabled false 26 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 27 | } 28 | } 29 | } 30 | 31 | dependencies { 32 | implementation fileTree(dir: 'libs', include: ['*.jar']) 33 | implementation 'com.android.support:appcompat-v7:26.1.0' 34 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 35 | testImplementation 'junit:junit:4.12' 36 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 37 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 38 | 39 | // implementation 'com.github.JuneLeo:mock-method-annotation:1.0.8' 40 | implementation 'com.github.JuneLeo.mock-method:mock-method-android:1.1.6' 41 | // implementation project(':mock-method-android') 42 | implementation project(':mock-method-lib-1') 43 | 44 | // annotationProcessor project(':compiler') 45 | 46 | 47 | } 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /mock-method-plugin/src/main/groovy/com/mock/internel/MockPlugin.groovy: -------------------------------------------------------------------------------- 1 | package com.mock.internel 2 | 3 | import com.android.build.gradle.AppPlugin 4 | import com.android.build.gradle.LibraryPlugin 5 | import org.gradle.api.GradleException 6 | import org.gradle.api.Plugin 7 | import org.gradle.api.Project 8 | 9 | class MockPlugin implements Plugin { 10 | @Override 11 | void apply(Project project) { 12 | //校验必须在application和library plugin 后面 13 | def androidPlugin = [AppPlugin, LibraryPlugin] 14 | .collect { project.plugins.findPlugin(it) } 15 | .find { it != null } 16 | if (!androidPlugin){ 17 | throw new GradleException('must be application or library') 18 | } 19 | //依赖 20 | project.gradle.addListener(new DependencyListener(project)) 21 | //拓展 22 | project.extensions.create(MockExtension.plugin, MockExtension) 23 | 24 | project.afterEvaluate { 25 | List strings = new ArrayList<>() 26 | project.rootProject.subprojects{ 27 | strings.add(it.project.name) 28 | } 29 | if (strings.contains('annotation')){ 30 | strings.remove('annotation') 31 | } 32 | if (strings.contains('compiler')){ 33 | strings.remove('compiler') 34 | } 35 | println '子项目数量:'+strings 36 | project.extensions[MockExtension.plugin].subprojects = strings 37 | } 38 | 39 | 40 | 41 | if (!project.extensions[MockExtension.plugin].isEnable){ 42 | return 43 | } 44 | 45 | //transform 46 | project.android.registerTransform(new MockTransform(project)) 47 | } 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /mock-method-android/src/main/res/layout/item_mock.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 17 | 24 | 25 | 33 | 40 | 45 | -------------------------------------------------------------------------------- /mock-method-plugin/.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /mock-method-android/src/main/java/com/mock/generator/MockMethodTransform.java: -------------------------------------------------------------------------------- 1 | package com.mock.generator; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * Created by spf on 2018/12/24. 10 | */ 11 | public class MockMethodTransform { 12 | private static List> classMock = new ArrayList<>(); 13 | 14 | static { 15 | try { 16 | Class> aClass = Class.forName("com.mock.generator.MockMethodGenerator"); 17 | IMockMethodMap iMockMethodMap = (IMockMethodMap) aClass.newInstance(); 18 | iMockMethodMap.initMackMock(classMock); 19 | } catch (Exception e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | 24 | public static List> getMockMap(){ 25 | return classMock; 26 | } 27 | 28 | public static Map> getMock() { 29 | Map> hashMap = new HashMap<>(); 30 | for (HashMap map : classMock) { 31 | String className = map.get("className"); 32 | List mockMethodModels = hashMap.get(className); 33 | 34 | if (mockMethodModels == null) { 35 | mockMethodModels = new ArrayList<>(); 36 | hashMap.put(className, mockMethodModels); 37 | } 38 | MockMethodModel mockMethodModel = new MockMethodModel(); 39 | mockMethodModel.className = className; 40 | mockMethodModel.methodName = map.get("methodName"); 41 | mockMethodModel.returnType = map.get("returnType"); 42 | mockMethodModel.values = map.get("values"); 43 | mockMethodModel.defaultValue = map.get("defaultValue"); 44 | mockMethodModels.add(mockMethodModel); 45 | } 46 | return hashMap; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /mock-method-sample/src/main/java/com/mock/sample/ui/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.mock.sample.ui; 2 | 3 | import android.content.Intent; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | import android.widget.TextView; 8 | 9 | import com.mock.generator.MockManager; 10 | import com.mock.generator.config.DefaultMockConfig; 11 | import com.mock.generator.ui.MockSettingActivity; 12 | import com.mock.sample.BuildConfig; 13 | import com.mock.sample.R; 14 | import com.mock.annotation.MockMethod; 15 | 16 | public class MainActivity extends AppCompatActivity { 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_main); 22 | MockManager.init(new DefaultMockConfig(getApplication()){ 23 | @Override 24 | public boolean isEnable() { 25 | return BuildConfig.DEBUG; 26 | } 27 | }); 28 | 29 | findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { 30 | @Override 31 | public void onClick(View v) { 32 | Intent intent = new Intent(MainActivity.this, MockSettingActivity.class); 33 | startActivity(intent); 34 | } 35 | }); 36 | ((TextView) findViewById(R.id.tv_a)).setText("a方法:"+a()); 37 | ((TextView) findViewById(R.id.tv_a)).setOnClickListener(new View.OnClickListener() { 38 | @Override 39 | public void onClick(View v) { 40 | ((TextView) findViewById(R.id.tv_a)).setText("a方法:"+a()); 41 | } 42 | }); 43 | } 44 | 45 | @MockMethod(defaultValue = "修改为a", values = "修改为a,修改为b,ddddd") 46 | public String a() { 47 | 48 | return "我是a方法的返回值 - 设置后点击我改变值"; 49 | } 50 | 51 | @MockMethod(defaultValue = "3", values = "1,3,4,5") 52 | public Long b() { 53 | return 1L; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /debug.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: DebugPlugin 2 | 3 | class DebugPlugin implements Plugin { 4 | Project mProject 5 | StringBuilder builder = new StringBuilder(); 6 | 7 | @Override 8 | void apply(Project project) { 9 | mProject = project 10 | 11 | project.gradle.taskGraph.afterTask(new Action() { 12 | @Override 13 | void execute(Task task) { 14 | builder.append(" | inputs file|\n") 15 | builder.append(" |-----------------------------------------------------------------\n") 16 | if (task.inputs.files.getFiles().size() == 0) { 17 | builder.append(" | 没有inputs路径\n") 18 | } else { 19 | task.inputs.files.getFiles().each { 20 | builder.append(" | ${it.path}\n") 21 | } 22 | } 23 | builder.append(" |-----------------------------------------------------------------\n") 24 | builder.append(" | outputs file|\n") 25 | builder.append(" |-----------------------------------------------------------------\n") 26 | if (task.outputs.files.getFiles().size() == 0) { 27 | builder.append(" | 没有outputs路径\n") 28 | } else { 29 | task.outputs.files.getFiles().each { 30 | builder.append(" | ${it.path}\n") 31 | } 32 | } 33 | builder.append(" |-----------------------------------------------------------------\n") 34 | } 35 | }) 36 | 37 | project.gradle.taskGraph.beforeTask(new Action() { 38 | @Override 39 | void execute(Task task) { 40 | builder.append(" |------------------------------------------------------------------------\n") 41 | builder.append(" | ${task.name} , ${task.project.name}|\n") 42 | builder.append(" |------------------------------------------------------------------------\n") 43 | } 44 | }) 45 | 46 | project.gradle.buildFinished(new Action() { 47 | @Override 48 | void execute(BuildResult buildResult) { 49 | mProject.file(mProject.rootDir.path + '/task.txt').withPrintWriter { 50 | it.write(builder.toString()) 51 | } 52 | 53 | } 54 | }) 55 | 56 | } 57 | } -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /mock-method-android/src/main/java/com/mock/generator/config/DefaultMockConfig.java: -------------------------------------------------------------------------------- 1 | package com.mock.generator.config; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | import android.content.SharedPreferences; 6 | import android.text.TextUtils; 7 | 8 | import com.google.gson.Gson; 9 | import com.google.gson.reflect.TypeToken; 10 | import com.mock.generator.BuildConfig; 11 | import com.mock.generator.MockManager; 12 | import com.mock.generator.MockMethodTransform; 13 | import com.mock.generator.MockMethodModel; 14 | 15 | import java.util.List; 16 | import java.util.Map; 17 | 18 | /** 19 | * Created by spf on 2018/12/24. 20 | */ 21 | public class DefaultMockConfig implements IMockConfig{ 22 | private static SharedPreferences sharedPreferences; 23 | private final Application application; 24 | private static final String NAME = "abtest"; 25 | private static final String KEY = "key_abtest"; 26 | 27 | public DefaultMockConfig(Application application){ 28 | this.application = application; 29 | } 30 | 31 | public static List getMockItem() { 32 | return MockManager.getMockItem(); 33 | } 34 | 35 | @Override 36 | public boolean isEnable() { 37 | return true; 38 | } 39 | 40 | @Override 41 | public Map> getMockMap() { 42 | 43 | sharedPreferences = application.getSharedPreferences(NAME, Context.MODE_PRIVATE); 44 | String abtest = sharedPreferences.getString(KEY, ""); 45 | Gson gson = new Gson(); 46 | if (TextUtils.isEmpty(abtest)) { 47 | Map> mock = MockMethodTransform.getMock(); 48 | abtest = gson.toJson(mock); 49 | sharedPreferences.edit().putString(KEY, abtest).apply(); 50 | } 51 | 52 | Map> map = gson.fromJson(abtest, new TypeToken>>() { 53 | }.getType()); 54 | 55 | 56 | return map; 57 | } 58 | 59 | @Override 60 | public void save() { 61 | Map> mockMap = MockManager.getMockMap(); 62 | Gson gson = new Gson(); 63 | sharedPreferences.edit().putString(KEY, gson.toJson(mockMap)).apply(); 64 | } 65 | 66 | @Override 67 | public void saveModel(MockMethodModel mockMethodModel) { 68 | Map> mockMap = MockManager.getMockMap(); 69 | for (Map.Entry> entry : mockMap.entrySet()) { 70 | if (entry.getKey().equals(mockMethodModel.className)){ 71 | for (MockMethodModel model : entry.getValue()) { 72 | if (model.methodName.equals(mockMethodModel.methodName)){ 73 | model.switcher = mockMethodModel.switcher; 74 | model.defaultValue = mockMethodModel.defaultValue; 75 | } 76 | } 77 | } 78 | } 79 | Gson gson = new Gson(); 80 | sharedPreferences.edit().putString(KEY, gson.toJson(mockMap)).apply(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /mock-method-android/src/main/java/com/mock/generator/MockManager.java: -------------------------------------------------------------------------------- 1 | package com.mock.generator; 2 | 3 | import com.mock.generator.config.IMockConfig; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * Created by spf on 2018/12/29. 11 | */ 12 | public class MockManager { 13 | private static boolean isEnable = false; 14 | private static Map> map; 15 | private static IMockConfig mMockConfig; 16 | 17 | public static void init(IMockConfig iMockConfig) { 18 | mMockConfig = iMockConfig; 19 | MockManager.map = iMockConfig.getMockMap(); 20 | isEnable = iMockConfig.isEnable(); 21 | } 22 | 23 | //////////////////// 24 | // start 不能动 25 | /////////////////// 26 | /** 27 | * 是否可以mock 28 | * @param className 29 | * @param methodName 30 | * @return 31 | */ 32 | public static boolean isMock(String className, String methodName) { 33 | MockMethodModel model = getMockModel(className, methodName); 34 | return isEnable && model != null && model.switcher; 35 | } 36 | 37 | /** 38 | * mock返回值 39 | * @param className 40 | * @param methodName 41 | * @return 42 | */ 43 | public static String getValue(String className, String methodName) { 44 | MockMethodModel model = getMockModel(className, methodName); 45 | if (model != null) { 46 | return model.defaultValue; 47 | } 48 | return ""; 49 | } 50 | 51 | public static MockMethodModel getMockModel(String className, String methodName) { 52 | if (map == null) { 53 | return null; 54 | } 55 | List mockMethodModels = map.get(className); 56 | if (mockMethodModels == null) { 57 | return null; 58 | } 59 | for (MockMethodModel mockMethodModel : mockMethodModels) { 60 | if (methodName.equals(mockMethodModel.methodName)) { 61 | return mockMethodModel; 62 | } 63 | } 64 | return null; 65 | } 66 | /////////////// 67 | // end 68 | /////////////// 69 | 70 | /** 71 | * 获取Map 72 | * @return 73 | */ 74 | public static Map> getMockMap(){ 75 | return map; 76 | } 77 | 78 | /** 79 | * 获取所有的Mock数据 80 | * @return 81 | */ 82 | public static List getMockItem() { 83 | List list = new ArrayList<>(); 84 | for (Map.Entry> entry : map.entrySet()) { 85 | list.addAll(entry.getValue()); 86 | } 87 | return list; 88 | } 89 | 90 | /** 91 | * 根据类名获取 类中方法 92 | * @param clz 93 | * @return 94 | */ 95 | public static List getMockItemByClz(String clz){ 96 | for (Map.Entry> entry : map.entrySet()) { 97 | if (entry.getKey().equals(clz)){ 98 | return entry.getValue(); 99 | } 100 | } 101 | return null; 102 | } 103 | 104 | public static IMockConfig getMockConfig(){ 105 | return mMockConfig; 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /mock-method-plugin/src/main/groovy/com/mock/internel/MockTransform.groovy: -------------------------------------------------------------------------------- 1 | package com.mock.internel 2 | 3 | import com.android.build.api.transform.* 4 | import com.android.build.gradle.internal.pipeline.TransformManager 5 | import com.android.utils.FileUtils 6 | import org.apache.commons.codec.digest.DigestUtils 7 | import org.gradle.api.Project 8 | 9 | class MockTransform extends Transform { 10 | 11 | Project project 12 | 13 | MockTransform(Project project) { 14 | this.project = project 15 | } 16 | 17 | @Override 18 | String getName() { 19 | return "MockTransform" 20 | } 21 | 22 | @Override 23 | Set getInputTypes() { 24 | return TransformManager.CONTENT_CLASS 25 | } 26 | 27 | @Override 28 | Set super QualifiedContent.Scope> getScopes() { 29 | return TransformManager.SCOPE_FULL_PROJECT 30 | } 31 | 32 | @Override 33 | boolean isIncremental() { 34 | return false 35 | } 36 | 37 | @Override 38 | void transform(Context context, Collection inputs, Collection referencedInputs, TransformOutputProvider outputProvider, boolean isIncremental) throws IOException, TransformException, InterruptedException { 39 | super.transform(context, inputs, referencedInputs, outputProvider, isIncremental) 40 | JarInput injectJarInput 41 | 42 | inputs.each { TransformInput input -> 43 | 44 | 45 | //ClassPool中注入注解 46 | input.jarInputs.each { JarInput jarInput-> 47 | if (jarInput.file.absolutePath.contains('mock-method-annotation')){ 48 | Inject.addPoolPath(jarInput.file.absolutePath) 49 | } 50 | } 51 | //jar 52 | input.jarInputs.each { JarInput jarInput -> 53 | //辅助类 54 | if (jarInput.file.absolutePath.contains('mock-method-android')) { 55 | Inject.addPoolPath(jarInput.file.getAbsolutePath()) 56 | injectJarInput = jarInput 57 | } else { 58 | //jar 59 | Inject.injectJarPath(jarInput.file.getAbsolutePath(), project) 60 | 61 | def jarName = jarInput.name 62 | def md5Name = DigestUtils.md5Hex(jarInput.file.getAbsolutePath()) 63 | if (jarName.endsWith(".jar")) { 64 | jarName = jarName.substring(0, jarName.length() - 4) 65 | } 66 | def dest = outputProvider.getContentLocation(jarName + md5Name, 67 | jarInput.contentTypes, jarInput.scopes, Format.JAR) 68 | FileUtils.copyFile(jarInput.file, dest) 69 | } 70 | } 71 | 72 | //文件 73 | input.directoryInputs.each { DirectoryInput directoryInput -> 74 | println '输出路径:' + directoryInput.file.absolutePath 75 | Inject.injectDir(directoryInput.file.absolutePath, project) 76 | def dest = outputProvider.getContentLocation(directoryInput.name, 77 | directoryInput.contentTypes, directoryInput.scopes, 78 | Format.DIRECTORY) 79 | FileUtils.copyDirectory(directoryInput.file, dest) 80 | } 81 | } 82 | 83 | //辅助类 84 | if (injectJarInput != null) { 85 | println '辅助类' 86 | println injectJarInput.file.absolutePath 87 | 88 | 89 | Inject.generatorMockMap(injectJarInput.file.getAbsolutePath(), project) 90 | def jarName = injectJarInput.name 91 | def md5Name = DigestUtils.md5Hex(injectJarInput.file.getAbsolutePath()) 92 | if (jarName.endsWith(".jar")) { 93 | jarName = jarName.substring(0, jarName.length() - 4) 94 | } 95 | def dest = outputProvider.getContentLocation(jarName + md5Name, 96 | injectJarInput.contentTypes, injectJarInput.scopes, Format.JAR) 97 | FileUtils.copyFile(injectJarInput.file, dest) 98 | 99 | } 100 | 101 | } 102 | } -------------------------------------------------------------------------------- /mock-method-android/src/main/java/com/mock/generator/ui/MockSettingActivity.java: -------------------------------------------------------------------------------- 1 | package com.mock.generator.ui; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.widget.SwitchCompat; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.AdapterView; 11 | import android.widget.ArrayAdapter; 12 | import android.widget.BaseAdapter; 13 | import android.widget.CompoundButton; 14 | import android.widget.ListView; 15 | import android.widget.Spinner; 16 | import android.widget.TextView; 17 | import com.mock.generator.MockManager; 18 | import com.mock.generator.MockMethodModel; 19 | import com.mock.generator.R; 20 | import com.mock.generator.config.DefaultMockConfig; 21 | 22 | import java.util.List; 23 | 24 | /** 25 | * Created by spf on 2018/12/29. 26 | */ 27 | public class MockSettingActivity extends Activity { 28 | @Override 29 | protected void onCreate(@Nullable Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_setting); 32 | List mockMethodModels = DefaultMockConfig.getMockItem(); 33 | ListView listView = findViewById(R.id.list); 34 | MockAdapter mockAdapter = new MockAdapter(mockMethodModels); 35 | listView.setAdapter(mockAdapter); 36 | } 37 | 38 | 39 | @Override 40 | protected void onDestroy() { 41 | super.onDestroy(); 42 | MockManager.getMockConfig().save(); 43 | } 44 | 45 | private class MockAdapter extends BaseAdapter { 46 | 47 | private List mockMethodModels; 48 | 49 | public MockAdapter(List mockMethodModels) { 50 | this.mockMethodModels = mockMethodModels; 51 | } 52 | 53 | @Override 54 | public int getCount() { 55 | return mockMethodModels == null ? 0 : mockMethodModels.size(); 56 | } 57 | 58 | @Override 59 | public Object getItem(int position) { 60 | return mockMethodModels.get(position); 61 | } 62 | 63 | @Override 64 | public long getItemId(int position) { 65 | return position; 66 | } 67 | 68 | @Override 69 | public View getView(int position, View convertView, ViewGroup parent) { 70 | convertView = LayoutInflater.from(MockSettingActivity.this).inflate(R.layout.item_mock, null); 71 | final MockMethodModel model = mockMethodModels.get(position); 72 | TextView tvClass = convertView.findViewById(R.id.tv_class); 73 | tvClass.setText("class:" + model.className); 74 | 75 | TextView tvMethod = convertView.findViewById(R.id.tv_method); 76 | tvMethod.setText("method:" + model.methodName); 77 | 78 | 79 | SwitchCompat switchCompat = convertView.findViewById(R.id.switcher); 80 | switchCompat.setChecked(model.switcher); 81 | 82 | switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 83 | @Override 84 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 85 | model.switcher = isChecked; 86 | } 87 | }); 88 | 89 | final String[] values = model.values.split(","); 90 | int index = 0; 91 | for (int i = 0; i < values.length; i++) { 92 | if (values[i].equals(model.defaultValue)) { 93 | index = i; 94 | break; 95 | } 96 | } 97 | Spinner spinner = convertView.findViewById(R.id.spinner); 98 | ArrayAdapter adapter = new ArrayAdapter<>(MockSettingActivity.this, R.layout.item_abtest_spinner, R.id.item_spinner); 99 | adapter.addAll(values); 100 | spinner.setAdapter(adapter); 101 | spinner.setSelection(index); 102 | spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 103 | @Override 104 | public void onItemSelected(AdapterView> parent, View view, int position, long id) { 105 | model.defaultValue = values[position]; 106 | } 107 | 108 | @Override 109 | public void onNothingSelected(AdapterView> parent) { 110 | 111 | } 112 | }); 113 | 114 | 115 | return convertView; 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /mock-method-plugin/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /mock-method-sample/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /mock-method-plugin/src/main/java/com/mock/internel/JarUtils.java: -------------------------------------------------------------------------------- 1 | package com.mock.internel; 2 | 3 | import java.io.BufferedInputStream; 4 | import java.io.BufferedOutputStream; 5 | import java.io.File; 6 | import java.io.FileInputStream; 7 | import java.io.FileOutputStream; 8 | import java.io.InputStream; 9 | import java.io.OutputStream; 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | import java.util.jar.JarInputStream; 13 | import java.util.jar.JarOutputStream; 14 | import java.util.jar.Manifest; 15 | import java.util.zip.ZipEntry; 16 | 17 | /** 18 | * Created by lizhaoxuan on 2017/12/31. 19 | * 20 | * This class is copied from Intimate . 21 | * 22 | * https://github.com/JustKiddingBaby/Intimate 23 | */ 24 | public class JarUtils { 25 | 26 | /** 27 | * 返回推荐的 jar 解压目录 28 | * 29 | * @param path jar 文件路径 30 | * @return 解压的 jar 路径 31 | */ 32 | public static String getExtractJarPath(String path) { 33 | return getExtractJarPath(new File(path)); 34 | } 35 | 36 | /** 37 | * 返回推荐的 jar 解压目录 38 | * 39 | * @param jarFile jar 文件 40 | * @return 解压的 jar 路径 41 | */ 42 | public static String getExtractJarPath(File jarFile) { 43 | return jarFile.getParent() + "/" + jarFile.getName().replace(".jar", ""); 44 | } 45 | 46 | /** 47 | * 压缩 jar目录 48 | * @param unJarFile 49 | * @return 50 | */ 51 | public static String getExtractJarOriginPath(File unJarFile) { 52 | return unJarFile.getParent() + "/" + unJarFile.getName() + ".jar"; 53 | } 54 | 55 | public static void jar(File desJar, File jarDir) throws Exception { 56 | BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(desJar)); 57 | File[] src = jarDir.listFiles(); 58 | jar(out, src); 59 | } 60 | 61 | public static void jar(OutputStream out, File[] src) throws Exception { 62 | jar(out, src, null, null); 63 | } 64 | 65 | public static void jar(OutputStream out, File src) throws Exception { 66 | jar(out, new File[]{src}, null, null); 67 | } 68 | 69 | public static void jar(OutputStream out, File[] src, String prefix, Manifest man) throws Exception { 70 | JarOutputStream jout = null; 71 | if (man == null) { 72 | jout = new JarOutputStream(out); 73 | } else { 74 | jout = new JarOutputStream(out, man); 75 | } 76 | 77 | if (prefix != null && prefix.trim().length() > 0 && !prefix.equals("/")) { 78 | if (prefix.charAt(0) == '/') { 79 | prefix = prefix.substring(1); 80 | } 81 | if (prefix.charAt(prefix.length() - 1) != '/') { 82 | prefix = prefix + "/"; 83 | } 84 | } else { 85 | prefix = ""; 86 | } 87 | for (File f : src) { 88 | jar(f, prefix, jout); 89 | } 90 | jout.close(); 91 | } 92 | 93 | private static void jar(File src, String prefix, JarOutputStream jout) throws Exception { 94 | if (src.isDirectory()) { 95 | prefix = prefix + src.getName() + "/"; 96 | ZipEntry entry = new ZipEntry(prefix); 97 | entry.setTime(src.lastModified()); 98 | entry.setMethod(ZipEntry.STORED); 99 | entry.setSize(0l); 100 | entry.setCrc(0l); 101 | jout.putNextEntry(entry); 102 | jout.closeEntry(); 103 | File[] files = src.listFiles(); 104 | if (files != null) { 105 | for (File file : files) { 106 | jar(file, prefix, jout); 107 | } 108 | } 109 | } else { 110 | byte[] buffer = new byte[8092]; 111 | ZipEntry entry = new ZipEntry(prefix + src.getName()); 112 | entry.setTime(src.lastModified()); 113 | jout.putNextEntry(entry); 114 | FileInputStream inputStream = new FileInputStream(src); 115 | int len; 116 | while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) { 117 | jout.write(buffer, 0, len); 118 | } 119 | inputStream.close(); 120 | jout.closeEntry(); 121 | } 122 | } 123 | 124 | public static List unJar(File jarFile, File unJarDir) throws Exception { 125 | List list = new ArrayList<>(); 126 | 127 | BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(jarFile)); 128 | unJar(inputStream, unJarDir, list); 129 | 130 | return list; 131 | } 132 | 133 | public static List unJar(InputStream inputStream, File unJarDir, List list) throws Exception { 134 | if (!unJarDir.exists()) { 135 | unJarDir.mkdirs(); 136 | } 137 | 138 | JarInputStream jin = new JarInputStream(inputStream); 139 | byte[] buffer = new byte[8092]; 140 | 141 | ZipEntry entry = jin.getNextEntry(); 142 | while (entry != null) { 143 | String fileName = entry.getName(); 144 | if (File.separatorChar != '/') { 145 | fileName = fileName.replace('/', File.separatorChar); 146 | } 147 | if (fileName.charAt(fileName.length() - 1) == '/') { 148 | fileName = fileName.substring(0, fileName.length() - 1); 149 | } 150 | if (fileName.charAt(0) == '/') { 151 | fileName = fileName.substring(1); 152 | } 153 | String entryName = entry.getName(); 154 | if (entryName.endsWith(".class")) { 155 | String className = entryName.replace('\\', '.').replace('/', '.'); 156 | list.add(className); 157 | } 158 | 159 | File file = new File(unJarDir, fileName); 160 | if (!file.getName().endsWith(".class")) { 161 | boolean re = file.mkdirs(); 162 | } else { 163 | File parent = file.getParentFile(); 164 | if (!parent.exists()) { 165 | parent.mkdirs(); 166 | } 167 | BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 168 | int len = 0; 169 | while ((len = jin.read(buffer, 0, buffer.length)) != -1) { 170 | out.write(buffer, 0, len); 171 | } 172 | out.flush(); 173 | out.close(); 174 | file.setLastModified(entry.getTime()); 175 | } 176 | jin.closeEntry(); 177 | entry = jin.getNextEntry(); 178 | } 179 | 180 | Manifest mf = jin.getManifest(); 181 | if (mf != null) { 182 | File file = new File(unJarDir, "META-INF/MANIFEST.MF"); 183 | File parent = file.getParentFile(); 184 | if (!parent.exists()) { 185 | parent.mkdirs(); 186 | } 187 | OutputStream out = new FileOutputStream(file); 188 | mf.write(out); 189 | out.flush(); 190 | out.close(); 191 | } 192 | 193 | jin.close(); 194 | 195 | return list; 196 | } 197 | 198 | } 199 | -------------------------------------------------------------------------------- /mock-method-plugin/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | Android 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 1544951263083 142 | 143 | 144 | 1544951263083 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 1.8 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | -------------------------------------------------------------------------------- /mock-method-plugin/src/main/groovy/com/mock/internel/Inject.groovy: -------------------------------------------------------------------------------- 1 | package com.mock.internel 2 | 3 | import com.mock.annotation.MockMethod 4 | import javassist.ClassPool 5 | import javassist.CtClass 6 | import javassist.CtMethod 7 | import org.gradle.api.Project 8 | 9 | 10 | class Inject { 11 | private static ClassPool pool = ClassPool.getDefault() 12 | private static List generatorMockMap = new ArrayList<>(); 13 | 14 | static void addPoolPath(String path) { 15 | pool.appendClassPath(path) 16 | } 17 | 18 | static void generatorMockMap(String path, Project project) { 19 | File jarFile = new File(path) 20 | String jarZipDir = JarUtils.getExtractJarPath(jarFile) 21 | File unJar = new File(jarZipDir) 22 | List strings = JarUtils.unJar(jarFile, unJar) 23 | addPoolPath(unJar.path) 24 | //todo inject代码 25 | if (strings.contains('com.mock.generator.MockMethodGenerator.class')) { 26 | CtClass ctClass = pool.getCtClass('com.mock.generator.MockMethodGenerator') 27 | try { 28 | CtMethod ctMethod = ctClass.getDeclaredMethod('initMackMock') 29 | 30 | StringBuilder builder = new StringBuilder() 31 | generatorMockMap.each { 32 | builder.append(getMockMapDes(it.className, it.methodName, it.values, it.defaultValue) + "\n") 33 | } 34 | println '方法' + builder.toString() 35 | ctMethod.insertBefore(builder.toString()) 36 | ctClass.writeFile(unJar.path) 37 | ctClass.detach() 38 | } catch (Exception e) { 39 | e.printStackTrace() 40 | } finally { 41 | generatorMockMap.clear() 42 | } 43 | } 44 | println '解压路径:' + new File(jarZipDir).path 45 | println '压缩路径:' + new File(path).path 46 | jarFile.delete() 47 | JarUtils.jar(jarFile, unJar) 48 | } 49 | 50 | private 51 | static String getMockMapDes(String className, String methodName, String values, String defaultValue) { 52 | return String.format("mockMethodModels.add(getMockMap(\"%s\",\"%s\",\"%s\",\"%s\"));", className, methodName, values, defaultValue) 53 | } 54 | 55 | static void injectMockMap(String className, String methodName, String values, String defaultValue) { 56 | MockMethodModel mockMethodModel = new MockMethodModel() 57 | mockMethodModel.values = values 58 | mockMethodModel.defaultValue = defaultValue 59 | mockMethodModel.methodName = methodName 60 | mockMethodModel.className = className 61 | generatorMockMap.add(mockMethodModel) 62 | } 63 | 64 | 65 | static void injectJarPath(String path, Project project) { 66 | //注入依赖与generator中的类,所以注入jar path 67 | project.extensions[MockExtension.plugin].subprojects.each { 68 | if (path.contains(it)) { 69 | File jarFile = new File(path) 70 | String jarZipDir = JarUtils.getExtractJarPath(jarFile) 71 | File unJar = new File(jarZipDir) 72 | List strings = JarUtils.unJar(jarFile, unJar) 73 | injectDir(unJar.path, project) 74 | println '解压路径:' + new File(jarZipDir).path 75 | println '压缩路径:' + new File(path).path 76 | jarFile.delete() 77 | JarUtils.jar(jarFile, unJar) 78 | } 79 | } 80 | 81 | } 82 | 83 | 84 | static void injectDir(String path, Project project) { 85 | println("--------path----:" + path) 86 | addPoolPath(path) 87 | processorDir(path, project) 88 | } 89 | 90 | private static void processorDir(String path, Project project) { 91 | File dir = new File(path) 92 | if (dir.isDirectory()) { 93 | dir.eachFileRecurse { File file -> 94 | String classPath = getClassPath(file, path) 95 | String className = classPath.replace('\\', '.').replace('/', '.') 96 | if (className.endsWith(".class") 97 | && !className.contains('R$') 98 | && !className.contains('R.class') 99 | && !className.contains("BuildConfig.class")) { 100 | 101 | project.extensions[MockExtension.plugin].packages.each { String pkg -> 102 | if (className.contains(pkg)) { 103 | className = className.substring(0, className.length() - 6) 104 | println 'className:' + className 105 | try { 106 | injectClass(path, className) 107 | } catch (Exception e) { 108 | e.printStackTrace() 109 | } 110 | } 111 | } 112 | } 113 | } 114 | } 115 | } 116 | 117 | private static String getClassPath(File file, String path) { 118 | String filePath = file.absolutePath 119 | return filePath.substring(path.length() + 1, filePath.length()) 120 | 121 | } 122 | 123 | 124 | private static void injectClass(String path, String className) { 125 | CtClass ctClass = pool.get(className) 126 | CtMethod[] methods = ctClass.getDeclaredMethods() 127 | if (methods == null || methods.length == 0) { 128 | return 129 | } 130 | methods.each { CtMethod method -> 131 | MockMethod mockMethod = method.getAnnotation(MockMethod.class) 132 | if (mockMethod == null) { 133 | return 134 | } 135 | println "method return : " + method.returnType.name 136 | if (!isAllow(method.returnType.name)) { 137 | return 138 | } 139 | injectMockMap(className, method.name, mockMethod.values(), mockMethod.defaultValue()) 140 | println("methodDes:" + getMethodDes(className, method.name, method.returnType.name)) 141 | 142 | method.insertBefore(getMethodDes(className, method.name, method.returnType.name)) 143 | 144 | } 145 | ctClass.writeFile(path) 146 | ctClass.detach() 147 | } 148 | static List dataType = [ 149 | String.class.name, 150 | int.class.name, 151 | long.class.name, 152 | double.class.name, 153 | float.class.name, 154 | boolean.class.name, 155 | short.class.name, 156 | Integer.class.name, 157 | Long.class.name, 158 | Double.class.name, 159 | Float.class.name, 160 | Boolean.class.name, 161 | Short.class.name,].collect() 162 | 163 | static boolean isAllow(String returnType) { 164 | if (returnType == null) { 165 | return false 166 | } 167 | 168 | 169 | 170 | if (dataType.contains(returnType)) { 171 | return true 172 | } 173 | return false 174 | } 175 | 176 | private static String getMethodDes(String className, String methodName, String returnType) { 177 | return String.format("if (com.mock.generator.MockManager.isMock(\"%s\", \"%s\")) {\n" + 178 | " String value = com.mock.generator.MockManager.getValue(\"%s\", \"%s\");\n" + 179 | " return " + getReturnValueByStr(returnType) + ";\n" + 180 | " }", className, methodName, className, methodName) 181 | } 182 | 183 | private static String getReturnValueByStr(String returnType) { 184 | if (returnType.contains("String")) { 185 | return 'value' 186 | } else if (returnType.contains("int")) { 187 | return 'Integer.parseInt(value)' 188 | } else if (returnType.contains("long")) { 189 | return 'Long.parseLong(value)' 190 | } else if (returnType.contains("double")) { 191 | return 'Double.parseDouble(value)' 192 | } else if (returnType.contains("float")) { 193 | return 'Float.parseFloat(value)' 194 | } else if (returnType.contains("boolean")) { 195 | return 'Boolean.parseBoolean(value)' 196 | } else if (returnType.contains("short")) { 197 | return 'Short.parseShort(value)' 198 | } else if (returnType.contains("Integer")) { 199 | return 'Integer.valueOf(value)' 200 | } else if (returnType.contains("Long")) { 201 | return 'Long.valueOf(value)' 202 | } else if (returnType.contains("Double")) { 203 | return 'Double.valueOf(value)' 204 | } else if (returnType.contains("Float")) { 205 | return 'Float.valueOf(value)' 206 | } else if (returnType.contains("Boolean")) { 207 | return 'Boolean.valueOf(value)' 208 | } else if (returnType.contains("Short")) { 209 | return 'Short.valueOf(value)' 210 | } 211 | } 212 | 213 | 214 | } 215 | --------------------------------------------------------------------------------
20 | * This class is copied from Intimate . 21 | *
22 | * https://github.com/JustKiddingBaby/Intimate 23 | */ 24 | public class JarUtils { 25 | 26 | /** 27 | * 返回推荐的 jar 解压目录 28 | * 29 | * @param path jar 文件路径 30 | * @return 解压的 jar 路径 31 | */ 32 | public static String getExtractJarPath(String path) { 33 | return getExtractJarPath(new File(path)); 34 | } 35 | 36 | /** 37 | * 返回推荐的 jar 解压目录 38 | * 39 | * @param jarFile jar 文件 40 | * @return 解压的 jar 路径 41 | */ 42 | public static String getExtractJarPath(File jarFile) { 43 | return jarFile.getParent() + "/" + jarFile.getName().replace(".jar", ""); 44 | } 45 | 46 | /** 47 | * 压缩 jar目录 48 | * @param unJarFile 49 | * @return 50 | */ 51 | public static String getExtractJarOriginPath(File unJarFile) { 52 | return unJarFile.getParent() + "/" + unJarFile.getName() + ".jar"; 53 | } 54 | 55 | public static void jar(File desJar, File jarDir) throws Exception { 56 | BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(desJar)); 57 | File[] src = jarDir.listFiles(); 58 | jar(out, src); 59 | } 60 | 61 | public static void jar(OutputStream out, File[] src) throws Exception { 62 | jar(out, src, null, null); 63 | } 64 | 65 | public static void jar(OutputStream out, File src) throws Exception { 66 | jar(out, new File[]{src}, null, null); 67 | } 68 | 69 | public static void jar(OutputStream out, File[] src, String prefix, Manifest man) throws Exception { 70 | JarOutputStream jout = null; 71 | if (man == null) { 72 | jout = new JarOutputStream(out); 73 | } else { 74 | jout = new JarOutputStream(out, man); 75 | } 76 | 77 | if (prefix != null && prefix.trim().length() > 0 && !prefix.equals("/")) { 78 | if (prefix.charAt(0) == '/') { 79 | prefix = prefix.substring(1); 80 | } 81 | if (prefix.charAt(prefix.length() - 1) != '/') { 82 | prefix = prefix + "/"; 83 | } 84 | } else { 85 | prefix = ""; 86 | } 87 | for (File f : src) { 88 | jar(f, prefix, jout); 89 | } 90 | jout.close(); 91 | } 92 | 93 | private static void jar(File src, String prefix, JarOutputStream jout) throws Exception { 94 | if (src.isDirectory()) { 95 | prefix = prefix + src.getName() + "/"; 96 | ZipEntry entry = new ZipEntry(prefix); 97 | entry.setTime(src.lastModified()); 98 | entry.setMethod(ZipEntry.STORED); 99 | entry.setSize(0l); 100 | entry.setCrc(0l); 101 | jout.putNextEntry(entry); 102 | jout.closeEntry(); 103 | File[] files = src.listFiles(); 104 | if (files != null) { 105 | for (File file : files) { 106 | jar(file, prefix, jout); 107 | } 108 | } 109 | } else { 110 | byte[] buffer = new byte[8092]; 111 | ZipEntry entry = new ZipEntry(prefix + src.getName()); 112 | entry.setTime(src.lastModified()); 113 | jout.putNextEntry(entry); 114 | FileInputStream inputStream = new FileInputStream(src); 115 | int len; 116 | while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) { 117 | jout.write(buffer, 0, len); 118 | } 119 | inputStream.close(); 120 | jout.closeEntry(); 121 | } 122 | } 123 | 124 | public static List unJar(File jarFile, File unJarDir) throws Exception { 125 | List list = new ArrayList<>(); 126 | 127 | BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(jarFile)); 128 | unJar(inputStream, unJarDir, list); 129 | 130 | return list; 131 | } 132 | 133 | public static List unJar(InputStream inputStream, File unJarDir, List list) throws Exception { 134 | if (!unJarDir.exists()) { 135 | unJarDir.mkdirs(); 136 | } 137 | 138 | JarInputStream jin = new JarInputStream(inputStream); 139 | byte[] buffer = new byte[8092]; 140 | 141 | ZipEntry entry = jin.getNextEntry(); 142 | while (entry != null) { 143 | String fileName = entry.getName(); 144 | if (File.separatorChar != '/') { 145 | fileName = fileName.replace('/', File.separatorChar); 146 | } 147 | if (fileName.charAt(fileName.length() - 1) == '/') { 148 | fileName = fileName.substring(0, fileName.length() - 1); 149 | } 150 | if (fileName.charAt(0) == '/') { 151 | fileName = fileName.substring(1); 152 | } 153 | String entryName = entry.getName(); 154 | if (entryName.endsWith(".class")) { 155 | String className = entryName.replace('\\', '.').replace('/', '.'); 156 | list.add(className); 157 | } 158 | 159 | File file = new File(unJarDir, fileName); 160 | if (!file.getName().endsWith(".class")) { 161 | boolean re = file.mkdirs(); 162 | } else { 163 | File parent = file.getParentFile(); 164 | if (!parent.exists()) { 165 | parent.mkdirs(); 166 | } 167 | BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 168 | int len = 0; 169 | while ((len = jin.read(buffer, 0, buffer.length)) != -1) { 170 | out.write(buffer, 0, len); 171 | } 172 | out.flush(); 173 | out.close(); 174 | file.setLastModified(entry.getTime()); 175 | } 176 | jin.closeEntry(); 177 | entry = jin.getNextEntry(); 178 | } 179 | 180 | Manifest mf = jin.getManifest(); 181 | if (mf != null) { 182 | File file = new File(unJarDir, "META-INF/MANIFEST.MF"); 183 | File parent = file.getParentFile(); 184 | if (!parent.exists()) { 185 | parent.mkdirs(); 186 | } 187 | OutputStream out = new FileOutputStream(file); 188 | mf.write(out); 189 | out.flush(); 190 | out.close(); 191 | } 192 | 193 | jin.close(); 194 | 195 | return list; 196 | } 197 | 198 | } 199 | -------------------------------------------------------------------------------- /mock-method-plugin/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | Android 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 1544951263083 142 | 143 | 144 | 1544951263083 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 1.8 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | -------------------------------------------------------------------------------- /mock-method-plugin/src/main/groovy/com/mock/internel/Inject.groovy: -------------------------------------------------------------------------------- 1 | package com.mock.internel 2 | 3 | import com.mock.annotation.MockMethod 4 | import javassist.ClassPool 5 | import javassist.CtClass 6 | import javassist.CtMethod 7 | import org.gradle.api.Project 8 | 9 | 10 | class Inject { 11 | private static ClassPool pool = ClassPool.getDefault() 12 | private static List generatorMockMap = new ArrayList<>(); 13 | 14 | static void addPoolPath(String path) { 15 | pool.appendClassPath(path) 16 | } 17 | 18 | static void generatorMockMap(String path, Project project) { 19 | File jarFile = new File(path) 20 | String jarZipDir = JarUtils.getExtractJarPath(jarFile) 21 | File unJar = new File(jarZipDir) 22 | List strings = JarUtils.unJar(jarFile, unJar) 23 | addPoolPath(unJar.path) 24 | //todo inject代码 25 | if (strings.contains('com.mock.generator.MockMethodGenerator.class')) { 26 | CtClass ctClass = pool.getCtClass('com.mock.generator.MockMethodGenerator') 27 | try { 28 | CtMethod ctMethod = ctClass.getDeclaredMethod('initMackMock') 29 | 30 | StringBuilder builder = new StringBuilder() 31 | generatorMockMap.each { 32 | builder.append(getMockMapDes(it.className, it.methodName, it.values, it.defaultValue) + "\n") 33 | } 34 | println '方法' + builder.toString() 35 | ctMethod.insertBefore(builder.toString()) 36 | ctClass.writeFile(unJar.path) 37 | ctClass.detach() 38 | } catch (Exception e) { 39 | e.printStackTrace() 40 | } finally { 41 | generatorMockMap.clear() 42 | } 43 | } 44 | println '解压路径:' + new File(jarZipDir).path 45 | println '压缩路径:' + new File(path).path 46 | jarFile.delete() 47 | JarUtils.jar(jarFile, unJar) 48 | } 49 | 50 | private 51 | static String getMockMapDes(String className, String methodName, String values, String defaultValue) { 52 | return String.format("mockMethodModels.add(getMockMap(\"%s\",\"%s\",\"%s\",\"%s\"));", className, methodName, values, defaultValue) 53 | } 54 | 55 | static void injectMockMap(String className, String methodName, String values, String defaultValue) { 56 | MockMethodModel mockMethodModel = new MockMethodModel() 57 | mockMethodModel.values = values 58 | mockMethodModel.defaultValue = defaultValue 59 | mockMethodModel.methodName = methodName 60 | mockMethodModel.className = className 61 | generatorMockMap.add(mockMethodModel) 62 | } 63 | 64 | 65 | static void injectJarPath(String path, Project project) { 66 | //注入依赖与generator中的类,所以注入jar path 67 | project.extensions[MockExtension.plugin].subprojects.each { 68 | if (path.contains(it)) { 69 | File jarFile = new File(path) 70 | String jarZipDir = JarUtils.getExtractJarPath(jarFile) 71 | File unJar = new File(jarZipDir) 72 | List strings = JarUtils.unJar(jarFile, unJar) 73 | injectDir(unJar.path, project) 74 | println '解压路径:' + new File(jarZipDir).path 75 | println '压缩路径:' + new File(path).path 76 | jarFile.delete() 77 | JarUtils.jar(jarFile, unJar) 78 | } 79 | } 80 | 81 | } 82 | 83 | 84 | static void injectDir(String path, Project project) { 85 | println("--------path----:" + path) 86 | addPoolPath(path) 87 | processorDir(path, project) 88 | } 89 | 90 | private static void processorDir(String path, Project project) { 91 | File dir = new File(path) 92 | if (dir.isDirectory()) { 93 | dir.eachFileRecurse { File file -> 94 | String classPath = getClassPath(file, path) 95 | String className = classPath.replace('\\', '.').replace('/', '.') 96 | if (className.endsWith(".class") 97 | && !className.contains('R$') 98 | && !className.contains('R.class') 99 | && !className.contains("BuildConfig.class")) { 100 | 101 | project.extensions[MockExtension.plugin].packages.each { String pkg -> 102 | if (className.contains(pkg)) { 103 | className = className.substring(0, className.length() - 6) 104 | println 'className:' + className 105 | try { 106 | injectClass(path, className) 107 | } catch (Exception e) { 108 | e.printStackTrace() 109 | } 110 | } 111 | } 112 | } 113 | } 114 | } 115 | } 116 | 117 | private static String getClassPath(File file, String path) { 118 | String filePath = file.absolutePath 119 | return filePath.substring(path.length() + 1, filePath.length()) 120 | 121 | } 122 | 123 | 124 | private static void injectClass(String path, String className) { 125 | CtClass ctClass = pool.get(className) 126 | CtMethod[] methods = ctClass.getDeclaredMethods() 127 | if (methods == null || methods.length == 0) { 128 | return 129 | } 130 | methods.each { CtMethod method -> 131 | MockMethod mockMethod = method.getAnnotation(MockMethod.class) 132 | if (mockMethod == null) { 133 | return 134 | } 135 | println "method return : " + method.returnType.name 136 | if (!isAllow(method.returnType.name)) { 137 | return 138 | } 139 | injectMockMap(className, method.name, mockMethod.values(), mockMethod.defaultValue()) 140 | println("methodDes:" + getMethodDes(className, method.name, method.returnType.name)) 141 | 142 | method.insertBefore(getMethodDes(className, method.name, method.returnType.name)) 143 | 144 | } 145 | ctClass.writeFile(path) 146 | ctClass.detach() 147 | } 148 | static List dataType = [ 149 | String.class.name, 150 | int.class.name, 151 | long.class.name, 152 | double.class.name, 153 | float.class.name, 154 | boolean.class.name, 155 | short.class.name, 156 | Integer.class.name, 157 | Long.class.name, 158 | Double.class.name, 159 | Float.class.name, 160 | Boolean.class.name, 161 | Short.class.name,].collect() 162 | 163 | static boolean isAllow(String returnType) { 164 | if (returnType == null) { 165 | return false 166 | } 167 | 168 | 169 | 170 | if (dataType.contains(returnType)) { 171 | return true 172 | } 173 | return false 174 | } 175 | 176 | private static String getMethodDes(String className, String methodName, String returnType) { 177 | return String.format("if (com.mock.generator.MockManager.isMock(\"%s\", \"%s\")) {\n" + 178 | " String value = com.mock.generator.MockManager.getValue(\"%s\", \"%s\");\n" + 179 | " return " + getReturnValueByStr(returnType) + ";\n" + 180 | " }", className, methodName, className, methodName) 181 | } 182 | 183 | private static String getReturnValueByStr(String returnType) { 184 | if (returnType.contains("String")) { 185 | return 'value' 186 | } else if (returnType.contains("int")) { 187 | return 'Integer.parseInt(value)' 188 | } else if (returnType.contains("long")) { 189 | return 'Long.parseLong(value)' 190 | } else if (returnType.contains("double")) { 191 | return 'Double.parseDouble(value)' 192 | } else if (returnType.contains("float")) { 193 | return 'Float.parseFloat(value)' 194 | } else if (returnType.contains("boolean")) { 195 | return 'Boolean.parseBoolean(value)' 196 | } else if (returnType.contains("short")) { 197 | return 'Short.parseShort(value)' 198 | } else if (returnType.contains("Integer")) { 199 | return 'Integer.valueOf(value)' 200 | } else if (returnType.contains("Long")) { 201 | return 'Long.valueOf(value)' 202 | } else if (returnType.contains("Double")) { 203 | return 'Double.valueOf(value)' 204 | } else if (returnType.contains("Float")) { 205 | return 'Float.valueOf(value)' 206 | } else if (returnType.contains("Boolean")) { 207 | return 'Boolean.valueOf(value)' 208 | } else if (returnType.contains("Short")) { 209 | return 'Short.valueOf(value)' 210 | } 211 | } 212 | 213 | 214 | } 215 | --------------------------------------------------------------------------------