Skip to content

Commit 942d1ff

Browse files
1071213885@qq.com1071213885@qq.com
authored andcommitted
update review code
1 parent 11bd6ab commit 942d1ff

File tree

7 files changed

+53
-39
lines changed

7 files changed

+53
-39
lines changed

.idea/gradle.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FactoryBox/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
apply plugin: 'java-library'
2-
version '1.0.0'
2+
version '1.0.1'
33

44
jar {
55
manifest {

FactoryBox/src/main/java/com/owant/compiler/create/FactoryTemp.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,22 @@ public static void create(ProcessingEnvironment processingEnvironment,
4242
int size = productChildes.size();
4343
for (int i = 0; i < size; i++) {
4444
Product product = productChildes.get(i);
45+
String productKey = product.key;
46+
String productClassName = product.className;
47+
String productClassSimpleName = product.className
48+
.substring(productClassName.lastIndexOf(".") + 1);
49+
String productPackageName = productClassName
50+
.substring(0, productClassName.lastIndexOf("."));
51+
4552
if (i == 0) {
4653
methodCreateBuilder
47-
.beginControlFlow("if($S.equals(key))", product.key)
48-
.addStatement("products.put(key , new $T())",ClassName.get(packageName, product.className));
54+
.beginControlFlow("if($S.equals(key))", productKey)
55+
.addStatement("products.put(key , new $T())",
56+
ClassName.get(productPackageName, productClassSimpleName));
4957
} else {
50-
methodCreateBuilder.nextControlFlow("else if($S.equals(key))", product.key)
51-
.addStatement("products.put(key , new $T())",ClassName.get(packageName, product.className));
58+
methodCreateBuilder.nextControlFlow("else if($S.equals(key))", productKey)
59+
.addStatement("products.put(key , new $T())",
60+
ClassName.get(productPackageName, productClassSimpleName));
5261
}
5362
}
5463

FactoryBox/src/main/java/com/owant/compiler/factorybox/FactoryBoxProcessor.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import javax.lang.model.SourceVersion;
2121
import javax.lang.model.element.Element;
2222
import javax.lang.model.element.TypeElement;
23+
import javax.lang.model.type.DeclaredType;
24+
import javax.lang.model.type.MirroredTypeException;
2325
import javax.lang.model.util.Elements;
2426
import javax.tools.Diagnostic;
2527

@@ -56,12 +58,22 @@ public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnv
5658
productTypes.clear();
5759
for (Element element : roundEnvironment.getElementsAnnotatedWith(FactoryBox.class)) {
5860

59-
String key = element.getAnnotation(FactoryBox.class).key();
60-
//获取注解的所有文本情况
61-
String annotationValue = element.getAnnotationMirrors().get(0).toString()
62-
.replaceAll("\"", "");
61+
FactoryBox factoryBoxAnnotation = element.getAnnotation(FactoryBox.class);
62+
63+
//产品检索和判断的key
64+
String key = factoryBoxAnnotation.key();
65+
6366
//实现的接口
64-
String interfaceName = productInterface("product", annotationValue);
67+
String interfaceName = "";
68+
try {
69+
Class<?> clazz = factoryBoxAnnotation.product();
70+
interfaceName = clazz.getCanonicalName();
71+
} catch (MirroredTypeException mte) {
72+
DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror();
73+
TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement();
74+
interfaceName = classTypeElement.getQualifiedName().toString();
75+
}
76+
6577
//被注解所在的包
6678
String productPackageName = mElementUtils.getPackageOf(element).getQualifiedName()
6779
.toString();
@@ -102,16 +114,6 @@ public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnv
102114

103115
}
104116

105-
public static String productInterface(String annotationClassKey, String context) {
106-
String productFatherName = "";
107-
String patternKey = String.format("%s=([^,\\s]*).class", annotationClassKey);
108-
Pattern pattern = Pattern.compile(patternKey);
109-
Matcher matcher = pattern.matcher(context);
110-
if (matcher.find()) {
111-
productFatherName = matcher.group(1);
112-
}
113-
return productFatherName;
114-
}
115117

116118
private void printlnProcessorInfo(String info) {
117119
mMessager.printMessage(Diagnostic.Kind.NOTE, info);

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
```
66
implementation 'com.owant.factorybox:factorybox:1.0.1'
7+
annotationProcessor 'com.google.dagger:dagger-compiler:2.2'
8+
79
```
810

911
使用google的auto-service和AbstractProcessor进行代码生成。看了不少讲解AbstractProcessor的教程,后来我一直在需要找其应用的实例。后来在项目中的一个配置文件经常改动想到了自动生成代码,于是考虑实践一下
@@ -50,33 +52,33 @@ import java.lang.String;
5052
import java.util.HashMap;
5153
5254
public final class EventFactory {
55+
5356
private final HashMap<String, Event> products;
5457
5558
public EventFactory() {
56-
this.products = new HashMap<String,Event>();
59+
this.products = new HashMap<String, Event>();
5760
}
5861
5962
public Event create(String key) throws Exception {
60-
Event cache=products.get(key);
61-
if(cache!=null) {
63+
Event cache = products.get(key);
64+
if (cache != null) {
6265
return cache;
6366
}
64-
if("sub_event".equals(key)) {
65-
products.put(key , new com.owant.createcode.sub.SubEvent());
66-
} else if("create".equals(key)) {
67-
products.put(key , new com.owant.createcode.CreateEvent());
68-
} else if("destroy".equals(key)) {
69-
products.put(key , new com.owant.createcode.testcode.DestroyEvent());
70-
} else if("resume".equals(key)) {
71-
products.put(key , new com.owant.createcode.testcode.ResumeEvent());
67+
if ("sub_event".equals(key)) {
68+
products.put(key, new SubEvent());
69+
} else if ("create".equals(key)) {
70+
products.put(key, new CreateEvent());
71+
} else if ("destroy".equals(key)) {
72+
products.put(key, new DestroyEvent());
73+
} else if ("resume".equals(key)) {
74+
products.put(key, new ResumeEvent());
7275
} else {
73-
throw new Exception(String.format("没有到key=%s对应的实现",key));
76+
throw new Exception(String.format("没有到key=%s对应的实现", key));
7477
}
7578
return products.get(key);
7679
}
7780
}
7881
79-
8082
```
8183

8284

exemple/build.gradle

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ dependencies {
4141
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
4242

4343
annotationProcessor 'com.google.dagger:dagger-compiler:2.2'
44-
implementation 'com.google.dagger:dagger:2.2'
45-
compileOnly 'org.glassfish:javax.annotation:10.0-b28'
4644

47-
// implementation project(path: ':FactoryBox')
48-
implementation 'com.owant.factorybox:factorybox:1.0.1'
45+
implementation project(path: ':FactoryBox')
46+
// implementation 'com.owant.factorybox:factorybox:1.0.1'
4947
}

0 commit comments

Comments
 (0)