-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added reduction or equals and hashcode
- Loading branch information
Showing
6 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
app/src/main/java/de/lomboker/app/equalsAndHashCode/ReduceFuzzyEaH.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package de.lomboker.app.equalsAndHashCode; | ||
|
||
import picocli.CommandLine; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
|
||
import static de.lomboker.lib.FuzzyEqualsAndHashCode.reduceFuzzyEqualsAndHashCode; | ||
|
||
@CommandLine.Command(name = "equalsAndHashCode", description = "reduce equals and hash code") | ||
public class ReduceFuzzyEaH implements Runnable { | ||
|
||
@CommandLine.Parameters(index = "0") | ||
File file; | ||
|
||
public static void main(String[] args) { | ||
int exitCode = new CommandLine(new ReduceFuzzyEaH()).execute(args); | ||
System.exit(exitCode); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
String code = Files.readString(file.toPath()); | ||
String converted = reduceFuzzyEqualsAndHashCode(code); | ||
Files.writeString(file.toPath(), converted); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
lib/src/main/java/de/lomboker/lib/FuzzyEqualsAndHashCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
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.body.Parameter; | ||
import com.github.javaparser.ast.expr.AnnotationExpr; | ||
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter; | ||
|
||
import java.util.Optional; | ||
|
||
public class FuzzyEqualsAndHashCode { | ||
|
||
public static String reduceFuzzyEqualsAndHashCode(String code) { | ||
ClassWrapper wrapper = new ClassWrapper(code); | ||
|
||
CompilationUnit cu = wrapper.cu; | ||
LexicalPreservingPrinter.setup(cu); | ||
|
||
//the equals method, if it exists | ||
Optional<MethodDeclaration> equals = wrapper.methods | ||
.stream() | ||
.filter(FuzzyEqualsAndHashCode::isEquals) | ||
.findAny(); | ||
|
||
//the hashCode method, if it exists | ||
Optional<MethodDeclaration> hashCode = wrapper.methods | ||
.stream() | ||
.filter(FuzzyEqualsAndHashCode::isHashCode) | ||
.findAny(); | ||
|
||
if (equals.isPresent() && hashCode.isPresent()) { | ||
cu.addImport("lombok.EqualsAndHashCode"); | ||
|
||
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("EqualsAndHashCode"); | ||
|
||
//remove equals, hashCode | ||
equals.get().removeJavaDocComment(); | ||
equals.get().remove(); | ||
hashCode.get().removeJavaDocComment(); | ||
hashCode.get().remove(); | ||
} | ||
|
||
return LexicalPreservingPrinter.print(cu); | ||
|
||
} | ||
|
||
static boolean isEquals(MethodDeclaration md) { | ||
boolean isPublic = AccessSpecifier.PUBLIC.equals(md.getAccessSpecifier()); | ||
boolean isBoolean = "boolean".equals(md.getTypeAsString()); | ||
boolean oneParameter = md.getParameters().size() == 1; | ||
|
||
return isPublic | ||
&& isBoolean | ||
&& oneParameter | ||
&& paramIsObject(md); | ||
} | ||
|
||
static boolean paramIsObject(MethodDeclaration md) { | ||
var op = md.getParameters().getFirst(); | ||
if(op.isEmpty()) { | ||
return false; | ||
} | ||
|
||
Parameter p = op.get(); | ||
return "Object".equals(p.getTypeAsString()); | ||
} | ||
|
||
static boolean isHashCode(MethodDeclaration md) { | ||
boolean isPublic = AccessSpecifier.PUBLIC.equals(md.getAccessSpecifier()); | ||
boolean isInt = "int".equals(md.getTypeAsString()); | ||
boolean noParameter = md.getParameters().isEmpty(); | ||
|
||
return isPublic | ||
&& isInt | ||
&& noParameter; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package de.lomboker.lib; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.util.StringJoiner; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class FuzzyEaHTests extends FileTest { | ||
|
||
@Test | ||
public void testFuzzyEaH() throws IOException { | ||
String fileName = "Fuzzy/EqualsAndHashCode/EaH.java"; | ||
String fileNameRef = "Fuzzy/EqualsAndHashCode/EaH.reduced.java"; | ||
String input = readFile(fileName); | ||
String expected = readFile(fileNameRef); | ||
|
||
assertEquals(expected, FuzzyEqualsAndHashCode.reduceFuzzyEqualsAndHashCode(input)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class A { | ||
|
||
private int number = 5; | ||
|
||
public boolean equals(Object o) { | ||
return false; | ||
} | ||
|
||
public int hashCode() { | ||
return 0; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
lib/src/test/resources/Fuzzy/EqualsAndHashCode/EaH.reduced.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import lombok.EqualsAndHashCode; | ||
|
||
@EqualsAndHashCode | ||
class A { | ||
|
||
private int number = 5; | ||
|
||
} |