Skip to content

Commit e9efa90

Browse files
authored
🆕 #1392 增加weixin-graal模块,配合graal以产生native-image配置。
目的:解决native-image中gson功能无效的问题。 可以通过项目的 native-image Profile 来启用: mvn -P native-image -Dmaven.test.skip=true clean source:jar install 编译时会在各包中增加2个文件: META-INF/native-image/.../reflection-config.json META-INF/native-image/.../native-image.properties
1 parent ccb2534 commit e9efa90

File tree

10 files changed

+413
-9
lines changed

10 files changed

+413
-9
lines changed

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
</scm>
100100

101101
<modules>
102+
<module>weixin-graal</module>
102103
<module>weixin-java-common</module>
103104
<module>weixin-java-cp</module>
104105
<module>weixin-java-mp</module>
@@ -331,6 +332,14 @@
331332
</plugins>
332333
</build>
333334
</profile>
335+
336+
<profile>
337+
<id>native-image</id>
338+
<activation>
339+
<activeByDefault>false</activeByDefault>
340+
</activation>
341+
</profile>
342+
334343
</profiles>
335344

336345
<build>

weixin-graal/pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.github.binarywang</groupId>
8+
<artifactId>wx-java</artifactId>
9+
<version>3.6.7.B</version>
10+
</parent>
11+
12+
<artifactId>weixin-graal</artifactId>
13+
<name>WxJava - Graal</name>
14+
<description>微信开发Java内部配合graal以产生native-image配置的辅助工具, 可以通过项目的 native-image Profile 来启用: mvn -P native-image ...
15+
</description>
16+
17+
<dependencies>
18+
19+
<dependency>
20+
<groupId>org.projectlombok</groupId>
21+
<artifactId>lombok</artifactId>
22+
<scope>compile</scope>
23+
</dependency>
24+
25+
</dependencies>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-compiler-plugin</artifactId>
32+
<version>3.5.1</version>
33+
<configuration>
34+
<proc>none</proc>
35+
</configuration>
36+
</plugin>
37+
</plugins>
38+
</build>
39+
40+
</project>
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package cn.binarywang.wx.graal;
2+
3+
import lombok.Data;
4+
5+
import javax.annotation.processing.AbstractProcessor;
6+
import javax.annotation.processing.RoundEnvironment;
7+
import javax.annotation.processing.SupportedAnnotationTypes;
8+
import javax.annotation.processing.SupportedSourceVersion;
9+
import javax.lang.model.SourceVersion;
10+
import javax.lang.model.element.TypeElement;
11+
import javax.lang.model.type.DeclaredType;
12+
import javax.lang.model.type.TypeKind;
13+
import javax.lang.model.type.TypeMirror;
14+
import javax.lang.model.util.ElementFilter;
15+
import javax.tools.FileObject;
16+
import javax.tools.StandardLocation;
17+
import java.io.IOException;
18+
import java.io.Writer;
19+
import java.util.Set;
20+
import java.util.SortedSet;
21+
import java.util.TreeSet;
22+
23+
// 目前仅仅处理@Data,且必须在lombok自己的processor之前执行,千万注意!!!!!
24+
@SupportedAnnotationTypes("lombok.Data")
25+
@SupportedSourceVersion(SourceVersion.RELEASE_7)
26+
public class GraalProcessor extends AbstractProcessor {
27+
28+
private static final String REFLECTION_CONFIG_JSON = "reflection-config.json";
29+
private static final String NATIVE_IMAGE_PROPERTIES = "native-image.properties";
30+
31+
private SortedSet<String> classSet = new TreeSet<>();
32+
private String shortestPackageName = null;
33+
34+
@Override
35+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
36+
for (TypeElement annotatedClass : ElementFilter.typesIn(roundEnv.getElementsAnnotatedWith(Data.class))) {
37+
38+
registerClass(annotatedClass.getQualifiedName().toString());
39+
handleSuperClass(annotatedClass);
40+
}
41+
42+
//只有最后一轮才可以写文件,否则文件会被重复打开,报错!
43+
if (!roundEnv.processingOver()) return false;
44+
45+
// 如果没有文件要写,跳过
46+
if (classSet.isEmpty()) return false;
47+
48+
writeFiles();
49+
50+
//必须返回false,以便让lombok能继续处理。
51+
return false;
52+
}
53+
54+
/**
55+
* 设置当前最短的package名称
56+
*
57+
* @param packageName 包名
58+
*/
59+
private void setShortestPackageName(String packageName) {
60+
if (shortestPackageName == null) {
61+
shortestPackageName = packageName;
62+
} else if (packageName.length() < shortestPackageName.length()) {
63+
shortestPackageName = packageName;
64+
}
65+
}
66+
67+
/**
68+
* 更加完整的类名来获取package名称
69+
*
70+
* @param fullClassName 完整的类名
71+
* @return package name
72+
*/
73+
private String getPackageName(String fullClassName) {
74+
int last = fullClassName.lastIndexOf('.');
75+
if (last == -1) return fullClassName;
76+
return fullClassName.substring(0, last);
77+
}
78+
79+
/**
80+
* 保存文件
81+
* META-INF/native-image/.../reflection-config.json
82+
* META-INF/native-image/.../native-image.properties
83+
*/
84+
private void writeFiles() {
85+
String basePackage = shortestPackageName;
86+
87+
String module;
88+
if (basePackage.contains(".")) {
89+
final int i = basePackage.lastIndexOf('.');
90+
module = basePackage.substring(i + 1);
91+
basePackage = basePackage.substring(0, i);
92+
} else {
93+
module = basePackage;
94+
}
95+
96+
String path = "META-INF/native-image/" + basePackage + "/" + module + "/";
97+
String reflectFile = path + REFLECTION_CONFIG_JSON;
98+
String propsFile = path + NATIVE_IMAGE_PROPERTIES;
99+
try {
100+
FileObject fileObject = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", propsFile);
101+
Writer writer = fileObject.openWriter();
102+
writer.append("Args = -H:ReflectionConfigurationResources=${.}/" + REFLECTION_CONFIG_JSON);
103+
writer.close();
104+
} catch (IOException e) {
105+
e.printStackTrace();
106+
}
107+
108+
try {
109+
FileObject fileObject = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", reflectFile);
110+
Writer writer = fileObject.openWriter();
111+
writer.write("[\n");
112+
boolean first = true;
113+
for (String name : classSet) {
114+
if (first) {
115+
first = false;
116+
} else {
117+
writer.write(",");
118+
}
119+
writer.write(assetGraalJsonElement(name));
120+
writer.append('\n');
121+
}
122+
writer.write("]");
123+
writer.close();
124+
} catch (IOException e) {
125+
e.printStackTrace();
126+
}
127+
128+
}
129+
130+
private String assetGraalJsonElement(String className) {
131+
return "{\n" +
132+
" \"name\" : \"" + className + "\",\n" +
133+
" \"allDeclaredFields\":true,\n" +
134+
" \"allDeclaredMethods\":true,\n" +
135+
" \"allDeclaredConstructors\":true,\n" +
136+
" \"allPublicMethods\" : true\n" +
137+
"}";
138+
}
139+
140+
/**
141+
* 登记一个class
142+
*
143+
* @param className 完整的类名
144+
*/
145+
private void registerClass(String className) {
146+
classSet.add(className);
147+
setShortestPackageName(getPackageName(className));
148+
}
149+
150+
/**
151+
* 获取一个类型的所有的父类,并登记
152+
*
153+
* @param typeElement 类型元素
154+
*/
155+
private void handleSuperClass(TypeElement typeElement) {
156+
TypeMirror superclass = typeElement.getSuperclass();
157+
if (superclass.getKind() == TypeKind.DECLARED) {
158+
TypeElement s = (TypeElement) ((DeclaredType) superclass).asElement();
159+
String sName = s.toString();
160+
// ignore java.**/javax.**
161+
if (sName.startsWith("java.") || sName.startsWith("javax.")) return;
162+
registerClass(sName);
163+
handleSuperClass(s);
164+
}
165+
}
166+
167+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cn.binarywang.wx.graal.GraalProcessor

