Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

signingConfigs {
Expand Down Expand Up @@ -54,4 +54,4 @@ dependencies {

implementation "com.android.support:support-v4:${SUPPORT_LIB_VERSION}"
implementation "com.android.support:appcompat-v7:${SUPPORT_LIB_VERSION}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ public void loadInto(Map<String, RouteMeta> atlas) {
.withObject("objList", objList)
.withObject("map", map).navigation(this);
break;
case R.id.route1:
ARouter.getInstance().build("/module/1").navigation();
break;
case R.id.route2:
ARouter.getInstance().build("/test/multiRoute").navigation();
break;

default:
break;
}
Expand Down
30 changes: 29 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,33 @@
android:onClick="onClick"
android:text="动态路由测试" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/between_cell"
android:background="@drawable/bg_test_area"
android:orientation="vertical"
android:padding="@dimen/test_area_padding">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="多路由(同一个页面 or service 指定多个路由)" />

<Button
android:id="@+id/route1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="用路由1跳转" />

<Button
android:id="@+id/route2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="用路由2跳转" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</ScrollView>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.alibaba.android.arouter.facade.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand All @@ -12,6 +13,7 @@
* @version 1.0
* @since 16/8/15 下午9:29
*/
@Repeatable(Routes.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.CLASS)
public @interface Route {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.alibaba.android.arouter.facade.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.CLASS)
public @interface Routes {
Route[] value();
}
4 changes: 2 additions & 2 deletions arouter-compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ compileJava {
}

dependencies {
implementation 'com.alibaba:arouter-annotation:1.0.6'
implementation project(':arouter-annotation')

annotationProcessor 'com.google.auto.service:auto-service:1.0-rc7'
compileOnly 'com.google.auto.service:auto-service-annotations:1.0-rc7'
Expand All @@ -19,4 +19,4 @@ dependencies {
implementation 'com.alibaba:fastjson:1.2.69'
}

apply from: rootProject.file('gradle/publish.gradle')
apply from: rootProject.file('gradle/publish.gradle')
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.alibaba.android.arouter.compiler.utils.Consts;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.annotation.Routes;
import com.alibaba.android.arouter.facade.enums.RouteType;
import com.alibaba.android.arouter.facade.enums.TypeKind;
import com.alibaba.android.arouter.facade.model.RouteMeta;
Expand All @@ -27,6 +28,7 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -46,6 +48,7 @@
import static com.alibaba.android.arouter.compiler.utils.Consts.ACTIVITY;
import static com.alibaba.android.arouter.compiler.utils.Consts.ANNOTATION_TYPE_AUTOWIRED;
import static com.alibaba.android.arouter.compiler.utils.Consts.ANNOTATION_TYPE_ROUTE;
import static com.alibaba.android.arouter.compiler.utils.Consts.ANNOTATION_TYPE_ROUTES;
import static com.alibaba.android.arouter.compiler.utils.Consts.FRAGMENT;
import static com.alibaba.android.arouter.compiler.utils.Consts.IPROVIDER_GROUP;
import static com.alibaba.android.arouter.compiler.utils.Consts.IROUTE_GROUP;
Expand All @@ -69,7 +72,7 @@
* @since 16/8/15 下午10:08
*/
@AutoService(Processor.class)
@SupportedAnnotationTypes({ANNOTATION_TYPE_ROUTE, ANNOTATION_TYPE_AUTOWIRED})
@SupportedAnnotationTypes({ANNOTATION_TYPE_ROUTE,ANNOTATION_TYPE_ROUTES, ANNOTATION_TYPE_AUTOWIRED})
public class RouteProcessor extends BaseProcessor {
private Map<String, Set<RouteMeta>> groupMap = new HashMap<>(); // ModuleName and routeMeta.
private Map<String, String> rootMap = new TreeMap<>(); // Map of root metas, used for generate class file in order.
Expand Down Expand Up @@ -108,9 +111,14 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (CollectionUtils.isNotEmpty(annotations)) {
Set<? extends Element> routeElements = roundEnv.getElementsAnnotatedWith(Route.class);
Set<? extends Element> routesElements = roundEnv.getElementsAnnotatedWith(Routes.class);

Set<Element> set = new HashSet<>();
set.addAll(routeElements);
set.addAll(routesElements);
try {
logger.info(">>> Found routes, start... <<<");
this.parseRoutes(routeElements);
this.parseRoutes(set);

} catch (Exception e) {
logger.error(e);
Expand Down Expand Up @@ -182,38 +190,17 @@ private void parseRoutes(Set<? extends Element> routeElements) throws IOExceptio
// Follow a sequence, find out metas of group first, generate java file, then statistics them as root.
for (Element element : routeElements) {
TypeMirror tm = element.asType();

Route route = element.getAnnotation(Route.class);
RouteMeta routeMeta;

// Activity or Fragment
if (types.isSubtype(tm, type_Activity) || types.isSubtype(tm, fragmentTm) || types.isSubtype(tm, fragmentTmV4)) {
// Get all fields annotation by @Autowired
Map<String, Integer> paramsType = new HashMap<>();
Map<String, Autowired> injectConfig = new HashMap<>();
injectParamCollector(element, paramsType, injectConfig);

if (types.isSubtype(tm, type_Activity)) {
// Activity
logger.info(">>> Found activity route: " + tm.toString() + " <<<");
routeMeta = new RouteMeta(route, element, RouteType.ACTIVITY, paramsType);
} else {
// Fragment
logger.info(">>> Found fragment route: " + tm.toString() + " <<<");
routeMeta = new RouteMeta(route, element, RouteType.parse(FRAGMENT), paramsType);
Routes routes = element.getAnnotation(Routes.class);
if (route == null) {
Route[] routeArr = routes.value();
for (Route r : routeArr) {
initRouteMeta(type_Activity, type_Service, fragmentTm, fragmentTmV4, element, tm, r);
}

routeMeta.setInjectConfig(injectConfig);
} else if (types.isSubtype(tm, iProvider)) { // IProvider
logger.info(">>> Found provider route: " + tm.toString() + " <<<");
routeMeta = new RouteMeta(route, element, RouteType.PROVIDER, null);
} else if (types.isSubtype(tm, type_Service)) { // Service
logger.info(">>> Found service route: " + tm.toString() + " <<<");
routeMeta = new RouteMeta(route, element, RouteType.parse(SERVICE), null);
} else {
throw new RuntimeException("The @Route is marked on unsupported class, look at [" + tm.toString() + "].");
}else {
initRouteMeta(type_Activity, type_Service, fragmentTm, fragmentTmV4, element, tm, route);
}

categories(routeMeta);
}

MethodSpec.Builder loadIntoMethodOfProviderBuilder = MethodSpec.methodBuilder(METHOD_LOAD_INTO)
Expand Down Expand Up @@ -369,6 +356,39 @@ private void parseRoutes(Set<? extends Element> routeElements) throws IOExceptio
}
}

private void initRouteMeta(TypeMirror type_Activity, TypeMirror type_Service, TypeMirror fragmentTm, TypeMirror fragmentTmV4, Element element, TypeMirror tm, Route route) {
RouteMeta routeMeta;
// Activity or Fragment
if (types.isSubtype(tm, type_Activity) || types.isSubtype(tm, fragmentTm) || types.isSubtype(tm, fragmentTmV4)) {
// Get all fields annotation by @Autowired
Map<String, Integer> paramsType = new HashMap<>();
Map<String, Autowired> injectConfig = new HashMap<>();
injectParamCollector(element, paramsType, injectConfig);

if (types.isSubtype(tm, type_Activity)) {
// Activity
logger.info(">>> Found activity route: " + tm.toString() + " <<<");
routeMeta = new RouteMeta(route, element, RouteType.ACTIVITY, paramsType);
} else {
// Fragment
logger.info(">>> Found fragment route: " + tm.toString() + " <<<");
routeMeta = new RouteMeta(route, element, RouteType.parse(FRAGMENT), paramsType);
}

routeMeta.setInjectConfig(injectConfig);
} else if (types.isSubtype(tm, iProvider)) { // IProvider
logger.info(">>> Found provider route: " + tm.toString() + " <<<");
routeMeta = new RouteMeta(route, element, RouteType.PROVIDER, null);
} else if (types.isSubtype(tm, type_Service)) { // Service
logger.info(">>> Found service route: " + tm.toString() + " <<<");
routeMeta = new RouteMeta(route, element, RouteType.parse(SERVICE), null);
} else {
throw new RuntimeException("The @Route is marked on unsupported class, look at [" + tm.toString() + "].");
}

categories(routeMeta);
}

/**
* Recursive inject config collector.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@ public class Consts {
// Annotation type
public static final String ANNOTATION_TYPE_INTECEPTOR = FACADE_PACKAGE + ".annotation.Interceptor";
public static final String ANNOTATION_TYPE_ROUTE = FACADE_PACKAGE + ".annotation.Route";
public static final String ANNOTATION_TYPE_ROUTES = FACADE_PACKAGE + ".annotation.Routes";
public static final String ANNOTATION_TYPE_AUTOWIRED = FACADE_PACKAGE + ".annotation.Autowired";
}
}
6 changes: 3 additions & 3 deletions module-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ android {
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

buildTypes {
Expand All @@ -39,4 +39,4 @@ android {

lintOptions { abortOnError false }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.alibaba.android.arouter.facade.annotation.Route;

@Route(path = "/module/1")
@Route(path = "/test/multiRoute")
public class TestModuleActivity extends Activity {

@Override
Expand Down