2025 init

This commit is contained in:
Romain Lefeuvre
2025-11-18 14:41:54 +01:00
commit 7b185c9c0c
19 changed files with 417 additions and 0 deletions

3
code/Exercise3/README.md Normal file
View File

@@ -0,0 +1,3 @@
# Code of your exercise
Put here all the code created for this exercise

3
code/Exercise4/README.md Normal file
View File

@@ -0,0 +1,3 @@
# Code of your exercise
Put here all the code created for this exercise

3
code/Exercise5/README.md Normal file
View File

@@ -0,0 +1,3 @@
# Code of your exercise
Put here all the code created for this exercise

3
code/Exercise6/README.md Normal file
View File

@@ -0,0 +1,3 @@
# Code of your exercise
Put here all the code created for this exercise

View 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>

View 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;
});
}
}

View File

@@ -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));
}
}