├── META-INF
├── MANIFEST.MF
└── services
│ └── javax.annotation.processing.Processor
├── images
└── InjectDagger.png
├── .gitignore
├── libs
└── android-support-v4.jar
├── simple_injector_demo
├── ic_launcher-web.png
├── libs
│ ├── view_prosessor.jar
│ └── android-support-v4.jar
├── res
│ ├── drawable-hdpi
│ │ └── ic_launcher.png
│ ├── drawable-mdpi
│ │ └── ic_launcher.png
│ ├── drawable-xhdpi
│ │ └── ic_launcher.png
│ ├── drawable-xxhdpi
│ │ └── ic_launcher.png
│ ├── values
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── layout
│ │ ├── fragment_main.xml
│ │ └── activity_main.xml
├── .factorypath
├── apt_generated
│ └── org
│ │ └── simple
│ │ └── annotation
│ │ └── demo
│ │ ├── TestFragment$InjectAdapter.java
│ │ ├── MainActivity$InjectAdapter.java
│ │ └── TestActivity$InjectAdapter.java
├── project.properties
├── src
│ └── org
│ │ └── simple
│ │ └── annotation
│ │ └── demo
│ │ ├── TestActivity.java
│ │ ├── MainActivity.java
│ │ └── TestFragment.java
├── proguard-project.txt
└── AndroidManifest.xml
├── AndroidManifest.xml
├── README.md
├── project.properties
├── proguard-project.txt
└── src
└── org
└── simple
└── injector
├── adapter
├── InjectAdapter.java
└── NullAdapter.java
├── anno
├── ViewInjector.java
└── handler
│ ├── AnnotationHandler.java
│ └── ViewInjectHandler.java
├── apt
├── writer
│ ├── AdapterWriter.java
│ ├── DefaultJavaFileWriter.java
│ └── AbsWriter.java
└── ViewInjectorProcessor.java
├── util
├── IOUtil.java
├── AnnotationUtil.java
└── ViewFinder.java
└── SimpleDagger.java
/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
--------------------------------------------------------------------------------
/META-INF/services/javax.annotation.processing.Processor:
--------------------------------------------------------------------------------
1 | org.simple.injector.apt.ViewInjectorProcessor
--------------------------------------------------------------------------------
/images/InjectDagger.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/InjectDagger/HEAD/images/InjectDagger.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.project
2 | *.classpath
3 | bin/
4 | gen/
5 | *.DS_Store
6 | */bin/
7 | */gen/
8 | .settings/
9 |
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/InjectDagger/HEAD/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/simple_injector_demo/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/InjectDagger/HEAD/simple_injector_demo/ic_launcher-web.png
--------------------------------------------------------------------------------
/simple_injector_demo/libs/view_prosessor.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/InjectDagger/HEAD/simple_injector_demo/libs/view_prosessor.jar
--------------------------------------------------------------------------------
/simple_injector_demo/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/InjectDagger/HEAD/simple_injector_demo/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/simple_injector_demo/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/InjectDagger/HEAD/simple_injector_demo/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/simple_injector_demo/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/InjectDagger/HEAD/simple_injector_demo/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/simple_injector_demo/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/InjectDagger/HEAD/simple_injector_demo/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/simple_injector_demo/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/InjectDagger/HEAD/simple_injector_demo/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/simple_injector_demo/.factorypath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/simple_injector_demo/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | simple_annotation_demo
5 | Hello world!
6 | Second
7 | This is in fragment
8 |
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # InjectDagger
2 |
3 | * [基础知识](http://qiushao.net/2015/07/07/Annotation-Processing-Tool%E8%AF%A6%E8%A7%A3/)
4 |
5 | 使用APT进行注解的库,类似于ButterKnife。目前只完成View id的注解,有兴趣的朋友可以参考一下。
6 |
7 | 为了使用Annotation Processor Tools,在导入eclipse之后需要添加jre库的引用,步骤如图所示:
8 |
9 | 
10 |
11 | 在Java Build Path的Liararies中选择add Library,然后选中JRE库,点击next之后选择finish即可。对于使用Android Studio的同学可以自行搜索如何引用
12 |
--------------------------------------------------------------------------------
/simple_injector_demo/apt_generated/org/simple/annotation/demo/TestFragment$InjectAdapter.java:
--------------------------------------------------------------------------------
1 | package org.simple.annotation.demo ;
2 |
3 | import org.simple.injector.adapter.InjectAdapter ;
4 | import org.simple.injector.util.ViewFinder;
5 |
6 |
7 | /* This class is generated by Simple ViewInjector, please don't modify! */
8 | public class TestFragment$InjectAdapter implements InjectAdapter {
9 |
10 | public void injects(TestFragment target) {
11 | target.textView = ViewFinder.findViewById(target, 2131099651 ) ;
12 | }
13 |
14 | }
--------------------------------------------------------------------------------
/simple_injector_demo/res/layout/fragment_main.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
13 |
14 |
--------------------------------------------------------------------------------
/simple_injector_demo/apt_generated/org/simple/annotation/demo/MainActivity$InjectAdapter.java:
--------------------------------------------------------------------------------
1 | package org.simple.annotation.demo ;
2 |
3 | import org.simple.injector.adapter.InjectAdapter ;
4 | import org.simple.injector.util.ViewFinder;
5 |
6 |
7 | /* This class is generated by Simple ViewInjector, please don't modify! */
8 | public class MainActivity$InjectAdapter implements InjectAdapter {
9 |
10 | public void injects(MainActivity target) {
11 | target.mTextView2 = ViewFinder.findViewById(target, 2131099649 ) ;
12 | target.mTextView = ViewFinder.findViewById(target, 2131099648 ) ;
13 | }
14 |
15 | }
--------------------------------------------------------------------------------
/simple_injector_demo/apt_generated/org/simple/annotation/demo/TestActivity$InjectAdapter.java:
--------------------------------------------------------------------------------
1 | package org.simple.annotation.demo ;
2 |
3 | import org.simple.injector.adapter.InjectAdapter ;
4 | import org.simple.injector.util.ViewFinder;
5 |
6 |
7 | /* This class is generated by Simple ViewInjector, please don't modify! */
8 | public class TestActivity$InjectAdapter implements InjectAdapter {
9 |
10 | public void injects(TestActivity target) {
11 | target.mTextView2 = ViewFinder.findViewById(target, 2131099649 ) ;
12 | target.mTextView = ViewFinder.findViewById(target, 2131099648 ) ;
13 | }
14 |
15 | }
--------------------------------------------------------------------------------
/simple_injector_demo/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-16
15 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-10
15 | android.library=true
16 |
--------------------------------------------------------------------------------
/simple_injector_demo/src/org/simple/annotation/demo/TestActivity.java:
--------------------------------------------------------------------------------
1 |
2 | package org.simple.annotation.demo;
3 |
4 | import android.app.Activity;
5 | import android.os.Bundle;
6 | import android.widget.TextView;
7 |
8 | import org.simple.injector.anno.ViewInjector;
9 |
10 | public class TestActivity extends Activity {
11 |
12 | @ViewInjector(R.id.my_tv)
13 | protected TextView mTextView;
14 |
15 | @ViewInjector(R.id.my_tv2)
16 | protected TextView mTextView2;
17 |
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_main);
22 | }
23 |
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/simple_injector_demo/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/simple_injector_demo/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/simple_injector_demo/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
13 |
14 |
19 |
20 |
24 |
25 |
--------------------------------------------------------------------------------
/simple_injector_demo/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/simple_injector_demo/src/org/simple/annotation/demo/MainActivity.java:
--------------------------------------------------------------------------------
1 |
2 | package org.simple.annotation.demo;
3 |
4 | import android.os.Bundle;
5 | import android.support.v4.app.FragmentActivity;
6 | import android.util.Log;
7 | import android.widget.TextView;
8 |
9 | import org.simple.injector.SimpleDagger;
10 | import org.simple.injector.anno.ViewInjector;
11 |
12 | public class MainActivity extends FragmentActivity {
13 |
14 | @ViewInjector(R.id.my_tv)
15 | protected TextView mTextView;
16 |
17 | @ViewInjector(R.id.my_tv2)
18 | protected TextView mTextView2;
19 |
20 | @Override
21 | protected void onCreate(Bundle savedInstanceState) {
22 | super.onCreate(savedInstanceState);
23 | setContentView(R.layout.activity_main);
24 |
25 | getSupportFragmentManager().beginTransaction().add(R.id.container, new TestFragment())
26 | .commit();
27 |
28 | //
29 | SimpleDagger.inject(this);
30 |
31 | if (mTextView != null) {
32 | Log.e("", "### my text view : " + mTextView.getText());
33 | }
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/org/simple/injector/adapter/InjectAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.injector.adapter;
26 |
27 | /**
28 | * @author mrsimple
29 | * @param
30 | */
31 | public interface InjectAdapter {
32 | void injects(T target);
33 | }
34 |
--------------------------------------------------------------------------------
/src/org/simple/injector/adapter/NullAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.injector.adapter;
26 |
27 | public class NullAdapter implements InjectAdapter {
28 |
29 | @Override
30 | public void injects(T target) {
31 |
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/org/simple/injector/anno/ViewInjector.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.injector.anno;
26 |
27 | import java.lang.annotation.ElementType;
28 | import java.lang.annotation.Retention;
29 | import java.lang.annotation.RetentionPolicy;
30 | import java.lang.annotation.Target;
31 |
32 | @Target(ElementType.FIELD)
33 | @Retention(RetentionPolicy.CLASS)
34 | public @interface ViewInjector {
35 | int value();
36 | }
37 |
--------------------------------------------------------------------------------
/src/org/simple/injector/apt/writer/AdapterWriter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.injector.apt.writer;
26 |
27 | import java.util.List;
28 | import java.util.Map;
29 |
30 | import javax.lang.model.element.VariableElement;
31 |
32 | /**
33 | * @author mrsimple
34 | */
35 | public interface AdapterWriter {
36 | /**
37 | * @param typeMap
38 | */
39 | void generate(Map> typeMap);
40 | }
41 |
--------------------------------------------------------------------------------
/src/org/simple/injector/util/IOUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.injector.util;
26 |
27 | import java.io.Closeable;
28 | import java.io.IOException;
29 |
30 | /**
31 | * @author mrsimple
32 | */
33 | public final class IOUtil {
34 | public static void closeQuitly(Closeable closeable) {
35 | if (closeable != null) {
36 | try {
37 | closeable.close();
38 | } catch (IOException e) {
39 | e.printStackTrace();
40 | }
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/org/simple/injector/util/AnnotationUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.injector.util;
26 |
27 | import javax.annotation.processing.ProcessingEnvironment;
28 | import javax.lang.model.element.Element;
29 |
30 | /**
31 | * @author mrsimple
32 | */
33 | public final class AnnotationUtil {
34 | /**
35 | * @param processingEnv
36 | * @param element
37 | * @return
38 | */
39 | public static String getPackageName(ProcessingEnvironment processingEnv, Element element) {
40 | return processingEnv.getElementUtils().getPackageOf(element).getQualifiedName().toString();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/org/simple/injector/util/ViewFinder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.injector.util;
26 |
27 | import android.app.Activity;
28 | import android.support.v4.app.Fragment;
29 | import android.view.View;
30 |
31 | @SuppressWarnings("unchecked")
32 | public final class ViewFinder {
33 |
34 | public static T findViewById(Activity act, int id) {
35 | return (T) act.findViewById(id);
36 | }
37 |
38 | public static T findViewById(View rootView, int id) {
39 | return (T) rootView.findViewById(id);
40 | }
41 |
42 | public static T findViewById(Fragment fragment, int id) {
43 | return findViewById(fragment.getView(), id);
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/org/simple/injector/anno/handler/AnnotationHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.injector.anno.handler;
26 |
27 | import java.util.List;
28 | import java.util.Map;
29 |
30 | import javax.annotation.processing.ProcessingEnvironment;
31 | import javax.annotation.processing.RoundEnvironment;
32 | import javax.lang.model.element.VariableElement;
33 |
34 | /**
35 | * 注解处理接口
36 | *
37 | * @author mrsimple
38 | */
39 | public interface AnnotationHandler {
40 | /**
41 | * @param processingEnv
42 | */
43 | void attachProcessingEnv(ProcessingEnvironment processingEnv);
44 |
45 | /**
46 | * @param roundEnv
47 | * @return
48 | */
49 | Map> handleAnnotation(RoundEnvironment roundEnv);
50 | }
51 |
--------------------------------------------------------------------------------
/simple_injector_demo/src/org/simple/annotation/demo/TestFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.annotation.demo;
26 |
27 | import android.os.Bundle;
28 | import android.support.v4.app.Fragment;
29 | import android.util.Log;
30 | import android.view.LayoutInflater;
31 | import android.view.View;
32 | import android.view.ViewGroup;
33 | import android.widget.TextView;
34 |
35 | import org.simple.injector.SimpleDagger;
36 | import org.simple.injector.anno.ViewInjector;
37 |
38 | public class TestFragment extends Fragment {
39 | @ViewInjector(R.id.fragment_tv)
40 | TextView textView;
41 |
42 | @Override
43 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
44 | View rootView = inflater.inflate(R.layout.fragment_main, container, false);
45 |
46 | SimpleDagger.inject(this, rootView);
47 |
48 | if (textView != null) {
49 | Log.e(getTag(), "### my text view in fragment : " + textView.getText());
50 | }
51 | return rootView;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/org/simple/injector/anno/handler/ViewInjectHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.injector.anno.handler;
26 |
27 | import org.simple.injector.anno.ViewInjector;
28 | import org.simple.injector.util.AnnotationUtil;
29 |
30 | import java.util.HashMap;
31 | import java.util.LinkedList;
32 | import java.util.List;
33 | import java.util.Map;
34 | import java.util.Set;
35 |
36 | import javax.annotation.processing.ProcessingEnvironment;
37 | import javax.annotation.processing.RoundEnvironment;
38 | import javax.lang.model.element.Element;
39 | import javax.lang.model.element.TypeElement;
40 | import javax.lang.model.element.VariableElement;
41 |
42 | /**
43 | * @author mrsimple
44 | */
45 | public class ViewInjectHandler implements AnnotationHandler {
46 |
47 | ProcessingEnvironment mProcessingEnv;
48 |
49 | @Override
50 | public void attachProcessingEnv(ProcessingEnvironment processingEnv) {
51 | mProcessingEnv = processingEnv;
52 | }
53 |
54 | @Override
55 | public Map> handleAnnotation(RoundEnvironment roundEnv) {
56 | Map> annotationMap = new HashMap>();
57 | // 获取使用ViewInjector注解的所有元素
58 | Set extends Element> elementSet = roundEnv.getElementsAnnotatedWith(ViewInjector.class);
59 | for (Element element : elementSet) {
60 | // 注解的字段
61 | VariableElement varElement = (VariableElement) element;
62 | // 类型的完整路径名,比如某个Activity的完整路径
63 | String className = getParentClassName(varElement);
64 | // 获取这个类型的所有注解,例如某个Activity中的所有View的注解对象
65 | List cacheElements = annotationMap.get(className);
66 | if (cacheElements == null) {
67 | cacheElements = new LinkedList();
68 | }
69 | // 将元素添加到该类型对应的字段列表中
70 | cacheElements.add(varElement);
71 | // 以类的路径为key,字段列表为value,存入map.
72 | // 这里是将所在字段按所属的类型进行分类
73 | annotationMap.put(className, cacheElements);
74 | }
75 |
76 | return annotationMap;
77 | }
78 |
79 | /**
80 | * 获取某个字段所属的类的完整路径
81 | *
82 | * @param varElement 字段元素
83 | * @return
84 | */
85 | private String getParentClassName(VariableElement varElement) {
86 | // 获取该元素所在的类型,例如某个View是某个Activity的字段,这里就是获取这个Activity的类型
87 | TypeElement typeElement = (TypeElement) varElement.getEnclosingElement();
88 | // 获取typeElement的包名
89 | String packageName = AnnotationUtil.getPackageName(mProcessingEnv, typeElement);
90 | // 类型的完整路径名,比如某个Activity的完整路径
91 | return packageName + "." + typeElement.getSimpleName().toString();
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/org/simple/injector/apt/writer/DefaultJavaFileWriter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.injector.apt.writer;
26 |
27 | import org.simple.injector.anno.ViewInjector;
28 |
29 | import java.io.IOException;
30 | import java.io.Writer;
31 |
32 | import javax.annotation.processing.ProcessingEnvironment;
33 | import javax.lang.model.element.VariableElement;
34 |
35 | /**
36 | * @author mrsimple
37 | */
38 | public class DefaultJavaFileWriter extends AbsWriter {
39 |
40 | public DefaultJavaFileWriter(ProcessingEnvironment processingEnv) {
41 | super(processingEnv);
42 | }
43 |
44 | /*
45 | * (non-Javadoc)
46 | * @see
47 | * org.simple.injector.apt.writer.AbsWriter#generateImport(java.io.Writer,
48 | * org.simple.injector.apt.ViewInjectorProcessor.InjectorInfo)
49 | */
50 | @Override
51 | protected void generateImport(Writer writer, InjectorInfo info)
52 | throws IOException {
53 | writer.write("package " + info.packageName + " ;");
54 | writer.write("\n\n");
55 | writer.write("import org.simple.injector.adapter.InjectAdapter ;");
56 | writer.write("\n");
57 | writer.write("import org.simple.injector.util.ViewFinder;");
58 |
59 | writer.write("\n\n\n");
60 | writer.write("/* This class is generated by Simple ViewInjector, please don't modify! */ ");
61 | writer.write("\n");
62 | writer.write("public class " + info.newClassName
63 | + " implements InjectAdapter<" + info.classlName + "> { ");
64 | writer.write("\n");
65 | writer.write("\n");
66 | // 查找方法
67 | writer.write(" public void injects(" + info.classlName
68 | + " target) { ");
69 | writer.write("\n");
70 | }
71 |
72 | /*
73 | * (non-Javadoc)
74 | * @see org.simple.injector.apt.writer.AbsWriter#writeEnd(java.io.Writer)
75 | */
76 | @Override
77 | protected void writeEnd(Writer writer) throws IOException {
78 | writer.write(" }");
79 | writer.write("\n\n");
80 | writer.write(" } ");
81 | }
82 |
83 | /*
84 | * (non-Javadoc)
85 | * @see org.simple.injector.apt.writer.AbsWriter#writeField(java.io.Writer,
86 | * javax.lang.model.element.VariableElement,
87 | * org.simple.injector.apt.ViewInjectorProcessor.InjectorInfo)
88 | */
89 | @Override
90 | protected void writeField(Writer writer, VariableElement element, InjectorInfo info)
91 | throws IOException {
92 | ViewInjector injector = element.getAnnotation(ViewInjector.class);
93 | String fieldName = element.getSimpleName().toString();
94 | writer.write(" target." + fieldName + " = ViewFinder.findViewById(target, "
95 | + injector.value() + " ) ; ");
96 | writer.write("\n");
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/src/org/simple/injector/apt/ViewInjectorProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | * The MIT License (MIT)
4 | *
5 | * Copyright (c) 2014-2015 Umeng, Inc
6 | *
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
8 | * of this software and associated documentation files (the "Software"), to deal
9 | * in the Software without restriction, including without limitation the rights
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | * copies of the Software, and to permit persons to whom the Software is
12 | * furnished to do so, subject to the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be included in
15 | * all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 | * THE SOFTWARE.
24 | */
25 |
26 | package org.simple.injector.apt;
27 |
28 | import static javax.tools.Diagnostic.Kind.ERROR;
29 |
30 | import org.simple.injector.anno.handler.AnnotationHandler;
31 | import org.simple.injector.anno.handler.ViewInjectHandler;
32 | import org.simple.injector.apt.writer.AdapterWriter;
33 | import org.simple.injector.apt.writer.DefaultJavaFileWriter;
34 |
35 | import java.util.HashMap;
36 | import java.util.LinkedList;
37 | import java.util.List;
38 | import java.util.Map;
39 | import java.util.Set;
40 |
41 | import javax.annotation.processing.AbstractProcessor;
42 | import javax.annotation.processing.ProcessingEnvironment;
43 | import javax.annotation.processing.RoundEnvironment;
44 | import javax.annotation.processing.SupportedAnnotationTypes;
45 | import javax.annotation.processing.SupportedSourceVersion;
46 | import javax.lang.model.SourceVersion;
47 | import javax.lang.model.element.Element;
48 | import javax.lang.model.element.TypeElement;
49 | import javax.lang.model.element.VariableElement;
50 |
51 | /**
52 | * @author mrsimple
53 | */
54 | @SupportedAnnotationTypes("org.simple.injector.anno.*")
55 | @SupportedSourceVersion(SourceVersion.RELEASE_6)
56 | public class ViewInjectorProcessor extends AbstractProcessor {
57 |
58 | /**
59 | * 所有注解处理器的列表
60 | */
61 | List mHandlers = new LinkedList();
62 | /**
63 | * 类型与字段的关联表,用于在写入Java文件时按类型来写不同的文件和字段
64 | */
65 | final Map> map = new HashMap>();
66 | /**
67 | *
68 | */
69 | AdapterWriter mWriter;
70 |
71 | @Override
72 | public synchronized void init(ProcessingEnvironment processingEnv) {
73 | super.init(processingEnv);
74 | registerHandlers();
75 | mWriter = new DefaultJavaFileWriter(processingEnv);
76 | }
77 |
78 | /**
79 | *
80 | */
81 | private void registerHandlers() {
82 | mHandlers.add(new ViewInjectHandler());
83 | }
84 |
85 | @Override
86 | public boolean process(Set extends TypeElement> annotations, RoundEnvironment roundEnv) {
87 | for (AnnotationHandler handler : mHandlers) {
88 | // 关联ProcessingEnvironment
89 | handler.attachProcessingEnv(processingEnv);
90 | // 解析注解相关的信息
91 | map.putAll(handler.handleAnnotation(roundEnv));
92 | }
93 | // 将解析到的数据写入到具体的类型中
94 | mWriter.generate(map);
95 | return true;
96 | }
97 |
98 | /**
99 | * @param element
100 | * @param message
101 | * @param args
102 | */
103 | protected void error(Element element, String message, Object... args) {
104 | if (args.length > 0) {
105 | message = String.format(message, args);
106 | }
107 | processingEnv.getMessager().printMessage(ERROR, message, element);
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/src/org/simple/injector/SimpleDagger.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.injector;
26 |
27 | import android.app.Activity;
28 | import android.support.v4.app.Fragment;
29 | import android.util.Log;
30 | import android.view.View;
31 |
32 | import org.simple.injector.adapter.NullAdapter;
33 | import org.simple.injector.adapter.InjectAdapter;
34 |
35 | import java.lang.reflect.Field;
36 | import java.util.HashMap;
37 | import java.util.Map;
38 |
39 | /**
40 | * @author mrsimple
41 | */
42 | public final class SimpleDagger {
43 | /**
44 | *
45 | */
46 | public static final String SUFFIX = "$InjectAdapter";
47 | /**
48 | *
49 | */
50 | static Map, InjectAdapter>> sInjectCache = new HashMap, InjectAdapter>>();
51 |
52 | /**
53 | * @param activity
54 | */
55 | public static void inject(Activity activity) {
56 | InjectAdapter adapter = getViewAdapter(activity.getClass());
57 | adapter.injects(activity);
58 | }
59 |
60 | /**
61 | * @param fragment
62 | */
63 | public static void inject(Fragment fragment, View rootView) {
64 | InjectAdapter adapter = getViewAdapter(fragment.getClass());
65 | if (fuckTheFragment(fragment, rootView)) {
66 | adapter.injects(fragment);
67 | }
68 | }
69 |
70 | /**
71 | * 到Fragment 中找到mView字段,然后设置rootView给mView
72 | *
73 | * @param fragment
74 | * @param rootView
75 | * @return
76 | */
77 | private static boolean fuckTheFragment(Fragment fragment, View rootView) {
78 | try {
79 | Class> v4fragmentClass = fragment.getClass();
80 | while (v4fragmentClass != Object.class
81 | && !v4fragmentClass.equals(Fragment.class)) {
82 | v4fragmentClass = v4fragmentClass.getSuperclass();
83 | }
84 | Field rootViewField = v4fragmentClass.getDeclaredField("mView");
85 | rootViewField.setAccessible(true);
86 | rootViewField.set(fragment, rootView);
87 | Log.e("", "### getView " + fragment.getView()) ;
88 | return true;
89 | } catch (SecurityException e) {
90 | e.printStackTrace();
91 | } catch (NoSuchFieldException e) {
92 | e.printStackTrace();
93 | } catch (IllegalArgumentException e) {
94 | e.printStackTrace();
95 | } catch (IllegalAccessException e) {
96 | e.printStackTrace();
97 | }
98 |
99 | return false;
100 | }
101 |
102 | /**
103 | * @param view
104 | */
105 | public static void inject(View view) {
106 |
107 | }
108 |
109 | @SuppressWarnings({
110 | "unchecked", "rawtypes"
111 | })
112 | private static InjectAdapter getViewAdapter(Class> clazz) {
113 | InjectAdapter> adapter = sInjectCache.get(clazz);
114 | if (adapter == null) {
115 | String adapterClassName = clazz.getName() + SUFFIX;
116 | try {
117 | Class> adapterClass = Class.forName(adapterClassName);
118 | adapter = (InjectAdapter>) adapterClass.newInstance();
119 | sInjectCache.put(adapterClass, adapter);
120 | } catch (ClassNotFoundException e) {
121 | e.printStackTrace();
122 | } catch (InstantiationException e) {
123 | e.printStackTrace();
124 | } catch (IllegalAccessException e) {
125 | e.printStackTrace();
126 | }
127 | }
128 |
129 | Log.e("", "### find adapter : " + adapter);
130 |
131 | return adapter == null ? new NullAdapter() : adapter;
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/src/org/simple/injector/apt/writer/AbsWriter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2014-2015 Umeng, Inc
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package org.simple.injector.apt.writer;
26 |
27 | import org.simple.injector.SimpleDagger;
28 | import org.simple.injector.util.AnnotationUtil;
29 | import org.simple.injector.util.IOUtil;
30 |
31 | import java.io.File;
32 | import java.io.IOException;
33 | import java.io.Writer;
34 | import java.util.Iterator;
35 | import java.util.List;
36 | import java.util.Map;
37 | import java.util.Map.Entry;
38 |
39 | import javax.annotation.processing.Filer;
40 | import javax.annotation.processing.ProcessingEnvironment;
41 | import javax.lang.model.element.TypeElement;
42 | import javax.lang.model.element.VariableElement;
43 | import javax.tools.JavaFileObject;
44 |
45 | /**
46 | * @author mrsimple
47 | */
48 | public abstract class AbsWriter implements AdapterWriter {
49 |
50 | ProcessingEnvironment mProcessingEnv;
51 | Filer mFiler;
52 |
53 | public AbsWriter(ProcessingEnvironment processingEnv) {
54 | mProcessingEnv = processingEnv;
55 | mFiler = processingEnv.getFiler();
56 | }
57 |
58 | @Override
59 | public void generate(Map> typeMap) {
60 | Iterator>> iterator = typeMap.entrySet().iterator();
61 | while (iterator.hasNext()) {
62 | Entry> entry = iterator.next();
63 | List cacheElements = entry.getValue();
64 | if (cacheElements == null || cacheElements.size() == 0) {
65 | continue;
66 | }
67 |
68 | // 取第一个元素来构造注入信息
69 | InjectorInfo info = createInjectorInfo(cacheElements.get(0));
70 | Writer writer = null;
71 | JavaFileObject javaFileObject;
72 | try {
73 | javaFileObject = mFiler.createSourceFile(info.getClassFullPath());
74 | writer = javaFileObject.openWriter();
75 | // 写入package, import, class以及findViews函数等代码段
76 | generateImport(writer, info);
77 | // 写入该类中的所有字段到findViews方法中
78 | for (VariableElement variableElement : entry.getValue()) {
79 | writeField(writer, variableElement, info);
80 | }
81 | // 写入findViews函数的大括号以及类的大括号
82 | writeEnd(writer);
83 | } catch (IOException e) {
84 | e.printStackTrace();
85 | } finally {
86 | IOUtil.closeQuitly(writer);
87 | }
88 |
89 | }
90 | }
91 |
92 | /**
93 | * @param element
94 | * @return
95 | */
96 | protected InjectorInfo createInjectorInfo(VariableElement element) {
97 | TypeElement typeElement = (TypeElement) element.getEnclosingElement();
98 | String packageName = AnnotationUtil.getPackageName(mProcessingEnv, typeElement);
99 | String className = typeElement.getSimpleName().toString();
100 | return new InjectorInfo(packageName, className);
101 | }
102 |
103 | /**
104 | * @param writer
105 | * @param info
106 | * @throws IOException
107 | */
108 | protected abstract void generateImport(Writer writer, InjectorInfo info)
109 | throws IOException;
110 |
111 | /**
112 | * @param writer
113 | * @param element
114 | * @param info
115 | * @throws IOException
116 | */
117 | protected abstract void writeField(Writer writer, VariableElement element, InjectorInfo info)
118 | throws IOException;
119 |
120 | /**
121 | * @param writer
122 | * @throws IOException
123 | */
124 | protected abstract void writeEnd(Writer writer) throws IOException;
125 |
126 | /**
127 | * 注解相关的信息实体类
128 | *
129 | * @author mrsimple
130 | */
131 | public static class InjectorInfo {
132 | /**
133 | * 被注解的类的包名
134 | */
135 | public String packageName;
136 | /**
137 | * 被注解的类的类名
138 | */
139 | public String classlName;
140 | /**
141 | * 要创建的InjectAdapter类的完整路径,新类的名字为被注解的类名 + "$InjectAdapter", 与被注解的类在同一个包下
142 | */
143 | public String newClassName;
144 |
145 | public InjectorInfo(String packageName, String classlName) {
146 | this.packageName = packageName;
147 | newClassName = classlName + SimpleDagger.SUFFIX;
148 | this.classlName = classlName;
149 | }
150 |
151 | public String getClassFullPath() {
152 | return packageName + File.separator + newClassName;
153 | }
154 | }
155 |
156 | }
157 |
--------------------------------------------------------------------------------