Skip to content

Commit

Permalink
Merge branch '1.x/master' into 1.x/master-java8
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojinzi123 committed Apr 14, 2022
2 parents bfa56e2 + 11e2ba8 commit d4ae42f
Show file tree
Hide file tree
Showing 71 changed files with 470 additions and 169 deletions.
4 changes: 2 additions & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
@Retention(RetentionPolicy.CLASS)
public @interface RouterAnno {

/**
* 这个这个值设置了
* host 和 path 就不会起作用了
*/
String regex() default "";

/**
* 定义 scheme, 如果不写, 就是默认的
*/
String scheme() default "";

/**
* 定义host
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
public abstract class BaseHostProcessor extends BaseProcessor {

// 在每一个 module 中配置的 HOST 的信息
protected String componentHost = null;
protected String componentModuleName = null;

@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
Map<String, String> options = processingEnv.getOptions();
if (options != null) {
componentHost = options.get("HOST");
componentModuleName = options.get("ModuleName");
if (componentModuleName == null || componentModuleName.isEmpty()) {
componentModuleName = options.get("HOST");
}
}
if (componentHost == null || componentHost.isEmpty()) {
if (componentModuleName == null || componentModuleName.isEmpty()) {
throw NULLHOSTEXCEPTION;
}
/*boolean isMatch = componentHost.matches(ComponentConstants.HOST_REGEX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public synchronized void init(ProcessingEnvironment processingEnvironment) {

@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
if (componentHost == null || componentHost.isEmpty()) {
if (componentModuleName == null || componentModuleName.isEmpty()) {
return false;
}
if (CollectionUtils.isNotEmpty(set)) {
Expand Down Expand Up @@ -107,7 +107,7 @@ private void parseAnnotation(Set<? extends Element> annoElements) {

private void createImpl() {

String claName = ComponentUtil.genHostFragmentClassName(componentHost);
String claName = ComponentUtil.genHostFragmentClassName(componentModuleName);
//pkg
String pkg = claName.substring(0, claName.lastIndexOf('.'));
//simpleName
Expand Down Expand Up @@ -264,7 +264,7 @@ private MethodSpec generateInitHostMethod() {
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC);

openUriMethodSpecBuilder.addStatement("return $S", componentHost);
openUriMethodSpecBuilder.addStatement("return $S", componentModuleName);
return openUriMethodSpecBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void parseNormalInterceptAnnotation(Set<? extends Element> normalInterce
}

private void createImpl() {
String claName = com.xiaojinzi.component.ComponentUtil.genHostInterceptorClassName(componentHost);
String claName = com.xiaojinzi.component.ComponentUtil.genHostInterceptorClassName(componentModuleName);
//pkg
String pkg = claName.substring(0, claName.lastIndexOf('.'));
//simpleName
Expand Down Expand Up @@ -242,7 +242,7 @@ private MethodSpec generateInitHostMethod() {
.returns(returnType)
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC);
openUriMethodSpecBuilder.addStatement("return $S", componentHost);
openUriMethodSpecBuilder.addStatement("return $S", componentModuleName);
return openUriMethodSpecBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ private void parseAnnotation(Set<? extends Element> moduleAppElements) {
private void createImpl(boolean isDefault) {

String claName = isDefault ?
ComponentUtil.genDefaultHostModuleApplicationClassName(componentHost) :
ComponentUtil.genHostModuleApplicationClassName(componentHost);
ComponentUtil.genDefaultHostModuleApplicationClassName(componentModuleName) :
ComponentUtil.genHostModuleApplicationClassName(componentModuleName);

String classJavaDoc = null;
if (isDefault) {
Expand Down Expand Up @@ -136,8 +136,6 @@ private void createImpl(boolean isDefault) {
.addMethod(initHostMethod)
.addMethod(getPriorityMethod)
.addMethod(initMapMethod)
.addMethod(onCreateMethod)
.addMethod(onDestroyMethod)
.addJavadoc(classJavaDoc)
.build();
try {
Expand Down Expand Up @@ -182,7 +180,7 @@ private MethodSpec generateOnCreateMethod() {
.addModifiers(Modifier.PUBLIC);
methodSpecBuilder.addStatement("super.onCreate(application)");

methodSpecBuilder.addStatement("$T.getInstance().register(getHost())", centerServiceTypeElement);
/*methodSpecBuilder.addStatement("$T.getInstance().register(getHost())", centerServiceTypeElement);
MethodSpec.Builder runnableMethodBuilder = MethodSpec
.methodBuilder("run")
Expand All @@ -206,7 +204,7 @@ private MethodSpec generateOnCreateMethod() {
methodSpecBuilder.endControlFlow();
methodSpecBuilder.beginControlFlow("else");
methodSpecBuilder.addStatement("r.run()");
methodSpecBuilder.endControlFlow();
methodSpecBuilder.endControlFlow();*/

return methodSpecBuilder.build();
}
Expand All @@ -219,13 +217,13 @@ private MethodSpec generateOnDestroyMethod() {
.addModifiers(Modifier.PUBLIC);

methodSpecBuilder.addStatement("super.onDestroy()");
methodSpecBuilder.addStatement("$T.getInstance().unregister(getHost())", centerServiceTypeElement);
/*methodSpecBuilder.addStatement("$T.getInstance().unregister(getHost())", centerServiceTypeElement);
methodSpecBuilder.addStatement("$T.getInstance().unregister(getHost())", routerCenterTypeElement);
methodSpecBuilder.addStatement("$T.getInstance().unregister(getHost())", centerInterceptorTypeElement);
methodSpecBuilder.addStatement("$T.getInstance().unregister(getHost())", centerRouterDegradeTypeElement);
methodSpecBuilder.addStatement("$T.getInstance().unregister(getHost())", centerFragmentTypeElement);
methodSpecBuilder.addComment("清空缓存");
methodSpecBuilder.addStatement("$T.clear()", classCacheTypeElement);
methodSpecBuilder.addStatement("$T.clear()", classCacheTypeElement);*/
return methodSpecBuilder.build();
}

