Skip to content

Commit

Permalink
Fixed marking, and annotation error
Browse files Browse the repository at this point in the history
  • Loading branch information
timo-a committed Mar 25, 2021
1 parent 6ef66d8 commit b147af5
Show file tree
Hide file tree
Showing 17 changed files with 345 additions and 56 deletions.
42 changes: 42 additions & 0 deletions lib/src/main/java/de/lomboker/lib/Fuzzy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.lomboker.lib;

import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.stmt.ExpressionStmt;

import java.util.Optional;

public class Fuzzy {

protected static void markWithStringLiteral(MethodDeclaration md, String message) {
Expression info = new StringLiteralExpr(message);
Expression e2 = new MethodCallExpr(info, "trim");

//it is callers responsibility to ensure that there is a body
md.getBody().get().getStatements().add(new ExpressionStmt(e2));
//md.getBody().get().getStatements().add(0, new ExpressionStmt(e2));
//inserting in first place messes up indentation
}

/* Discontinued: I was unable to find a way to add comments on their own line
* so they can be easily be removed
* and coexist with existing comments */
@Deprecated
private static MethodDeclaration addTodo(MethodDeclaration md, String message) {
Optional<Comment> c = md.getComment();
if (c.isEmpty()) {
md.setLineComment(message);
} else {
String content = c.get().getContent();
if (!content.isEmpty())
content += "\n ";
c.get().setContent(content + message);
}
return md;
}


}
10 changes: 8 additions & 2 deletions lib/src/main/java/de/lomboker/lib/FuzzyGetters.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

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

public class FuzzyGetters {
public class FuzzyGetters extends Fuzzy {

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

Expand All @@ -29,11 +31,15 @@ public static String markFuzzyGetters(String code){

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

return LexicalPreservingPrinter.print(cu);
}

private static void markMethod(MethodDeclaration md) {
markWithStringLiteral(md, CHECK_COMMENT);
}

private static boolean isNonTrivialGetter(MethodDeclaration md, Set<String> fields) {
if (!isGetter(md)) {
return false;
Expand Down
15 changes: 11 additions & 4 deletions lib/src/main/java/de/lomboker/lib/FuzzySetters.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.LineComment;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

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

public class FuzzySetters {
public class FuzzySetters extends Fuzzy {

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.
* Marks all Setters that are not trivial i.e. might need manual refactoring(renaming) first.
* */
public static String markFuzzySetters(String code){
CompilationUnit cu = StaticJavaParser.parse(code);
Expand All @@ -29,12 +32,16 @@ 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);});
.forEach(FuzzySetters::markMethod);

return LexicalPreservingPrinter.print(cu);
}

public static void markMethod(MethodDeclaration md) {
markWithStringLiteral(md, CHECK_COMMENT);
}


private static boolean isNonTrivialSetter(MethodDeclaration md, Set<String> fields) {
if (!isSetter(md)) {
return false;
Expand Down
15 changes: 12 additions & 3 deletions lib/src/main/java/de/lomboker/lib/Trivial.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.MarkerAnnotationExpr;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class Trivial {

public static boolean onlyTrivialAnnotations(MethodDeclaration md){
var annotations = md.getAnnotations();
annotations.remove(new MarkerAnnotationExpr("Override"));
return annotations.isEmpty();
List<String> names = md.getAnnotations()
.stream()
.map(a-> a.getName().asString())
.collect(Collectors.toList());

var whiteList = Set.of("Override");

return whiteList.containsAll(names);
}

}
44 changes: 40 additions & 4 deletions lib/src/test/java/de/lomboker/lib/FuzzyTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.StringJoiner;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -14,8 +15,8 @@ public class FuzzyTests {

@Test
public void testGetterMarker() throws IOException {
String fileName = "ClassBWithGetterInput.java";
String fileNameRef = "ClassBWithGetterComment.java";
String fileName = "Fuzzy/Getter.java";
String fileNameRef = "Fuzzy/Getter.marked.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

Expand All @@ -25,8 +26,8 @@ public void testGetterMarker() throws IOException {

@Test
public void testSetterMarker() throws IOException {
String fileName = "ClassBWithSetterInput.java";
String fileNameRef = "ClassBWithSetterComment.java";
String fileName = "Fuzzy/Setter.java";
String fileNameRef = "Fuzzy/Setter.marked.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

Expand All @@ -45,13 +46,48 @@ public void shouldKeepAnnotations() throws IOException {

}

@Test
public void shouldKeepAnnotationsDebug() throws IOException {
String input = new StringJoiner("\n")
.add("class A {")
.add("")
.add(" private int number;")
.add("")
.add(" @SuppressWarnings(\"javadoc\")")
.add(" @Override")
.add(" public void setTheNumber(int i) {")
.add(" number = i;")
.add(" }")
.add("")
.add("}")
.add("")
.toString();

String actual = FuzzySetters.markFuzzySetters(input);
System.out.println(actual);

}

@Test
public void shouldKeepJavadoc() throws IOException {
String fileName = "Fuzzy/SetterWithJavadoc.java";
String fileNameRef = "Fuzzy/SetterWithJavadoc.marked.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

String actual = FuzzySetters.markFuzzySetters(input);

assertEquals(expected, actual);

}

@Test
public void shouldSurviveEmptyJavadoc() throws IOException {
String fileName = "Fuzzy/SetterWithEmptyJavadoc.java";
String fileNameRef = "Fuzzy/SetterWithEmptyJavadoc.marked.java";
String input = readFile(fileName);
String expected = readFile(fileNameRef);

assertEquals(expected, FuzzySetters.markFuzzySetters(input));

}
Expand Down
Loading

0 comments on commit b147af5

Please sign in to comment.