From c73b6011dea3e650bb948303eca79ecccf9f7f37 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Fri, 24 Feb 2023 23:38:07 +0400 Subject: [PATCH 1/2] Progress with Eclipse CleanUp as Refactorer --- java-eclipse-cleanup/pom.xml | 119 ++++++++++++++++++ .../cleanup/EclipseCleanupProcessor.java | 27 ++-- .../refactorer/EclipseCleanupMutator.java | 83 ++++++++++++ .../refactorer/EclipseCleanupRefactorer.java | 71 +++++++++++ .../refactorer/ProxyForEclipseAstParser.java | 76 +++++++++++ pom.xml | 5 +- 6 files changed, 369 insertions(+), 12 deletions(-) create mode 100644 java-eclipse-cleanup/pom.xml rename {java-eclipse => java-eclipse-cleanup}/src/main/java/eu/solven/cleanthat/engine/java/eclipse/cleanup/EclipseCleanupProcessor.java (84%) create mode 100644 java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/EclipseCleanupMutator.java create mode 100644 java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/EclipseCleanupRefactorer.java create mode 100644 java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/ProxyForEclipseAstParser.java diff --git a/java-eclipse-cleanup/pom.xml b/java-eclipse-cleanup/pom.xml new file mode 100644 index 000000000..37d1e63a5 --- /dev/null +++ b/java-eclipse-cleanup/pom.xml @@ -0,0 +1,119 @@ + + + 4.0.0 + + + io.github.solven-eu.cleanthat + aggregator-cleanthat + 2.7-SNAPSHOT + + + java-eclipse-cleanup + + + + + + fr.jmini.ecentral + eclipse-platform-dependencies + ${eclipse.version} + pom + import + + + + + + + io.github.solven-eu.cleanthat + refactorer + ${project.version} + + + io.github.solven-eu.cleanthat + code-cleaners + ${project.version} + + + + + org.projectlombok + lombok + provided + + + + + com.google.code.findbugs + annotations + 3.0.1u2 + provided + + + + + + org.eclipse.jdt + org.eclipse.jdt.core + + + + + + org.eclipse.jdt + org.eclipse.jdt.core.manipulation + + + + + org.apache.commons + commons-digester3 + 3.2 + + + + + com.google.googlejavaformat + google-java-format + 1.15.0 + + + + + io.spring.javaformat + spring-javaformat-formatter + 0.0.38 + + + + + io.github.java-diff-utils + java-diff-utils + 4.12 + + + + + + org.apache.commons + commons-text + 1.10.0 + + + + io.github.solven-eu.cleanthat + test-helpers + ${project.version} + test + + + + + + + + ecentral + https://raw.githubusercontent.com/jmini/ecentral/HEAD/repo + + + diff --git a/java-eclipse/src/main/java/eu/solven/cleanthat/engine/java/eclipse/cleanup/EclipseCleanupProcessor.java b/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/eclipse/cleanup/EclipseCleanupProcessor.java similarity index 84% rename from java-eclipse/src/main/java/eu/solven/cleanthat/engine/java/eclipse/cleanup/EclipseCleanupProcessor.java rename to java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/eclipse/cleanup/EclipseCleanupProcessor.java index 4d7fa0b11..1ae24d53a 100644 --- a/java-eclipse/src/main/java/eu/solven/cleanthat/engine/java/eclipse/cleanup/EclipseCleanupProcessor.java +++ b/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/eclipse/cleanup/EclipseCleanupProcessor.java @@ -28,7 +28,8 @@ import org.eclipse.jdt.core.manipulation.CleanUpRequirementsCore; import org.eclipse.jdt.core.manipulation.ICleanUpFixCore; import org.eclipse.jdt.core.refactoring.CompilationUnitChange; -import org.eclipse.jdt.internal.ui.fix.PlainReplacementCleanUpCore; +import org.eclipse.jdt.internal.corext.fix.ICleanUpCore; +import org.eclipse.jdt.internal.ui.fix.InvertEqualsCleanUpCore; import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,11 +53,19 @@ public CleanUpContextCore makeContext(ICompilationUnit unit, CompilationUnit ast public void simplifyRegex(Map options, IJavaProject project, - ICompilationUnit[] units, + ICompilationUnit unit, IProgressMonitor monitor, CleanUpContextCore context) throws CoreException { - PlainReplacementCleanUpCore cleanup = new PlainReplacementCleanUpCore(options); - RefactoringStatus preStatus = cleanup.checkPreConditions(project, units, monitor); + ICleanUpCore cleanup = new InvertEqualsCleanUpCore(options); + applyCleanup(project, unit, monitor, context, cleanup); + } + + private void applyCleanup(IJavaProject project, + ICompilationUnit unit, + IProgressMonitor monitor, + CleanUpContextCore context, + ICleanUpCore cleanup) throws CoreException { + RefactoringStatus preStatus = cleanup.checkPreConditions(project, new ICompilationUnit[] { unit }, monitor); LOGGER.info("pre status: {}", preStatus); CleanUpRequirementsCore requirements = cleanup.getRequirementsCore(); @@ -77,14 +86,10 @@ public static void main(String[] args) { + "\n" + "public class CleanClass {\n" + "\n" - + " final LocalDate someLocalDate;\n" - + "\n" - + " final LocalDateTime someLocalDateTime;\n" + + " final boolean b;\n" + "\n" - + " public CleanClass(LocalDate someLocalDate, LocalDateTime someLocalDateTime) {\n" - + " super();\n" - + " this.someLocalDate = someLocalDate;\n" - + " this.someLocalDateTime = someLocalDateTime;\n" + + " public CleanClass(String a, String b) {\n" + + " this.b = a.equals(a + b);\n" + " }\n" + "}\n"; diff --git a/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/EclipseCleanupMutator.java b/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/EclipseCleanupMutator.java new file mode 100644 index 000000000..1850559a2 --- /dev/null +++ b/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/EclipseCleanupMutator.java @@ -0,0 +1,83 @@ +/* + * Copyright 2023 Benoit Lacelle - SOLVEN + * + * Licensed 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 eu.solven.cleanthat.engine.java.refactorer; + +import java.util.Optional; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.manipulation.CleanUpContextCore; +import org.eclipse.jdt.core.manipulation.CleanUpRequirementsCore; +import org.eclipse.jdt.core.manipulation.ICleanUpFixCore; +import org.eclipse.jdt.core.refactoring.CompilationUnitChange; +import org.eclipse.jdt.internal.corext.fix.ICleanUpCore; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import eu.solven.cleanthat.engine.java.refactorer.meta.IMutator; +import eu.solven.cleanthat.engine.java.refactorer.meta.IWalkingMutator; + +/** + * A {@link IMutator} configuring over an OpenRewrite {@link Recipe} + * + * @author Benoit Lacelle + * + */ +@Deprecated(since = "Not functional at all") +public class EclipseCleanupMutator implements IWalkingMutator { + private static final Logger LOGGER = LoggerFactory.getLogger(EclipseCleanupMutator.class); + + final ICleanUpCore cleanup; + + public EclipseCleanupMutator(ICleanUpCore cleanup) { + this.cleanup = cleanup; + } + + @Override + public Optional walkAst(CompilationUnit pre) { + try { + applyCleanup(null, (ICompilationUnit) pre, null, null, cleanup); + } catch (CoreException e) { + throw new RuntimeException(e); + } + + return Optional.empty(); + } + + private void applyCleanup(IJavaProject project, + ICompilationUnit unit, + IProgressMonitor monitor, + CleanUpContextCore context, + ICleanUpCore cleanup) throws CoreException { + RefactoringStatus preStatus = cleanup.checkPreConditions(project, new ICompilationUnit[] { unit }, monitor); + LOGGER.info("pre status: {}", preStatus); + + CleanUpRequirementsCore requirements = cleanup.getRequirementsCore(); + LOGGER.info("requirements: {}", requirements); + + ICleanUpFixCore fixed = cleanup.createFixCore(context); + CompilationUnitChange change = fixed.createChange(monitor); + LOGGER.info("change: {}", change); + + RefactoringStatus postStatus = cleanup.checkPostConditions(monitor); + LOGGER.info("post status: {}", postStatus); + } + +} diff --git a/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/EclipseCleanupRefactorer.java b/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/EclipseCleanupRefactorer.java new file mode 100644 index 000000000..00c817862 --- /dev/null +++ b/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/EclipseCleanupRefactorer.java @@ -0,0 +1,71 @@ +/* + * Copyright 2023 Benoit Lacelle - SOLVEN + * + * Licensed 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 eu.solven.cleanthat.engine.java.refactorer; + +import java.io.IOException; +import java.util.List; + +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.internal.core.dom.NaiveASTFlattener; +import org.eclipse.jdt.internal.corext.fix.ICleanUpCore; + +import eu.solven.cleanthat.engine.java.refactorer.meta.IMutator; + +/** + * A {@link IMutator} configuring over an Eclipse Cleanup {@link ICleanUpCore} + * + * @author Benoit Lacelle + * + */ +// https://help.eclipse.org/latest/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Fguide%2Fjdt_api_manip.htm +@Deprecated(since = "Not functional at all") +public class EclipseCleanupRefactorer + extends AAstRefactorer { + public EclipseCleanupRefactorer(List mutators) { + super(mutators); + } + + @Override + public String doFormat(String content) throws IOException { + return applyTransformers(content); + } + + @Override + public String getId() { + return "openrewrite"; + } + + @Override + protected ProxyForEclipseAstParser makeAstParser() { + return new ProxyForEclipseAstParser(AST.getJLSLatest(), JavaCore.VERSION_1_8); + } + + @Override + protected CompilationUnit parseSourceCode(ProxyForEclipseAstParser astParser, String sourceCode) { + return astParser.parseSourceCode(sourceCode); + } + + @Override + protected String toString(CompilationUnit compilationUnit) { + // see compilationUnit.toString(); + NaiveASTFlattener printer = new NaiveASTFlattener(); + compilationUnit.accept(printer); + return printer.getResult(); + } + +} diff --git a/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/ProxyForEclipseAstParser.java b/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/ProxyForEclipseAstParser.java new file mode 100644 index 000000000..d3fc1f59a --- /dev/null +++ b/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/ProxyForEclipseAstParser.java @@ -0,0 +1,76 @@ +package eu.solven.cleanthat.engine.java.refactorer; + +import java.util.Map; + +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.CompilationUnit; + +public class ProxyForEclipseAstParser { + + private int jlsVersion; + private String javaVersion; + + public ProxyForEclipseAstParser(int jlsVersion, String javaVersion) { + this.jlsVersion = jlsVersion; + this.javaVersion = javaVersion; + } + + public CompilationUnit parseSourceCode(String sourceCode) { + ASTParser parser = ASTParser.newParser(jlsVersion); + parser.setKind(ASTParser.K_COMPILATION_UNIT); + // parser.setSource(sourceCode.toCharArray()); + parser.setResolveBindings(true); + + Map options = JavaCore.getOptions(); + JavaCore.setComplianceOptions(javaVersion, options); + parser.setCompilerOptions(options); + + return (CompilationUnit) parser.createAST(null); + + // see org.eclipse.jdt.core.dom.CompilationUnitResolver.parse(String[], String[], FileASTRequestor, int, Map, + // int, IProgressMonitor) + // see org.eclipse.jdt.core.dom.ASTParser.createASTs(String[], String[], String[], FileASTRequestor, + // IProgressMonitor) + // see org.eclipse.jdt.core.dom.ASTParser.createAST(IProgressMonitor) + // javaParser.createASTs(new String[] {}, null, null, null, null); + + // org.eclipse.jdt.internal.compiler.batch.CompilationUnit compilationUnit = + // new org.eclipse.jdt.internal.compiler.batch.CompilationUnit(sourceCode.toCharArray(), + // "someFileName.java", + // StandardCharsets.UTF_8.name()); + // org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = compilationUnit; + // CompilationResult compilationResult = + // new CompilationResult(sourceUnit, 0, 0, compilerOptions.maxProblemsPerUnit); + // CompilationUnitDeclaration compilationUnitDeclaration = parser.dietParse(sourceUnit, compilationResult); + // + // if (compilationUnitDeclaration.ignoreMethodBodies) { + // compilationUnitDeclaration.ignoreFurtherInvestigation = true; + // // if initial diet parse did not work, no need to dig into method bodies. + // continue; + // } + + // fill the methods bodies in order for the code to be generated + // real parse of the method.... + // org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] types = compilationUnitDeclaration.types; + // if (types != null) { + // for (int j = 0, typeLength = types.length; j < typeLength; j++) { + // types[j].parseMethods(parser, compilationUnitDeclaration); + // } + // } + + // convert AST + // CompilationUnit node = CompilationUnitResolver.convert(compilationUnitDeclaration, + // parser.scanner.getSource(), + // apiLevel, + // options, + // false/* don't resolve binding */, + // null/* no owner needed */, + // null/* no binding table needed */, + // flags /* flags */, + // iterationMonitor, + // true); + // node.setTypeRoot(null); + } + +} diff --git a/pom.xml b/pom.xml index c267bb21b..a66746a41 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ java - java-eclipse + java-eclipse-cleanup @@ -35,6 +35,9 @@ test-helpers refactorer-test-helpers + + + java-eclipse maven From 7c408e0754c7d33b8375f302f751627cca5da8ea Mon Sep 17 00:00:00 2001 From: "cleanthat[bot]" <65607995+cleanthat[bot]@users.noreply.github.com> Date: Fri, 24 Feb 2023 19:39:46 +0000 Subject: [PATCH 2/2] engine=spotless nb_files_formatted: 1 nb_files_already_formatted: 5 eventKey: random-61283bc4-be85-4a52-8681-45d12c5c34c2 --- .../java/refactorer/ProxyForEclipseAstParser.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/ProxyForEclipseAstParser.java b/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/ProxyForEclipseAstParser.java index d3fc1f59a..3e390687b 100644 --- a/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/ProxyForEclipseAstParser.java +++ b/java-eclipse-cleanup/src/main/java/eu/solven/cleanthat/engine/java/refactorer/ProxyForEclipseAstParser.java @@ -1,3 +1,18 @@ +/* + * Copyright 2023 Benoit Lacelle - SOLVEN + * + * Licensed 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 eu.solven.cleanthat.engine.java.refactorer; import java.util.Map;