diff --git a/compiler/src/main/java/com/lzh/nonview/router/compiler/Constants.java b/compiler/src/main/java/com/lzh/nonview/router/compiler/Constants.java index 669f3ac..112b472 100644 --- a/compiler/src/main/java/com/lzh/nonview/router/compiler/Constants.java +++ b/compiler/src/main/java/com/lzh/nonview/router/compiler/Constants.java @@ -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 @@ -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"; } diff --git a/compiler/src/main/java/com/lzh/nonview/router/compiler/factory/RuleFactory.java b/compiler/src/main/java/com/lzh/nonview/router/compiler/factory/RuleFactory.java index 73922e0..819c267 100644 --- a/compiler/src/main/java/com/lzh/nonview/router/compiler/factory/RuleFactory.java +++ b/compiler/src/main/java/com/lzh/nonview/router/compiler/factory/RuleFactory.java @@ -30,17 +30,21 @@ public class RuleFactory { private ClassName clzName; private List activityParser = new ArrayList<>(); private List actionParser = new ArrayList<>(); + private List 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 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); } } } @@ -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 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()); } diff --git a/compiler/src/main/java/com/lzh/nonview/router/compiler/util/Utils.java b/compiler/src/main/java/com/lzh/nonview/router/compiler/util/Utils.java index 0a3bae7..38d0812 100644 --- a/compiler/src/main/java/com/lzh/nonview/router/compiler/util/Utils.java +++ b/compiler/src/main/java/com/lzh/nonview/router/compiler/util/Utils.java @@ -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 { @@ -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; } diff --git a/routerlib/src/main/java/com/lzh/nonview/router/CreatorRouter.java b/routerlib/src/main/java/com/lzh/nonview/router/CreatorRouter.java new file mode 100644 index 0000000..7a3f0f4 --- /dev/null +++ b/routerlib/src/main/java/com/lzh/nonview/router/CreatorRouter.java @@ -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 createTarget() { + try { + Map 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; + } + } +} diff --git a/routerlib/src/main/java/com/lzh/nonview/router/module/CreatorRouteRule.java b/routerlib/src/main/java/com/lzh/nonview/router/module/CreatorRouteRule.java new file mode 100644 index 0000000..613a235 --- /dev/null +++ b/routerlib/src/main/java/com/lzh/nonview/router/module/CreatorRouteRule.java @@ -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 { + + private Class target; + + public CreatorRouteRule(Class clz) { + super(clz.getCanonicalName()); + this.target = clz; + } + + public Class getTarget() { + return target; + } +} diff --git a/routerlib/src/main/java/com/lzh/nonview/router/module/RouteCreator.java b/routerlib/src/main/java/com/lzh/nonview/router/module/RouteCreator.java index 39a7617..2ea39a4 100644 --- a/routerlib/src/main/java/com/lzh/nonview/router/module/RouteCreator.java +++ b/routerlib/src/main/java/com/lzh/nonview/router/module/RouteCreator.java @@ -35,4 +35,6 @@ public interface RouteCreator { */ Map createActionRouteRules(); + Map createCreatorRouteRule(); + } diff --git a/routerlib/src/main/java/com/lzh/nonview/router/tools/Cache.java b/routerlib/src/main/java/com/lzh/nonview/router/tools/Cache.java index 5ed55c6..441acb0 100644 --- a/routerlib/src/main/java/com/lzh/nonview/router/tools/Cache.java +++ b/routerlib/src/main/java/com/lzh/nonview/router/tools/Cache.java @@ -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; @@ -40,6 +41,7 @@ public final class Cache { /** A map to contains all of route rule created by creatorList*/ private static Map activityRouteMap = new HashMap<>(); private static Map actionRouteMap = new HashMap<>(); + private static Map creatorRouteMap = new HashMap<>(); public static final int TYPE_ACTIVITY_ROUTE = 0; public static final int TYPE_ACTION_ROUTE = 1; @@ -60,6 +62,11 @@ public static void addCreator (RouteCreator creator) { shouldReload = true; } + public static Map getCreatorRules() { + obtainRouteRulesIfNeed(); + return creatorRouteMap; + } + public static Map getActionRules() { obtainRouteRulesIfNeed(); return actionRouteMap; @@ -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; } diff --git a/sample/build.gradle b/sample/build.gradle index b4892be..4def640 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -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" }