From 1821e23b0869bc476fabeffd971f3c91669a56f5 Mon Sep 17 00:00:00 2001 From: DhillonTaran <132304214+DhillonTaran@users.noreply.github.com> Date: Mon, 13 Nov 2023 16:34:48 -0800 Subject: [PATCH] Add files via upload for issue #35 --- .../EntityAssociationsAnalyzer.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main/java/org/codevillage/EntityAssociationsAnalyzer.java diff --git a/src/main/java/org/codevillage/EntityAssociationsAnalyzer.java b/src/main/java/org/codevillage/EntityAssociationsAnalyzer.java new file mode 100644 index 00000000..315eea56 --- /dev/null +++ b/src/main/java/org/codevillage/EntityAssociationsAnalyzer.java @@ -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 getAssociatedClassNames(Object entity) { + Set 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 associatedClassNames = getAssociatedClassNames(person); + + for (String className : associatedClassNames) { + System.out.println(className); + } + } +}