Skip to content

Commit cc555d2

Browse files
committed
Avoid shipping AWT classes in native images
This commit provides an alternative java.beans.Introspector#findCustomizerClass implementation via a GraalVM substitution that avoids to include thousands of unused AWT classes in the native image. Closes gh-29060
1 parent cced3cb commit cc555d2

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.aot.graalvm;
18+
19+
import com.oracle.svm.core.annotate.Alias;
20+
import com.oracle.svm.core.annotate.TargetClass;
21+
22+
/**
23+
* Allow to reference {@code com.sun.beans.finder.ClassFinder} from
24+
* {@link Target_Introspector}.
25+
*
26+
* @author Sebastien Deleuze
27+
* @since 6.0
28+
*/
29+
@TargetClass(className = "com.sun.beans.finder.ClassFinder")
30+
final class Target_ClassFinder {
31+
32+
@Alias
33+
public static Class<?> findClass(String name, ClassLoader loader) throws ClassNotFoundException {
34+
return null;
35+
}
36+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.aot.graalvm;
18+
19+
import java.beans.Customizer;
20+
21+
import com.oracle.svm.core.annotate.Substitute;
22+
import com.oracle.svm.core.annotate.TargetClass;
23+
24+
/**
25+
* {@link java.beans.Introspector} substitution with a refined {@code findCustomizerClass} implementation
26+
* designed to avoid thousands of AWT classes to be included in the native image.
27+
*
28+
* @author Sebastien Deleuze
29+
* @since 6.0
30+
*/
31+
@TargetClass(className = "java.beans.Introspector")
32+
final class Target_Introspector {
33+
34+
@Substitute
35+
private static Class<?> findCustomizerClass(Class<?> type) {
36+
String name = type.getName() + "Customizer";
37+
try {
38+
type = Target_ClassFinder.findClass(name, type.getClassLoader());
39+
if (Customizer.class.isAssignableFrom(type)) {
40+
Class<?> c = type;
41+
do {
42+
c = c.getSuperclass();
43+
if (c.getName().equals("java.awt.Component")) {
44+
return type;
45+
}
46+
} while (!c.getName().equals("java.lang.Object"));
47+
}
48+
}
49+
catch (Exception exception) {
50+
}
51+
return null;
52+
}
53+
54+
}

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<suppress files="SocketUtils" checks="HideUtilityClassConstructor"/>
3838
<suppress files="ResolvableType" checks="FinalClass"/>
3939
<suppress files="[\\/]src[\\/]testFixtures[\\/]java[\\/].+" checks="IllegalImport" id="bannedJUnitJupiterImports"/>
40+
<suppress files="Target_ClassFinder" checks="HideUtilityClassConstructor"/>
4041

4142
<!-- spring-core-test -->
4243
<suppress files="CompileWithTargetClassAccess" checks="IllegalImport" id="bannedJUnitJupiterImports" />

0 commit comments

Comments
 (0)