push ex4 and ex5
This commit is contained in:
@@ -4,7 +4,14 @@ import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.*;
|
||||
import com.github.javaparser.ast.visitor.VoidVisitorWithDefaults;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class GetterChecker extends VoidVisitorWithDefaults<Void> {
|
||||
private final PrintWriter out;
|
||||
public GetterChecker(PrintWriter out) {
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(CompilationUnit unit, Void arg) {
|
||||
for(TypeDeclaration<?> type : unit.getTypes()) {
|
||||
@@ -16,13 +23,14 @@ public class GetterChecker extends VoidVisitorWithDefaults<Void> {
|
||||
public void visit(final ClassOrInterfaceDeclaration n, final Void arg) {
|
||||
if (!n.isPublic())
|
||||
return;
|
||||
System.out.println(n.getNameAsString());
|
||||
|
||||
out.println(n.getNameAsString());
|
||||
for (FieldDeclaration field : n.getFields()) {
|
||||
if (!field.isPrivate()) continue;
|
||||
for (VariableDeclarator variable : field.getVariables()) {
|
||||
boolean hasGetter = false;
|
||||
String fieldName = variable.getNameAsString();
|
||||
System.out.println("---------------"+fieldName+"---------------");
|
||||
out.println("---------------"+fieldName+"---------------");
|
||||
String getterName1 = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
|
||||
|
||||
for (MethodDeclaration method : n.getMethods()) {
|
||||
@@ -32,10 +40,10 @@ public class GetterChecker extends VoidVisitorWithDefaults<Void> {
|
||||
}
|
||||
}
|
||||
if (!hasGetter) {
|
||||
System.out.println(" Missing getter for field: " + fieldName);
|
||||
out.println(" Missing getter for field: " + fieldName);
|
||||
}
|
||||
}
|
||||
}
|
||||
super.visit(n, arg);
|
||||
out.println(" ");
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@@ -28,10 +29,13 @@ public class Main {
|
||||
}
|
||||
|
||||
SourceRoot root = new SourceRoot(file.toPath());
|
||||
GetterChecker checker = new GetterChecker();
|
||||
root.parse("", (localPath, absolutePath, result) -> {
|
||||
result.ifSuccessful(unit -> unit.accept(checker, null));
|
||||
return SourceRoot.Callback.Result.DONT_SAVE;
|
||||
});
|
||||
|
||||
try (PrintWriter writer = new PrintWriter("result_exercise4.txt")) {
|
||||
GetterChecker checker = new GetterChecker(writer);
|
||||
root.parse("", (localPath, absolutePath, result) -> {
|
||||
result.ifSuccessful(unit -> unit.accept(checker, null));
|
||||
return SourceRoot.Callback.Result.DONT_SAVE;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user