Skip to content

Commit

Permalink
methods with annotations no longer trivial, javadoc is removed, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timo-a committed Mar 19, 2021
1 parent 68eab48 commit 9aeffff
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 24 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,33 @@ Lomboker helps you scale.

## Usage

Running from source
```
lomboker$ cp ./lib/src/test/resources/ClassAInput.java ./lib/src/test/resources/ClassAOutput.java
lomboker$ ./gradlew :app:run --args=".reduce getter ./lib/src/test/resources/ClassAInput.java"
```
Building from source
```
lomboker$ ./gradlew :app:assemble
lomboker$ alias lomboker='java -jar ./app/build/libs/lomboker.jar'
lomboker$ lomboker reduce getter lib/src/test/resources/ClassAInput.java
lomboker$ ls lib/src/test/resources/Class*.java | lomboker count | column -t
lomboker$ find . -name '*.java' | lomboker count | awk '{gt+=$2; gf+=$3; st+=$4; sf+=$5} END {printf " trivial fuzzy\ngetter %5d %5d\nsetter %5d %5d\n", gt,gf,st, sf}'
```
Converting a project
```
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
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
```


## TODO
- github actions
- test required for push
- build jars
- delete javadoc
- stop when there is annotation
- getter setter shall have their own line
8 changes: 8 additions & 0 deletions lib/src/main/java/de/lomboker/lib/ClassWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;

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

Expand All @@ -19,12 +21,18 @@ public class ClassWrapper {

public Set<String> fieldNames;

public Map<String, FieldDeclaration> fieldsByName = new HashMap<>();

public ClassWrapper(String code) {
this.cu = StaticJavaParser.parse(code);
this.fields = cu.findAll(FieldDeclaration.class);
this.methods = cu.findAll(MethodDeclaration.class);
this.fieldNames = fields.stream()
.map(f -> f.getVariable(0).getName().asString())
.collect(Collectors.toSet());

for (FieldDeclaration fd: this.fields) {
fieldsByName.put(fd.getVariable(0).getName().asString(), fd);
}
}
}
14 changes: 9 additions & 5 deletions lib/src/main/java/de/lomboker/lib/TrivialGetters.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import com.github.javaparser.ast.NodeList;
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.Expression;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.javadoc.Javadoc;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;

