Skip to content

Commit

Permalink
Support nested object type.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhi1ong committed Oct 26, 2017
1 parent 1e5a821 commit 03c5a0b
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static Activity activity;
Expand Down Expand Up @@ -126,6 +131,11 @@ public void onInterrupt(Postcard postcard) {
case R.id.autoInject:
TestParcelable testParcelable = new TestParcelable("jack", 666);
TestObj testObj = new TestObj("Rose", 777);
List<TestObj> objList = new ArrayList<>();
objList.add(testObj);

Map<String, List<TestObj>> map = new HashMap<>();
map.put("testMap", objList);

ARouter.getInstance().build("/test/activity1")
.withString("name", "老王")
Expand All @@ -135,6 +145,8 @@ public void onInterrupt(Postcard postcard) {
.withString("url", "https://a.b.c")
.withParcelable("pac", testParcelable)
.withObject("obj", testObj)
.withObject("objList", objList)
.withObject("map", map)
.navigation();
break;
case R.id.navByName:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import java.util.List;
import java.util.Map;

/**
* https://m.aliyun.com/test/activity1?name=老王&age=23&boy=true&high=180
*/
Expand Down Expand Up @@ -45,6 +48,12 @@ public class Test1Activity extends AppCompatActivity {
@Autowired
TestObj obj;

@Autowired
List<TestObj> objList;

@Autowired
Map<String, List<TestObj>> map;

private long high;

@Autowired
Expand All @@ -68,7 +77,7 @@ protected void onCreate(Bundle savedInstanceState) {
// url = getIntent().getStringExtra("url");

String params = String.format(
"name=%s,\n age=%s, \n height=%s,\n girl=%s,\n high=%s,\n url=%s,\n pac=%s,\n obj=%s \n ch=%s \n fl = %s, \n dou = %s",
"name=%s,\n age=%s, \n height=%s,\n girl=%s,\n high=%s,\n url=%s,\n pac=%s,\n obj=%s \n ch=%s \n fl = %s, \n dou = %s, \n objList=%s, \n map=%s",
name,
age,
height,
Expand All @@ -79,7 +88,9 @@ protected void onCreate(Bundle savedInstanceState) {
obj,
ch,
fl,
dou
dou,
objList,
map
);
helloService.sayHello("Hello moto.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.alibaba.android.arouter.facade.service.SerializationService;
import com.alibaba.fastjson.JSON;

import java.lang.reflect.Type;

/**
* Used for json converter
*
Expand All @@ -29,4 +31,9 @@ public <T> T json2Object(String text, Class<T> clazz) {
public String object2Json(Object instance) {
return JSON.toJSONString(instance);
}

@Override
public <T> T parseObject(String input, Type clazz) {
return JSON.parseObject(input, clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.alibaba.android.arouter.facade.model;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
* Used for get type of target object.
*
* @author Alex <a href="mailto:[email protected]">Contact me.</a>
* @version 1.0
* @since 17/10/26 11:56:22
*/
public class TypeWrapper<T> {
protected final Type type;

protected TypeWrapper() {
Type superClass = getClass().getGenericSuperclass();

type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
}

public Type getType() {
return type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.alibaba.android.arouter.facade.template.IProvider;

import java.lang.reflect.Type;

/**
* Used for parse json string.
*
Expand All @@ -10,20 +12,33 @@
* @since 2017/4/10 下午1:43
*/
public interface SerializationService extends IProvider {

/**
* Parse json object.
* Parse json to object
*
* USE @parseObject PLEASE
*
* @param json json str
* @param input json string
* @param clazz object type
* @param <T> type
* @return instance
* @return instance of object
*/
<T> T json2Object(String json, Class<T> clazz);
@Deprecated
<T> T json2Object(String input, Class<T> clazz);

/**
* Object to json
*
* @param instance obj
* @return json string
*/
String object2Json(Object instance);

/**
* Parse json to object
*
* @param input json string
* @param clazz object type
* @return instance of object
*/
<T> T parseObject(String input, Type clazz);
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,22 @@ private void generateHelper() throws IOException, IllegalAccessException {
}
} else { // It's normal intent value
String originalValue = "substitute." + fieldName;
String statment = "substitute." + fieldName + " = substitute.";
String statement = "substitute." + fieldName + " = substitute.";
boolean isActivity = false;
if (types.isSubtype(parent.asType(), activityTm)) { // Activity, then use getIntent()
isActivity = true;
statment += "getIntent().";
statement += "getIntent().";
} else if (types.isSubtype(parent.asType(), fragmentTm) || types.isSubtype(parent.asType(), fragmentTmV4)) { // Fragment, then use getArguments()
statment += "getArguments().";
statement += "getArguments().";
} else {
throw new IllegalAccessException("The field [" + fieldName + "] need autowired from intent, its parent must be activity or fragment!");
}

statment = buildStatement(originalValue, statment, typeUtils.typeExchange(element), isActivity);
if (statment.startsWith("serializationService.")) { // Not mortals
statement = buildStatement(originalValue, statement, typeUtils.typeExchange(element), isActivity);
if (statement.startsWith("serializationService.")) { // Not mortals
injectMethodBuilder.beginControlFlow("if (null != serializationService)");
injectMethodBuilder.addStatement(
"substitute." + fieldName + " = " + statment,
"substitute." + fieldName + " = " + statement,
(StringUtils.isEmpty(fieldConfig.name()) ? fieldName : fieldConfig.name()),
ClassName.get(element.asType())
);
Expand All @@ -198,7 +198,7 @@ private void generateHelper() throws IOException, IllegalAccessException {
"$T.e(\"" + Consts.TAG + "\", \"You want automatic inject the field '" + fieldName + "' in class '$T' , then you should implement 'SerializationService' to support object auto inject!\")", AndroidLog, ClassName.get(parent));
injectMethodBuilder.endControlFlow();
} else {
injectMethodBuilder.addStatement(statment, StringUtils.isEmpty(fieldConfig.name()) ? fieldName : fieldConfig.name());
injectMethodBuilder.addStatement(statement, StringUtils.isEmpty(fieldConfig.name()) ? fieldName : fieldConfig.name());
}

// Validator
Expand Down Expand Up @@ -245,7 +245,7 @@ private String buildStatement(String originalValue, String statement, int type,
} else if (type == TypeKind.PARCELABLE.ordinal()) {
statement += (isActivity ? ("getParcelableExtra($S)") : ("getParcelable($S)"));
} else if (type == TypeKind.OBJECT.ordinal()) {
statement = "serializationService.json2Object(substitute." + (isActivity ? "getIntent()." : "getArguments().") + (isActivity ? "getStringExtra($S)" : "getString($S)") + ", $T.class)";
statement = "serializationService.parseObject(substitute." + (isActivity ? "getIntent()." : "getArguments().") + (isActivity ? "getStringExtra($S)" : "getString($S)") + ", new com.alibaba.android.arouter.facade.model.TypeWrapper<$T>(){}.getType())";
}

return statement;
Expand All @@ -262,7 +262,7 @@ private void categories(Set<? extends Element> elements) throws IllegalAccessExc
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();

if (element.getModifiers().contains(Modifier.PRIVATE)) {
throw new IllegalAccessException("The autowired fields CAN NOT BE 'private'!!! please check field ["
throw new IllegalAccessException("The inject fields CAN NOT BE 'private'!!! please check field ["
+ element.getSimpleName() + "] in class [" + enclosingElement.getQualifiedName() + "]");
}

Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ SUPPORT_LIB_VERSION=25.2.0
MIN_SDK_VERSION=12
TARGET_SDK_VERSION=25

arouter_compiler_version=1.1.3
arouter_api_version=1.2.2
arouter_annotation_version=1.0.3
arouter_compiler_version=1.1.4
arouter_api_version=1.2.4
arouter_annotation_version=1.0.4

bintrayRepo=maven
publishedGroupId=com.alibaba
Expand Down

0 comments on commit 03c5a0b

Please sign in to comment.