Skip to content

Commit

Permalink
Support Creator Route
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed May 25, 2018
1 parent ca393e2 commit c9a5d9a
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Constants {
public static final String CLASSNAME_ACTIVITY_ROUTE_MAP = "com.lzh.nonview.router.module.ActivityRouteRule";
// ActionRouteRule
public static final String CLASSNAME_ACTION_ROUTE_MAP = "com.lzh.nonview.router.module.ActionRouteRule";
// CreatorRouteRule
public static final String CLASSNAME_CREATOR_ROUTE_MAP = "com.lzh.nonview.router.module.CreatorRouteRule";
// RouteCreator
public static final String CLASSNAME_ROUTE_CREATOR = "com.lzh.nonview.router.module.RouteCreator";
// MainThreadExecutor
Expand All @@ -27,5 +29,7 @@ public class Constants {
public static final String METHODNAME_CREATE_ACTIVITY_ROUTER = "createActivityRouteRules";
// RouteCreator.createActionRouteRule
public static final String METHODNAME_CREATE_ACTION_ROUTER = "createActionRouteRules";
// RouteCreator.createCreatorRouteRule
public static final String METHODNAME_CREATE_CREATOR_ROUTER = "createCreatorRouteRule";

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ public class RuleFactory {
private ClassName clzName;
private List<Parser> activityParser = new ArrayList<>();
private List<Parser> actionParser = new ArrayList<>();
private List<Parser> creatorParser = new ArrayList<>();
private ClassName routeMap = ClassName.bestGuess(Constants.CLASSNAME_ROUTE_MAP);
private ClassName activityRouteMap = ClassName.bestGuess(Constants.CLASSNAME_ACTIVITY_ROUTE_MAP);
private ClassName actionRouteMap = ClassName.bestGuess(Constants.CLASSNAME_ACTION_ROUTE_MAP);
private ClassName creatorRouteMap = ClassName.bestGuess(Constants.CLASSNAME_CREATOR_ROUTE_MAP);

public RuleFactory(ClassName name, List<Parser> parserList) {
this.clzName = name;
for (Parser item : parserList) {
if (Utils.isSuperClass(item.getType(), Constants.CLASSNAME_ACTIVITY)) {
activityParser.add(item);
} else {
} else if (Utils.isSuperClass(item.getType(), Constants.CLASSNAME_ACTION_SUPPORT)){
actionParser.add(item);
} else {
creatorParser.add(item);
}
}
}
Expand Down Expand Up @@ -71,8 +75,20 @@ public void generateCode () throws IOException {
}
methodActionRulesCreator.addStatement("return routes");

MethodSpec.Builder methodCreatorRulesCreator = MethodSpec.overriding(getOverrideMethod(creator, Constants.METHODNAME_CREATE_CREATOR_ROUTER));
methodCreatorRulesCreator.addStatement("$T<String,$T> routes = new $T<>()",Map.class, creatorRouteMap, HashMap.class);
for (Parser parser : creatorParser ) {
String[] schemaes = parser.getScheme();
for (String schema : schemaes) {

appendMethod(parser,methodCreatorRulesCreator,schema, Constants.CLASSNAME_CREATOR_ROUTE_MAP);
}
}
methodCreatorRulesCreator.addStatement("return routes");

typeBuilder.addMethod(methodActivityRulesCreator.build());
typeBuilder.addMethod(methodActionRulesCreator.build());
typeBuilder.addMethod(methodCreatorRulesCreator.build());
JavaFile.Builder javaBuilder = JavaFile.builder(clzName.packageName(), typeBuilder.build());
javaBuilder.build().writeTo(UtilMgr.getMgr().getFiler());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package com.lzh.nonview.router.compiler.util;

import com.lzh.compiler.parceler.annotation.Arg;
import com.lzh.nonview.router.compiler.Constants;
import com.lzh.nonview.router.compiler.exception.RouterException;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.TypeName;

import java.lang.annotation.Annotation;
import java.util.Set;

import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.MirroredTypeException;
import javax.lang.model.type.TypeMirror;

public class Utils {

Expand All @@ -34,9 +28,6 @@ public static boolean checkTypeValid (TypeElement type) {
} else if (modifiers.contains(Modifier.ABSTRACT)) {
// skip it
return false;
} else if (!isSuperClass(type, Constants.CLASSNAME_ACTIVITY)
&& !isSuperClass(type, Constants.CLASSNAME_ACTION_SUPPORT)) {
throw new RouterException(String.format("The class %s you annotated by RouterRule should be a subclass of Activity or ActionSupport",type.getSimpleName()),type);
}
return true;
}
Expand Down
66 changes: 66 additions & 0 deletions routerlib/src/main/java/com/lzh/nonview/router/CreatorRouter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.lzh.nonview.router;

import android.net.Uri;
import android.os.Bundle;

import com.lzh.compiler.parceler.Parceler;
import com.lzh.nonview.router.module.CreatorRouteRule;
import com.lzh.nonview.router.parser.URIParser;
import com.lzh.nonview.router.tools.Cache;
import com.lzh.nonview.router.tools.RouterLog;
import com.lzh.nonview.router.tools.Utils;

import java.util.Map;

/**
* @author haoge on 2018/5/25
*/
public class CreatorRouter {

Uri uri;
Bundle extra = new Bundle();

private CreatorRouter(Uri uri) {
this.uri = uri;
}

public static CreatorRouter create(String url) {
return new CreatorRouter(Uri.parse(url));
}

public static CreatorRouter create(Uri uri) {
return new CreatorRouter(uri);
}

public CreatorRouter addExtras(Bundle extra){
if (extra != null) {
this.extra.putAll(extra);
}
return this;
}

public <T> T createTarget() {
try {
Map<String, CreatorRouteRule> rules = Cache.getCreatorRules();
URIParser parser = new URIParser(uri);
String route = parser.getRoute();
CreatorRouteRule rule = rules.get(route);
if (rule == null) {
RouterLog.d("Could not match rule for this uri");
return null;
}

Object instance = rule.getTarget().newInstance();

Bundle bundle = Utils.parseRouteMapToBundle(parser, rule);
if (Utils.PARCELER_SUPPORT) {
Parceler.toEntity(instance, bundle);
}

return (T) instance;
} catch (Throwable e) {
RouterLog.e("Create target class from CreatorRouter failed. cause by:" + e.getMessage(), e);
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.lzh.nonview.router.module;

import com.lzh.nonview.router.launcher.Launcher;

/**
* @author haoge on 2018/5/25
*/
public class CreatorRouteRule extends RouteRule<CreatorRouteRule, Launcher> {

private Class target;

public CreatorRouteRule(Class clz) {
super(clz.getCanonicalName());
this.target = clz;
}

public Class getTarget() {
return target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ public interface RouteCreator {
*/
Map<String, ActionRouteRule> createActionRouteRules();

Map<String, CreatorRouteRule> createCreatorRouteRule();

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.lzh.nonview.router.executors.MainThreadExecutor;
import com.lzh.nonview.router.module.ActionRouteRule;
import com.lzh.nonview.router.module.ActivityRouteRule;
import com.lzh.nonview.router.module.CreatorRouteRule;
import com.lzh.nonview.router.module.RouteCreator;
import com.lzh.nonview.router.module.RouteRule;
import com.lzh.nonview.router.parser.URIParser;
Expand All @@ -40,6 +41,7 @@ public final class Cache {
/** A map to contains all of route rule created by creatorList*/
private static Map<String,ActivityRouteRule> activityRouteMap = new HashMap<>();
private static Map<String,ActionRouteRule> actionRouteMap = new HashMap<>();
private static Map<String,CreatorRouteRule> creatorRouteMap = new HashMap<>();
public static final int TYPE_ACTIVITY_ROUTE = 0;
public static final int TYPE_ACTION_ROUTE = 1;

Expand All @@ -60,6 +62,11 @@ public static void addCreator (RouteCreator creator) {
shouldReload = true;
}

public static Map<String, CreatorRouteRule> getCreatorRules() {
obtainRouteRulesIfNeed();
return creatorRouteMap;
}

public static Map<String, ActionRouteRule> getActionRules() {
obtainRouteRulesIfNeed();
return actionRouteMap;
Expand All @@ -85,10 +92,12 @@ private static void obtainRouteRulesIfNeed() {
if (shouldReload) {
activityRouteMap.clear();
actionRouteMap.clear();
creatorRouteMap.clear();
int count = creatorList == null ? 0 : creatorList.size();
for (int i = 0; i < count; i++) {
addAll(activityRouteMap, creatorList.get(i).createActivityRouteRules());
addAll(actionRouteMap, creatorList.get(i).createActionRouteRules());
addAll(creatorRouteMap, creatorList.get(i).createCreatorRouteRule());
}
shouldReload = false;
}
Expand Down
8 changes: 4 additions & 4 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ dependencies {
kapt "com.github.yjfnypeu.Parceler:compiler:$parceler_version"
compile "com.github.yjfnypeu.Parceler:api:$parceler_version"

compile "com.github.yjfnypeu.Router:router-api:$router_version"
kapt "com.github.yjfnypeu.Router:router-compiler:$router_version"
// compile "com.github.yjfnypeu.Router:router-api:$router_version"
// kapt "com.github.yjfnypeu.Router:router-compiler:$router_version"

// compile project(':routerlib')
// kapt project(':compiler')
compile project(':routerlib')
kapt project(':compiler')
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

0 comments on commit c9a5d9a

Please sign in to comment.