Skip to content

Commit c8835c8

Browse files
committed
Rewrite NSL class to drop System.getProperty call
Update `EclipseRewriter` so that the NSL class is rewritten to remove the System.getProperty("osgi.nls.warnings") call. This update should allow us to support Gradle's configuration caching feature. Closes gh-262
1 parent 51cde82 commit c8835c8

File tree

4 files changed

+72
-84
lines changed

4 files changed

+72
-84
lines changed

spring-javaformat/spring-javaformat-formatter-eclipse-rewriter/src/main/java/io/spring/javaformat/formatter/eclipse/rewrite/EclipseRewriter.java

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
import java.util.Collections;
3030
import java.util.LinkedHashSet;
3131
import java.util.Set;
32+
import java.util.function.Function;
3233

3334
import org.objectweb.asm.ClassReader;
3435
import org.objectweb.asm.ClassVisitor;
@@ -72,20 +73,30 @@ public void rewrite(String file) throws IOException {
7273
}
7374

7475
private void rewrite(FileSystem zip) throws IOException {
75-
Path path = zip.getPath("org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.class");
76-
ClassWriter writer = new ClassWriter(0);
76+
rewrite(zip, "org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.class",
77+
DefaultCodeFormatterManipulator::new);
78+
rewrite(zip, "org/eclipse/osgi/util/NLS$1.class", NlsManipulator::new);
79+
}
80+
81+
private void rewrite(FileSystem zip, String name, Function<ClassWriter, ClassVisitor> manipulator)
82+
throws IOException {
83+
ClassWriter classWriter = new ClassWriter(0);
84+
Path path = zip.getPath(name);
7785
try (InputStream in = Files.newInputStream(path)) {
78-
DefaultCodeFormatterManipulator manipulator = new DefaultCodeFormatterManipulator(writer);
7986
ClassReader reader = new ClassReader(in);
80-
reader.accept(manipulator, 0);
87+
reader.accept(manipulator.apply(classWriter), 0);
8188
}
82-
Files.copy(new ByteArrayInputStream(writer.toByteArray()), path, StandardCopyOption.REPLACE_EXISTING);
89+
Files.copy(new ByteArrayInputStream(classWriter.toByteArray()), path, StandardCopyOption.REPLACE_EXISTING);
8390
}
8491

8592
public static void main(String[] args) throws Exception {
8693
new EclipseRewriter().rewrite(args[0]);
8794
}
8895

