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

Progress with Eclipse CleanUp as Refactorer #423

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
119 changes: 119 additions & 0 deletions java-eclipse-cleanup/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.github.solven-eu.cleanthat</groupId>
<artifactId>aggregator-cleanthat</artifactId>
<version>2.7-SNAPSHOT</version>
</parent>

<artifactId>java-eclipse-cleanup</artifactId>

<dependencyManagement>
<dependencies>
<!-- Commented-out as we observe unexpected formattings -->
<dependency>
<groupId>fr.jmini.ecentral</groupId>
<artifactId>eclipse-platform-dependencies</artifactId>
<version>${eclipse.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.github.solven-eu.cleanthat</groupId>
<artifactId>refactorer</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.solven-eu.cleanthat</groupId>
<artifactId>code-cleaners</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<!-- For Eclipse: 'Install New Software' over https://projectlombok.org/p2 -->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<!-- https://stackoverflow.com/questions/1829904/is-there-a-way-to-ignore-a-single-findbugs-warning -->
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>3.0.1u2</version>
<scope>provided</scope>
</dependency>

<!-- https://github.com/revelc/formatter-maven-plugin/blob/master/pom.xml#L278 -->
<dependency>
<!-- Used to generate Eclipse stylesheet -->
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.core</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.jdt/org.eclipse.jdt.core.manipulation -->
<dependency>
<!-- https://bugs.eclipse.org/bugs/show_bug.cgi?id=178429 -->
<!-- Used to rely on Eclipse CleanUp actions -->
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.core.manipulation</artifactId>
</dependency>

<dependency>
<!-- Used to parse Eclipse XML config files -->
<groupId>org.apache.commons</groupId>
<artifactId>commons-digester3</artifactId>
<version>3.2</version>
</dependency>

<dependency>
<!-- https://github.com/google/google-java-format -->
<groupId>com.google.googlejavaformat</groupId>
<artifactId>google-java-format</artifactId>
<version>1.15.0</version>
</dependency>

<dependency>
<!-- https://github.com/spring-io/spring-javaformat -->
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-formatter</artifactId>
<version>0.0.38</version>
</dependency>

<dependency>
<!-- Used to clean unexpected changes by Javaparser -->
<groupId>io.github.java-diff-utils</groupId>
<artifactId>java-diff-utils</artifactId>
<version>4.12</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
<dependency>
<!-- Used to find longest common for diff -->
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>

<dependency>
<groupId>io.github.solven-eu.cleanthat</groupId>
<artifactId>test-helpers</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<!-- https://stackoverflow.com/questions/72625637/maven-build-failed-due-to-jdt-dependencies-no-versions-available-for-org-osgi -->
<!-- https://jmini.github.io/ecentral/ -->
<repositories>
<repository>
<id>ecentral</id>
<url>https://raw.githubusercontent.com/jmini/ecentral/HEAD/repo</url>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,11 +53,19 @@ public CleanUpContextCore makeContext(ICompilationUnit unit, CompilationUnit ast

public void simplifyRegex(Map<String, String> 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();
Expand All @@ -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";

Expand Down
Original file line number Diff line number Diff line change
@@ -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<CompilationUnit, CompilationUnit> {
private static final Logger LOGGER = LoggerFactory.getLogger(EclipseCleanupMutator.class);

final ICleanUpCore cleanup;

public EclipseCleanupMutator(ICleanUpCore cleanup) {
this.cleanup = cleanup;
}

@Override
public Optional<CompilationUnit> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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<CompilationUnit, ProxyForEclipseAstParser, CompilationUnit, EclipseCleanupMutator> {
public EclipseCleanupRefactorer(List<EclipseCleanupMutator> 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();
}

}
Loading