Skip to content

Commit 6624553

Browse files
committed
[anno] [add check logic for path]
1 parent 188e3a7 commit 6624553

File tree

2 files changed

+39
-91
lines changed

2 files changed

+39
-91
lines changed

router-anno-compiler/src/main/java/com/ljsw/router/compiler/processor/RouterProcessor.java

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ public class RouterProcessor extends AbstractProcessor {
7171
private Types types;
7272
private Elements elements;
7373

74+
/**
75+
* (group,hostInfo)
76+
*/
7477
private Map<String, HostInfo> routers;
78+
/**
79+
* (group,List(Node))
80+
*/
7581
private Map<String, List<Node>> routerNodes;
7682

7783
private TypeMirror type_TextUtils;
@@ -141,58 +147,6 @@ public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnv
141147
return false;
142148
}
143149

144-
// @Deprecated
145-
// private void write2Util() {
146-
// Set<String> groups = routers.keySet();
147-
//
148-
// ClassName type_UiRouterLoader = ClassName.get(elements.getTypeElement(TYPE_UIROUTER_LOADER));
149-
// for (String group : groups) {
150-
// logger.info(">>> write for group:" + group);
151-
//
152-
// TypeMirror value = routers.get(group);
153-
// String path = value.toString();
154-
//
155-
// String pkg = path.substring(0, path.lastIndexOf("."));
156-
// String cn = path.substring(path.lastIndexOf(".") + 1) + "Loader";
157-
//
158-
// ParameterizedTypeName inputMapTypeOfGroup = ParameterizedTypeName.get(
159-
// ClassName.get(Map.class),
160-
// ClassName.get(String.class),
161-
// ClassName.get(Class.class)
162-
// );
163-
//
164-
// ParameterSpec groupParamSpec =
165-
// ParameterSpec.builder(inputMapTypeOfGroup, "mapper").build();
166-
//
167-
//
168-
// MethodSpec.Builder loadIntoMethodOfRootBuilder =
169-
// MethodSpec.methodBuilder(ROUTER_UTIL_METHOD_ADDTO)
170-
// .addParameter(groupParamSpec)
171-
// .addAnnotation(Override.class)
172-
// .addModifiers(PUBLIC);
173-
//
174-
// List<Node> nodes = routerNodes.get(group);
175-
// for (Node node : nodes) {
176-
// loadIntoMethodOfRootBuilder.addStatement(
177-
// "mapper.put($S,$T.class)",
178-
// node.getPath(),
179-
// ClassName.get((TypeElement) node.getRawType()));
180-
// }
181-
//
182-
//
183-
// try {
184-
// JavaFile.builder(pkg, TypeSpec.classBuilder(cn)
185-
// .addModifiers(PUBLIC)
186-
// .addSuperinterface(type_UiRouterLoader)
187-
// .addMethod(loadIntoMethodOfRootBuilder.build())
188-
// .build()
189-
// ).build().writeTo(mFiler);
190-
// } catch (IOException e) {
191-
// e.printStackTrace();
192-
// }
193-
// }
194-
// }
195-
196150
private void generateRouterImpl() {
197151
Set<String> groups = routers.keySet();
198152

@@ -255,6 +209,8 @@ private void parseRouteNodes(Set<? extends Element> routeElements) {
255209

256210
TypeMirror type_Activity = elements.getTypeElement(ACTIVITY).asType();
257211

212+
Map<String, List<String>> cacheForConflictCheck = new HashMap<>();
213+
258214
for (Element element : routeElements) {
259215
TypeMirror tm = element.asType();
260216
RouteNode route = element.getAnnotation(RouteNode.class);
@@ -263,9 +219,23 @@ private void parseRouteNodes(Set<? extends Element> routeElements) {
263219
logger.info(">>> Found activity route: " + tm.toString() + " <<<");
264220

265221
String group = route.group();
222+
List<String> groupPaths;
223+
224+
if (cacheForConflictCheck.containsKey(group)) {
225+
groupPaths = cacheForConflictCheck.get(group);
226+
} else {
227+
groupPaths = new ArrayList<>();
228+
cacheForConflictCheck.put(group, groupPaths);
229+
}
266230

267231
Node node = new Node();
268-
node.setPath(route.path());
232+
String path = route.path();
233+
234+
checkPath(group, path, groupPaths);
235+
236+
groupPaths.add(path);
237+
238+
node.setPath(path);
269239
node.setPriority(route.priority());
270240
node.setNodeType(NodeType.ACTIVITY);
271241
node.setRawType(element);
@@ -283,6 +253,21 @@ private void parseRouteNodes(Set<? extends Element> routeElements) {
283253
}
284254
}
285255

256+
private void checkPath(String group, String path, List<String> groupPaths) {
257+
if (groupPaths.contains(path))
258+
throw new IllegalStateException("conflict path in group:" + group + ",path is:" + path);
259+
260+
if (path == null || path.isEmpty() || !path.startsWith("/"))
261+
throw new IllegalArgumentException("path cannot be null or empty,and should start with /,this is:" + path);
262+
263+
if (path.contains("//") || path.contains("&") || path.contains("?"))
264+
throw new IllegalArgumentException("path should not contain // ,& or ?,this is:" + path);
265+
266+
if (path.endsWith("/"))
267+
throw new IllegalArgumentException("path should not endWith /,this is:" + path
268+
+ ";or append a token:index");
269+
}
270+
286271
private void foundRouters(Set<? extends Element> routers) {
287272
for (Element element : routers) {
288273
Router router = element.getAnnotation(Router.class);
@@ -517,4 +502,5 @@ private MethodSpec generateVerify() {
517502
return openUriMethodSpecBuilder.build();
518503
}
519504

505+
520506
}
Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.ljsw.router.compiler.utils;
22

33
import com.ljsw.router.compiler.model.MethodInfo;
4-
import com.squareup.javapoet.ClassName;
54
import com.squareup.javapoet.ParameterSpec;
65
import com.squareup.javapoet.ParameterizedTypeName;
76
import com.squareup.javapoet.TypeName;
87

98
import java.util.ArrayList;
10-
import java.util.Iterator;
119
import java.util.List;
1210

1311
import javax.lang.model.element.Element;
@@ -48,17 +46,6 @@ public static String getSimpleName(Element element) {
4846
return element.getSimpleName().toString();
4947
}
5048

51-
// public static ParameterSpec generateMethodParameterSpec(String clzPkg,
52-
// String clzSimpleName,
53-
// String paramName) {
54-
// ClassName cn = ClassName.get(clzPkg, clzSimpleName);
55-
//
56-
// ParameterizedTypeName ptn =
57-
// ParameterizedTypeName.get(cn);
58-
//
59-
// return ParameterSpec.builder(ptn, paramName).build();
60-
// }
61-
6249
public static ParameterSpec generateMethodParameterSpec(TypeMirror typeMirror,
6350
String paramName) {
6451
TypeName tn =
@@ -67,30 +54,5 @@ public static ParameterSpec generateMethodParameterSpec(TypeMirror typeMirror,
6754
return ParameterSpec.builder(tn, paramName).build();
6855
}
6956

70-
// public static ParameterSpec generateMethodParameterSpec(Class clz,
71-
// String paramName) {
72-
// ClassName cn = ClassName.get(clz);
73-
//
74-
// ParameterizedTypeName ptn =
75-
// ParameterizedTypeName.get(cn);
76-
//
77-
// return ParameterSpec.builder(ptn, paramName).build();
78-
// }
7957

80-
// public static String join(Iterator iterator, String separator) {
81-
// if (separator == null) {
82-
// separator = "";
83-
// }
84-
//
85-
// StringBuilder buf = new StringBuilder(256);
86-
//
87-
// while (iterator.hasNext()) {
88-
// buf.append(iterator.next());
89-
// if (iterator.hasNext()) {
90-
// buf.append(separator);
91-
// }
92-
// }
93-
//
94-
// return buf.toString();
95-
// }
9658
}

0 commit comments

Comments
 (0)