Skip to content

Commit 066d0df

Browse files
committed
Added javaparser PoC successfully
1 parent c9ffe2c commit 066d0df

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

omod/pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,12 @@
184184
<plugin>
185185
<groupId>org.apache.maven.plugins</groupId>
186186
<artifactId>maven-dependency-plugin</artifactId>
187-
<executions>
188-
<execution>
187+
<executions> <execution>
189188
<id>Expand resources</id>
190189
<goals>
191190
<goal>unpack-dependencies</goal>
192191
</goals>
193-
<phase>generate-resources</phase>
192+
<phase>prepare-package</phase>
194193
<configuration>
195194
<includeGroupIds>${project.parent.groupId}</includeGroupIds>
196195
<includeArtifactIds>${project.parent.artifactId}-omod-common</includeArtifactIds>

tools/openapi-generator-maven-plugin/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
<version>3.8.6</version>
4343
<scope>provided</scope>
4444
</dependency>
45+
46+
<!-- JavaParser for extracting Javadoc -->
47+
<dependency>
48+
<groupId>com.github.javaparser</groupId>
49+
<artifactId>javaparser-core</artifactId>
50+
<version>3.25.5</version>
51+
</dependency>
4552
</dependencies>
4653

4754
<build>

tools/openapi-generator-maven-plugin/src/main/java/org/openmrs/plugin/HelloMojo.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,78 @@
1515
import org.apache.maven.plugin.MojoFailureException;
1616
import org.apache.maven.plugins.annotations.LifecyclePhase;
1717
import 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)
2032
public 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

Comments
 (0)