-
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
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/org/codevillage/EntityAssociationsAnalyzer.java
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,55 @@ | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class EntityAssociationsAnalyzer { | ||
|
||
public static Set<String> getAssociatedClassNames(Object entity) { | ||
Set<String> classNames = new HashSet<>(); | ||
Class<?> entityClass = entity.getClass(); | ||
|
||
// Inspect fields | ||
Field[] fields = entityClass.getDeclaredFields(); | ||
for (Field field : fields) { | ||
Class<?> fieldType = field.getType(); | ||
classNames.add(fieldType.getName()); | ||
} | ||
|
||
// Inspect methods | ||
Method[] methods = entityClass.getDeclaredMethods(); | ||
for (Method method : methods) { | ||
Class<?> returnType = method.getReturnType(); | ||
classNames.add(returnType.getName()); | ||
Class<?>[] parameterTypes = method.getParameterTypes(); | ||
for (Class<?> parameterType : parameterTypes) { | ||
classNames.add(parameterType.getName()); | ||
} | ||
} | ||
|
||
return classNames; | ||
} | ||
|
||
public static void main(String[] args) { | ||
// Example usage | ||
class Address { | ||
String street; | ||
String city; | ||
String state; | ||
} | ||
|
||
class Person { | ||
String name; | ||
int age; | ||
Address address; | ||
void speak() {} | ||
} | ||
|
||
Person person = new Person(); | ||
Set<String> associatedClassNames = getAssociatedClassNames(person); | ||
|
||
for (String className : associatedClassNames) { | ||
System.out.println(className); | ||
} | ||
} | ||
} |