Skip to content

Commit

Permalink
fix boolean getter, have fuzzy not include trivials
Browse files Browse the repository at this point in the history
  • Loading branch information
timo-a committed Mar 22, 2021
1 parent 500f7e2 commit 45f4d8d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/src/main/java/de/lomboker/lib/FuzzyGetterMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.stream.Collectors;

import static de.lomboker.lib.TrivialGetters.isGetter;
import static de.lomboker.lib.TrivialGetters.isTrivialGetter;

public class FuzzyGetterMarker {

Expand All @@ -28,6 +29,7 @@ public static String markFuzzyGetters(String code){

cu.findAll(MethodDeclaration.class).stream()
.filter(md -> isNonTrivialGetter(md, fieldNames))
.filter(md -> !isTrivialGetter(md, fieldNames))
.forEach(md -> {md.setLineComment(CHECK_COMMENT);});

return LexicalPreservingPrinter.print(cu);
Expand Down
2 changes: 2 additions & 0 deletions lib/src/main/java/de/lomboker/lib/FuzzySetterMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.stream.Collectors;

import static de.lomboker.lib.TrivialSetters.isSetter;
import static de.lomboker.lib.TrivialSetters.isTrivialSetter;

public class FuzzySetterMarker {

Expand All @@ -27,6 +28,7 @@ public static String markFuzzySetters(String code){

cu.findAll(MethodDeclaration.class).stream()
.filter(md -> isNonTrivialSetter(md, fieldNames))
.filter(md -> !isTrivialSetter(md, fieldNames))
.forEach(md -> {md.setLineComment(CHECK_COMMENT);});

return LexicalPreservingPrinter.print(cu);
Expand Down
9 changes: 6 additions & 3 deletions lib/src/main/java/de/lomboker/lib/TrivialGetters.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,17 @@ public static boolean isTrivialGetter(MethodDeclaration md, Set<String> fields)
//assumptions: type is not void
private static boolean nameMatch(String methodName, String type, String variable) {

boolean correctPrefix = methodName.startsWith("boolean".equals(type) ? "has" : "get");
boolean longEnough = methodName.length() > Math.max("get".length(), "has".length());
String prefix = "boolean".equals(type) ? "is" : "get";

boolean correctPrefix = methodName.startsWith(prefix);
boolean longEnough = methodName.length() > prefix.length();

if (!correctPrefix || !longEnough) {
return false;
}

String tail = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
String tail = Character.toLowerCase(methodName.charAt(prefix.length()))
+ methodName.substring(prefix.length() + 1);
return tail.equals(variable);

}
Expand Down
11 changes: 11 additions & 0 deletions lib/src/test/java/de/lomboker/lib/TrivialTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public void testGetter() throws IOException {

}

@Test
public void testBooleanGetter() throws IOException {
String fileName = "ClassWithBooleanGetterInput.java";
String fileNameRef = "ClassWithBooleanGetterLombok.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

assertEquals(expected, TrivialGetters.reduceGetters(input));

}

@Test
public void testSetter() throws IOException {
String fileName = "ClassAWithSetterInput.java";
Expand Down
9 changes: 9 additions & 0 deletions lib/src/test/resources/ClassWithBooleanGetterInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class A {

private boolean b = true;

public boolean isB() {
return b;
}

}
8 changes: 8 additions & 0 deletions lib/src/test/resources/ClassWithBooleanGetterLombok.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import lombok.Getter;

class A {

@Getter
private boolean b = true;

}

0 comments on commit 45f4d8d

Please sign in to comment.