Skip to content

Commit 78dc01d

Browse files
ppkarwaszvy
andcommitted
Use binary names in GraalVmProcessor (#3996)
* Use binary names in `GraalVmProcessor` `GraalVmProcessor` currently emits canonical type names (JLS §6.7) for parameter types in the GraalVM reachability metadata. However, testing shows that GraalVM expects **binary names** (JLS §13.1) for reference types. For example: * Canonical: `org.apache.logging.log4j.core.Filter.Result` * Required (binary): `org.apache.logging.log4j.core.Filter$Result` For array types, GraalVM accepts two forms: * The JVM descriptor form: `[L<component_type>;` * The Java-like form: `<component_type>[]` This PR updates the processor to use binary names and emits the simpler Java-like syntax for arrays. * Fix changelog entry type --------- Co-authored-by: Volkan Yazıcı <volkan@yazi.ci>
1 parent c09b012 commit 78dc01d

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/GraalVmProcessorTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ class GraalVmProcessorTest {
7979
"org.apache.logging.log4j.core.config.Configuration",
8080
"org.apache.logging.log4j.core.config.Node",
8181
"org.apache.logging.log4j.core.LoggerContext",
82-
"java.lang.String"))),
82+
"java.lang.String",
83+
"org.apache.logging.log4j.core.Filter$Result",
84+
"org.apache.logging.log4j.core.Filter[]"))),
8385
"fields",
8486
emptyList());
8587
private static final String FAKE_PLUGIN_BUILDER_NAME = FAKE_PLUGIN_NAME + "$Builder";
@@ -93,9 +95,11 @@ class GraalVmProcessorTest {
9395
asMap("name", "attribute"),
9496
asMap("name", "attributeWithoutPublicSetterButWithSuppressAnnotation"),
9597
asMap("name", "config"),
98+
asMap("name", "filters"),
9699
asMap("name", "layout"),
97100
asMap("name", "loggerContext"),
98101
asMap("name", "node"),
102+
asMap("name", "onMatch"),
99103
asMap("name", "value")));
100104
private static final String FAKE_PLUGIN_NESTED_NAME = FAKE_PLUGIN_NAME + "$Nested";
101105
private static final Object FAKE_PLUGIN_NESTED = onlyNoArgsConstructor(FAKE_PLUGIN_NESTED_NAME);
@@ -229,7 +233,7 @@ void whenNoGroupIdAndArtifactId_thenWarningIsPrinted(@TempDir(cleanup = CleanupM
229233
}
230234
// The generated folder name should be deterministic and based solely on the descriptor content.
231235
// If the descriptor changes, this test and the expected folder name must be updated accordingly.
232-
assertThat(reachabilityMetadataFolders).hasSize(1).containsExactly(path.resolve("72c240aa"));
236+
assertThat(reachabilityMetadataFolders).hasSize(1).containsExactly(path.resolve("791e18c8"));
233237
assertThat(reachabilityMetadataFolders.get(0).resolve("reflect-config.json"))
234238
.as("Reachability metadata file")
235239
.exists();

log4j-core-test/src/test/resources/GraalVmProcessorTest/java/FakePlugin.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package example;
1818

1919
import java.io.Serializable;
20+
import org.apache.logging.log4j.core.Filter;
2021
import org.apache.logging.log4j.core.Layout;
2122
import org.apache.logging.log4j.core.LoggerContext;
2223
import org.apache.logging.log4j.core.config.Configuration;
@@ -49,7 +50,9 @@ public static FakePlugin newPlugin(
4950
@PluginConfiguration Configuration config,
5051
@PluginNode Node node,
5152
@PluginLoggerContext LoggerContext loggerContext,
52-
@PluginValue("value") String value) {
53+
@PluginValue("value") String value,
54+
@PluginValue("onMatch") Filter.Result onMatch,
55+
@PluginElement("filters") Filter[] filters) {
5356
return null;
5457
}
5558

@@ -82,6 +85,12 @@ public static class Builder implements org.apache.logging.log4j.core.util.Builde
8285
@PluginValue("value")
8386
private String value;
8487

88+
@PluginValue("onMatch")
89+
private Filter.Result onMatch;
90+
91+
@PluginElement("filters")
92+
private Filter[] filters;
93+
8594
@Override
8695
public FakePlugin build() {
8796
return null;

log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/GraalVmProcessor.java

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,13 @@
3636
import javax.annotation.processing.SupportedOptions;
3737
import javax.lang.model.SourceVersion;
3838
import javax.lang.model.element.Element;
39-
import javax.lang.model.element.ElementKind;
4039
import javax.lang.model.element.ExecutableElement;
4140
import javax.lang.model.element.Modifier;
42-
import javax.lang.model.element.PackageElement;
4341
import javax.lang.model.element.TypeElement;
4442
import javax.lang.model.element.VariableElement;
4543
import javax.lang.model.type.ArrayType;
4644
import javax.lang.model.type.DeclaredType;
4745
import javax.lang.model.type.TypeMirror;
48-
import javax.lang.model.util.SimpleElementVisitor8;
4946
import javax.lang.model.util.SimpleTypeVisitor8;
5047
import javax.tools.Diagnostic;
5148
import javax.tools.StandardLocation;
@@ -300,9 +297,7 @@ public String visitArray(final ArrayType t, @Nullable Void unused) {
300297

301298
@Override
302299
public @Nullable String visitDeclared(final DeclaredType t, final Void unused) {
303-
return safeCast(t.asElement(), TypeElement.class)
304-
.getQualifiedName()
305-
.toString();
300+
return GraalVmProcessor.this.toString(safeCast(t.asElement(), TypeElement.class));
306301
}
307302
},
308303
null);
@@ -313,28 +308,7 @@ public String visitArray(final ArrayType t, @Nullable Void unused) {
313308
*
314309
* @param element A Java language element.
315310
*/
316-
private String toString(Element element) {
317-
return element.accept(
318-
new SimpleElementVisitor8<String, @Nullable Void>() {
319-
@Override
320-
public String visitPackage(PackageElement e, @Nullable Void unused) {
321-
return e.getQualifiedName().toString();
322-
}
323-
324-
@Override
325-
public String visitType(TypeElement e, @Nullable Void unused) {
326-
Element parent = e.getEnclosingElement();
327-
String separator = parent.getKind() == ElementKind.PACKAGE ? "." : "$";
328-
return visit(parent, unused)
329-
+ separator
330-
+ e.getSimpleName().toString();
331-
}
332-
333-
@Override
334-
protected String defaultAction(Element e, @Nullable Void unused) {
335-
return "";
336-
}
337-
},
338-
null);
311+
private String toString(TypeElement element) {
312+
return processingEnv.getElementUtils().getBinaryName(element).toString();
339313
}
340314
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns="https://logging.apache.org/xml/ns"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="
5+
https://logging.apache.org/xml/ns
6+
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
7+
type="fixed">
8+
<issue id="3871" link="https://github.com/apache/logging-log4j2/issues/3871"/>
9+
<issue id="3996" link="https://github.com/apache/logging-log4j2/pull/3996"/>
10+
<description format="asciidoc">
11+
Fix GraalVM metadata for nested classes to use binary names instead of canonical names.
12+
</description>
13+
</entry>

0 commit comments

Comments
 (0)