Skip to content

Commit e53833c

Browse files
committed
Restore Eclipse 2019-6 compatibility
See gh-143
1 parent 25d8680 commit e53833c

File tree

2 files changed

+74
-5
lines changed
  • spring-javaformat
    • spring-javaformat-formatter-eclipse/src/main/java/org/eclipse/jdt/internal/formatter
    • spring-javaformat-formatter-eclipse-rewriter/src/main/java/io/spring/javaformat/formatter/eclipse/rewrite

2 files changed

+74
-5
lines changed

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
import java.nio.file.Paths;
2828
import java.nio.file.StandardCopyOption;
2929
import java.util.Collections;
30+
import java.util.LinkedHashSet;
31+
import java.util.Set;
3032

3133
import org.objectweb.asm.ClassReader;
3234
import org.objectweb.asm.ClassVisitor;
3335
import org.objectweb.asm.ClassWriter;
36+
import org.objectweb.asm.FieldVisitor;
3437
import org.objectweb.asm.MethodVisitor;
3538
import org.objectweb.asm.Opcodes;
3639

@@ -41,6 +44,22 @@
4144
*/
4245
public final class EclipseRewriter {
4346

47+
private static final Set<String> UPDATED_METHODS;
48+
static {
49+
Set<String> updatedMethods = new LinkedHashSet<String>();
50+
updatedMethods.add("prepareWraps");
51+
updatedMethods.add("tokenizeSource");
52+
UPDATED_METHODS = Collections.unmodifiableSet(updatedMethods);
53+
}
54+
55+
private static final Set<String> UPDATED_FIELDS;
56+
static {
57+
Set<String> updatedFields = new LinkedHashSet<String>();
58+
updatedFields.add("sourceLevel");
59+
updatedFields.add("tokens");
60+
UPDATED_FIELDS = Collections.unmodifiableSet(updatedFields);
61+
}
62+
4463
private EclipseRewriter() {
4564
}
4665

@@ -73,10 +92,18 @@ private static class DefaultCodeFormatterManipulator extends ClassVisitor {
7392
super(Opcodes.ASM5, visitor);
7493
}
7594

95+
@Override
96+
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
97+
if (access == Opcodes.ACC_PRIVATE && UPDATED_FIELDS.contains(name)) {
98+
access = Opcodes.ACC_PROTECTED;
99+
}
100+
return super.visitField(access, name, desc, signature, value);
101+
}
102+
76103
@Override
77104
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
78-
if ("prepareWraps".equals(name) && Opcodes.ACC_PRIVATE == access) {
79-
return super.visitMethod(Opcodes.ACC_PROTECTED, name, desc, signature, exceptions);
105+
if (access == Opcodes.ACC_PRIVATE && UPDATED_METHODS.contains(name)) {
106+
access = Opcodes.ACC_PROTECTED;
80107
}
81108
return new DefaultCodeFormatterMethodManipulator(
82109
super.visitMethod(access, name, desc, signature, exceptions));
@@ -92,9 +119,8 @@ private static class DefaultCodeFormatterMethodManipulator extends MethodVisitor
92119

93120
@Override
94121
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
95-
if ("prepareWraps".equals(name) && opcode == Opcodes.INVOKESPECIAL) {
96-
super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, name, desc, itf);
97-
return;
122+
if (opcode == Opcodes.INVOKESPECIAL && UPDATED_METHODS.contains(name)) {
123+
opcode = Opcodes.INVOKEVIRTUAL;
98124
}
99125
super.visitMethodInsn(opcode, owner, name, desc, itf);
100126
}

spring-javaformat/spring-javaformat-formatter-eclipse/src/main/java/org/eclipse/jdt/internal/formatter/ExtendedCodeFormatter.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import org.eclipse.jdt.core.compiler.InvalidInputException;
2425
import org.eclipse.jdt.core.dom.ASTNode;
26+
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
27+
import org.eclipse.jdt.internal.compiler.parser.Scanner;
28+
import org.eclipse.jdt.internal.compiler.parser.TerminalTokens;
2529
import org.eclipse.jdt.internal.formatter.Preparator.Phase;
2630

2731
/**
@@ -32,6 +36,8 @@
3236
*/
3337
public class ExtendedCodeFormatter extends DefaultCodeFormatter {
3438

39+
private static boolean useLegacyTokenize = false;
40+
3541
private final List<Preparator> preparators = new ArrayList<>();
3642

3743
public ExtendedCodeFormatter() {
@@ -54,6 +60,43 @@ protected void addPreparator(Preparator preparator) {
5460
this.preparators.add(preparator);
5561
}
5662

63+
@Override
64+
protected void tokenizeSource(int kind) {
65+
if (useLegacyTokenize) {
66+
legacyTokenizeSource(kind);
67+
return;
68+
}
69+
try {
70+
super.tokenizeSource(kind);
71+
}
72+
catch (NoSuchMethodError ex) {
73+
useLegacyTokenize = true;
74+
legacyTokenizeSource(kind);
75+
}
76+
}
77+
78+
private void legacyTokenizeSource(int kind) {
79+
this.tokens.clear();
80+
long sourceLevel = CompilerOptions.versionToJdkLevel(this.sourceLevel);
81+
Scanner scanner = new Scanner(true, false, false, sourceLevel, null, null, false);
82+
scanner.setSource(this.sourceArray);
83+
scanner.fakeInModule = (kind & K_MODULE_INFO) != 0;
84+
while (true) {
85+
try {
86+
int tokenType = scanner.getNextToken();
87+
if (tokenType == TerminalTokens.TokenNameEOF) {
88+
break;
89+
}
90+
Token token = Token.fromCurrent(scanner, tokenType);
91+
this.tokens.add(token);
92+
}
93+
catch (InvalidInputException ex) {
94+
Token token = Token.fromCurrent(scanner, TerminalTokens.TokenNameNotAToken);
95+
this.tokens.add(token);
96+
}
97+
}
98+
}
99+
57100
@Override
58101
protected void prepareWraps(int kind) {
59102
ASTNode astRoot = getField("astRoot", ASTNode.class);

0 commit comments

Comments
 (0)