-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from zhuozp/v1.0.0
feat: view的注入使用apt动态生成类文件的形式
- Loading branch information
Showing
7 changed files
with
109 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
apt/src/main/java/com/gibbon/etc/apt/factory/ViewInjectStateFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<A> {\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) { | ||
|
||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters