diff --git a/code/Exercise4/pom.xml b/code/Exercise4/pom.xml new file mode 100644 index 0000000..39ebc8b --- /dev/null +++ b/code/Exercise4/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + + fr.istic.vv + javaparser-starter + 1.0 + + + UTF-8 + 9 + 9 + + + + + com.github.javaparser + javaparser-core + 3.16.2 + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.1 + + + + true + fr.istic.vv.Main + + + + + + + + maven-assembly-plugin + + + + true + fr.istic.vv.Main + + + + jar-with-dependencies + + + + + make-my-jar-with-dependencies + package + + single + + + + + + + + \ No newline at end of file diff --git a/code/Exercise4/src/main/java/fr/istic/vv/Employe.java b/code/Exercise4/src/main/java/fr/istic/vv/Employe.java new file mode 100644 index 0000000..2d1697a --- /dev/null +++ b/code/Exercise4/src/main/java/fr/istic/vv/Employe.java @@ -0,0 +1,13 @@ +package fr.istic.vv; + +public class Employe { + private int age; + private String name; + private int num_badge; + + public String getName() { return name; } + + public boolean isAdult() { + return age > 17; + } +} \ No newline at end of file diff --git a/code/Exercise4/src/main/java/fr/istic/vv/GetterChecker.java b/code/Exercise4/src/main/java/fr/istic/vv/GetterChecker.java new file mode 100644 index 0000000..ee9cb54 --- /dev/null +++ b/code/Exercise4/src/main/java/fr/istic/vv/GetterChecker.java @@ -0,0 +1,41 @@ +package fr.istic.vv; + +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.body.*; +import com.github.javaparser.ast.visitor.VoidVisitorWithDefaults; + +public class GetterChecker extends VoidVisitorWithDefaults { + @Override + public void visit(CompilationUnit unit, Void arg) { + for(TypeDeclaration type : unit.getTypes()) { + type.accept(this, null); + } + } + + @Override + public void visit(final ClassOrInterfaceDeclaration n, final Void arg) { + if (!n.isPublic()) + return; + System.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+"---------------"); + String getterName1 = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); + + for (MethodDeclaration method : n.getMethods()) { + if (method.getNameAsString().equals(getterName1)) { + hasGetter = true; + break; + } + } + if (!hasGetter) { + System.out.println(" Missing getter for field: " + fieldName); + } + } + } + super.visit(n, arg); + } +} \ No newline at end of file diff --git a/code/Exercise4/src/main/java/fr/istic/vv/Main.java b/code/Exercise4/src/main/java/fr/istic/vv/Main.java new file mode 100644 index 0000000..1d69996 --- /dev/null +++ b/code/Exercise4/src/main/java/fr/istic/vv/Main.java @@ -0,0 +1,37 @@ +package fr.istic.vv; + +import com.github.javaparser.Problem; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; +import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.visitor.VoidVisitor; +import com.github.javaparser.ast.visitor.VoidVisitorAdapter; +import com.github.javaparser.utils.SourceRoot; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class Main { + + public static void main(String[] args) throws IOException { + if(args.length == 0) { + System.err.println("Should provide the path to the source code"); + System.exit(1); + } + + File file = new File(args[0]); + if(!file.exists() || !file.isDirectory() || !file.canRead()) { + System.err.println("Provide a path to an existing readable directory"); + System.exit(2); + } + + 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; + }); + } +} diff --git a/code/Exercise4/src/main/java/fr/istic/vv/Person.java b/code/Exercise4/src/main/java/fr/istic/vv/Person.java new file mode 100644 index 0000000..df741c6 --- /dev/null +++ b/code/Exercise4/src/main/java/fr/istic/vv/Person.java @@ -0,0 +1,12 @@ +package fr.istic.vv; + +public class Person { + private int age; + private String name; + + public String getName() { return name; } + + public boolean isAdult() { + return age > 17; + } +} \ No newline at end of file