From 94bba66887e3e96a02a3cae79a0570cd342a689a Mon Sep 17 00:00:00 2001 From: trochas Date: Fri, 28 Nov 2025 11:25:49 +0100 Subject: [PATCH] Q3 --- code/Exercise3/AmazingClass.java | 15 ++++ code/Exercise5/pom.xml | 69 ++++++++++++++++++ .../src/main/java/fr/istic/vv/Main.java | 39 ++++++++++ .../fr/istic/vv/PublicElementsPrinter.java | 48 ++++++++++++ .../target/classes/fr/istic/vv/Main.class | Bin 0 -> 3457 bytes .../fr/istic/vv/PublicElementsPrinter.class | Bin 0 -> 4317 bytes exercises/extending-pmd.md | 34 +++++++++ 7 files changed, 205 insertions(+) create mode 100644 code/Exercise3/AmazingClass.java create mode 100644 code/Exercise5/pom.xml create mode 100644 code/Exercise5/src/main/java/fr/istic/vv/Main.java create mode 100644 code/Exercise5/src/main/java/fr/istic/vv/PublicElementsPrinter.java create mode 100644 code/javaparser-starter/target/classes/fr/istic/vv/Main.class create mode 100644 code/javaparser-starter/target/classes/fr/istic/vv/PublicElementsPrinter.class diff --git a/code/Exercise3/AmazingClass.java b/code/Exercise3/AmazingClass.java new file mode 100644 index 0000000..6bbe7a0 --- /dev/null +++ b/code/Exercise3/AmazingClass.java @@ -0,0 +1,15 @@ +public class AmazingClass { + + + public void something() { + if(true){ + while(true){ + if(true){ + if(true){ + + } + } + } + } + } +} \ No newline at end of file diff --git a/code/Exercise5/pom.xml b/code/Exercise5/pom.xml new file mode 100644 index 0000000..39ebc8b --- /dev/null +++ b/code/Exercise5/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/Exercise5/src/main/java/fr/istic/vv/Main.java b/code/Exercise5/src/main/java/fr/istic/vv/Main.java new file mode 100644 index 0000000..04df2d0 --- /dev/null +++ b/code/Exercise5/src/main/java/fr/istic/vv/Main.java @@ -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; + }); + } + + +} diff --git a/code/Exercise5/src/main/java/fr/istic/vv/PublicElementsPrinter.java b/code/Exercise5/src/main/java/fr/istic/vv/PublicElementsPrinter.java new file mode 100644 index 0000000..7065232 --- /dev/null +++ b/code/Exercise5/src/main/java/fr/istic/vv/PublicElementsPrinter.java @@ -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 { + + @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)); + } + +} diff --git a/code/javaparser-starter/target/classes/fr/istic/vv/Main.class b/code/javaparser-starter/target/classes/fr/istic/vv/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..15c08582a5727a6e8d2622f5aa03ba9130b320f4 GIT binary patch literal 3457 zcmb7H>sAy;5dK;g2G$V}1VxQ53TAgvMocs&t3(BiB+GSi4VuLC?lv$oJF}S!k=)G9 z++*&^F;9@6i9aAm&+!3r@(y{CoSan8EV$sH%O9qzy7;>AHZ?^qF@U{ z^Ng+Oj;rUjg#~Sd>!yMTNw4t*t{L2%)y8tyMBZhnJEfbt+s{xFPh=S)16DyqQG*5* zwWwogO6#T=^@=%RPw|`~7+TU+o*P+i>+*M57;)!xNA9inngMet&(9=jQmnK{cd3bB%HE$9UibLT{?#ND}=Yx!%(^6b2b<^2Tg#Xbd3RK!wY zPQ`x27?hIqU=Xslco?uKg?vE8L3B|H#C4sL9f=PovI-6{s4E!H>jve!8*!QMm|P`Q z^x!a6hR!%HC6y|3S#I>I(B#He-5Jzvs-9&pN_n4(V^Xf5+p z9EIsxA$N#2Wx5#}SII9~B@fwDEQWZMfzgS8)Pio(!75V8 zur1?KhDLa)+z09nUd$DE*HMPwswqYoV?t%&H5-@woD_~{xSOwQSNUKI7%r)J8PmjO zo|nCd;aoM*mkBSghc@GxdD+9X0qSF3QP@PAD|l7GD@5(bMz7*ET&1?4&tyD`z;R|g zgCSNX*#-_0*#>ar733IBgu7UOb5}`S2Ct$3Vot;0*yz;N%-QS^!^tYyvfdQ2r@@e; zA!r9X!pcYpdCsv6&lQ1Io$VJ-(k&c7WreB7teriJrIqL)NORPJM#sD=!iFrUaqQG%`D4TeNG?vSV?MX3c0ZgidHkp76!{GIrl_MmhyCT(8hed{)q4O{7qHIu=%G*%w@ObE+ zHsk#$wY-m1OyMF0_8|$@Jz|2_A@QUcpGe)$RTN>8?q>|WkK*6@izF?4Nwkn9dvF5KQMGZM6WQCDENtC-=kH@ZonvX666|sVE_?Y z(L0#_D)dXEFFig`n_NcyAGFwlDE*b&Kr^<|Uo}vm?>73?fJQWtRx_=osqMa*e`p;c z$<^fjU(s|2tt}l(*c15^o%hf^ovb;wj3?7QOGw`+Lc~i*lbyX}bOfUWVHoWg!#IJOAaE0yBp(^lpnn>+|BL+!y0@551BMqso^d#95to2putf-O#7akp}!2%uFf;VxUzU{QSL9!3< mF>d;+Pw@r5!q>F7o8-6XLHli;oV>U2-I)%2k00?fI{pVq^5fqC literal 0 HcmV?d00001 diff --git a/code/javaparser-starter/target/classes/fr/istic/vv/PublicElementsPrinter.class b/code/javaparser-starter/target/classes/fr/istic/vv/PublicElementsPrinter.class new file mode 100644 index 0000000000000000000000000000000000000000..ef760365c9f3a3435a48571cebea5ccd4acaac64 GIT binary patch literal 4317 zcmbtXSz8=M6g>q)dNOGf!~_i4$u^K>Viq+Sj3xvWC6Gu&CB|3`4Rq+~9=m%mxbOSE z8-4e|r}%(heDI5}{tMsy6~Yj6#D*pQS_dfs}!%qg91a?gM zDLV-5Oloc}buK^Q*qMIE%35w1obzorw0r|Gf#Vr3o0_)6nfyfRiky=<=?9jdl0lf7 zvjaQy{M49dPmUGdpQPa#Yf9#wFc4VPYrA%MQlL56Jth$A^CqnXnz7nM3swoN8?s$% zIG>%c{0nlzu>{(Nyo_|lq;IQnG#LwLY@+QPYUpuVphbHna56bm+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@ -23,3 +23,37 @@ Use your rule with different projects and describe you findings below. See the [ ## Answer + + +```xml + + + + Ruleset pour détecter 3 niveaux (ou plus) de if imbriqués. + + + + Three or more nested `if` ! + + 3 + + + + + +``` + +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