From 1d82b3f26e061d9e8d01d008cd9385eaa5e4ebfd Mon Sep 17 00:00:00 2001 From: "zhipeng.zhuo" Date: Sat, 14 Mar 2020 22:37:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20view=E7=9A=84=E6=B3=A8=E5=85=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8apt=E5=8A=A8=E6=80=81=E7=94=9F=E6=88=90?= =?UTF-8?q?=E7=B1=BB=E6=96=87=E4=BB=B6=E7=9A=84=E5=BD=A2=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../etc/demo/AptAndAspectBothActivity.java | 20 ++--- .../java/com/gibbon/etc/apt/AptProcessor.java | 4 + .../java/com/gibbon/etc/apt/ProxyInfo.java | 8 ++ .../apt/factory/ViewInjectStateFactory.java | 86 +++++++++++++++++++ .../com/gibbon/etc/inject/ViewInject.java | 9 -- .../com/gibbon/etc/inject/ViewInjector.java | 39 --------- gradle.properties | 6 +- 7 files changed, 109 insertions(+), 63 deletions(-) create mode 100644 apt/src/main/java/com/gibbon/etc/apt/factory/ViewInjectStateFactory.java delete mode 100644 etc/src/main/java/com/gibbon/etc/inject/ViewInject.java delete mode 100644 etc/src/main/java/com/gibbon/etc/inject/ViewInjector.java diff --git a/app/src/main/java/com/gibbon/etc/demo/AptAndAspectBothActivity.java b/app/src/main/java/com/gibbon/etc/demo/AptAndAspectBothActivity.java index 2588518..2b9cb21 100644 --- a/app/src/main/java/com/gibbon/etc/demo/AptAndAspectBothActivity.java +++ b/app/src/main/java/com/gibbon/etc/demo/AptAndAspectBothActivity.java @@ -17,6 +17,7 @@ import androidx.recyclerview.widget.StaggeredGridLayoutManager; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; +import com.gibbon.etc.annotation.Background; import com.gibbon.etc.annotation.UiThread; import com.gibbon.etc.apt.annotation.ViewById; import com.gibbon.etc.apt.annotation.DownloadMoreListener; @@ -80,20 +81,15 @@ void recyclerView() { } @RefreshListner() + @Background void refreshView() { // TODO, in other thread, such as downloading res - new Thread(new Runnable() { - @Override - public void run() { - // - try { - Thread.sleep(3000); - stopRefresh(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - }).start(); + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + stopRefresh(); } @UiThread diff --git a/apt/src/main/java/com/gibbon/etc/apt/AptProcessor.java b/apt/src/main/java/com/gibbon/etc/apt/AptProcessor.java index bfdcb23..8abe2a3 100644 --- a/apt/src/main/java/com/gibbon/etc/apt/AptProcessor.java +++ b/apt/src/main/java/com/gibbon/etc/apt/AptProcessor.java @@ -3,6 +3,7 @@ import com.gibbon.etc.apt.annotation.DownloadMoreListener; import com.gibbon.etc.apt.annotation.RefreshListner; import com.gibbon.etc.apt.annotation.ViewById; +import com.gibbon.etc.apt.factory.ViewInjectStateFactory; import com.gibbon.etc.apt.uitls.Utils; import com.google.auto.service.AutoService; @@ -80,6 +81,9 @@ public boolean process(Set set, RoundEnvironment roundEnv return false; } + ViewInjectStateFactory.generateViewInject(filer); + ViewInjectStateFactory.generateViewInjector(filer); + //生成类 for (String key : proxyInfoHashMap.keySet()) { ProxyInfo proxyInfo = proxyInfoHashMap.get(key); diff --git a/apt/src/main/java/com/gibbon/etc/apt/ProxyInfo.java b/apt/src/main/java/com/gibbon/etc/apt/ProxyInfo.java index a3bdbed..f593955 100644 --- a/apt/src/main/java/com/gibbon/etc/apt/ProxyInfo.java +++ b/apt/src/main/java/com/gibbon/etc/apt/ProxyInfo.java @@ -54,6 +54,14 @@ public ProxyInfo(Elements elements, TypeElement typeElement) { this.proxyClassName = className + "$" + PROXY; } + /** + * 获取包名 + * + * */ + public String getPackageName() { + return packageName; + } + /** * 获取全名 * diff --git a/apt/src/main/java/com/gibbon/etc/apt/factory/ViewInjectStateFactory.java b/apt/src/main/java/com/gibbon/etc/apt/factory/ViewInjectStateFactory.java new file mode 100644 index 0000000..2b48206 --- /dev/null +++ b/apt/src/main/java/com/gibbon/etc/apt/factory/ViewInjectStateFactory.java @@ -0,0 +1,86 @@ +package com.gibbon.etc.apt.factory; + +import java.io.IOException; +import java.io.Writer; + +import javax.annotation.processing.Filer; +import javax.tools.JavaFileObject; + +/** + * @author zhipeng.zhuo + * @date 2020-03-14 + */ +public class ViewInjectStateFactory { + + public static final String VIEW_INJECT_NAME = "com.gibbon.etc.inject.ViewInject"; + public static final String VIEW_INJECTOR_NAME = "com.gibbon.etc.inject.ViewInjector"; + + public static void generateViewInject(Filer filer) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("package com.gibbon.etc.inject;\n" + + "\n" + + "public interface ViewInject {\n" + + " void inject(A a, Object source);\n" + + "}"); + + try { + JavaFileObject jfo = filer.createSourceFile(VIEW_INJECT_NAME, null); + Writer writer = jfo.openWriter(); + writer.write(stringBuilder.toString()); + writer.flush(); + writer.close(); + } catch (IOException e) { + + } + } + + public static void generateViewInjector(Filer filer) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("package com.gibbon.etc.inject;\n" + + "\n" + + "import android.app.Activity;\n" + + "import android.view.View;\n" + + "\n" + + + "public class ViewInjector {\n" + + "\n" + + " private static final String SUFFIX = \"$ViewInject\";\n" + + "\n" + + " public static void inject(Activity activity) {\n" + + " ViewInject viewInject = getProxyInject(activity);\n" + + " if (viewInject != null) {\n" + + " viewInject.inject(activity, activity);\n" + + " }\n" + + " }\n" + + "\n" + + " public static void inject(Object object, View view) {\n" + + " ViewInject viewInject = getProxyInject(object);\n" + + " if (viewInject != null) {\n" + + " viewInject.inject(object, view);\n" + + " }\n" + + " }\n" + + "\n" + + " private static ViewInject getProxyInject(Object object) {\n" + + " try {\n" + + " Class cls = object.getClass();\n" + + " Class injectorCls = Class.forName(cls.getName() + SUFFIX);\n" + + " return (ViewInject) injectorCls.newInstance();\n" + + " } catch (Exception e) {\n" + + "\n" + + " }\n" + + "\n" + + " return null;\n" + + " }\n" + + "}"); + + try { + JavaFileObject jfo = filer.createSourceFile(VIEW_INJECTOR_NAME , null); + Writer writer = jfo.openWriter(); + writer.write(stringBuilder.toString()); + writer.flush(); + writer.close(); + } catch (IOException e) { + + } + } +} diff --git a/etc/src/main/java/com/gibbon/etc/inject/ViewInject.java b/etc/src/main/java/com/gibbon/etc/inject/ViewInject.java deleted file mode 100644 index 0e0e8c4..0000000 --- a/etc/src/main/java/com/gibbon/etc/inject/ViewInject.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.gibbon.etc.inject; - -/** - * @author zhipeng.zhuo - * @date 2020-03-13 - */ -public interface ViewInject { - void inject(A a, Object source); -} diff --git a/etc/src/main/java/com/gibbon/etc/inject/ViewInjector.java b/etc/src/main/java/com/gibbon/etc/inject/ViewInjector.java deleted file mode 100644 index 4ad4d35..0000000 --- a/etc/src/main/java/com/gibbon/etc/inject/ViewInjector.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.gibbon.etc.inject; - -import android.app.Activity; -import android.view.View; - -/** - * @author zhipeng.zhuo - * @date 2020-03-13 - */ -public class ViewInjector { - - private static final String SUFFIX = "$ViewInject"; - - public static void inject(Activity activity) { - ViewInject viewInject = getProxyInject(activity); - if (viewInject != null) { - viewInject.inject(activity, activity); - } - } - - public static void inject(Object object, View view) { - ViewInject viewInject = getProxyInject(object); - if (viewInject != null) { - viewInject.inject(object, view); - } - } - - private static ViewInject getProxyInject(Object object) { - try { - Class cls = object.getClass(); - Class injectorCls = Class.forName(cls.getName() + SUFFIX); - return (ViewInject) injectorCls.newInstance(); - } catch (Exception e) { - - } - - return null; - } -} diff --git a/gradle.properties b/gradle.properties index 54860f2..9e34009 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ # http://www.gradle.org/docs/current/userguide/build_environment.html # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. -org.gradle.jvmargs=-Xmx1536m +#org.gradle.jvmargs=-Xmx1536m # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects @@ -18,5 +18,5 @@ android.useAndroidX=true # Automatically convert third-party libraries to use AndroidX android.enableJetifier=true -#org.gradle.daemon=true -#org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 \ No newline at end of file +org.gradle.daemon=true +org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 \ No newline at end of file