Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
for issue #31
  • Loading branch information
DhillonTaran authored Nov 14, 2023
1 parent 990bfdf commit dca083f
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/main/java/org/codevillage/JavaEntityTypeFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.utils.SourceRoot;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;

public class JavaEntityTypeFinder {

public static Optional<String> getEntityType(String filePath) {
try {
Path path = Path.of(filePath);
SourceRoot sourceRoot = new SourceRoot(path);
CompilationUnit cu = sourceRoot.parse(path);
List<TypeDeclaration<?>> types = cu.getTypes();

if (!types.isEmpty()) {
TypeDeclaration<?> typeDeclaration = types.get(0); // assuming there's only one type in the file
SimpleName typeName = typeDeclaration.getName();
return Optional.of(typeDeclaration.getClass().getSimpleName() + ": " + typeName);
}
} catch (IOException e) {
e.printStackTrace();
}

return Optional.empty();
}

public static void main(String[] args) {
String filePath = "TestJavaFile.java"; // file path
Optional<String> entityType = getEntityType(filePath);

if (entityType.isPresent()) {
System.out.println("Entity Type: " + entityType.get());
} else {
System.out.println("Failed to determine entity type.");
}
}
}

0 comments on commit dca083f

Please sign in to comment.