2025 init
This commit is contained in:
39
code/javaparser-starter/src/main/java/fr/istic/vv/Main.java
Normal file
39
code/javaparser-starter/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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user