Skip to content

Commit

Permalink
feat: 调整单例生成与路由表数据类型
Browse files Browse the repository at this point in the history
  • Loading branch information
wzbos committed Aug 31, 2020
1 parent 769b48d commit ab420b7
Show file tree
Hide file tree
Showing 19 changed files with 214 additions and 231 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ siteUrl=https://github.com/wzbos/Android-Rudolph-Router
gitUrl=https://github.com/wzbos/Android-Rudolph-Router.git
licenseName=The Apache Software License, Version 2.0
licenseUrl=http://www.apache.org/licenses/LICENSE-2.0.txt
rudolph_version=1.1.0
rudolph_version=1.2.0
developerId=wzbos
developerName=zongbo.wu
developerEmail=[email protected]
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.wzbos.android.rudolph;

import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;

Expand All @@ -10,16 +9,16 @@
*/
public class RouteInfo {
private String path;
private Class<?> target;
private Map<String, Type> params;
private String target;
private Map<String, String> params;
private RouteType routeType;
private String tag;

public String getTag() {
return tag;
}

public Class<?> getTarget() {
public String getTarget() {
return target;
}

Expand All @@ -31,7 +30,7 @@ public String getPath() {
return path;
}

public Map<String, Type> getParams() {
public Map<String, String> getParams() {
return params;
}

Expand All @@ -49,7 +48,7 @@ public RouteType getType() {

private RouteInfo(Builder builder) {
this.path = builder.path;
this.target = builder.destination;
this.target = builder.target;
this.routeType = builder.routeType;
this.params = builder.params;
this.tag = builder.tag;
Expand Down Expand Up @@ -81,8 +80,8 @@ boolean equals(String currPath) {

public static class Builder {
private String path;
private Class<?> destination; // Destination
private Map<String, Type> params;
private String target;
private Map<String, String> params;
private RouteType routeType;
private String tag;

Expand All @@ -91,12 +90,12 @@ public Builder path(String path) {
return this;
}

public Builder destination(Class<?> destination) {
this.destination = destination;
public Builder target(String target) {
this.target = target;
return this;
}

public Builder putParam(String key, Type type) {
public Builder putParam(String key, String type) {
if (this.params == null) {
this.params = new LinkedHashMap<>();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,19 @@
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.FileObject;
import javax.tools.StandardLocation;

import cn.wzbos.android.rudolph.annotations.Extra;
import cn.wzbos.android.rudolph.annotations.Component;
import cn.wzbos.android.rudolph.annotations.Exclude;
import cn.wzbos.android.rudolph.annotations.Export;
import cn.wzbos.android.rudolph.annotations.Extra;
import cn.wzbos.android.rudolph.annotations.Route;

import static cn.wzbos.android.rudolph.Consts.RAW_URI;
import static javax.lang.model.element.Modifier.ABSTRACT;
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PUBLIC;
Expand Down Expand Up @@ -292,43 +287,39 @@ private void generateRouteTable(MethodSpec.Builder builder, Element element, Cla
List<Element> arrays = new ArrayList<>();
if (element.getKind() == ElementKind.CLASS) {
builder.addCode(
"\n\n$T.addRoute(new $T().routeType($T." + routetype + ")\n.destination($T.class)\n.path($S)\n.tag($S)",
"\n\n$T.addRoute(new $T().routeType($T." + routetype + ")\n.target($S)\n.path($S)",
rudolph,
ClassName.get(RouteInfo.Builder.class), //RouteInfo.Builder
ClassName.get(RouteType.class), //RouteType
target,
getRoutePath(element, route), route.tag());
getRoutePath(element, route));
arrays.addAll(element.getEnclosedElements());
} else {
ExecutableElement method = (ExecutableElement) element;
builder.addCode(
"\n\n$T.addRoute(new $T().routeType($T." + routetype + ")\n.destination($T.class)\n.path($S)\n.tag($S)",
"\n\n$T.addRoute(new $T().routeType($T." + routetype + ")\n.target($S)\n.path($S)",
rudolph,
ClassName.get(RouteInfo.Builder.class),//RouteInfo.Builder
ClassName.get(RouteType.class),//RouteType
target,
getRoutePath(element, route), route.tag());
getRoutePath(element, route));

arrays.addAll(method.getParameters());
}

if (StringUtils.isNotEmpty(route.tag())) {
builder.addCode("\n.tag($S)", route.tag());
}

for (Element field : arrays) {
Extra param = field.getAnnotation(Extra.class);
if (param == null)
continue;

String argName = getArgName(field, param);
String clsName = ClassName.get(field.asType()).toString();
if (clsName.contains("<") && clsName.contains(">")) {
builder.addCode("\n.putParam($S,new $T<$T>(){}.getType())",
argName,
TypeToken,
ClassName.get(field.asType()));
} else {
builder.addCode("\n.putParam($S,$T.class)",
argName,
ClassName.get(field.asType()));
}
builder.addCode("\n.putParam($S,$S)",
argName,
ClassName.get(field.asType()));
}
builder.addCode(".build());");
}
Expand Down Expand Up @@ -382,13 +373,15 @@ private void generateRouterCls(TypeElement element, RouteType routeType, Route r
.addJavadoc(Constant.WARNING_TIPS)
.addModifiers(PUBLIC);

//生成单例
if (routeType == RouteType.SERVICE) {

if (interfaceClsName == null) {
logger.error("@Route clazz:" + route.clazz().getName() + " 不存在!");
return;
}

if (route.singleton()) {
//生成单例
clsRouterBuilder.addField(interfaceClsName, "instance", PRIVATE, VOLATILE, STATIC);
clsRouterBuilder.addMethod(MethodSpec.methodBuilder("get")
.returns(interfaceClsName)
Expand All @@ -397,27 +390,40 @@ private void generateRouterCls(TypeElement element, RouteType routeType, Route r
.beginControlFlow("if (instance == null)")
.beginControlFlow("synchronized (" + clsName + ".class)")
.beginControlFlow("if (instance == null)")
.addStatement("instance = builder().build().open()")
.addStatement("Object result = $T.builder($S).build().open()", rudolph, getRoutePath(element, route))
.beginControlFlow("if(result instanceof $T)", interfaceClsName)
.addStatement("instance = ($T)result", interfaceClsName)
.endControlFlow()
.endControlFlow()
.addStatement("return instance")
.endControlFlow()
.endControlFlow()
.addStatement("return instance")
.build())
.build());
} else {
clsRouterBuilder.addMethod(MethodSpec.methodBuilder("newInstance")
.addJavadoc("create new instance\n")
.returns(interfaceClsName)
.addModifiers(PUBLIC, STATIC)
.addStatement("return builder()")
.build());
}
}

ClassName routerBuilderClsName = ClassName.get(clsName, "Builder");
TypeSpec builderTypeSpec = generate(interfaceClsName, routerBuilderClsName, element, routeType);
clsRouterBuilder.addType(builderTypeSpec);
//构件构造方法(Context context, Class<?> clazz)
clsRouterBuilder.addMethod(MethodSpec.methodBuilder("builder")
.addModifiers(PUBLIC, STATIC)
.returns(routerBuilderClsName)
.addStatement("return new $T()", routerBuilderClsName)
.build());
if (routeType != RouteType.SERVICE && !route.singleton()) {
ClassName routerBuilderClsName = ClassName.get(clsName, "Builder");
TypeSpec builderTypeSpec = generate(interfaceClsName, routerBuilderClsName, element, routeType);
clsRouterBuilder.addType(builderTypeSpec);
//构件构造方法(Context context, Class<?> clazz)
clsRouterBuilder.addMethod(MethodSpec.methodBuilder("builder")
.addModifiers(PUBLIC, STATIC)
.returns(routerBuilderClsName)
.addStatement("return new $T()", routerBuilderClsName)
.build());
// logger.error("getExportApiPackageName:"+getExportApiPackageName(element));
}


try {
JavaFile file = JavaFile.builder(getExportApiPackageName(element, export != null), clsRouterBuilder.build())
Expand Down Expand Up @@ -510,7 +516,6 @@ private TypeSpec generate(TypeName interfaceClsName, ClassName builderType, Type
continue;
// logger.error("field:"+field.getSimpleName().toString());


String fieldName = field.getSimpleName().toString();
String argName = getArgName(field, param);

Expand Down Expand Up @@ -577,82 +582,6 @@ String getPackageName(TypeElement element) {
return qualifiedName.substring(0, qualifiedName.lastIndexOf("."));
}

/**
* 生成路由服务类的接口
*/
private ClassName generateServiceInterface(TypeElement element, boolean export) {

String interfaceName = "I" + element.getSimpleName();


TypeSpec.Builder clsRouterBuilder = TypeSpec.interfaceBuilder(interfaceName)
//增加注释
.addJavadoc(Constant.WARNING_TIPS)
.addSuperinterface(ClassName.get(serviceTm))
.addModifiers(PUBLIC);

List<? extends Element> elements = element.getEnclosedElements();

for (Element e : elements) {
if (e.getModifiers().contains(Modifier.PUBLIC)) {
if (e.getAnnotation(Exclude.class) == null) {
if (e instanceof ExecutableElement) {
ExecutableElement executableElement = (ExecutableElement) e;
if (executableElement.getKind() != ElementKind.CONSTRUCTOR) {
MethodSpec.Builder builder = MethodSpec.methodBuilder(executableElement.getSimpleName().toString());
builder.addModifiers(PUBLIC, ABSTRACT);
if (executableElement.getReturnType().getKind() != TypeKind.VOID) {
builder.returns(ClassName.get(executableElement.getReturnType()));
}
for (VariableElement variableElement : executableElement.getParameters()) {
builder.addParameter(ClassName.get(variableElement.asType()), variableElement.getSimpleName().toString());
}
clsRouterBuilder.addMethod(builder.build());
}
}
}
}
}

try {

ClassName className = ClassName.get(getExportApiPackageName(element, export), interfaceName);
JavaFile file = JavaFile.builder(getExportApiPackageName(element, export), clsRouterBuilder.build())
.build();
File out_directory;
if (export && (out_directory = getOutputDirectory()) != null) {
logger.warning("output:" + out_directory.getAbsolutePath());
file.writeTo(out_directory);
} else {
file.writeTo(mFiler);
}
return className;
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
return null;
}

// /**
// * generate extend class from service
// */
// private ClassName generateServiceEx(TypeElement element, ClassName interfaceTypeName) throws IOException {
// String clsName = element.getSimpleName() + "Provider";
// TypeSpec.Builder clsRouterBuilder = TypeSpec.classBuilder(clsName)
// //增加注释
// .addJavadoc(Constant.WARNING_TIPS)
// .superclass(ClassName.get(element))
// .addSuperinterface(interfaceTypeName)
// .addModifiers(PUBLIC);
//
// String pkgName = getPackageName(element);
// JavaFile.builder(pkgName, clsRouterBuilder.build())
// .build().writeTo(mFiler);
//
// return ClassName.get(pkgName, clsName);
// }


/**
* 生成路由参数注入类 xxxRouteBinder.java
Expand Down Expand Up @@ -759,6 +688,8 @@ private void generateRouteBinderCls(TypeElement element, ClassName target) throw
varCode = "args.getFloat(" + extraName + ")";
} else if ("java.lang.Double".equals(cls) || "double".equals(cls)) {
varCode = "args.getDouble(" + extraName + ")";
} else if ("android.os.Bundle".equals(cls)) {
varCode = "args.getBundle(" + extraName + ")";
} else {
if (types.isSubtype(typeMirror, parcelableTM)) {
varCode = "args.getParcelable(" + extraName + ")";
Expand Down
17 changes: 17 additions & 0 deletions rudolph/src/main/java/cn/wzbos/android/rudolph/ILogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.wzbos.android.rudolph;

interface ILogger {
void v(String tag, String message);

void d(String tag, String message);

void i(String tag, String message);

void w(String tag, String message);

void e(String tag, String message);

void e(String tag, Throwable throwable);

void e(String tag, String message, Throwable throwable);
}
Loading

0 comments on commit ab420b7

Please sign in to comment.