weixin-java-common/pom.xml

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
2-
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4-
xmlns="http://maven.apache.org/POM/4.0.0">
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0">
55
<modelVersion>4.0.0</modelVersion>
66
<parent>
77
<groupId>com.github.binarywang</groupId>
@@ -134,4 +134,36 @@
134134
</plugins>
135135
</build>
136136

137+
<profiles>
138+
<profile>
139+
<id>native-image</id>
140+
<activation>
141+
<activeByDefault>false</activeByDefault>
142+
</activation>
143+
144+
<build>
145+
<plugins>
146+
<plugin>
147+
<groupId>org.apache.maven.plugins</groupId>
148+
<artifactId>maven-compiler-plugin</artifactId>
149+
<version>3.5.1</version>
150+
<configuration>
151+
<annotationProcessors>
152+
cn.binarywang.wx.graal.GraalProcessor,lombok.launch.AnnotationProcessorHider$AnnotationProcessor,lombok.launch.AnnotationProcessorHider$ClaimingProcessor
153+
</annotationProcessors>
154+
<annotationProcessorPaths>
155+
<path>
156+
<groupId>com.github.binarywang</groupId>
157+
<artifactId>weixin-graal</artifactId>
158+
<version>${project.version}</version>
159+
</path>
160+
</annotationProcessorPaths>
161+
</configuration>
162+
</plugin>
163+
</plugins>
164+
</build>
165+
166+
</profile>
167+
</profiles>
168+
137169
</project>

