Skip to content

Commit

Permalink
Support inject params in kotlin activity.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhi1ong committed Oct 20, 2020
1 parent f21a53d commit 4d80a3a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
import android.widget.TextView;

import com.alibaba.android.arouter.demo.testinject.TestObj;
import com.alibaba.android.arouter.demo.testinject.TestParcelable;
import com.alibaba.android.arouter.demo.testinject.TestSerializable;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;

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

/**
* A simple {@link Fragment} subclass.
*/
Expand All @@ -24,11 +29,40 @@ public class BlankFragment extends Fragment {
@Autowired(required = true)
TestObj obj;

@Autowired
int age = 10;

@Autowired
int height = 175;

@Autowired(name = "boy", required = true)
boolean girl;

@Autowired
char ch = 'A';

@Autowired
float fl = 12.00f;

@Autowired
double dou = 12.01d;

@Autowired
TestSerializable ser;

@Autowired
TestParcelable pac;

@Autowired
List<TestObj> objList;

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

public BlankFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public static Activity getThis() {
*/
@Override
public void onClick(View v) {
// Build test data.
TestSerializable testSerializable = new TestSerializable("Titanic", 555);
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);

switch (v.getId()) {
case R.id.openLog:
ARouter.openLog();
Expand Down Expand Up @@ -130,15 +139,6 @@ public void onInterrupt(Postcard postcard) {
.navigation();
break;
case R.id.autoInject:
TestSerializable testSerializable = new TestSerializable("Titanic", 555);
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", "老王")
.withInt("age", 18)
Expand Down Expand Up @@ -206,7 +206,17 @@ public void onInterrupt(Postcard postcard) {
.navigation(this, 666);
break;
case R.id.getFragment:
Fragment fragment = (Fragment) ARouter.getInstance().build("/test/fragment").navigation();
Fragment fragment = (Fragment) ARouter.getInstance().build("/test/fragment")
.withString("name", "老王")
.withInt("age", 18)
.withBoolean("boy", true)
.withLong("high", 180)
.withString("url", "https://a.b.c")
.withSerializable("ser", testSerializable)
.withParcelable("pac", testParcelable)
.withObject("obj", testObj)
.withObject("objList", objList)
.withObject("map", map).navigation();
Toast.makeText(this, "找到Fragment:" + fragment.toString(), Toast.LENGTH_SHORT).show();
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.annotation.processing.SupportedOptions;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -159,7 +160,7 @@ private void generateHelper() throws IOException, IllegalAccessException {
throw new IllegalAccessException("The field [" + fieldName + "] need autowired from intent, its parent must be activity or fragment!");
}

statement = buildStatement(originalValue, statement, typeUtils.typeExchange(element), isActivity);
statement = buildStatement(originalValue, statement, typeUtils.typeExchange(element), isActivity, isKtClass(parent));
if (statement.startsWith("serializationService.")) { // Not mortals
injectMethodBuilder.beginControlFlow("if (null != serializationService)");
injectMethodBuilder.addStatement(
Expand Down Expand Up @@ -197,41 +198,54 @@ private void generateHelper() throws IOException, IllegalAccessException {
}
}

private boolean isKtClass(Element element) {
for (AnnotationMirror annotationMirror : elementUtils.getAllAnnotationMirrors(element)) {
if (annotationMirror.getAnnotationType().toString().contains("kotlin")) {
return true;
}
}

return false;
}

private String buildCastCode(Element element) {
if (typeUtils.typeExchange(element) == TypeKind.SERIALIZABLE.ordinal()) {
return CodeBlock.builder().add("($T) ", ClassName.get(element.asType())).build().toString();
}
return "";
}

private String buildStatement(String originalValue, String statement, int type, boolean isActivity) {
/**
* Build param inject statement
*/
private String buildStatement(String originalValue, String statement, int type, boolean isActivity, boolean isKt) {
switch (TypeKind.values()[type]) {
case BOOLEAN:
statement += (isActivity ? ("getBooleanExtra($S, " + originalValue + ")") : ("getBoolean($S)"));
statement += "getBoolean" + (isActivity ? "Extra" : "") + "($S, " + originalValue + ")";
break;
case BYTE:
statement += (isActivity ? ("getByteExtra($S, " + originalValue + ")") : ("getByte($S)"));
statement += "getByte" + (isActivity ? "Extra" : "") + "($S, " + originalValue + ")";
break;
case SHORT:
statement += (isActivity ? ("getShortExtra($S, " + originalValue + ")") : ("getShort($S)"));
statement += "getShort" + (isActivity ? "Extra" : "") + "($S, " + originalValue + ")";
break;
case INT:
statement += (isActivity ? ("getIntExtra($S, " + originalValue + ")") : ("getInt($S)"));
statement += "getInt" + (isActivity ? "Extra" : "") + "($S, " + originalValue + ")";
break;
case LONG:
statement += (isActivity ? ("getLongExtra($S, " + originalValue + ")") : ("getLong($S)"));
statement += "getLong" + (isActivity ? "Extra" : "") + "($S, " + originalValue + ")";
break;
case CHAR:
statement += (isActivity ? ("getCharExtra($S, " + originalValue + ")") : ("getChar($S)"));
statement += "getChar" + (isActivity ? "Extra" : "") + "($S, " + originalValue + ")";
break;
case FLOAT:
statement += (isActivity ? ("getFloatExtra($S, " + originalValue + ")") : ("getFloat($S)"));
statement += "getFloat" + (isActivity ? "Extra" : "") + "($S, " + originalValue + ")";
break;
case DOUBLE:
statement += (isActivity ? ("getDoubleExtra($S, " + originalValue + ")") : ("getDouble($S)"));
statement += "getDouble" + (isActivity ? "Extra" : "") + "($S, " + originalValue + ")";
break;
case STRING:
statement += (isActivity ? ("getExtras() == null ? " + originalValue + " : substitute.getIntent().getExtras().getString($S, " + originalValue + ")") : ("getString($S)"));
statement += (isActivity ? ("getExtras() == null ? " + originalValue + " : substitute.getIntent().getExtras().getString($S") : ("getString($S")) + ", " + originalValue + ")";
break;
case SERIALIZABLE:
statement += (isActivity ? ("getSerializableExtra($S)") : ("getSerializable($S)"));
Expand Down

0 comments on commit 4d80a3a

Please sign in to comment.