Search in sources :

Example 1 with RouteMeta

use of com.xsm.easy.annotation.modle.RouteMeta in project EasyRouter by Xiasm.

the class RouterProcessor method processorRoute.

private void processorRoute(Set<? extends Element> rootElements) {
    // 获得Activity这个类的节点信息
    TypeElement activity = elementUtils.getTypeElement(Constant.ACTIVITY);
    TypeElement service = elementUtils.getTypeElement(Constant.ISERVICE);
    for (Element element : rootElements) {
        RouteMeta routeMeta;
        // 类信息
        TypeMirror typeMirror = element.asType();
        log.i("Route class:" + typeMirror.toString());
        Route route = element.getAnnotation(Route.class);
        if (typeUtils.isSubtype(typeMirror, activity.asType())) {
            routeMeta = new RouteMeta(RouteMeta.Type.ACTIVITY, route, element);
        } else if (typeUtils.isSubtype(typeMirror, service.asType())) {
            routeMeta = new RouteMeta(RouteMeta.Type.ISERVICE, route, element);
        } else {
            throw new RuntimeException("Just support Activity or IService Route: " + element);
        }
        categories(routeMeta);
    }
    TypeElement iRouteGroup = elementUtils.getTypeElement(Constant.IROUTE_GROUP);
    TypeElement iRouteRoot = elementUtils.getTypeElement(Constant.IROUTE_ROOT);
    // 生成Group记录分组表
    generatedGroup(iRouteGroup);
    // 生成Root类 作用:记录<分组,对应的Group类>
    generatedRoot(iRouteRoot, iRouteGroup);
}
Also used : RouteMeta(com.xsm.easy.annotation.modle.RouteMeta) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) Route(com.xsm.easy.annotation.Route)

Example 2 with RouteMeta

use of com.xsm.easy.annotation.modle.RouteMeta in project EasyRouter by Xiasm.

the class RouterProcessor method generatedGroup.

private void generatedGroup(TypeElement iRouteGroup) {
    // 创建参数类型 Map<String, RouteMeta>
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), ClassName.get(RouteMeta.class));
    ParameterSpec altas = ParameterSpec.builder(parameterizedTypeName, "atlas").build();
    for (Map.Entry<String, List<RouteMeta>> entry : groupMap.entrySet()) {
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(Constant.METHOD_LOAD_INTO).addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(altas);
        String groupName = entry.getKey();
        List<RouteMeta> groupData = entry.getValue();
        for (RouteMeta routeMeta : groupData) {
            // 函数体的添加
            methodBuilder.addStatement("atlas.put($S,$T.build($T.$L,$T.class,$S,$S))", routeMeta.getPath(), ClassName.get(RouteMeta.class), ClassName.get(RouteMeta.Type.class), routeMeta.getType(), ClassName.get(((TypeElement) routeMeta.getElement())), routeMeta.getPath(), routeMeta.getGroup());
        }
        String groupClassName = Constant.NAME_OF_GROUP + groupName;
        TypeSpec typeSpec = TypeSpec.classBuilder(groupClassName).addSuperinterface(ClassName.get(iRouteGroup)).addModifiers(Modifier.PUBLIC).addMethod(methodBuilder.build()).build();
        JavaFile javaFile = JavaFile.builder(Constant.PACKAGE_OF_GENERATE_FILE, typeSpec).build();
        try {
            javaFile.writeTo(filerUtils);
        } catch (IOException e) {
            e.printStackTrace();
        }
        rootMap.put(groupName, groupClassName);
    }
}
Also used : RouteMeta(com.xsm.easy.annotation.modle.RouteMeta) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) IOException(java.io.IOException) JavaFile(com.squareup.javapoet.JavaFile) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

RouteMeta (com.xsm.easy.annotation.modle.RouteMeta)2 JavaFile (com.squareup.javapoet.JavaFile)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 ParameterSpec (com.squareup.javapoet.ParameterSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 Route (com.xsm.easy.annotation.Route)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1