import java.util.*;
Expand All @@ -26,10 +28,7 @@ public static String reduceGetters(String code) {
CompilationUnit cu = wrapper.cu;
LexicalPreservingPrinter.setup(cu);

Map<String, FieldDeclaration> members = new HashMap<>();
for (FieldDeclaration fd: wrapper.fields) {
members.put(fd.getVariable(0).getName().asString(), fd);
}
Map<String, FieldDeclaration> members = wrapper.fieldsByName;

//all methods that return a field
Map<String, MethodDeclaration> methods = wrapper.methods
Expand All @@ -48,7 +47,9 @@ public static String reduceGetters(String code) {
//add annotation and remove method
for (String name: intersection) {
members.get(name).addMarkerAnnotation("Getter");
methods.get(name).remove();
var method = methods.get(name);
method.removeJavaDocComment();
method.remove();
}

return LexicalPreservingPrinter.print(cu);
Expand Down Expand Up @@ -108,6 +109,9 @@ public static boolean isTrivialGetter(MethodDeclaration md, Set<String> fields)
return false;
}

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

//Verify that the name is what lombok would create
String methodName = md.getNameAsString();
String type = md.getTypeAsString();
Expand Down
14 changes: 8 additions & 6 deletions lib/src/main/java/de/lomboker/lib/TrivialSetters.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public static String reduceSetters(String code) {
CompilationUnit cu = wrapper.cu;
LexicalPreservingPrinter.setup(cu);

Map<String, FieldDeclaration> members = new HashMap<>();
for (FieldDeclaration fd: wrapper.fields) {
members.put(fd.getVariable(0).getName().asString(), fd);
}
Map<String, FieldDeclaration> members = wrapper.fieldsByName;

//all methods that return a field
Map<String, MethodDeclaration> methods = wrapper.methods
Expand All @@ -52,7 +49,9 @@ public static String reduceSetters(String code) {
//add annotation and remove method
for (String name: intersection) {
members.get(name).addMarkerAnnotation("Setter");
methods.get(name).remove();
var method = methods.get(name);
method.removeJavaDocComment();
method.remove();
}

return LexicalPreservingPrinter.print(cu);
Expand Down Expand Up @@ -146,11 +145,14 @@ private static Optional<Expression> getSetStatement(MethodDeclaration md) {
* </ul>
*
*/
private static boolean isTrivialSetter(MethodDeclaration md, Set<String> fields) {
public static boolean isTrivialSetter(MethodDeclaration md, Set<String> fields) {
if (!isSetter(md)) {
return false;
}

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

//Verify that the name is what lombok would create
String methodName = md.getNameAsString();
if (fields.stream().noneMatch(f -> nameMatch(methodName, f)))
Expand Down
64 changes: 64 additions & 0 deletions lib/src/test/java/de/lomboker/lib/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.Files;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class Tests {

Expand All @@ -33,6 +34,69 @@ public void testSetter() throws IOException {

}

@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
31 changes: 23 additions & 8 deletions lib/src/test/java/de/lomboker/lib/TrivialSettersTest.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
package de.lomboker.lib;

import javax.annotation.Nonnull;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;

import org.junit.jupiter.api.Test;
import java.util.StringJoiner;

public class TrivialSettersTest {

@Test
public void testSetter() {
String classString = "class A { private int i; public void setI(int j){ this.i=j;}}";
public static void main(String[] args) {
System.out.println(annotate());
}

public static String annotate() {
String classString = new StringJoiner("\n")
.add("class A {")
.add("")
.add(" @ExistingAnnotation")
.add(" private int i;")
.add("")
.add("}")
.toString();

CompilationUnit cu = StaticJavaParser.parse(classString);
LexicalPreservingPrinter.setup(cu);

MethodDeclaration md = cu.findFirst(MethodDeclaration.class).get();
TrivialSetters.isSetter(md);
cu.findFirst(FieldDeclaration.class).get()
.addAnnotation("Annotation")
.addAnnotation(Nonnull.class)
.addMarkerAnnotation("MarkerAnnotation");

return LexicalPreservingPrinter.print(cu);
}

}
12 changes: 12 additions & 0 deletions lib/src/test/resources/ClassWithGetterWithAnnotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import javax.annotation.Nonnull;

class A {

private int number;

@Nonnull
public int getNumber() {
return number;
}

}
12 changes: 12 additions & 0 deletions lib/src/test/resources/ClassWithGetterWithAnnotationOnField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import javax.annotation.Nonnull;

class A {

@Nonnull
private int number;

public int getNumber() {
return number;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import javax.annotation.Nonnull;
import lombok.Setter;

class A {

@Nonnull @Getter
private int number;

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

private int number;

/**
* blabla
* @return
*/
public int getNumber() {
return number;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import lombok.Getter;

class A {

@Getter
private int number;

}
12 changes: 12 additions & 0 deletions lib/src/test/resources/ClassWithSetterWithAnnotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import javax.annotation.Nonnull;

class A {

private int number;

@Nonnull
public void setNumber(int i) {
number = i;
}

}
12 changes: 12 additions & 0 deletions lib/src/test/resources/ClassWithSetterWithAnnotationOnField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import javax.annotation.Nonnull;

class A {

@Nonnull
private int number;

public void setNumber(int i) {
number = i;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import javax.annotation.Nonnull;
import lombok.Setter;

class A {

@Nonnull @Setter
private int number;

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

private int number;

/**
* blabla
* @return
*/
public void setNumber(int i) {
number = i;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import lombok.Setter;

class A {

@Setter
private int number;

}

0 comments on commit 9aeffff

Please sign in to comment.