1515import org .apache .maven .plugin .MojoFailureException ;
1616import org .apache .maven .plugins .annotations .LifecyclePhase ;
1717import org .apache .maven .plugins .annotations .Mojo ;
18+ import org .apache .maven .plugins .annotations .Parameter ;
19+ import org .apache .maven .project .MavenProject ;
20+
21+ import com .github .javaparser .StaticJavaParser ;
22+ import com .github .javaparser .ast .CompilationUnit ;
23+ import com .github .javaparser .ast .body .ClassOrInterfaceDeclaration ;
24+ import com .github .javaparser .ast .comments .JavadocComment ;
25+
26+ import java .io .File ;
27+ import java .io .FileNotFoundException ;
28+ import java .util .List ;
29+ import java .util .Optional ;
1830
1931@ Mojo (name = "hello" , defaultPhase = LifecyclePhase .PROCESS_CLASSES )
2032public class HelloMojo extends AbstractMojo {
33+
34+ private static final String TARGET_CLASS_NAME = "org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PatientResource1_8" ;
35+
36+ @ Parameter (defaultValue = "${project}" , required = true , readonly = true )
37+ private MavenProject project ;
38+
2139 @ Override
2240 public void execute () throws MojoExecutionException , MojoFailureException {
2341 getLog ().info ("Hello from OpenMRS Maven Plugin!" );
42+ getLog ().info ("Starting Javadoc parsing for target class: " + TARGET_CLASS_NAME );
43+
44+ // Convert fully qualified class name to relative path
45+ String classRelativePath = TARGET_CLASS_NAME .replace ('.' , File .separatorChar ) + ".java" ;
46+
47+ // Extract simple class name for later use
48+ String targetClassSimpleName = TARGET_CLASS_NAME .substring (TARGET_CLASS_NAME .lastIndexOf ('.' ) + 1 );
49+
50+ // Try to find the source file in compile source roots
51+ File targetSourceFile = null ;
52+ List <String > sourceRoots = project .getCompileSourceRoots ();
53+
54+ for (String sourceRoot : sourceRoots ) {
55+ File possibleFile = new File (sourceRoot , classRelativePath );
56+ if (possibleFile .exists ()) {
57+ targetSourceFile = possibleFile ;
58+ getLog ().info ("Found source file: " + possibleFile .getAbsolutePath ());
59+ break ;
60+ }
61+ }
62+
63+ if (targetSourceFile == null ) {
64+ getLog ().warn ("Could not find source file for class: " + TARGET_CLASS_NAME );
65+ return ;
66+ }
67+
68+ // Parse with JavaParser
69+ try {
70+ CompilationUnit cu = StaticJavaParser .parse (targetSourceFile );
71+ Optional <ClassOrInterfaceDeclaration > classOptional = cu .getClassByName (targetClassSimpleName );
72+
73+ if (classOptional .isPresent ()) {
74+ ClassOrInterfaceDeclaration classDecl = classOptional .get ();
75+ Optional <JavadocComment > javadocOptional = classDecl .getJavadocComment ();
76+
77+ if (javadocOptional .isPresent ()) {
78+ String javadocText = javadocOptional .get ().getContent ();
79+ getLog ().info ("Class Javadoc for " + TARGET_CLASS_NAME + ":\n " + javadocText );
80+ } else {
81+ getLog ().info ("No class-level Javadoc found for " + TARGET_CLASS_NAME );
82+ }
83+ } else {
84+ getLog ().warn ("Could not find class " + targetClassSimpleName + " in parsed file " + targetSourceFile .getName ());
85+ }
86+ } catch (FileNotFoundException e ) {
87+ getLog ().error ("Could not find source file for Javadoc parsing: " + targetSourceFile .getAbsolutePath (), e );
88+ } catch (Exception e ) {
89+ getLog ().error ("Error parsing Javadoc for " + TARGET_CLASS_NAME , e );
90+ }
2491 }
2592}
0 commit comments