Skip to content

Commit

Permalink
Merge pull request alibaba#174 from crazy1235/master
Browse files Browse the repository at this point in the history
优化自动生成的源码文件 && 自动注入支持优先使用默认值
  • Loading branch information
zhi1ong authored Sep 5, 2017
2 parents 1bd4eb3 + d603227 commit 1e5a821
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,26 @@
public class Test1Activity extends AppCompatActivity {

@Autowired
String name;
String name = "jack";

@Autowired
int age;
int age = 10;

@Autowired
int height = 175;

@Autowired(name = "boy")
boolean girl;

@Autowired
char ch = 'A';

@Autowired
float fl = 12.00f;

@Autowired
double dou = 12.01d;

@Autowired
TestParcelable pac;

Expand Down Expand Up @@ -56,18 +68,22 @@ protected void onCreate(Bundle savedInstanceState) {
// url = getIntent().getStringExtra("url");

String params = String.format(
"name=%s,\n age=%s,\n girl=%s,\n high=%s,\n url=%s,\n pac=%s,\n obj=%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",
name,
age,
height,
girl,
high,
url,
pac,
obj
obj,
ch,
fl,
dou
);
helloService.sayHello("Hello moto.");

((TextView)findViewById(R.id.test)).setText("I am " + Test1Activity.class.getName());
((TextView)findViewById(R.id.test2)).setText(params);
((TextView) findViewById(R.id.test)).setText("I am " + Test1Activity.class.getName());
((TextView) findViewById(R.id.test2)).setText(params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private void generateHelper() throws IOException, IllegalAccessException {
FieldSpec jsonServiceField = FieldSpec.builder(TypeName.get(type_JsonService.asType()), "serializationService", Modifier.PRIVATE).build();
helper.addField(jsonServiceField);

injectMethodBuilder.addStatement("serializationService = $T.getInstance().navigation($T.class);", ARouterClass, ClassName.get(type_JsonService));
injectMethodBuilder.addStatement("serializationService = $T.getInstance().navigation($T.class)", ARouterClass, ClassName.get(type_JsonService));
injectMethodBuilder.addStatement("$T substitute = ($T)target", ClassName.get(parent), ClassName.get(parent));

// Generate method body, start inject.
Expand Down Expand Up @@ -173,6 +173,7 @@ private void generateHelper() throws IOException, IllegalAccessException {
injectMethodBuilder.endControlFlow();
}
} else { // It's normal intent value
String originalValue = "substitute." + fieldName;
String statment = "substitute." + fieldName + " = substitute.";
boolean isActivity = false;
if (types.isSubtype(parent.asType(), activityTm)) { // Activity, then use getIntent()
Expand All @@ -184,7 +185,7 @@ private void generateHelper() throws IOException, IllegalAccessException {
throw new IllegalAccessException("The field [" + fieldName + "] need autowired from intent, its parent must be activity or fragment!");
}

statment = buildStatement(statment, typeUtils.typeExchange(element), isActivity);
statment = buildStatement(originalValue, statment, typeUtils.typeExchange(element), isActivity);
if (statment.startsWith("serializationService.")) { // Not mortals
injectMethodBuilder.beginControlFlow("if (null != serializationService)");
injectMethodBuilder.addStatement(
Expand All @@ -194,7 +195,7 @@ private void generateHelper() throws IOException, IllegalAccessException {
);
injectMethodBuilder.nextControlFlow("else");
injectMethodBuilder.addStatement(
"$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));
"$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());
Expand All @@ -204,7 +205,7 @@ private void generateHelper() throws IOException, IllegalAccessException {
if (fieldConfig.required() && !element.asType().getKind().isPrimitive()) { // Primitive wont be check.
injectMethodBuilder.beginControlFlow("if (null == substitute." + fieldName + ")");
injectMethodBuilder.addStatement(
"$T.e(\"" + Consts.TAG + "\", \"The field '" + fieldName + "' is null, in class '\" + $T.class.getName() + \"!\")", AndroidLog, ClassName.get(parent));
"$T.e(\"" + Consts.TAG + "\", \"The field '" + fieldName + "' is null, in class '\" + $T.class.getName() + \"!\")", AndroidLog, ClassName.get(parent));
injectMethodBuilder.endControlFlow();
}
}
Expand All @@ -222,30 +223,32 @@ private void generateHelper() throws IOException, IllegalAccessException {
}
}

private String buildStatement(String statment, int type, boolean isActivity) {
private String buildStatement(String originalValue, String statement, int type, boolean isActivity) {
if (type == TypeKind.BOOLEAN.ordinal()) {
statment += (isActivity ? ("getBooleanExtra($S, false)") : ("getBoolean($S)"));
statement += (isActivity ? ("getBooleanExtra($S, " + originalValue + ")") : ("getBoolean($S)"));
} else if (type == TypeKind.BYTE.ordinal()) {
statment += (isActivity ? ("getByteExtra($S, (byte) 0)") : ("getByte($S)"));
statement += (isActivity ? ("getByteExtra($S, " + originalValue + "") : ("getByte($S)"));
} else if (type == TypeKind.SHORT.ordinal()) {
statment += (isActivity ? ("getShortExtra($S, (short) 0)") : ("getShort($S)"));
statement += (isActivity ? ("getShortExtra($S, " + originalValue + ")") : ("getShort($S)"));
} else if (type == TypeKind.INT.ordinal()) {
statment += (isActivity ? ("getIntExtra($S, 0)") : ("getInt($S)"));
statement += (isActivity ? ("getIntExtra($S, " + originalValue + ")") : ("getInt($S)"));
} else if (type == TypeKind.LONG.ordinal()) {
statment += (isActivity ? ("getLongExtra($S, 0)") : ("getLong($S)"));
statement += (isActivity ? ("getLongExtra($S, " + originalValue + ")") : ("getLong($S)"));
}else if(type == TypeKind.CHAR.ordinal()){
statement += (isActivity ? ("getCharExtra($S, " + originalValue + ")") : ("getChar($S)"));
} else if (type == TypeKind.FLOAT.ordinal()) {
statment += (isActivity ? ("getFloatExtra($S, 0)") : ("getFloat($S)"));
statement += (isActivity ? ("getFloatExtra($S, " + originalValue + ")") : ("getFloat($S)"));
} else if (type == TypeKind.DOUBLE.ordinal()) {
statment += (isActivity ? ("getDoubleExtra($S, 0)") : ("getDouble($S)"));
statement += (isActivity ? ("getDoubleExtra($S, " + originalValue + ")") : ("getDouble($S)"));
} else if (type == TypeKind.STRING.ordinal()) {
statment += (isActivity ? ("getStringExtra($S)") : ("getString($S)"));
statement += (isActivity ? ("getStringExtra($S)") : ("getString($S)"));
} else if (type == TypeKind.PARCELABLE.ordinal()) {
statment += (isActivity ? ("getParcelableExtra($S)") : ("getParcelable($S)"));
statement += (isActivity ? ("getParcelableExtra($S)") : ("getParcelable($S)"));
} else if (type == TypeKind.OBJECT.ordinal()) {
statment = "serializationService.json2Object(substitute." + (isActivity ? "getIntent()." : "getArguments().") + (isActivity ? "getStringExtra($S)" : "getString($S)") + ", $T.class)";
statement = "serializationService.json2Object(substitute." + (isActivity ? "getIntent()." : "getArguments().") + (isActivity ? "getStringExtra($S)" : "getString($S)") + ", $T.class)";
}

return statment;
return statement;
}

/**
Expand Down

0 comments on commit 1e5a821

Please sign in to comment.