96+
/**
97+
* {@link ClassVisitor} to make some fields and methods from
98+
* {@code DefaultCodeFormatter} public.
99+
*/
89100
private static class DefaultCodeFormatterManipulator extends ClassVisitor {
90101

91102
DefaultCodeFormatterManipulator(ClassVisitor visitor) {
@@ -111,6 +122,10 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si
111122

112123
}
113124

125+
/**
126+
* {@link MethodVisitor} to make some fields and methods from
127+
* {@code DefaultCodeFormatter} public.
128+
*/
114129
private static class DefaultCodeFormatterMethodManipulator extends MethodVisitor {
115130

116131
DefaultCodeFormatterMethodManipulator(MethodVisitor mv) {
@@ -127,4 +142,49 @@ public void visitMethodInsn(int opcode, String owner, String name, String desc,
127142

128143
}
129144

145+
/**
146+
* {@link ClassVisitor} to update the {@code NLS} class so it doesn't use a System
147+
* property to disable warning messages.
148+
*/
149+
private static class NlsManipulator extends ClassVisitor {
150+
151+
NlsManipulator(ClassVisitor visitor) {
152+
super(Opcodes.ASM5, visitor);
153+
}
154+
155+
@Override
156+
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
157+
if ("run".equals(name) && desc.contains("Boolean")) {
158+
return new NslMethodManipulator(super.visitMethod(access, name, desc, signature, exceptions));
159+
}
160+
return super.visitMethod(access, name, desc, signature, exceptions);
161+
}
162+
163+
}
164+
165+
/**
166+
* {@link MethodVisitor} to update the {@code NLS} class so it doesn't use a System
167+
* property to disable warning messages.
168+
*/
169+
private static class NslMethodManipulator extends MethodVisitor {
170+
171+
private final MethodVisitor methodVisitor;
172+
173+
NslMethodManipulator(MethodVisitor mv) {
174+
super(Opcodes.ASM5, null);
175+
this.methodVisitor = mv;
176+
}
177+
178+
@Override
179+
public void visitEnd() {
180+
MethodVisitor mv = this.methodVisitor;
181+
mv.visitCode();
182+
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Boolean", "TRUE", "Ljava/lang/Boolean;");
183+
mv.visitInsn(Opcodes.ARETURN);
184+
mv.visitMaxs(1, 1);
185+
mv.visitEnd();
186+
}
187+
188+
}
189+
130190
}

spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/FileFormatter.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@ public FileFormatter() {
4141
this(new Formatter());
4242
}
4343

44-
public FileFormatter(FormatterOption... options) {
45-
this(new Formatter(options));
46-
}
47-
48-
public FileFormatter(JavaFormatConfig javaFormatConfig, FormatterOption... options) {
49-
this(new Formatter(javaFormatConfig, options));
44+
public FileFormatter(JavaFormatConfig javaFormatConfig) {
45+
this(new Formatter(javaFormatConfig));
5046
}
5147

5248
public FileFormatter(Formatter formatter) {

spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
21-
import java.util.Arrays;
2221
import java.util.Collections;
23-
import java.util.HashSet;
2422
import java.util.Map;
2523
import java.util.Properties;
26-
import java.util.Set;
27-
import java.util.function.Supplier;
2824

2925
import org.eclipse.jdt.core.formatter.CodeFormatter;
3026
import org.eclipse.jface.text.IRegion;
@@ -58,35 +54,21 @@ public class Formatter extends CodeFormatter {
5854
*/
5955
public static final String DEFAULT_LINE_SEPARATOR = null;
6056

61-
private static final FormatterOption[] EMPTY_OPTIONS = {};
62-
63-
private final Set<FormatterOption> options;
64-
6557
private final CodeFormatter delegate;
6658

6759
/**
6860
* Create a new formatter instance.
6961
*/
7062
public Formatter() {
71-
this(JavaFormatConfig.DEFAULT, EMPTY_OPTIONS);
72-
}
73-
74-
/**
75-
* Create a new formatter instance.
76-
* @param options formatter options
77-
*/
78-
public Formatter(FormatterOption... options) {
79-
this(JavaFormatConfig.DEFAULT, options);
63+
this(JavaFormatConfig.DEFAULT);
8064
}
8165

8266
/**
8367
* Create a new formatter instance.
8468
* @param javaFormatConfig the java format config to use
85-
* @param options formatter options
8669
*/
87-
public Formatter(JavaFormatConfig javaFormatConfig, FormatterOption... options) {
70+
public Formatter(JavaFormatConfig javaFormatConfig) {
8871
this.delegate = new DelegateCodeFormatter(javaFormatConfig);
89-
this.options = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(options)));
9072
}
9173

9274
/**
@@ -134,9 +116,7 @@ public TextEdit format(String source, int offset, int length, String lineSeparat
134116
@Override
135117
public TextEdit format(int kind, String source, int offset, int length, int indentationLevel,
136118
String lineSeparator) {
137-
return nlsSafe(() -> {
138-
return this.delegate.format(kind, source, offset, length, indentationLevel, lineSeparator);
139-
});
119+
return this.delegate.format(kind, source, offset, length, indentationLevel, lineSeparator);
140120
}
141121

142122
/**
@@ -162,7 +142,7 @@ public TextEdit format(String source, IRegion[] regions, String lineSeparator) {
162142

163143
@Override
164144
public TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) {
165-
return nlsSafe(() -> this.delegate.format(kind, source, regions, indentationLevel, lineSeparator));
145+
return this.delegate.format(kind, source, regions, indentationLevel, lineSeparator);
166146
}
167147

168148
@Override
@@ -175,23 +155,6 @@ public void setOptions(Map<String, String> options) {
175155
this.delegate.setOptions(options);
176156
}
177157

178-
private <T> T nlsSafe(Supplier<T> formatted) {
179-
if (this.options.contains(FormatterOption.SHOW_NLS_WARNINGS)) {
180-
return formatted.get();
181-
}
182-
String nlsWarnings = System.getProperty("osgi.nls.warnings");
183-
try {
184-
System.setProperty("osgi.nls.warnings", "ignore");
185-
return formatted.get();
186-
}
187-
finally {
188-
if (nlsWarnings != null) {
189-
System.setProperty("osgi.nls.warnings", nlsWarnings);
190-
}
191-
}
192-
193-
}
194-
195158
/**
196159
* Internal delegate code formatter to apply Spring {@literal formatter.prefs} and add
197160
* {@link Preparator Preparators}.

spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/FormatterOption.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)