Skip to content

Commit c654f36

Browse files
committed
add throw function to data delivering;
== when one field except primitive type is notated with Autowired and announced to be required, DDComponentLib will output log for the error. if both announced to be required and throwOnNull: NullPointerException will be threw when missing required params! **Caution to this feature!**
1 parent f01b2eb commit c654f36

File tree

7 files changed

+51
-5
lines changed

7 files changed

+51
-5
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ android {
2929
}
3030

3131
debug {
32+
signingConfig signingConfigs.debug
3233
minifyEnabled false
3334
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
3435
}

componentlib/src/main/java/com/luojilab/component/componentlib/service/AutowiredService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
*/
1010

1111
public interface AutowiredService {
12+
boolean THROW_CONFIG = true;
1213

1314
/**
1415
* Autowired core.
16+
*
1517
* @param instance the instance who need autowired.
1618
*/
1719
void autowire(Object instance);

componentlib/src/main/java/com/luojilab/component/componentlib/service/AutowiredServiceImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.luojilab.component.componentlib.service;
22

3+
import android.util.Log;
34
import android.util.LruCache;
45

56
import com.luojilab.component.componentlib.router.ISyringe;
@@ -37,8 +38,15 @@ public void autowire(Object instance) {
3738
}
3839
autowiredHelper.inject(instance);
3940
classCache.put(className, autowiredHelper);
41+
} else {
42+
// TODO: 2017/12/21 change to specific log system
43+
Log.d("[DDComponent]", "[autowire] " + className + "is in blacklist, ignore data inject");
4044
}
4145
} catch (Exception ex) {
46+
if (ex instanceof NullPointerException) { // may define custom exception better
47+
throw new NullPointerException(ex.getMessage());
48+
}
49+
ex.printStackTrace();
4250
blackList.add(className); // This instance need not autowired.
4351
}
4452
}

readercomponent/src/main/java/com/luojilab/reader/ReaderFragment.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,17 @@ private void goToShareActivityWithUri() {
7272
Author author = new Author();
7373
author.setName("Barack Obama");
7474
author.setCounty("New York");
75+
76+
//参数合法的uri字符串
77+
final String URI_LEGAL = "DDComp://share/shareMagazine?bookName=NYTIME&author=";
78+
79+
//参数非法的uri字符串 bookName是必须参数,并在注解中配置了错误外显策略。
80+
final String URI_ILLEGAL = "DDComp://share/shareMagazine?bookNameeee=NYTIME&author=";
81+
82+
/* TODO: 2017/12/21 change the secondary param to see difference between
83+
legal and illegal data delivering*/
7584
UIRouter.getInstance().openUri(getActivity(),
76-
"DDComp://share/shareMagazine?bookName=NYTIME&author="
85+
URI_LEGAL
7786
+ JsonService.Factory.getInstance().create().toJsonString(author), null);
7887
}
7988

router-anno-compiler/src/main/java/com/luojilab/router/compiler/processor/AutowiredProcessor.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public class AutowiredProcessor extends AbstractProcessor {
7373

7474
private static final ClassName AndroidLog = ClassName.get("android.util", "Log");
7575

76+
private static final ClassName NullPointerException = ClassName.get("java.lang", "NullPointerException");
77+
7678
private static final String SUFFIX_AUTOWIRED = "$$Router$$Autowired";
7779

7880
@Override
@@ -194,7 +196,15 @@ private void generateHelper() throws IOException, IllegalAccessException {
194196
if (fieldConfig.required() && !element.asType().getKind().isPrimitive()) { // Primitive wont be check.
195197
injectMethodBuilder.beginControlFlow("if (null == substitute." + fieldName + ")");
196198
injectMethodBuilder.addStatement(
197-
"$T.e(\"" + TAG + "\", \"The field '" + fieldName + "' is null, in class '\" + $T.class.getName() + \"!\")", AndroidLog, ClassName.get(parent));
199+
"$T.e(\"" + TAG + "\", \"The field '" + fieldName + "' is null," + "field description is:" + fieldConfig.desc() +
200+
",in class '\" + $T.class.getName() + \"!\")", AndroidLog, ClassName.get(parent));
201+
202+
if (fieldConfig.throwOnNull()) {
203+
injectMethodBuilder.addStatement("throw new $T(" +
204+
"\"The field '" + fieldName + "' is null," + "field description is:" + fieldConfig.desc() +
205+
",in class '\" + $T.class.getName() + \"!\")", NullPointerException, ClassName.get(parent));
206+
}
207+
198208
injectMethodBuilder.endControlFlow();
199209
}
200210
}

router-annotation/src/main/java/com/luojilab/router/facade/annotation/Autowired.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
@Target({ElementType.FIELD})
1616
@Retention(RetentionPolicy.CLASS)
1717
public @interface Autowired {
18+
1819
/**
1920
* @return param's name or service name.
2021
*/
@@ -23,14 +24,28 @@
2324
/**
2425
* <em>primitive java type check will be ignore</em>
2526
* check the result of DI, if inject failed, the value of
26-
* the field will be null, if required, burst crash
27+
* the field will be null, if required, output log
2728
*
2829
* @return true for required,false otherwise
2930
*/
3031
boolean required() default false;
3132

3233
/**
33-
* @return feild description
34+
* throw exception when the required field is null after inject.
35+
* <p>
36+
* It can help developer find most data delivering bugs when developing.
37+
* but not suggest to open this function after release.
38+
* <p>
39+
* I suggest to define a Constant maintained manually
40+
* <p>
41+
* only activated when required = true and throwOnNull = true.
42+
*
43+
* @return true if throwing exception when null is required, false otherwise
44+
*/
45+
boolean throwOnNull() default false;
46+
47+
/**
48+
* @return field description
3449
*/
3550
String desc() default "none desc.";
3651
}

sharecomponent/src/main/java/com/luojilab/share/Share2Activity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
*/
1717
@RouteNode(path = "/shareMagazine", desc = "分享杂志页面")
1818
public class Share2Activity extends AppCompatActivity {
19+
static final boolean enablethrow = true;
1920

20-
@Autowired(name = "bookName")
21+
@Autowired(name = "bookName", required = true, throwOnNull = AutowiredService.THROW_CONFIG)
2122
String magazineName;
2223

2324
@Autowired

0 commit comments

Comments
 (0)