Skip to content

Commit

Permalink
require name match in equals and hash
Browse files Browse the repository at this point in the history
  • Loading branch information
timo-a committed Apr 7, 2021
1 parent 36ced6e commit 53bf94e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/src/main/java/de/lomboker/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package de.lomboker.app;

import de.lomboker.app.equalsAndHashCode.ReduceFuzzyEaH;
import de.lomboker.app.toString.ReduceFuzzyToString;
import de.lomboker.lib.FuzzyEqualsAndHashCode;
import picocli.CommandLine;
import picocli.CommandLine.Command;
Expand All @@ -13,7 +14,8 @@
CounterApp.class,
Reduce.class,
Mark.class,
ReduceFuzzyEaH.class
ReduceFuzzyEaH.class,
ReduceFuzzyToString.class
})
public class App implements Runnable {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.lomboker.app.toString;

import picocli.CommandLine;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import static de.lomboker.lib.FuzzyToString.reduceFuzzyToString;

@CommandLine.Command(name = "toString", description = "reduce equals and hash code")
public class ReduceFuzzyToString implements Runnable {

@CommandLine.Parameters(index = "0")
File file;

public static void main(String[] args) {
int exitCode = new CommandLine(new ReduceFuzzyToString()).execute(args);
System.exit(exitCode);
}

@Override
public void run() {
try {
String code = Files.readString(file.toPath());
String converted = reduceFuzzyToString(code);
Files.writeString(file.toPath(), converted);
} catch (IOException e) {
e.printStackTrace();
}

}

}
67 changes: 67 additions & 0 deletions lib/src/main/java/de/lomboker/lib/FuzzyToString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package de.lomboker.lib;

import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;

import java.util.Optional;

import static de.lomboker.lib.Utils.nameMatch;

public class FuzzyToString {

public static String reduceFuzzyToString(String code) {
ClassWrapper wrapper = new ClassWrapper(code);

CompilationUnit cu = wrapper.cu;
LexicalPreservingPrinter.setup(cu);

//the equals method, if it exists
Optional<MethodDeclaration> toString = wrapper.methods
.stream()
.filter(FuzzyToString::isToString)
.findAny();

if (toString.isPresent()) {
cu.addImport("lombok.ToString");

cu.findAll(ClassOrInterfaceDeclaration.class);

//add annotation
Optional<ClassOrInterfaceDeclaration> oFirstClass = cu.findFirst(ClassOrInterfaceDeclaration.class);
if(oFirstClass.isEmpty()) {
System.out.println("Error! no class found");
return LexicalPreservingPrinter.print(cu);
}

ClassOrInterfaceDeclaration firstClass = oFirstClass.get();

NodeList<AnnotationExpr> as = firstClass.getAnnotations();
firstClass.addMarkerAnnotation("ToString");

//remove equals, hashCode
toString.get().removeJavaDocComment();
toString.get().remove();
}

return LexicalPreservingPrinter.print(cu);

}

static boolean isToString(MethodDeclaration md) {
boolean nameMatch = nameMatch(md, "toString");
boolean isPublic = AccessSpecifier.PUBLIC.equals(md.getAccessSpecifier());
boolean isString = "String".equals(md.getTypeAsString());
boolean noParameter = md.getParameters().size() == 0;

return nameMatch
&& isPublic
&& isString
&& noParameter;
}

}

0 comments on commit 53bf94e

Please sign in to comment.