Skip to content

fix: Fix Java type for annotated array parameters #3797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ class GraalVmProcessorTest {
private static final Object FAKE_CONSTRAINT_VALIDATOR = onlyNoArgsConstructor(FAKE_CONSTRAINT_VALIDATOR_NAME);
private static final String FAKE_PLUGIN_VISITOR_NAME = "example.FakeAnnotations$FakePluginVisitor";
private static final Object FAKE_PLUGIN_VISITOR = onlyNoArgsConstructor(FAKE_PLUGIN_VISITOR_NAME);
private static final String FAKE_CONVERTER_NAME = "example.FakeConverter";
private static final Object FAKE_CONVERTER = asMap(
"name",
FAKE_CONVERTER_NAME,
"methods",
singletonList(asMap(
"name",
"newInstance",
"parameterTypes",
asList("org.apache.logging.log4j.core.config.Configuration", "java.lang.String[]"))),
"fields",
emptyList());

private static final String GROUP_ID = "groupId";
private static final String ARTIFACT_ID = "artifactId";
Expand Down Expand Up @@ -155,7 +167,8 @@ static Stream<Arguments> containsSpecificEntries() {
Arguments.of(FAKE_PLUGIN_BUILDER_NAME, FAKE_PLUGIN_BUILDER),
Arguments.of(FAKE_PLUGIN_NESTED_NAME, FAKE_PLUGIN_NESTED),
Arguments.of(FAKE_CONSTRAINT_VALIDATOR_NAME, FAKE_CONSTRAINT_VALIDATOR),
Arguments.of(FAKE_PLUGIN_VISITOR_NAME, FAKE_PLUGIN_VISITOR));
Arguments.of(FAKE_PLUGIN_VISITOR_NAME, FAKE_PLUGIN_VISITOR),
Arguments.of(FAKE_CONVERTER_NAME, FAKE_CONVERTER));
}

@ParameterizedTest
Expand All @@ -168,7 +181,9 @@ void containsSpecificEntries(String className, Object expectedJson) throws IOExc
assertThatJson(reachabilityMetadata)
.inPath(String.format("$[?(@.name == '%s')]", className))
.isArray()
.contains(json(expectedJson));
.hasSize(1)
.first()
.isEqualTo(json(expectedJson));
}

static Stream<Arguments> reachabilityMetadataPath() {
Expand Down Expand Up @@ -214,7 +229,7 @@ void whenNoGroupIdAndArtifactId_thenWarningIsPrinted(@TempDir(cleanup = CleanupM
}
// The generated folder name should be deterministic and based solely on the descriptor content.
// If the descriptor changes, this test and the expected folder name must be updated accordingly.
assertThat(reachabilityMetadataFolders).hasSize(1).containsExactly(path.resolve("62162090"));
assertThat(reachabilityMetadataFolders).hasSize(1).containsExactly(path.resolve("72c240aa"));
assertThat(reachabilityMetadataFolders.get(0).resolve("reflect-config.json"))
.as("Reachability metadata file")
.exists();
Expand Down Expand Up @@ -250,7 +265,6 @@ private static List<String> generateDescriptor(
}

// Compile the sources
final Path descriptorFilePath = outputDir.resolve("plugins.xml");
final DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
final JavaCompiler.CompilationTask task =
compiler.getTask(null, fileManager, diagnosticCollector, options, null, sources);
Expand All @@ -260,6 +274,8 @@ private static List<String> generateDescriptor(
return diagnosticCollector.getDiagnostics().stream()
.filter(d -> d.getKind() != Diagnostic.Kind.NOTE)
.map(d -> d.getMessage(Locale.ROOT))
// This message appears when the test runs on JDK 8
.filter(m -> !"unknown enum constant java.lang.annotation.ElementType.MODULE".equals(m))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.pattern.ConverterKeys;
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
import org.apache.logging.log4j.core.pattern.PatternConverter;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
@Plugin(name = "FakePatternConverter", category = PatternConverter.CATEGORY)
@ConverterKeys({"f", "fake"})
public final class FakeConverter extends LogEventPatternConverter {

private FakeConverter(@Nullable final Configuration config, @Nullable final String[] options) {
super("Fake", "fake");
}

public static FakeConverter newInstance(
@Nullable final Configuration config, @Nullable final String[] options) {
return new FakeConverter(config, options);
}

@Override
public void format(LogEvent event, StringBuilder toAppendTo) {
toAppendTo.append("FakeConverter: ").append(event.getMessage().getFormattedMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ public String visitArray(final ArrayType t, @Nullable Void unused) {

@Override
public @Nullable String visitDeclared(final DeclaredType t, final Void unused) {
return processingEnv.getTypeUtils().erasure(t).toString();
return safeCast(t.asElement(), TypeElement.class)
.getQualifiedName()
.toString();
}
},
null);
Expand Down
12 changes: 12 additions & 0 deletions src/changelog/.2.x.x/3796_annotated-array-parameters.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="3796" link="https://github.com/apache/logging-log4j2/issues/3796"/>
<description format="asciidoc">
Fix GraalVM reachability metadata generation for methods with annotated array type parameters, such as `@Nullable String[]`.
</description>
</entry>