use of com.luojilab.router.facade.model.Node in project ChidoriX by Chihiro23333.
the class RouterProcessor method parseRouteNodes.
private void parseRouteNodes(Set<? extends Element> routeElements) {
TypeMirror type_Activity = elements.getTypeElement(ACTIVITY).asType();
for (Element element : routeElements) {
TypeMirror tm = element.asType();
RouteNode route = element.getAnnotation(RouteNode.class);
if (types.isSubtype(tm, type_Activity)) {
// Activity
logger.info(">>> Found activity route: " + tm.toString() + " <<<");
Node node = new Node();
String path = route.path();
checkPath(path);
node.setPath(path);
node.setDesc(route.desc());
node.setPriority(route.priority());
node.setNodeType(NodeType.ACTIVITY);
node.setRawType(element);
Map<String, Integer> paramsType = new HashMap<>();
Map<String, String> paramsDesc = new HashMap<>();
for (Element field : element.getEnclosedElements()) {
if (field.getKind().isField() && field.getAnnotation(Autowired.class) != null) {
Autowired paramConfig = field.getAnnotation(Autowired.class);
paramsType.put(StringUtils.isEmpty(paramConfig.name()) ? field.getSimpleName().toString() : paramConfig.name(), typeUtils.typeExchange(field));
paramsDesc.put(StringUtils.isEmpty(paramConfig.name()) ? field.getSimpleName().toString() : paramConfig.name(), typeUtils.typeDesc(field));
}
}
node.setParamsType(paramsType);
node.setParamsDesc(paramsDesc);
if (!routerNodes.contains(node)) {
routerNodes.add(node);
}
} else {
throw new IllegalStateException("only activity can be annotated by RouteNode");
}
}
}
use of com.luojilab.router.facade.model.Node in project ChidoriX by Chihiro23333.
the class RouterProcessor method generateInitMapMethod.
/**
* create init map method
*/
private MethodSpec generateInitMapMethod() {
TypeName returnType = TypeName.VOID;
MethodSpec.Builder openUriMethodSpecBuilder = MethodSpec.methodBuilder("initMap").returns(returnType).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC);
openUriMethodSpecBuilder.addStatement("super.initMap()");
for (Node node : routerNodes) {
openUriMethodSpecBuilder.addStatement(mRouteMapperFieldName + ".put($S,$T.class)", node.getPath(), ClassName.get((TypeElement) node.getRawType()));
// Make map body for paramsType
StringBuilder mapBodyBuilder = new StringBuilder();
Map<String, Integer> paramsType = node.getParamsType();
if (MapUtils.isNotEmpty(paramsType)) {
for (Map.Entry<String, Integer> types : paramsType.entrySet()) {
mapBodyBuilder.append("put(\"").append(types.getKey()).append("\", ").append(types.getValue()).append("); ");
}
}
String mapBody = mapBodyBuilder.toString();
logger.info(">>> mapBody: " + mapBody + " <<<");
if (!StringUtils.isEmpty(mapBody)) {
openUriMethodSpecBuilder.addStatement(mParamsMapperFieldName + ".put($T.class," + "new java.util.HashMap<String, Integer>(){{" + mapBody + "}}" + ")", ClassName.get((TypeElement) node.getRawType()));
}
}
return openUriMethodSpecBuilder.build();
}
use of com.luojilab.router.facade.model.Node in project ChidoriX by Chihiro23333.
the class RouterProcessor method generateRouterTable.
/**
* generate HostRouterTable.txt
*/
private void generateRouterTable() {
String fileName = RouteUtils.genRouterTable(host);
if (FileUtils.createFile(fileName)) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("auto generated, do not change !!!! \n\n");
stringBuilder.append("HOST : " + host + "\n\n");
for (Node node : routerNodes) {
stringBuilder.append(node.getDesc() + "\n");
stringBuilder.append(node.getPath() + "\n");
Map<String, String> paramsType = node.getParamsDesc();
if (MapUtils.isNotEmpty(paramsType)) {
for (Map.Entry<String, String> types : paramsType.entrySet()) {
stringBuilder.append(types.getKey() + ":" + types.getValue() + "\n");
}
}
stringBuilder.append("\n");
}
FileUtils.writeStringToFile(fileName, stringBuilder.toString(), false);
}
}
Aggregations