Search in sources :

Example 1 with Node

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");
        }
    }
}
Also used : Autowired(com.luojilab.router.facade.annotation.Autowired) TypeMirror(javax.lang.model.type.TypeMirror) HashMap(java.util.HashMap) RouteNode(com.luojilab.router.facade.annotation.RouteNode) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) Node(com.luojilab.router.facade.model.Node) RouteNode(com.luojilab.router.facade.annotation.RouteNode)

Example 2 with Node

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();
}
Also used : TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) Node(com.luojilab.router.facade.model.Node) RouteNode(com.luojilab.router.facade.annotation.RouteNode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Node

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);
    }
}
Also used : Node(com.luojilab.router.facade.model.Node) RouteNode(com.luojilab.router.facade.annotation.RouteNode) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RouteNode (com.luojilab.router.facade.annotation.RouteNode)3 Node (com.luojilab.router.facade.model.Node)3 HashMap (java.util.HashMap)3 Map (java.util.Map)2 TypeElement (javax.lang.model.element.TypeElement)2 Autowired (com.luojilab.router.facade.annotation.Autowired)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 TypeName (com.squareup.javapoet.TypeName)1 Element (javax.lang.model.element.Element)1 TypeMirror (javax.lang.model.type.TypeMirror)1