Skip to content

Commit

Permalink
Merge pull request #35 from chenenyu/dev
Browse files Browse the repository at this point in the history
1.2.1
  • Loading branch information
chenenyu committed Jun 30, 2017
2 parents e7f4b03 + fadf06b commit 0fe36d7
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 74 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Router

建议浏览[中文wiki](https://github.com/chenenyu/Router/wiki), 也可体验[demo.apk](https://github.com/chenenyu/Router/raw/master/static/demo.apk) .
建议浏览[中文wiki](https://github.com/chenenyu/Router/wiki).

![screenshot](static/screenshot.gif)

Expand All @@ -17,7 +17,7 @@ buildscript {
}
dependencies {
...
classpath 'com.chenenyu.router:gradle-plugin:1.2.0'
classpath 'com.chenenyu.router:gradle-plugin:1.2.1'
}
}
Expand Down
12 changes: 12 additions & 0 deletions Sample/app/src/main/java/com/chenenyu/router/app/Model.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.chenenyu.router.app;

import java.io.Serializable;

/**
* Test model.
* <p>
* Created by Enyu Chen on 2017/6/28.
*/
public class Model implements Serializable {
// empty
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ public class TestActivity extends AppCompatActivity {
String id;
@InjectParam(key = "status")
private String sts;

@InjectParam
private short test1;
@InjectParam
byte[] test2;
@InjectParam
Model test3;
@InjectParam
private Model test4;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down
6 changes: 3 additions & 3 deletions VERSION.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# router gradle plugin version
GRADLE_PLUGIN_VERSION=1.2.0
GRADLE_PLUGIN_VERSION=1.2.1
# router library version
ROUTER_VERSION=1.2.0
ROUTER_VERSION=1.2.1
# compiler library version
COMPILER_VERSION=1.2.0
COMPILER_VERSION=1.2.1
# annotation library version
ANNOTATION_VERSION=0.3.0
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class RouterPlugin implements Plugin<Project> {
}
}
} else {
String routerVersion = "1.2.0"
String compilerVersion = "1.2.0"
String routerVersion = "1.2.1"
String compilerVersion = "1.2.1"
// org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension
ExtraPropertiesExtension ext = project.rootProject.ext
if (ext.has("routerVersion")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private void generate() throws IllegalAccessException, IOException {
.addStatement("$T $L = $T.class.getDeclaredField($S)",
ClassName.get(Field.class), reflectName, ClassName.get(parent), fieldName)
.addStatement("$L.setAccessible(true)", reflectName);
statement.append("$L.set($L, ");
statement.append("$L.set($L, $L.");
concatStatement(isActivity, param.asType(), statement);
statement.append(")");
injectMethodBuilder.addStatement(statement.toString(), reflectName, TARGET, TARGET,
Expand All @@ -162,10 +162,10 @@ private void generate() throws IllegalAccessException, IOException {
.addStatement("e.printStackTrace()")
.endControlFlow();
} else {
// target.field =
statement.append(TARGET).append(DOT).append(fieldName).append(" = ");
// target.field = (FieldType) target.
statement.append(TARGET).append(DOT).append(fieldName).append(" = ").append("($T) $L.");
concatStatement(isActivity, param.asType(), statement);
injectMethodBuilder.addStatement(statement.toString(), TARGET,
injectMethodBuilder.addStatement(statement.toString(), ClassName.get(param.asType()), TARGET,
isEmpty(injectParam.key()) ? fieldName : injectParam.key());
}
}
Expand All @@ -179,8 +179,8 @@ private void generate() throws IllegalAccessException, IOException {

private void concatStatement(boolean isActivity, TypeMirror type, StringBuilder statement) {
if (isActivity) {
// target.getIntent().getXXXExtra(key);
statement.append("$L.getIntent().get").append(getBundleAccessor(type)).append("Extra");
// getIntent().getXXXExtra(key);
statement.append("getIntent().get").append(getBundleAccessor(type)).append("Extra");
if (type.getKind().isPrimitive()) {
if (type.getKind() == TypeKind.BOOLEAN) {
statement.append("($S, false)");
Expand All @@ -197,8 +197,8 @@ private void concatStatement(boolean isActivity, TypeMirror type, StringBuilder
statement.append("($S)");
}
} else {
// target.getArguments().getXXX(key);
statement.append("$L.getArguments().get").append(getBundleAccessor(type)).append("($S)");
// getArguments().getXXX(key);
statement.append("getArguments().get").append(getBundleAccessor(type)).append("($S)");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import javax.lang.model.element.TypeElement;

import static com.chenenyu.router.compiler.util.Consts.CLASS_JAVA_DOC;
import static com.chenenyu.router.compiler.util.Consts.INTERCEPTORS;
import static com.chenenyu.router.compiler.util.Consts.INTERCEPTORS_METHOD_NAME;
import static com.chenenyu.router.compiler.util.Consts.HANDLE;
import static com.chenenyu.router.compiler.util.Consts.INTERCEPTOR_ANNOTATION_TYPE;
import static com.chenenyu.router.compiler.util.Consts.INTERCEPTOR_INTERFACE;
import static com.chenenyu.router.compiler.util.Consts.INTERCEPTOR_TABLE;
import static com.chenenyu.router.compiler.util.Consts.OPTION_MODULE_NAME;
import static com.chenenyu.router.compiler.util.Consts.PACKAGE_NAME;

Expand Down Expand Up @@ -99,8 +99,9 @@ private void generateInterceptors(String moduleName, Set<TypeElement> elements)
/*
* method
*/
MethodSpec.Builder handleInterceptors = MethodSpec.methodBuilder(INTERCEPTORS_METHOD_NAME)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
MethodSpec.Builder handleInterceptors = MethodSpec.methodBuilder(HANDLE)
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(mapParameterSpec);
for (TypeElement element : elements) {
mLogger.info(String.format("Found interceptor: %s", element.getQualifiedName()));
Expand All @@ -112,7 +113,8 @@ private void generateInterceptors(String moduleName, Set<TypeElement> elements)
/*
* class
*/
TypeSpec type = TypeSpec.classBuilder(capitalize(moduleName) + INTERCEPTORS)
TypeSpec type = TypeSpec.classBuilder(capitalize(moduleName) + INTERCEPTOR_TABLE)
.addSuperinterface(ClassName.get(PACKAGE_NAME, INTERCEPTOR_TABLE))
.addModifiers(Modifier.PUBLIC)
.addMethod(handleInterceptors.build())
.addJavadoc(CLASS_JAVA_DOC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@
import static com.chenenyu.router.compiler.util.Consts.CLASS_JAVA_DOC;
import static com.chenenyu.router.compiler.util.Consts.FRAGMENT_FULL_NAME;
import static com.chenenyu.router.compiler.util.Consts.FRAGMENT_V4_FULL_NAME;
import static com.chenenyu.router.compiler.util.Consts.INTERCEPTOR_TABLE;
import static com.chenenyu.router.compiler.util.Consts.INTERCEPTOR_TABLE_METHOD_NAME;
import static com.chenenyu.router.compiler.util.Consts.HANDLE;
import static com.chenenyu.router.compiler.util.Consts.OPTION_MODULE_NAME;
import static com.chenenyu.router.compiler.util.Consts.PACKAGE_NAME;
import static com.chenenyu.router.compiler.util.Consts.ROUTE_ANNOTATION_TYPE;
import static com.chenenyu.router.compiler.util.Consts.ROUTE_TABLE;
import static com.chenenyu.router.compiler.util.Consts.ROUTE_TABLE_FULL_NAME;
import static com.chenenyu.router.compiler.util.Consts.ROUTE_TABLE_METHOD_NAME;
import static com.chenenyu.router.compiler.util.Consts.TABLE_INTERCEPTORS;

/**
* {@link Route} annotation processor.
Expand Down Expand Up @@ -79,7 +78,7 @@ public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnv
if (mModuleName != null) {
String validModuleName = mModuleName.replace(".", "_").replace("-", "_");
generateRouteTable(validModuleName, typeElements);
generateInterceptorTable(validModuleName, typeElements);
generateTargetInterceptors(validModuleName, typeElements);
} else {
mLogger.error(String.format("No option `%s` passed to Route annotation processor.", OPTION_MODULE_NAME));
}
Expand Down Expand Up @@ -122,7 +121,7 @@ private void generateRouteTable(String moduleName, Set<TypeElement> elements) {
WildcardTypeName.subtypeOf(Object.class)));
ParameterSpec mapParameterSpec = ParameterSpec.builder(mapTypeName, "map").build();

MethodSpec.Builder methodHandle = MethodSpec.methodBuilder(ROUTE_TABLE_METHOD_NAME)
MethodSpec.Builder methodHandle = MethodSpec.methodBuilder(HANDLE)
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(mapParameterSpec);
Expand Down Expand Up @@ -150,18 +149,19 @@ private void generateRouteTable(String moduleName, Set<TypeElement> elements) {
}

/**
* InterceptorTable.
* TargetInterceptors.
*/
private void generateInterceptorTable(String moduleName, Set<TypeElement> elements) {
private void generateTargetInterceptors(String moduleName, Set<TypeElement> elements) {
// Map<Class<?>, String[]> map
ParameterizedTypeName mapTypeName = ParameterizedTypeName.get(
ClassName.get(Map.class),
ParameterizedTypeName.get(ClassName.get(Class.class),
WildcardTypeName.subtypeOf(Object.class)),
TypeName.get(String[].class));
ParameterSpec mapParameterSpec = ParameterSpec.builder(mapTypeName, "map").build();
MethodSpec.Builder methodHandle = MethodSpec.methodBuilder(INTERCEPTOR_TABLE_METHOD_NAME)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
MethodSpec.Builder methodHandle = MethodSpec.methodBuilder(HANDLE)
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(mapParameterSpec);
boolean hasInterceptor = false; // flag
for (TypeElement element : elements) {
Expand All @@ -184,7 +184,8 @@ private void generateInterceptorTable(String moduleName, Set<TypeElement> elemen
if (!hasInterceptor) { // if there are no interceptors, ignore.
return;
}
TypeSpec type = TypeSpec.classBuilder(capitalize(moduleName) + INTERCEPTOR_TABLE)
TypeSpec type = TypeSpec.classBuilder(capitalize(moduleName) + TABLE_INTERCEPTORS)
.addSuperinterface(ClassName.get(PACKAGE_NAME, TABLE_INTERCEPTORS))
.addModifiers(Modifier.PUBLIC)
.addMethod(methodHandle.build())
.addJavadoc(CLASS_JAVA_DOC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ public class Consts {
public static final String INTERCEPTOR_ANNOTATION_TYPE = "com.chenenyu.router.annotation.Interceptor";
public static final String PARAM_ANNOTATION_TYPE = "com.chenenyu.router.annotation.InjectParam";

public static final String ROUTE_TABLE_METHOD_NAME = "handle";
public static final String HANDLE = "handle";
public static final String INTERCEPTOR_INTERFACE = PACKAGE_NAME + DOT + "RouteInterceptor";

public static final String INTERCEPTORS = "Interceptors";
public static final String INTERCEPTORS_METHOD_NAME = "handle";

public static final String INTERCEPTOR_TABLE = "InterceptorTable";
public static final String INTERCEPTOR_TABLE_METHOD_NAME = "handle";

public static final String TABLE_INTERCEPTORS = "TargetInterceptors";

public static final String METHOD_INJECT = "inject";
public static final String METHOD_INJECT_PARAM = "obj";
Expand Down
53 changes: 26 additions & 27 deletions router/src/main/java/com/chenenyu/router/AptHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -20,17 +19,15 @@ class AptHub {
private static final String BUILD_INFO_FIELD = "ALL_MODULES";
private static final String ROUTE_TABLE = "RouteTable";
private static final String INTERCEPTORS = "Interceptors";
private static final String HANDLE_INTERCEPTORS = "handle";
private static final String INTERCEPTOR_TABLE = "InterceptorTable";
private static final String HANDLE_INTERCEPTOR_TABLE = "handle";
private static final String TARGET_INTERCEPTORS = "TargetInterceptors";
static final String PARAM_CLASS_SUFFIX = "$$Router$$ParamInjector";

// Uri -> Activity/Fragment
static Map<String, Class<?>> routeTable = new HashMap<>();
// Activity -> interceptors' name
static Map<Class<?>, String[]> interceptorTable = new HashMap<>();
// Activity/Fragment -> interceptorTable' name
static Map<Class<?>, String[]> targetInterceptors = new HashMap<>();
// interceptor's name -> interceptor
static Map<String, Class<? extends RouteInterceptor>> interceptors = new HashMap<>();
static Map<String, Class<? extends RouteInterceptor>> interceptorTable = new HashMap<>();
// injector's name -> injector
static Map<String, Class<ParamInjector>> injectors = new HashMap<>();

Expand All @@ -52,56 +49,58 @@ synchronized static void init() {
}

/* RouteTable */
String fullTableName;
String routeTableName;
for (String moduleName : modules) {
try {
fullTableName = PACKAGE_NAME + DOT + capitalize(moduleName) + ROUTE_TABLE;
Class<?> routeTableClz = Class.forName(fullTableName);
routeTableName = PACKAGE_NAME + DOT + capitalize(moduleName) + ROUTE_TABLE;
Class<?> routeTableClz = Class.forName(routeTableName);
Constructor constructor = routeTableClz.getConstructor();
RouteTable instance = (RouteTable) constructor.newInstance();
instance.handle(routeTable);
} catch (ClassNotFoundException e) {
RLog.i(String.format("There is no route table in module: %s.", moduleName));
RLog.i(String.format("There is no RouteTable in module: %s.", moduleName));
} catch (Exception e) {
RLog.w(e.getMessage());
}
}
RLog.i("RouteTable", routeTable.toString());

/* InterceptorTable */
String interceptorTableName;
/* TargetInterceptors */
String targetInterceptorsName;
for (String moduleName : modules) {
try {
interceptorTableName = PACKAGE_NAME + DOT + capitalize(moduleName) + INTERCEPTOR_TABLE;
Class<?> clz = Class.forName(interceptorTableName);
Method handle = clz.getMethod(HANDLE_INTERCEPTOR_TABLE, Map.class);
handle.invoke(null, interceptorTable);
targetInterceptorsName = PACKAGE_NAME + DOT + capitalize(moduleName) + TARGET_INTERCEPTORS;
Class<?> clz = Class.forName(targetInterceptorsName);
Constructor constructor = clz.getConstructor();
TargetInterceptors instance = (TargetInterceptors) constructor.newInstance();
instance.handle(targetInterceptors);
} catch (ClassNotFoundException e) {
RLog.i(String.format("There is no interceptor table in module: %s.", moduleName));
RLog.i(String.format("There is no TargetInterceptors in module: %s.", moduleName));
} catch (Exception e) {
RLog.w(e.getMessage());
}
}
if (!interceptorTable.isEmpty()) {
RLog.i("InterceptorTable", interceptorTable.toString());
if (!targetInterceptors.isEmpty()) {
RLog.i("TargetInterceptors", targetInterceptors.toString());
}

/* Interceptors */
/* InterceptorTable */
String interceptorName;
for (String moduleName : modules) {
try {
interceptorName = PACKAGE_NAME + DOT + capitalize(moduleName) + INTERCEPTORS;
Class<?> interceptorClz = Class.forName(interceptorName);
Method handle = interceptorClz.getMethod(HANDLE_INTERCEPTORS, Map.class);
handle.invoke(null, interceptors);
Class<?> clz = Class.forName(interceptorName);
Constructor constructor = clz.getConstructor();
InterceptorTable instance = (InterceptorTable) constructor.newInstance();
instance.handle(interceptorTable);
} catch (ClassNotFoundException e) {
RLog.i(String.format("There are no interceptors in module: %s.", moduleName));
RLog.i(String.format("There is no InterceptorTable in module: %s.", moduleName));
} catch (Exception e) {
RLog.w(e.getMessage());
}
}
if (!interceptors.isEmpty()) {
RLog.i("Interceptors", interceptors.toString());
if (!interceptorTable.isEmpty()) {
RLog.i("InterceptorTable", interceptorTable.toString());
}
}

Expand Down
17 changes: 17 additions & 0 deletions router/src/main/java/com/chenenyu/router/InterceptorTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.chenenyu.router;

import java.util.Map;

/**
* Interceptor table mapping.
* <p>
* Created by Enyu Chen on 2017/6/30.
*/
public interface InterceptorTable {
/**
* Mapping between name and interceptor.
*
* @param map name -> interceptor.
*/
void handle(Map<String, Class<? extends RouteInterceptor>> map);
}

0 comments on commit 0fe36d7

Please sign in to comment.