Expand All @@ -236,7 +234,7 @@ private MethodSpec generateInitHostMethod() {
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC);

openUriMethodSpecBuilder.addStatement("return $S", componentHost);
openUriMethodSpecBuilder.addStatement("return $S", componentModuleName);
return openUriMethodSpecBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private void parseRouterDegradeAnnotation(Set<? extends Element> routerDegradeEl
}

private void createImpl() {
String claName = ComponentUtil.genHostRouterDegradeClassName(componentHost);
String claName = ComponentUtil.genHostRouterDegradeClassName(componentModuleName);
//pkg
String pkg = claName.substring(0, claName.lastIndexOf('.'));
//simpleName
Expand All @@ -106,7 +106,7 @@ private void createImpl() {
.addAnnotation(mClassNameComponentGeneratedAnno)
.addModifiers(Modifier.PUBLIC)
.addModifiers(Modifier.FINAL)
.addJavadoc(componentHost + "路由降级的模块\n")
.addJavadoc(componentModuleName + "路由降级的模块\n")
.superclass(superClass)
.addMethod(initHostMethod)
.addMethod(initListMethod)
Expand All @@ -128,7 +128,7 @@ private MethodSpec generateInitHostMethod() {
.returns(returnType)
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC);
openUriMethodSpecBuilder.addStatement("return $S", componentHost);
openUriMethodSpecBuilder.addStatement("return $S", componentModuleName);
return openUriMethodSpecBuilder.build();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
Expand Down Expand Up @@ -97,7 +99,9 @@ public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnv
return false;
}

private final Map<String, RouterAnnoBean> routerMap = new HashMap<>();
private final String checkScheme = "__checkScheme__";
private final Set<String> routerCheckMap = new HashSet<>();
private final Set<RouterAnnoBean> routerMap = new HashSet<>();

/**
* 解析注解
Expand All @@ -111,11 +115,24 @@ private void parseAnno(Set<? extends Element> routeElements) {
continue;
}
final RouterAnnoBean routerBean = toRouterAnnoBean(element, router);
// 如果重复就抛出异常
if (routerMap.containsKey(routerBean.hostAndPath())) {
throw new ProcessException("the url value '" + routerBean.hostAndPath() + "' of " + element + " is alreay exist");
String scheme = routerBean.getScheme();
if (scheme == null || "".equals(scheme)) {
scheme = checkScheme;
}
String checkKey = scheme + "://" + routerBean.hostAndPath();
if (routerCheckMap.contains(checkKey)) {
if (checkScheme.equals(scheme)) {
throw new ProcessException("the uri '" + routerBean.hostAndPath() + "' of " + element + " is already exist");
} else {
throw new ProcessException("the uri '" + checkKey + "' of " + element + " is already exist");
}
}
routerMap.put(routerBean.hostAndPath(), routerBean);
routerCheckMap.add(checkKey);
// 如果重复就抛出异常, 加上 scheme 之后, 这个逻辑不成立了, 可能相同的 hostAndPath, 但是 scheme 不同
/*if (routerMap.containsKey(routerBean.hostAndPath())) {
throw new ProcessException("the url value '" + routerBean.hostAndPath() + "' of " + element + " is alreay exist");
}*/
routerMap.add(routerBean);
}
}

Expand All @@ -127,6 +144,9 @@ private RouterAnnoBean toRouterAnnoBean(Element element, RouterAnno routerAnno)
}

final RouterAnnoBean routerBean = new RouterAnnoBean();
routerBean.setRegex(routerAnno.regex());
routerBean.setScheme(routerAnno.scheme());

String host = routerAnno.host();
String path = routerAnno.path();

Expand All @@ -144,7 +164,7 @@ private RouterAnnoBean toRouterAnnoBean(Element element, RouterAnno routerAnno)
}
// 如果用户 host 没填
if (host == null || host.isEmpty()) {
host = componentHost;
host = componentModuleName;
}
// 如果 path 没有 / 开头,会自动加一个
if (path != null && path.length() > 0 && path.charAt(0) != '/') {
Expand Down Expand Up @@ -176,9 +196,9 @@ private RouterAnnoBean toRouterAnnoBean(Element element, RouterAnno routerAnno)
}

