Skip to content

Commit 941c44d

Browse files
committed
[anno] [optimize code in AutowiredProcessor]
1 parent be25e04 commit 941c44d

File tree

4 files changed

+67
-44
lines changed

4 files changed

+67
-44
lines changed

app/src/main/java/com/mrzhang/component/MainActivity.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
import android.support.v4.app.Fragment;
55
import android.support.v4.app.FragmentTransaction;
66
import android.support.v7.app.AppCompatActivity;
7-
import android.util.Log;
87
import android.view.View;
98
import android.widget.Button;
109

1110
import com.mrzhang.component.componentlib.router.Router;
12-
import com.mrzhang.componentservice.json.JsonService;
1311
import com.mrzhang.componentservice.readerbook.ReadBookService;
1412

1513
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
2525
if (rootView == null) {
2626
rootView = inflater.inflate(R.layout.readerbook_fragment_reader, container,
2727
false);
28+
29+
30+
2831
rootView.findViewById(R.id.tv_content).setOnClickListener(new View.OnClickListener() {
2932

3033
@Override
3134
public void onClick(View v) {
3235
Bundle bundle = new Bundle();
36+
final String _dto = "{\"s\":\"sss\"}";
3337
bundle.putString("bookName","testBookName");
38+
bundle.putString("testDto",_dto);
3439
UIRouter.getInstance().openUri(getActivity(), "componentdemo://share", bundle);
3540
}
3641
});

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

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ private void generateHelper() throws IOException, IllegalAccessException {
119119

120120
TypeElement type_JsonService = elements.getTypeElement(Constants.JSON_SERVICE);
121121

122-
// TypeMirror iProvider = elements.getTypeElement(Constants.IPROVIDER).asType();
123-
124122
TypeMirror activityTm = elements.getTypeElement(Constants.ACTIVITY).asType();
125123
TypeMirror fragmentTm = elements.getTypeElement(Constants.FRAGMENT).asType();
126124
TypeMirror fragmentTmV4 = elements.getTypeElement(Constants.FRAGMENT_V4).asType();
@@ -152,52 +150,53 @@ private void generateHelper() throws IOException, IllegalAccessException {
152150
.addModifiers(PUBLIC);
153151

154152
FieldSpec jsonServiceField = FieldSpec.builder(TypeName.get(type_JsonService.asType()),
155-
"JsonService", Modifier.PRIVATE).build();
153+
"jsonService", Modifier.PRIVATE).build();
156154
helper.addField(jsonServiceField);
157155

158-
// JsonService service = (JsonService) router.getService(JsonService.class.getSimpleName());
159-
//
160-
//
156+
/*
157+
* Router router = Router.getInstance();
158+
if (router.getService(JsonService.class.getSimpleName()) != null) {
159+
JsonService jsonService = (JsonService) router.getService(JsonService.class.getSimpleName());
160+
} else {
161+
throw new IllegalStateException("JsonService not found in Router");
162+
}
163+
* */
164+
165+
// String _statement =
166+
// " $T router = $T.getInstance();\n" +
167+
// " if (router.getService($T.class.getSimpleName()) != null) {\n" +
168+
// " jsonService = ($T) router.getService($T.class.getSimpleName());\n" +
169+
// " }";
170+
161171
logger.info("======== inject jsonservice");
162-
injectMethodBuilder.addStatement("JsonService = ($T) $T.getInstance().getService($T.class.getSimpleName())",
163-
ClassName.get(type_JsonService),
172+
173+
injectMethodBuilder.addStatement("$T router = $T.getInstance()",
164174
RouterClass,
175+
RouterClass);
176+
177+
injectMethodBuilder.beginControlFlow("if (router.getService($T.class.getSimpleName()) != null)",
178+
ClassName.get(type_JsonService));
179+
180+
injectMethodBuilder.addStatement("jsonService = ($T) router.getService($T.class.getSimpleName())",
181+
ClassName.get(type_JsonService),
165182
ClassName.get(type_JsonService));
166183

184+
185+
injectMethodBuilder.endControlFlow();
186+
187+
// injectMethodBuilder.addStatement(_statement,
188+
// RouterClass,
189+
// RouterClass,
190+
// ClassName.get(type_JsonService),
191+
// ClassName.get(type_JsonService),
192+
// ClassName.get(type_JsonService));
193+
167194
injectMethodBuilder.addStatement("$T substitute = ($T)target", ClassName.get(parent), ClassName.get(parent));
168195

169196
// Generate method body, start inject.
170197
for (Element element : childs) {
171198
Autowired fieldConfig = element.getAnnotation(Autowired.class);
172199
String fieldName = element.getSimpleName().toString();
173-
// if (types.isSubtype(element.asType(), iProvider)) { // It's provider
174-
// if ("".equals(fieldConfig.name())) { // User has not set service path, then use byType.
175-
//
176-
// // Getter
177-
// injectMethodBuilder.addStatement(
178-
// "substitute." + fieldName + " = $T.getInstance().navigation($T.class)",
179-
// ARouterClass,
180-
// ClassName.get(element.asType())
181-
// );
182-
// } else { // use byName
183-
// // Getter
184-
// injectMethodBuilder.addStatement(
185-
// "substitute." + fieldName + " = ($T)$T.getInstance().build($S).navigation();",
186-
// ClassName.get(element.asType()),
187-
// ARouterClass,
188-
// fieldConfig.name()
189-
// );
190-
// }
191-
//
192-
// // Validater
193-
// if (fieldConfig.required()) {
194-
// injectMethodBuilder.beginControlFlow("if (substitute." + fieldName + " == null)");
195-
// injectMethodBuilder.addStatement(
196-
// "throw new RuntimeException(\"The field '" + fieldName + "' is null, in class '\" + $T.class.getName() + \"!\")", ClassName.get(parent));
197-
// injectMethodBuilder.endControlFlow();
198-
// }
199-
// }
200-
// else { // It's normal intent value
201200
String originalValue = "substitute." + fieldName;
202201
String statment = "substitute." + fieldName + " = substitute.";
203202
boolean isActivity = false;
@@ -213,8 +212,8 @@ private void generateHelper() throws IOException, IllegalAccessException {
213212
}
214213

215214
statment = buildStatement(originalValue, statment, typeUtils.typeExchange(element), isActivity);
216-
if (statment.startsWith("serializationService.")) { // Not mortals
217-
injectMethodBuilder.beginControlFlow("if (null != serializationService)");
215+
if (statment.startsWith("jsonService.")) { // Not mortals
216+
injectMethodBuilder.beginControlFlow("if (null != jsonService)");
218217
injectMethodBuilder.addStatement(
219218
"substitute." + fieldName + " = " + statment,
220219
(StringUtils.isEmpty(fieldConfig.name()) ? fieldName : fieldConfig.name()),
@@ -224,8 +223,7 @@ private void generateHelper() throws IOException, IllegalAccessException {
224223
injectMethodBuilder.addStatement(
225224
"$T.e(\"" + TAG + "\", \"You want automatic inject the field '"
226225
+ fieldName + "' in class '$T' ," +
227-
" then you should implement 'SerializationService' to support " +
228-
"object auto inject!\")", AndroidLog, ClassName.get(parent));
226+
" but JsonService not found in Router\")", AndroidLog, ClassName.get(parent));
229227

230228
injectMethodBuilder.endControlFlow();
231229
} else {
@@ -240,7 +238,6 @@ private void generateHelper() throws IOException, IllegalAccessException {
240238
injectMethodBuilder.endControlFlow();
241239
}
242240
}
243-
// }
244241

245242
helper.addMethod(injectMethodBuilder.build());
246243

@@ -286,7 +283,7 @@ private String buildStatement(String originalValue, String statement, int type,
286283
} else if (type == Type.PARCELABLE.ordinal()) {
287284
statement += (isActivity ? ("getParcelableExtra($S)") : ("getParcelable($S)"));
288285
} else if (type == Type.OBJECT.ordinal()) {
289-
statement = "serializationService.json2Object(substitute." +
286+
statement = "jsonService.parseObject(substitute." +
290287
(isActivity ? "getIntent()." : "getArguments().") +
291288
(isActivity ? "getStringExtra($S)" : "getString($S)") + ", $T.class)";
292289
}

sharecomponent/src/main/java/com/mrzhang/share/ShareActivity.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,40 @@
33
import android.os.Bundle;
44
import android.support.annotation.Nullable;
55
import android.support.v7.app.AppCompatActivity;
6+
import android.util.Log;
67
import android.widget.TextView;
78

89
import com.ljsw.router.facade.annotation.Autowired;
910
import com.mrzhang.component.componentlib.router.Router;
1011
import com.mrzhang.componentservice.di.AutowiredService;
12+
import com.mrzhang.componentservice.json.JsonService;
1113

1214
/**
1315
* Created by mrzhang on 2017/6/20.
1416
*/
1517

1618
public class ShareActivity extends AppCompatActivity {
1719

20+
private static final String TAG = ShareActivity.class.getSimpleName();
21+
22+
static class TestDto {
23+
String s;
24+
25+
public String getS() {
26+
return s;
27+
}
28+
29+
public void setS(String s) {
30+
this.s = s;
31+
}
32+
}
33+
1834
@Autowired
1935
String bookName;
2036

37+
@Autowired
38+
TestDto testDto;
39+
2140
@Override
2241
protected void onCreate(@Nullable Bundle savedInstanceState) {
2342
super.onCreate(savedInstanceState);
@@ -34,6 +53,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3453
TextView textView = (TextView) findViewById(R.id.share_tv_tag);
3554
textView.setText("share: " + bookName);
3655

56+
Log.d(TAG,"dto:\r"+((JsonService)Router.getInstance()
57+
.getService(JsonService.class.getSimpleName()))
58+
.toJsonString(testDto));
59+
3760
}
3861

3962
}

0 commit comments

Comments
 (0)