-
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.
require name match in equals and hash
- Loading branch information
Showing
3 changed files
with
104 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/toString/ReduceFuzzyToString.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.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(); | ||
} | ||
|
||
} | ||
|
||
} |
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,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; | ||
} | ||
|
||
} |