Skip to content
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

.stapler file generation is nondeterministic #527

Merged
merged 1 commit into from
Apr 12, 2024
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 @@ -26,11 +26,14 @@
import javax.annotation.processing.AbstractProcessor;
import javax.lang.model.element.Element;
import javax.tools.FileObject;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

import static javax.tools.Diagnostic.Kind.*;
import static javax.tools.StandardLocation.*;
Expand Down Expand Up @@ -66,8 +69,13 @@ protected void notice(String msg, Element location) {

protected void writePropertyFile(Properties p, String name) throws IOException {
FileObject f = createResource(name);
try (OutputStream os = f.openOutputStream()) {
p.store(os,null);
/*
* This is somewhat fragile, but it is the only practical option on Java 11 and 17. In Java 21, we could instead
* set the "java.properties.date" system property to a fixed string and avoid the need to work around internal
* Java Platform implementation details.
*/
try (Writer w = f.openWriter(); BufferedWriter bw = new CommentStrippingBufferedWriter(w)) {
p.store(bw,null);
}
}

Expand All @@ -78,4 +86,19 @@ protected FileObject getResource(String name) throws IOException {
protected FileObject createResource(String name) throws IOException {
return processingEnv.getFiler().createResource(CLASS_OUTPUT, "", name);
}

private static class CommentStrippingBufferedWriter extends BufferedWriter {
private final AtomicInteger count = new AtomicInteger(0);

public CommentStrippingBufferedWriter(Writer out) {
super(out);
}

@Override
public void write(String str) throws IOException {
if (count.getAndIncrement() > 0 || !str.startsWith("#")) {
super.write(str);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.kohsuke.stapler.jsr269;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -8,6 +11,7 @@
import com.karuslabs.elementary.junit.annotations.Inline;
import com.karuslabs.elementary.junit.annotations.Options;
import com.karuslabs.elementary.junit.annotations.Processors;
import java.time.Year;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -159,5 +163,23 @@
List<Diagnostic<? extends JavaFileObject>> diagnostics = results.diagnostics;
assertEquals(0, diagnostics.size());
}
// TODO nested classes use qualified rather than binary name

Check warning on line 166 in core/src/test/java/org/kohsuke/stapler/jsr269/ConstructorProcessorTest.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: nested classes use qualified rather than binary name

//issue-526
@Inline(
name = "some.pkg.Stuff",
source = {
"package some.pkg;",
"import org.kohsuke.stapler.DataBoundConstructor;",
"public class Stuff {",
" @DataBoundConstructor public Stuff(int count, String name) {}",
"}",
})
@Test
void reproducibleBuild(Results results) {
assertEquals(Collections.emptyList(), results.diagnostics);
assertThat(
Utils.getGeneratedResource(results.sources, "some/pkg/Stuff.stapler"),
not(containsString(Integer.toString(Year.now().getValue()))));
}
}