exercise 4
This commit is contained in:
69
code/Exercise4/pom.xml
Normal file
69
code/Exercise4/pom.xml
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>fr.istic.vv</groupId>
|
||||||
|
<artifactId>javaparser-starter</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>9</maven.compiler.source>
|
||||||
|
<maven.compiler.target>9</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.javaparser</groupId>
|
||||||
|
<artifactId>javaparser-core</artifactId>
|
||||||
|
<version>3.16.2</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<!-- Creates a jar and sets the main class so it can be executed directly wit java -jar .-->
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<version>3.1.1</version>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<addClasspath>true</addClasspath>
|
||||||
|
<mainClass>fr.istic.vv.Main</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<!-- Creates a jar containing the compiled code and all dependencies. Useful for distribution. -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<addClasspath>true</addClasspath>
|
||||||
|
<mainClass>fr.istic.vv.Main</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
<descriptorRefs>
|
||||||
|
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||||
|
</descriptorRefs>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>make-my-jar-with-dependencies</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>single</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
13
code/Exercise4/src/main/java/fr/istic/vv/Employe.java
Normal file
13
code/Exercise4/src/main/java/fr/istic/vv/Employe.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
41
code/Exercise4/src/main/java/fr/istic/vv/GetterChecker.java
Normal file
41
code/Exercise4/src/main/java/fr/istic/vv/GetterChecker.java
Normal file
@@ -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<Void> {
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
37
code/Exercise4/src/main/java/fr/istic/vv/Main.java
Normal file
37
code/Exercise4/src/main/java/fr/istic/vv/Main.java
Normal file
@@ -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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
12
code/Exercise4/src/main/java/fr/istic/vv/Person.java
Normal file
12
code/Exercise4/src/main/java/fr/istic/vv/Person.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user