|
| 1 | +package com.ljsw.router.compiler.processor; |
| 2 | + |
| 3 | +import com.google.auto.service.AutoService; |
| 4 | +import com.ljsw.router.compiler.utils.Logger; |
| 5 | +import com.ljsw.router.facade.annotation.RouteNode; |
| 6 | +import com.ljsw.router.facade.annotation.Router; |
| 7 | +import com.ljsw.router.facade.enums.NodeType; |
| 8 | +import com.ljsw.router.facade.model.Node; |
| 9 | +import com.squareup.javapoet.ClassName; |
| 10 | +import com.squareup.javapoet.JavaFile; |
| 11 | +import com.squareup.javapoet.MethodSpec; |
| 12 | +import com.squareup.javapoet.ParameterSpec; |
| 13 | +import com.squareup.javapoet.ParameterizedTypeName; |
| 14 | +import com.squareup.javapoet.TypeSpec; |
| 15 | + |
| 16 | +import org.apache.commons.collections4.CollectionUtils; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import javax.annotation.processing.AbstractProcessor; |
| 26 | +import javax.annotation.processing.Filer; |
| 27 | +import javax.annotation.processing.ProcessingEnvironment; |
| 28 | +import javax.annotation.processing.Processor; |
| 29 | +import javax.annotation.processing.RoundEnvironment; |
| 30 | +import javax.annotation.processing.SupportedAnnotationTypes; |
| 31 | +import javax.annotation.processing.SupportedOptions; |
| 32 | +import javax.annotation.processing.SupportedSourceVersion; |
| 33 | +import javax.lang.model.SourceVersion; |
| 34 | +import javax.lang.model.element.Element; |
| 35 | +import javax.lang.model.element.TypeElement; |
| 36 | +import javax.lang.model.type.MirroredTypeException; |
| 37 | +import javax.lang.model.type.TypeMirror; |
| 38 | +import javax.lang.model.util.Elements; |
| 39 | +import javax.lang.model.util.Types; |
| 40 | + |
| 41 | +import static com.ljsw.router.compiler.utils.Constants.ACTIVITY; |
| 42 | +import static com.ljsw.router.compiler.utils.Constants.ANNOTATION_TYPE_ROUTER; |
| 43 | +import static com.ljsw.router.compiler.utils.Constants.ANNOTATION_TYPE_ROUTE_NODE; |
| 44 | +import static com.ljsw.router.compiler.utils.Constants.KEY_MODULE_NAME; |
| 45 | +import static com.ljsw.router.compiler.utils.Constants.ROUTER_UTIL_METHOD_ADDTO; |
| 46 | +import static com.ljsw.router.compiler.utils.Constants.TYPE_UIROUTER_LOADER; |
| 47 | +import static javax.lang.model.element.Modifier.PUBLIC; |
| 48 | + |
| 49 | +/** |
| 50 | + * <p><b>Package:</b> com.ljsw.router.compiler.processor </p> |
| 51 | + * <p><b>Project:</b> DDComponentForAndroid </p> |
| 52 | + * <p><b>Classname:</b> RouterProcessor </p> |
| 53 | + * <p><b>Description:</b> generate RouterLoader class for 'Router' annotated class, |
| 54 | + * parse 'RouteNode' annotated Activities to mapper |
| 55 | + * </p> |
| 56 | + * Created by leobert on 2017/9/18. |
| 57 | + */ |
| 58 | +@AutoService(Processor.class) |
| 59 | +@SupportedOptions(KEY_MODULE_NAME) |
| 60 | +@SupportedSourceVersion(SourceVersion.RELEASE_7) |
| 61 | +@SupportedAnnotationTypes({ANNOTATION_TYPE_ROUTE_NODE, ANNOTATION_TYPE_ROUTER}) |
| 62 | +public class RouterProcessor extends AbstractProcessor { |
| 63 | + private Logger logger; |
| 64 | + |
| 65 | + private Filer mFiler; // File util, write class file into disk. |
| 66 | + private Types types; |
| 67 | + private Elements elements; |
| 68 | + |
| 69 | + private Map<String, TypeMirror> routers; |
| 70 | + private Map<String, List<Node>> routerNodes; |
| 71 | + |
| 72 | + @Override |
| 73 | + public synchronized void init(ProcessingEnvironment processingEnv) { |
| 74 | + super.init(processingEnv); |
| 75 | + |
| 76 | + routers = new HashMap<>(); |
| 77 | + routerNodes = new HashMap<>(); |
| 78 | + |
| 79 | + mFiler = processingEnv.getFiler(); // Generate class. |
| 80 | + types = processingEnv.getTypeUtils(); // Get type utils. |
| 81 | + elements = processingEnv.getElementUtils(); // Get class meta. |
| 82 | + |
| 83 | + logger = new Logger(processingEnv.getMessager()); // Package the log utils. |
| 84 | + |
| 85 | + logger.info(">>> RouteProcessor init. <<<"); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { |
| 90 | + if (CollectionUtils.isNotEmpty(set)) { |
| 91 | + Set<? extends Element> routeService = |
| 92 | + roundEnvironment.getElementsAnnotatedWith(Router.class); |
| 93 | + try { |
| 94 | + logger.info(">>> Found routeService <<<"); |
| 95 | + this.foundRouters(routeService); |
| 96 | + } catch (Exception e) { |
| 97 | + logger.error(e); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + Set<? extends Element> routeNodes = roundEnvironment.getElementsAnnotatedWith(RouteNode.class); |
| 102 | + try { |
| 103 | + logger.info(">>> Found routes, start... <<<"); |
| 104 | + this.parseRouteNodes(routeNodes); |
| 105 | + } catch (Exception e) { |
| 106 | + logger.error(e); |
| 107 | + } |
| 108 | + |
| 109 | + this.write2Util(); |
| 110 | + |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + private void write2Util() { |
| 118 | + Set<String> groups = routers.keySet(); |
| 119 | + |
| 120 | + ClassName type_UiRouterLoader = ClassName.get(elements.getTypeElement(TYPE_UIROUTER_LOADER)); |
| 121 | + for (String group : groups) { |
| 122 | + logger.info(">>> write for group:" + group); |
| 123 | + |
| 124 | + TypeMirror value = routers.get(group); |
| 125 | + String path = value.toString(); |
| 126 | + |
| 127 | + String pkg = path.substring(0, path.lastIndexOf(".")); |
| 128 | + String cn = path.substring(path.lastIndexOf(".") + 1) + "Loader"; |
| 129 | + |
| 130 | + ParameterizedTypeName inputMapTypeOfGroup = ParameterizedTypeName.get( |
| 131 | + ClassName.get(Map.class), |
| 132 | + ClassName.get(String.class), |
| 133 | + ClassName.get(Class.class) |
| 134 | + ); |
| 135 | + |
| 136 | + ParameterSpec groupParamSpec = |
| 137 | + ParameterSpec.builder(inputMapTypeOfGroup, "mapper").build(); |
| 138 | + |
| 139 | + |
| 140 | + MethodSpec.Builder loadIntoMethodOfRootBuilder = |
| 141 | + MethodSpec.methodBuilder(ROUTER_UTIL_METHOD_ADDTO) |
| 142 | + .addParameter(groupParamSpec) |
| 143 | + .addAnnotation(Override.class) |
| 144 | + .addModifiers(PUBLIC); |
| 145 | + |
| 146 | + List<Node> nodes = routerNodes.get(group); |
| 147 | + for (Node node : nodes) { |
| 148 | + loadIntoMethodOfRootBuilder.addStatement( |
| 149 | + "mapper.put($S,$T.class)", |
| 150 | + node.getPath(), |
| 151 | + ClassName.get((TypeElement) node.getRawType())); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + try { |
| 156 | + JavaFile.builder(pkg, TypeSpec.classBuilder(cn) |
| 157 | + .addModifiers(PUBLIC) |
| 158 | + .addSuperinterface(type_UiRouterLoader) |
| 159 | + .addMethod(loadIntoMethodOfRootBuilder.build()) |
| 160 | + .build() |
| 161 | + ).build().writeTo(mFiler); |
| 162 | + } catch (IOException e) { |
| 163 | + e.printStackTrace(); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private void parseRouteNodes(Set<? extends Element> routeElements) { |
| 169 | + |
| 170 | + TypeMirror type_Activity = elements.getTypeElement(ACTIVITY).asType(); |
| 171 | + |
| 172 | + for (Element element : routeElements) { |
| 173 | + TypeMirror tm = element.asType(); |
| 174 | + RouteNode route = element.getAnnotation(RouteNode.class); |
| 175 | + |
| 176 | + if (types.isSubtype(tm, type_Activity)) { // Activity |
| 177 | + logger.info(">>> Found activity route: " + tm.toString() + " <<<"); |
| 178 | + |
| 179 | + String group = route.group(); |
| 180 | + |
| 181 | + Node node = new Node(); |
| 182 | + node.setPath(route.path()); |
| 183 | + node.setPriority(route.priority()); |
| 184 | + node.setNodeType(NodeType.ACTIVITY); |
| 185 | + node.setRawType(element); |
| 186 | + |
| 187 | + if (routerNodes.containsKey(group)) { |
| 188 | + routerNodes.get(group).add(node); |
| 189 | + } else { |
| 190 | + ArrayList<Node> nodes = new ArrayList<>(); |
| 191 | + nodes.add(node); |
| 192 | + routerNodes.put(group, nodes); |
| 193 | + } |
| 194 | + } else { |
| 195 | + throw new IllegalStateException("only activity can be annotated by RouteNode"); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private void foundRouters(Set<? extends Element> routers) { |
| 201 | + for (Element element : routers) { |
| 202 | + Router router = element.getAnnotation(Router.class); |
| 203 | + String group = router.group(); |
| 204 | + if (this.routers.containsKey(group)) |
| 205 | + throw new IllegalStateException("duplicated group at annotation," + |
| 206 | + "please check group:" + group); |
| 207 | + |
| 208 | + //use exception to get path |
| 209 | + TypeMirror value = null; |
| 210 | + try { |
| 211 | + router.classPath(); |
| 212 | + } catch (MirroredTypeException mte) { |
| 213 | + value = mte.getTypeMirror(); |
| 214 | + } |
| 215 | + |
| 216 | + this.routers.put(group, value); |
| 217 | + } |
| 218 | + } |
| 219 | +} |
0 commit comments