Skip to content

Commit

Permalink
restrict fuzzy, relax getter
Browse files Browse the repository at this point in the history
  • Loading branch information
timo-a committed Mar 22, 2021
1 parent 9aeffff commit 500f7e2
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 100 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,27 @@ alias lomboker='java -jar ./app/build/libs/lomboker.jar'
#analysis
find . -name '*.java' | lomboker count > counts.txt
cat counts.txt | awk '{gt+=$2; gf+=$3; st+=$4; sf+=$5} END {printf " trivial fuzzy\ngetter %5d %5d\nsetter %5d %5d\n", gt,gf,st, sf}'
cat counts.txt | awk '{print $1 $2}' | grep " 0$" | awk '{print $1}' > getterClasses.txt
# reduce getters
cat counts.txt | awk '{print $1, $2}' | grep " 0$" | awk '{print $1}' > getterClasses.txt
while read f; do lomboker reduce getter "$f"; done < getterClasses.txt;
# put annotations on their own lines // capture first annotation (1) and white space before it (2)
cat counts.txt | xargs sed 's/^\(\(\s\{1,\}\)@\w\{1,\}\) @Getter/\1\n\2@Getter/' /tmp/test.java
cat counts.txt | xargs sed 's/^\(\(\s\{1,\}\)@\w\{1,\}(\([A-Za-z", \])\)\) @Getter/\1\n\2@Getter/'
# reduce setters
cat counts.txt | awk '{print $1, $4}' | grep " 0$" | awk '{print $1}' > setterClasses.txt
while read f; do lomboker reduce setter "$f"; done < setterClasses.txt;
# put annotations on their own lines // capture first annotation (1) and white space before it (2)
cat counts.txt | xargs sed 's/^\(\(\s\{1,\}\)@\w\{1,\}(\([A-Za-z", \])\)\) @Setter/\1\n\2@Setter/'
# mark fuzzy getters
cat counts.txt | awk '{print $1, $3}' | grep " 0$" | awk '{print $1}' > fuzzyGetters.txt
while read f; do lomboker mark getter "$f"; done < fuzzyGetters.txt;
# mark fuzzy setters
cat counts.txt | awk '{print $1, $5}' | grep " 0$" | awk '{print $1}' > fuzzySetters.txt
while read f; do lomboker mark setter "$f"; done < fuzzySetters.txt;
```


## TODO
- github actions
- test required for push
- build jars
- delete javadoc
- stop when there is annotation
- getter setter shall have their own line
2 changes: 1 addition & 1 deletion lib/src/main/java/de/lomboker/lib/FuzzyGetterMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class FuzzyGetterMarker {

private static final String CHECK_COMMENT = "Lomboker says check this potential getter";
private static final String CHECK_COMMENT = "TODO Lomboker says check this potential getter";

/**
* Marks all Getters that are not trivial i.e. might need manual refactoring(renaming) first.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/de/lomboker/lib/FuzzySetterMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class FuzzySetterMarker {

private static final String CHECK_COMMENT = "Lomboker says check this potential setter";
private static final String CHECK_COMMENT = "TODO Lomboker says check this potential setter";
/**
* Marks all Getters that are not trivial i.e. might need manual refactoring(renaming) first.
* */
Expand Down
14 changes: 14 additions & 0 deletions lib/src/main/java/de/lomboker/lib/Trivial.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.lomboker.lib;

import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.MarkerAnnotationExpr;

public class Trivial {

public static boolean onlyTrivialAnnotations(MethodDeclaration md){
var annotations = md.getAnnotations();
annotations.remove(new MarkerAnnotationExpr("Override"));
return annotations.isEmpty();
}

}
19 changes: 14 additions & 5 deletions lib/src/main/java/de/lomboker/lib/TrivialGetters.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.MarkerAnnotationExpr;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.javadoc.Javadoc;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
Expand All @@ -19,7 +21,7 @@
import java.util.stream.Collectors;


public class TrivialGetters {
public class TrivialGetters extends Trivial {

public static String reduceGetters(String code) {

Expand Down Expand Up @@ -97,8 +99,13 @@ private static Optional<Expression> getReturnStatement(MethodDeclaration md) {

Optional<Expression> oExpression = onlyStmt.asReturnStmt().getExpression();

if (oExpression.isEmpty()
|| oExpression.get().toString().contains(" "))
if (oExpression.isEmpty())
return Optional.empty();

var rExpr = oExpression.get().toString();
if ( rExpr.contains(" ")
|| !rExpr.startsWith("this.") && rExpr.contains(".") // return member.getX()
)
return Optional.empty();

return oExpression;
Expand All @@ -109,7 +116,7 @@ public static boolean isTrivialGetter(MethodDeclaration md, Set<String> fields)
return false;
}

if (!md.getAnnotations().isEmpty())
if (!onlyTrivialAnnotations(md))
return false;

//Verify that the name is what lombok would create
Expand All @@ -121,6 +128,7 @@ public static boolean isTrivialGetter(MethodDeclaration md, Set<String> fields)
return true;
}


//assumptions: type is not void
private static boolean nameMatch(String methodName, String type, String variable) {

Expand All @@ -146,7 +154,8 @@ private static String getReturned(MethodDeclaration md) {
return md.getBody().get().getStatements().stream()
.filter(Statement::isReturnStmt)
.reduce((first, second) -> second).get() //last element
.asReturnStmt().getExpression().get().toString();
.asReturnStmt().getExpression().get().toString()
.replace("this.", "");
}

/* Count number of getters */
Expand Down
5 changes: 3 additions & 2 deletions lib/src/main/java/de/lomboker/lib/TrivialSetters.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.expr.AssignExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.MarkerAnnotationExpr;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
Expand All @@ -20,7 +21,7 @@
import java.util.stream.Collectors;


public class TrivialSetters {
public class TrivialSetters extends Trivial {

public static String reduceSetters(String code) {

Expand Down Expand Up @@ -150,7 +151,7 @@ public static boolean isTrivialSetter(MethodDeclaration md, Set<String> fields)
return false;
}

if (!md.getAnnotations().isEmpty())
if (!onlyTrivialAnnotations(md))
return false;

//Verify that the name is what lombok would create
Expand Down
85 changes: 0 additions & 85 deletions lib/src/test/java/de/lomboker/lib/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,91 +12,6 @@

public class Tests {

@Test
public void testGetter() throws IOException {
String fileName = "ClassAWithGetterInput.java";
String fileNameRef = "ClassAWithLombokGetter.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

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

}

@Test
public void testSetter() throws IOException {
String fileName = "ClassAWithSetterInput.java";
String fileNameRef = "ClassAWithLombokSetter.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

assertEquals(expected, TrivialSetters.reduceSetters(input));

}

@Test
public void shouldRemoveJavadocForGetter() throws IOException {
String fileName = "ClassWithGetterWithJavadoc.java";
String fileNameRef = "ClassWithGetterWithJavadoc.reduced.g.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

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

@Test
public void shouldRemoveJavadocForSetter() throws IOException {
String fileName = "ClassWithSetterWithJavadoc.java";
String fileNameRef = "ClassWithSetterWithJavadoc.reduced.s.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

assertEquals(expected, TrivialSetters.reduceSetters(input));
}

@Test
public void shouldRejectGetterWithAnnotation() throws IOException {
String fileName = "ClassWithGetterWithAnnotation.java";
String input = readFile(fileName);
ClassWrapper cw = new ClassWrapper(input);

assertFalse(TrivialGetters.isTrivialGetter(cw.methods.get(0), cw.fieldNames));
}

@Test
public void shouldRejectSetterWithAnnotation() throws IOException {
String fileName = "ClassWithSetterWithAnnotation.java";
String input = readFile(fileName);
ClassWrapper cw = new ClassWrapper(input);

assertFalse(TrivialSetters.isTrivialSetter(cw.methods.get(0), cw.fieldNames));

}

//@Test functionality not implemented https://stackoverflow.com/questions/66698723/how-to-put-annotations-on-a-new-line-with-javaparser
// workaround: use command line tools
public void getterReducerShouldPlaceAnnotationOnNewLine() throws IOException {
String fileName = "ClassWithGetterWithAnnotationOnField.java";
String fileNameRef = "ClassWithGetterWithAnnotationOnField.reduced.g.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

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

}

//@Test
public void setterReducerShouldPlaceAnnotationOnNewLine() throws IOException {
String fileName = "ClassWithSetterWithAnnotationOnField.java";
String fileNameRef = "ClassWithSetterWithAnnotationOnField.reduced.s.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

assertEquals(expected, TrivialSetters.reduceSetters(input));

}


@Test
public void testGetterMarker() throws IOException {
String fileName = "ClassBWithGetterInput.java";
Expand Down
147 changes: 147 additions & 0 deletions lib/src/test/java/de/lomboker/lib/TrivialTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
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 static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class TrivialTests {

@Test
public void testGetter() throws IOException {
String fileName = "ClassAWithGetterInput.java";
String fileNameRef = "ClassAWithLombokGetter.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

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

}

@Test
public void testSetter() throws IOException {
String fileName = "ClassAWithSetterInput.java";
String fileNameRef = "ClassAWithLombokSetter.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

assertEquals(expected, TrivialSetters.reduceSetters(input));

}

@Test
public void shouldRecognizeGetterWithThis() throws IOException {
String fileName = "ClassWithGetterWithThis.java";
String fileNameRef = "ClassAWithLombokGetter.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

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

}

@Test
public void shouldRecognizeSetterWithThis() throws IOException {
String fileName = "ClassAWithSetterInput.java";
String fileNameRef = "ClassAWithLombokSetter.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

assertEquals(expected, TrivialSetters.reduceSetters(input));

}

@Test
public void shouldRemoveJavadocForGetter() throws IOException {
String fileName = "ClassWithGetterWithJavadoc.java";
String fileNameRef = "ClassWithGetterWithJavadoc.reduced.g.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

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

@Test
public void shouldRemoveJavadocForSetter() throws IOException {
String fileName = "ClassWithSetterWithJavadoc.java";
String fileNameRef = "ClassWithSetterWithJavadoc.reduced.s.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

assertEquals(expected, TrivialSetters.reduceSetters(input));
}

@Test
public void shouldRejectGetterWithAnnotation() throws IOException {
String fileName = "ClassWithGetterWithAnnotation.java";
String input = readFile(fileName);
ClassWrapper cw = new ClassWrapper(input);

assertFalse(TrivialGetters.isTrivialGetter(cw.methods.get(0), cw.fieldNames));
}

@Test
public void shouldRejectSetterWithAnnotation() throws IOException {
String fileName = "ClassWithSetterWithAnnotation.java";
String input = readFile(fileName);
ClassWrapper cw = new ClassWrapper(input);

assertFalse(TrivialSetters.isTrivialSetter(cw.methods.get(0), cw.fieldNames));

}

@Test
public void shouldAllowOverrideAsAnnotation() throws IOException {
String fileName = "ClassWithGetterOverride.java";
String fileNameRef = "ClassAWithLombokGetter.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

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

}


//@Test functionality not implemented https://stackoverflow.com/questions/66698723/how-to-put-annotations-on-a-new-line-with-javaparser
// workaround: use command line tools
public void getterReducerShouldPlaceAnnotationOnNewLine() throws IOException {
String fileName = "ClassWithGetterWithAnnotationOnField.java";
String fileNameRef = "ClassWithGetterWithAnnotationOnField.reduced.g.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

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

}

//@Test
public void setterReducerShouldPlaceAnnotationOnNewLine() throws IOException {
String fileName = "ClassWithSetterWithAnnotationOnField.java";
String fileNameRef = "ClassWithSetterWithAnnotationOnField.reduced.s.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

assertEquals(expected, TrivialSetters.reduceSetters(input));

}



private String readFile(String fileName) throws IOException {
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource(fileName);

if (resource == null)
throw new IllegalArgumentException("file not found! " + fileName);

File f = new File(resource.getFile());

return Files.readString(f.toPath());
}

}
4 changes: 4 additions & 0 deletions lib/src/test/java/de/lomboker/lib/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package de.lomboker.lib;

public class Utils {
}
Loading

0 comments on commit 500f7e2

Please sign in to comment.