-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |