Skip to content

Commit

Permalink
fixed reduce noargs constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
timo-a committed Apr 10, 2021
1 parent 3c9194e commit f73c50d
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 12 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,3 @@ while read f; do lomboker mark setter "$f"; done < fuzzySetters.txt;
- test required for push
- build jars
- getter setter shall have their own line
- test noArgsConstructor
- test summarize getter/setter
45 changes: 38 additions & 7 deletions lib/src/main/java/de/lomboker/lib/NoArgsConstructor.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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.ConstructorDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
import org.checkerframework.checker.units.qual.cd;

import java.util.List;
import java.util.Map;
Expand All @@ -26,20 +28,39 @@ public static String reduceNoArgsConstructor(String code) {
List<ConstructorDeclaration> constructors = cu.findAll(ConstructorDeclaration.class);

for (var cd : constructors) {
if (cd.getBody().isEmpty()){
cu.addImport("lombok.NoArgsConstructor");

if (cd.getBody().isEmpty() && cd.getParameters().isEmpty()){
Optional<ClassOrInterfaceDeclaration> oFirstClass = cu.findFirst(ClassOrInterfaceDeclaration.class);

if(oFirstClass.isEmpty()) {
System.out.println("Error! no class found");
System.out.println("Error! no class found. Maybe an enum? we don't support those currently");
return LexicalPreservingPrinter.print(cu);
}

ClassOrInterfaceDeclaration firstClass = oFirstClass.get();

NodeList<AnnotationExpr> as = firstClass.getAnnotations();
firstClass.addMarkerAnnotation("NoArgsConstructor");
AccessSpecifier accessSpecifier = cd.getAccessSpecifier();
switch (accessSpecifier){
case PUBLIC:
firstClass.addMarkerAnnotation("NoArgsConstructor");
break;
case PROTECTED:
case PRIVATE:
case PACKAGE_PRIVATE:
cu.addImport("lombok.AccessLevel");

NameExpr n = new NameExpr("AccessLevel");

Expression accessLevel = new FieldAccessExpr(n, accessLevelMapper(accessSpecifier));

MemberValuePair mvp = new MemberValuePair("access", accessLevel);
NodeList<MemberValuePair> nodeList = NodeList.nodeList(mvp);
firstClass.addAnnotation(new NormalAnnotationExpr(new Name("NoArgsConstructor"), nodeList));
break;
default:
;
}
cu.addImport("lombok.NoArgsConstructor");


//remove no args constructor
cd.removeJavaDocComment();
Expand All @@ -49,4 +70,14 @@ public static String reduceNoArgsConstructor(String code) {
}
return LexicalPreservingPrinter.print(cu);
}

private static String accessLevelMapper(AccessSpecifier as){
switch (as) {
case PACKAGE_PRIVATE: return "PACKAGE";
case PRIVATE: return "PRIVATE";
case PROTECTED: return "PROTECTED";
case PUBLIC: return "PUBLIC";
}
return "error";
}
}
36 changes: 33 additions & 3 deletions lib/src/test/java/de/lomboker/lib/ReduceNoArgsConstructorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,39 @@
public class ReduceNoArgsConstructorTest extends FileTest {

@Test
public void testFuzzyToString() throws IOException {
String fileName = "Fuzzy/Constructor/NoArgs.java";
String fileNameRef = "Fuzzy/Constructor/NoArgs.reduced.java";
public void shouldConvertPublic() throws IOException {
String fileName = "Fuzzy/Constructor/NoArgs/Public.java";
String fileNameRef = "Fuzzy/Constructor/NoArgs/Public.reduced.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

assertEquals(expected, NoArgsConstructor.reduceNoArgsConstructor(input));
}

@Test
public void shouldConvertPrivate() throws IOException {
String fileName = "Fuzzy/Constructor/NoArgs/Private.java";
String fileNameRef = "Fuzzy/Constructor/NoArgs/Private.reduced.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

assertEquals(expected, NoArgsConstructor.reduceNoArgsConstructor(input));
}

@Test
public void shouldConvertProtected() throws IOException {
String fileName = "Fuzzy/Constructor/NoArgs/Protected.java";
String fileNameRef = "Fuzzy/Constructor/NoArgs/Protected.reduced.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

assertEquals(expected, NoArgsConstructor.reduceNoArgsConstructor(input));
}

@Test
public void shouldConvertPackage() throws IOException {
String fileName = "Fuzzy/Constructor/NoArgs/Package.java";
String fileNameRef = "Fuzzy/Constructor/NoArgs/Package.reduced.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

Expand Down
7 changes: 7 additions & 0 deletions lib/src/test/resources/Fuzzy/Constructor/NoArgs/Package.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class A {

A() {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PACKAGE)
class A {

}
7 changes: 7 additions & 0 deletions lib/src/test/resources/Fuzzy/Constructor/NoArgs/Private.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class A {

private A() {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
class A {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class A {

protected A() {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
class A {

}

0 comments on commit f73c50d

Please sign in to comment.