From e9fd301bc6e5d8fc56a6b077cff7f3b7553633ac Mon Sep 17 00:00:00 2001 From: Kostya Date: Wed, 22 Jul 2026 21:00:17 +0000 Subject: done i guess --- .../reflection/api/ReflectionInvestigator.java | 55 +++++++++++++++------- 1 file changed, 38 insertions(+), 17 deletions(-) (limited to 'ex1/src/main/java') diff --git a/ex1/src/main/java/reflection/api/ReflectionInvestigator.java b/ex1/src/main/java/reflection/api/ReflectionInvestigator.java index 2d18b34..d6902bd 100644 --- a/ex1/src/main/java/reflection/api/ReflectionInvestigator.java +++ b/ex1/src/main/java/reflection/api/ReflectionInvestigator.java @@ -1,5 +1,6 @@ package reflection.api; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; import java.util.Set; import java.util.stream.Collectors; @@ -57,50 +58,70 @@ public class ReflectionInvestigator implements Investigator { @Override public boolean isExtending() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'isExtending'"); + return classInfo.getSuperclass() != Object.class; } @Override public String getParentClassSimpleName() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getParentClassSimpleName'"); + return classInfo.getSuperclass().getSimpleName(); } @Override public boolean isParentClassAbstract() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'isParentClassAbstract'"); + return Modifier.isAbstract(classInfo.getSuperclass().getModifiers()); } @Override public Set getNamesOfAllFieldsIncludingInheritanceChain() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException( - "Unimplemented method 'getNamesOfAllFieldsIncludingInheritanceChain'"); + return Stream.of(classInfo.getFields()).map(f -> f.getName()).collect(Collectors.toSet()); } @Override public int invokeMethodThatReturnsInt(String methodName, Object... args) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'invokeMethodThatReturnsInt'"); + try { + return (int) classInfo.getMethod(methodName).invoke(subjectInstance, args); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + return 0; + } } @Override public Object createInstance(int numberOfArgs, Object... args) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'createInstance'"); + var ctors = classInfo.getDeclaredConstructors(); + for (var ctor : ctors) { + if (ctor.getGenericParameterTypes().length == numberOfArgs) { + try { + return ctor.newInstance(args); + } catch (InstantiationException + | IllegalAccessException + | IllegalArgumentException + | InvocationTargetException e) { + break; + } + } + } + return null; } @Override public Object elevateMethodAndInvoke(String name, Class[] parametersTypes, Object... args) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'elevateMethodAndInvoke'"); + try { + var method = classInfo.getMethod(name); + method.setAccessible(true); + return method.invoke(subjectInstance, args); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + return null; + } } @Override public String getInheritanceChain(String delimiter) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getInheritanceChain'"); + var sb = new StringBuilder(); + var cursor = classInfo; + while (cursor != null) { + sb.append(cursor.getName() + " -> "); + cursor = cursor.getSuperclass(); + } + return sb.toString(); } } -- cgit v1.2.3