private void adjustInterceptorPriorities() {
routerMap.forEach(new BiConsumer<String, RouterAnnoBean>() {
routerMap.forEach(new Consumer<RouterAnnoBean>() {
@Override
public void accept(String s, RouterAnnoBean value) {
public void accept(RouterAnnoBean value) {

int totalSize = 0;

Expand Down Expand Up @@ -238,7 +258,7 @@ public void accept(String s, RouterAnnoBean value) {
* 生成路由
*/
private void createRouterImpl() {
final String claName = ComponentUtil.genHostRouterClassName(componentHost);
final String claName = ComponentUtil.genHostRouterClassName(componentModuleName);
//pkg
final String pkg = claName.substring(0, claName.lastIndexOf('.'));
//simpleName
Expand All @@ -255,7 +275,7 @@ private void createRouterImpl() {
.superclass(superClass)
.addMethod(initHostMethod)
.addMethod(initMapMethod)
.addJavadoc(componentHost + "业务模块的路由表\n")
.addJavadoc(componentModuleName + "业务模块的路由表\n")
.build();
try {
JavaFile.builder(pkg, typeSpec)
Expand All @@ -280,8 +300,7 @@ private void createRouterJson() throws IOException {

int size = routerMap.size();
List<RouterDocBean> result = new ArrayList<>(size);
for (Map.Entry<String, RouterAnnoBean> entry : routerMap.entrySet()) {
RouterAnnoBean routerAnnoBean = entry.getValue();
for (RouterAnnoBean routerAnnoBean : routerMap) {
RouterDocBean item = new RouterDocBean();
item.setHost(routerAnnoBean.getHost());
item.setPath(routerAnnoBean.getPath());
Expand All @@ -307,7 +326,7 @@ private void createRouterJson() throws IOException {
if (!routerDocJsonFolder.exists()) {
routerDocJsonFolder.mkdirs();
}
File file = new File(routerDocJsonFolder, componentHost + ".json");
File file = new File(routerDocJsonFolder, componentModuleName + ".json");
if (file.exists()) {
file.delete();
}
Expand All @@ -324,9 +343,11 @@ private MethodSpec generateInitMapMethod() {
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC);
initMapMethodSpecBuilder.addStatement("super.initMap()");
routerMap.forEach(new BiConsumer<String, RouterAnnoBean>() {
initMapMethodSpecBuilder.addComment("默认的 scheme");
initMapMethodSpecBuilder.addStatement("String defaultScheme = com.xiaojinzi.component.Component.getConfig().getDefaultScheme()");
routerMap.forEach(new Consumer<RouterAnnoBean>() {
@Override
public void accept(String key, RouterAnnoBean routerBean) {
public void accept(RouterAnnoBean routerBean) {
// 生成变量的名字,每一个变量代表每一个目标界面的配置对象
String routerBeanName = "routerBean" + atomicInteger.incrementAndGet();
// 生成 Activity 的调用代码
Expand Down Expand Up @@ -366,8 +387,23 @@ public void accept(String key, RouterAnnoBean routerBean) {
);
}
}
// 存进 map 集合的代码
initMapMethodSpecBuilder.addStatement("routerBeanMap.put($S,$N)", key, routerBeanName);
String regexStr = routerBean.getRegex();
// 如果不是正则匹配
if (regexStr == null || "".equals(regexStr)) {
String scheme = routerBean.getScheme();
// 存进 map 集合的代码
if (scheme == null || "".equals(scheme)) {
initMapMethodSpecBuilder.addStatement(
"routerBeanMap.put(defaultScheme + $S,$N)",
"://" + routerBean.hostAndPath(), routerBeanName
);
} else {
String key = scheme + "://" + routerBean.hostAndPath();
initMapMethodSpecBuilder.addStatement("routerBeanMap.put($S,$N)", key, routerBeanName);
}
} else {
initMapMethodSpecBuilder.addStatement("regExRouterBeanMap.put($S,$N)", regexStr, routerBeanName);
}
initMapMethodSpecBuilder.addCode("\n");
}
});
Expand All @@ -383,7 +419,7 @@ private MethodSpec generateInitHostMethod() {
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC);

openUriMethodSpecBuilder.addStatement("return $S", componentHost);
openUriMethodSpecBuilder.addStatement("return $S", componentModuleName);
return openUriMethodSpecBuilder.build();

}
Expand Down Expand Up @@ -411,7 +447,7 @@ private String getHostAndPathFromAnno(RouterAnno anno) {
}
// 如果用户 host 没填
if (host == null || host.isEmpty()) {
host = componentHost;
host = componentModuleName;
}
// 如果 path 没有 / 开头,会自动加一个
if (path != null && path.length() > 0 && path.charAt(0) != '/') {
Expand Down
Loading

0 comments on commit d4ae42f

Please sign in to comment.