Q3
This commit is contained in:
15
code/Exercise3/AmazingClass.java
Normal file
15
code/Exercise3/AmazingClass.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
public class AmazingClass {
|
||||||
|
|
||||||
|
|
||||||
|
public void something() {
|
||||||
|
if(true){
|
||||||
|
while(true){
|
||||||
|
if(true){
|
||||||
|
if(true){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
69
code/Exercise5/pom.xml
Normal file
69
code/Exercise5/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>
|
||||||
39
code/Exercise5/src/main/java/fr/istic/vv/Main.java
Normal file
39
code/Exercise5/src/main/java/fr/istic/vv/Main.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
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());
|
||||||
|
PublicElementsPrinter printer = new PublicElementsPrinter();
|
||||||
|
root.parse("", (localPath, absolutePath, result) -> {
|
||||||
|
result.ifSuccessful(unit -> unit.accept(printer, null));
|
||||||
|
return SourceRoot.Callback.Result.DONT_SAVE;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package fr.istic.vv;
|
||||||
|
|
||||||
|
import com.github.javaparser.ast.CompilationUnit;
|
||||||
|
import com.github.javaparser.ast.body.*;
|
||||||
|
import com.github.javaparser.ast.visitor.VoidVisitorWithDefaults;
|
||||||
|
|
||||||
|
|
||||||
|
// This class visits a compilation unit and
|
||||||
|
// prints all public enum, classes or interfaces along with their public methods
|
||||||
|
public class PublicElementsPrinter extends VoidVisitorWithDefaults<Void> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(CompilationUnit unit, Void arg) {
|
||||||
|
for(TypeDeclaration<?> type : unit.getTypes()) {
|
||||||
|
type.accept(this, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void visitTypeDeclaration(TypeDeclaration<?> declaration, Void arg) {
|
||||||
|
if(!declaration.isPublic()) return;
|
||||||
|
System.out.println(declaration.getFullyQualifiedName().orElse("[Anonymous]"));
|
||||||
|
for(MethodDeclaration method : declaration.getMethods()) {
|
||||||
|
method.accept(this, arg);
|
||||||
|
}
|
||||||
|
// Printing nested types in the top level
|
||||||
|
for(BodyDeclaration<?> member : declaration.getMembers()) {
|
||||||
|
if (member instanceof TypeDeclaration)
|
||||||
|
member.accept(this, arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ClassOrInterfaceDeclaration declaration, Void arg) {
|
||||||
|
visitTypeDeclaration(declaration, arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(EnumDeclaration declaration, Void arg) {
|
||||||
|
visitTypeDeclaration(declaration, arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(MethodDeclaration declaration, Void arg) {
|
||||||
|
if(!declaration.isPublic()) return;
|
||||||
|
System.out.println(" " + declaration.getDeclarationAsString(true, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
BIN
code/javaparser-starter/target/classes/fr/istic/vv/Main.class
Normal file
BIN
code/javaparser-starter/target/classes/fr/istic/vv/Main.class
Normal file
Binary file not shown.
Binary file not shown.
@@ -23,3 +23,37 @@ Use your rule with different projects and describe you findings below. See the [
|
|||||||
|
|
||||||
## Answer
|
## Answer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ruleset name="Nested If Ruleset"
|
||||||
|
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
|
||||||
|
<description>
|
||||||
|
Ruleset pour détecter 3 niveaux (ou plus) de if imbriqués.
|
||||||
|
</description>
|
||||||
|
<rule name="nestedIfRule"
|
||||||
|
language="java"
|
||||||
|
message="nested If"
|
||||||
|
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
|
||||||
|
<description>
|
||||||
|
Three or more nested `if` !
|
||||||
|
</description>
|
||||||
|
<priority>3</priority>
|
||||||
|
<properties>
|
||||||
|
<property
|
||||||
|
name="xpath"
|
||||||
|
value =" //IfStatement[
|
||||||
|
descendant::IfStatement[
|
||||||
|
descendant::IfStatement
|
||||||
|
]
|
||||||
|
]"
|
||||||
|
/>
|
||||||
|
</properties>
|
||||||
|
</rule>
|
||||||
|
</ruleset>
|
||||||
|
```
|
||||||
|
|
||||||
|
en testant un programme Java, il détecte bien les imbrications de 'if' même s'ils ne sont pas directement imbriqués, séparés par un while par exemple
|
||||||
|
|||||||
Reference in New Issue
Block a user