weixin-java-cp/pom.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,36 @@
9595
</plugins>
9696
</build>
9797

98+
<profiles>
99+
<profile>
100+
<id>native-image</id>
101+
<activation>
102+
<activeByDefault>false</activeByDefault>
103+
</activation>
104+
105+
<build>
106+
<plugins>
107+
<plugin>
108+
<groupId>org.apache.maven.plugins</groupId>
109+
<artifactId>maven-compiler-plugin</artifactId>
110+
<version>3.5.1</version>
111+
<configuration>
112+
<annotationProcessors>
113+
cn.binarywang.wx.graal.GraalProcessor,lombok.launch.AnnotationProcessorHider$AnnotationProcessor,lombok.launch.AnnotationProcessorHider$ClaimingProcessor
114+
</annotationProcessors>
115+
<annotationProcessorPaths>
116+
<path>
117+
<groupId>com.github.binarywang</groupId>
118+
<artifactId>weixin-graal</artifactId>
119+
<version>${project.version}</version>
120+
</path>
121+
</annotationProcessorPaths>
122+
</configuration>
123+
</plugin>
124+
</plugins>
125+
</build>
126+
127+
</profile>
128+
</profiles>
129+
98130
</project>

weixin-java-miniapp/pom.xml

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@
8080
<groupId>org.projectlombok</groupId>
8181
<artifactId>lombok</artifactId>
8282
</dependency>
83-
<dependency>
84-
<groupId>com.github.jedis-lock</groupId>
85-
<artifactId>jedis-lock</artifactId>
86-
<version>1.0.0</version>
87-
<optional>true</optional>
88-
</dependency>
83+
<dependency>
84+
<groupId>com.github.jedis-lock</groupId>
85+
<artifactId>jedis-lock</artifactId>
86+
<version>1.0.0</version>
87+
<optional>true</optional>
88+
</dependency>
8989
</dependencies>
9090

9191
<build>
@@ -102,4 +102,35 @@
102102
</plugins>
103103
</build>
104104

105+
<profiles>
106+
<profile>
107+
<id>native-image</id>
108+
<activation>
109+
<activeByDefault>false</activeByDefault>
110+
</activation>
111+
112+
<build>
113+
<plugins>
114+
<plugin>
115+
<groupId>org.apache.maven.plugins</groupId>
116+
<artifactId>maven-compiler-plugin</artifactId>
117+
<version>3.5.1</version>
118+
<configuration>
119+
<annotationProcessors>
120+
cn.binarywang.wx.graal.GraalProcessor,lombok.launch.AnnotationProcessorHider$AnnotationProcessor,lombok.launch.AnnotationProcessorHider$ClaimingProcessor
121+
</annotationProcessors>
122+
<annotationProcessorPaths>
123+
<path>
124+
<groupId>com.github.binarywang</groupId>
125+
<artifactId>weixin-graal</artifactId>
126+
<version>${project.version}</version>
127+
</path>
128+
</annotationProcessorPaths>
129+
</configuration>
130+
</plugin>
131+
</plugins>
132+
</build>
133+
</profile>
134+
</profiles>
135+
105136
</project>

0 commit comments

Comments
 (0)