-
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.
reduce noargsconstructor and summarize getter setter
- Loading branch information
Showing
8 changed files
with
221 additions
and
8 deletions.
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
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,20 @@ | ||
package de.lomboker.app; | ||
|
||
import de.lomboker.app.constructor.ReduceNoArgsConstructor; | ||
import de.lomboker.app.getter.ReduceGetter; | ||
import de.lomboker.app.setter.ReduceSetter; | ||
import de.lomboker.app.summarize.SummarizeGetterSetter; | ||
import picocli.CommandLine.Command; | ||
|
||
@Command(name = "summarize", | ||
subcommands = { | ||
SummarizeGetterSetter.class}, | ||
description = "no options or positional parameters") | ||
public class Summarize implements Runnable { | ||
|
||
@Override | ||
public void run() { | ||
System.out.println("I'm Summarize. You need to call a subcommand like gs"); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
app/src/main/java/de/lomboker/app/constructor/ReduceNoArgsConstructor.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,33 @@ | ||
/* | ||
* This Java source file was generated by the Gradle 'init' task. | ||
*/ | ||
package de.lomboker.app.constructor; | ||
|
||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Parameters; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
|
||
import static de.lomboker.lib.NoArgsConstructor.reduceNoArgsConstructor; | ||
import static de.lomboker.lib.TrivialGetters.reduceGetters; | ||
|
||
@Command(name = "constructor", description = "reduce no args constructor") | ||
public class ReduceNoArgsConstructor implements Runnable { | ||
|
||
@Parameters(index = "0") | ||
File file; | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
String code = Files.readString(file.toPath()); | ||
String converted = reduceNoArgsConstructor(code); | ||
Files.writeString(file.toPath(), converted); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/src/main/java/de/lomboker/app/summarize/SummarizeGetterSetter.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,30 @@ | ||
package de.lomboker.app.summarize; | ||
|
||
import picocli.CommandLine; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
|
||
import static de.lomboker.lib.SummarizeGetterSetter.summarizeGetters; | ||
import static de.lomboker.lib.SummarizeGetterSetter.summarizeSetters; | ||
|
||
@CommandLine.Command(name = "gs", description = "summarize getters and setters") | ||
public class SummarizeGetterSetter implements Runnable { | ||
@CommandLine.Parameters(index = "0") | ||
File file; | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
String code = Files.readString(file.toPath()); | ||
String convG = summarizeGetters(code); | ||
String convGS = summarizeSetters(convG); | ||
Files.writeString(file.toPath(), convGS); | ||
} 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
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
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,52 @@ | ||
package de.lomboker.lib; | ||
|
||
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.ConstructorDeclaration; | ||
import com.github.javaparser.ast.body.FieldDeclaration; | ||
import com.github.javaparser.ast.expr.AnnotationExpr; | ||
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class NoArgsConstructor { | ||
|
||
public static String reduceNoArgsConstructor(String code) { | ||
|
||
ClassWrapper wrapper = new ClassWrapper(code); | ||
|
||
CompilationUnit cu = wrapper.cu; | ||
LexicalPreservingPrinter.setup(cu); | ||
|
||
Map<String, FieldDeclaration> members = wrapper.fieldsByName; | ||
|
||
List<ConstructorDeclaration> constructors = cu.findAll(ConstructorDeclaration.class); | ||
|
||
for (var cd : constructors) { | ||
if (cd.getBody().isEmpty()){ | ||
cu.addImport("lombok.NoArgsConstructor"); | ||
|
||
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("NoArgsConstructor"); | ||
|
||
//remove no args constructor | ||
cd.removeJavaDocComment(); | ||
cd.remove(); | ||
|
||
} | ||
} | ||
return LexicalPreservingPrinter.print(cu); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
lib/src/main/java/de/lomboker/lib/SummarizeGetterSetter.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,63 @@ | ||
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.FieldDeclaration; | ||
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.ast.expr.MarkerAnnotationExpr; | ||
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import static de.lomboker.lib.Utils.nameMatch; | ||
|
||
public class SummarizeGetterSetter { | ||
|
||
|
||
public static String summarizeGetters(String code) { | ||
return summarizeFieldAnnotation(code, "Getter"); | ||
} | ||
|
||
public static String summarizeSetters(String code) { | ||
return summarizeFieldAnnotation(code, "Setter"); | ||
} | ||
|
||
public static String summarizeFieldAnnotation(String code, final String annotation) { | ||
ClassWrapper wrapper = new ClassWrapper(code); | ||
|
||
CompilationUnit cu = wrapper.cu; | ||
LexicalPreservingPrinter.setup(cu); | ||
|
||
List<FieldDeclaration> allNonStatic = wrapper.fields.stream() | ||
.filter(fd -> !fd.isStatic()).collect(Collectors.toList()); | ||
|
||
MarkerAnnotationExpr mae = new MarkerAnnotationExpr(annotation); | ||
|
||
boolean fullyAnnotated = allNonStatic.stream() | ||
.allMatch(fd -> fd.getAnnotations().contains(mae)); | ||
|
||
if (fullyAnnotated) { | ||
|
||
if(!wrapper.writeAnnotationToClass(annotation)) { | ||
System.out.println("Error! no class found"); | ||
return LexicalPreservingPrinter.print(cu); | ||
} | ||
|
||
|
||
allNonStatic.forEach(fd -> | ||
fd.getAnnotations().remove(mae)); | ||
|
||
} | ||
|
||
return LexicalPreservingPrinter.print(cu); | ||
|
||
} | ||
|
||
} |