Skip to content

Commit 64aff5e

Browse files
committed
Merge pull request #143 from BoykoAlex
* pr/143: Restore Eclipse 2019-6 compatibility Update to latest Eclipse 2019-09 Closes gh-143
2 parents b101ce6 + e53833c commit 64aff5e

File tree

5 files changed

+81
-12
lines changed

5 files changed

+81
-12
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<main.basedir>${basedir}</main.basedir>
3232
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3333
<java.version>1.8</java.version>
34-
<eclipse.repository>https://download.eclipse.org/releases/2019-03/201903201000</eclipse.repository>
34+
<eclipse.repository>https://download.eclipse.org/releases/2019-09/201909181001/</eclipse.repository>
3535
<eclipse.checkstyle.repository>https://dl.bintray.com/eclipse-cs/eclipse-cs/8.18.0</eclipse.checkstyle.repository>
3636
<tycho.disableP2Mirrors>true</tycho.disableP2Mirrors>
3737
<ant.version>1.8.1</ant.version>

spring-javaformat-eclipse/io.spring.javaformat.eclipse/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Require-Bundle: org.eclipse.ui,
1616
org.eclipse.m2e.jdt;resolution:=optional,
1717
org.eclipse.m2e.core;resolution:=optional,
1818
org.eclipse.m2e.maven.runtime;resolution:=optional,
19-
org.eclipse.buildship.core;bundle-version="[3.0.0,3.1.0)";resolution:=optional,
19+
org.eclipse.buildship.core;bundle-version="[3.0.0,3.3.0)";resolution:=optional,
2020
net.sf.eclipsecs.core;bundle-version="8.18.0";resolution:=optional
2121
Bundle-ClassPath: .,
2222
lib/spring-javaformat-formatter-eclipse.jar,

spring-javaformat-eclipse/io.spring.javaformat.eclipse/src/io/spring/javaformat/eclipse/gradle/GradleProjectSettingsConfigurator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.util.Collection;
2122
import java.util.LinkedHashSet;
2223
import java.util.Optional;
2324
import java.util.Set;
@@ -30,7 +31,6 @@
3031
import org.eclipse.buildship.core.internal.workspace.FetchStrategy;
3132
import org.eclipse.buildship.core.internal.workspace.InternalGradleBuild;
3233
import org.eclipse.buildship.core.internal.workspace.InternalGradleWorkspace;
33-
import org.eclipse.buildship.core.internal.workspace.ModelProviderUtil;
3434
import org.eclipse.core.resources.IProject;
3535
import org.eclipse.core.runtime.CoreException;
3636
import org.eclipse.core.runtime.IProgressMonitor;
@@ -74,16 +74,16 @@ private void configureProject(IProject project, IProgressMonitor monitor) throws
7474
InternalGradleWorkspace workspace = CorePlugin.internalGradleWorkspace();
7575
Optional<GradleBuild> build = workspace.getBuild(project);
7676
if (build.isPresent()) {
77-
Set<EclipseProject> projects = ModelProviderUtil.fetchAllEclipseProjects((InternalGradleBuild) build.get(),
78-
this.tokenSource, FetchStrategy.FORCE_RELOAD, monitor);
77+
Collection<EclipseProject> projects = ((InternalGradleBuild) build.get()).getModelProvider()
78+
.fetchModels(EclipseProject.class, FetchStrategy.FORCE_RELOAD, this.tokenSource, monitor);
7979
if (hasSpringFormatPlugin(projects)) {
8080
ProjectSettingsFilesLocator locator = new ProjectSettingsFilesLocator(getSearchFolders(projects));
8181
locator.locateSettingsFiles().applyToProject(project, monitor);
8282
}
8383
}
8484
}
8585

86-
private boolean hasSpringFormatPlugin(Set<EclipseProject> projects) {
86+
private boolean hasSpringFormatPlugin(Collection<EclipseProject> projects) {
8787
for (EclipseProject project : projects) {
8888
for (GradleTask task : project.getGradleProject().getTasks()) {
8989
if (isSpringFormatPlugin(task)) {
@@ -98,7 +98,7 @@ private boolean isSpringFormatPlugin(GradleTask task) {
9898
return TASK_NAME.equals(task.getName());
9999
}
100100

101-
private Set<File> getSearchFolders(Set<EclipseProject> projects) {
101+
private Set<File> getSearchFolders(Collection<EclipseProject> projects) {
102102
Set<File> searchFolders = new LinkedHashSet<>();
103103
for (EclipseProject project : projects) {
104104
while (project != null) {

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)