diff options
| author | Kostya <mail@sartin.in> | 2026-07-22 21:00:17 +0000 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-07-22 21:00:17 +0000 |
| commit | e9fd301bc6e5d8fc56a6b077cff7f3b7553633ac (patch) | |
| tree | 5331eadd8663e382216538ff2b506c65bfdd7777 /ex1 | |
| parent | b75f528e932c079bc71d00066b5432823c02f729 (diff) | |
| download | java-course-e9fd301bc6e5d8fc56a6b077cff7f3b7553633ac.tar.gz java-course-e9fd301bc6e5d8fc56a6b077cff7f3b7553633ac.tar.xz java-course-e9fd301bc6e5d8fc56a6b077cff7f3b7553633ac.zip | |
done i guess
Diffstat (limited to 'ex1')
| -rw-r--r-- | ex1/src/main/java/reflection/api/ReflectionInvestigator.java | 55 | ||||
| -rw-r--r-- | ex1/target/classes/reflection/api/ReflectionInvestigator.class | bin | 5264 -> 6962 bytes |
2 files changed, 38 insertions, 17 deletions
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 @@ | |||
| 1 | package reflection.api; | 1 | package reflection.api; |
| 2 | 2 | ||
| 3 | import java.lang.reflect.InvocationTargetException; | ||
| 3 | import java.lang.reflect.Modifier; | 4 | import java.lang.reflect.Modifier; |
| 4 | import java.util.Set; | 5 | import java.util.Set; |
| 5 | import java.util.stream.Collectors; | 6 | import java.util.stream.Collectors; |
| @@ -57,50 +58,70 @@ public class ReflectionInvestigator implements Investigator { | |||
| 57 | 58 | ||
| 58 | @Override | 59 | @Override |
| 59 | public boolean isExtending() { | 60 | public boolean isExtending() { |
| 60 | // TODO Auto-generated method stub | 61 | return classInfo.getSuperclass() != Object.class; |
| 61 | throw new UnsupportedOperationException("Unimplemented method 'isExtending'"); | ||
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | @Override | 64 | @Override |
| 65 | public String getParentClassSimpleName() { | 65 | public String getParentClassSimpleName() { |
| 66 | // TODO Auto-generated method stub | 66 | return classInfo.getSuperclass().getSimpleName(); |
| 67 | throw new UnsupportedOperationException("Unimplemented method 'getParentClassSimpleName'"); | ||
| 68 | } | 67 | } |
| 69 | 68 | ||
| 70 | @Override | 69 | @Override |
| 71 | public boolean isParentClassAbstract() { | 70 | public boolean isParentClassAbstract() { |
| 72 | // TODO Auto-generated method stub | 71 | return Modifier.isAbstract(classInfo.getSuperclass().getModifiers()); |
| 73 | throw new UnsupportedOperationException("Unimplemented method 'isParentClassAbstract'"); | ||
| 74 | } | 72 | } |
| 75 | 73 | ||
| 76 | @Override | 74 | @Override |
| 77 | public Set<String> getNamesOfAllFieldsIncludingInheritanceChain() { | 75 | public Set<String> getNamesOfAllFieldsIncludingInheritanceChain() { |
| 78 | // TODO Auto-generated method stub | 76 | return Stream.of(classInfo.getFields()).map(f -> f.getName()).collect(Collectors.toSet()); |
| 79 | throw new UnsupportedOperationException( | ||
| 80 | "Unimplemented method 'getNamesOfAllFieldsIncludingInheritanceChain'"); | ||
| 81 | } | 77 | } |
| 82 | 78 | ||
| 83 | @Override | 79 | @Override |
| 84 | public int invokeMethodThatReturnsInt(String methodName, Object... args) { | 80 | public int invokeMethodThatReturnsInt(String methodName, Object... args) { |
| 85 | // TODO Auto-generated method stub | 81 | try { |
| 86 | throw new UnsupportedOperationException("Unimplemented method 'invokeMethodThatReturnsInt'"); | 82 | return (int) classInfo.getMethod(methodName).invoke(subjectInstance, args); |
| 83 | } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | ||
| 84 | return 0; | ||
| 85 | } | ||
| 87 | } | 86 | } |
| 88 | 87 | ||
| 89 | @Override | 88 | @Override |
| 90 | public Object createInstance(int numberOfArgs, Object... args) { | 89 | public Object createInstance(int numberOfArgs, Object... args) { |
| 91 | // TODO Auto-generated method stub | 90 | var ctors = classInfo.getDeclaredConstructors(); |
| 92 | throw new UnsupportedOperationException("Unimplemented method 'createInstance'"); | 91 | for (var ctor : ctors) { |
| 92 | if (ctor.getGenericParameterTypes().length == numberOfArgs) { | ||
| 93 | try { | ||
| 94 | return ctor.newInstance(args); | ||
| 95 | } catch (InstantiationException | ||
| 96 | | IllegalAccessException | ||
| 97 | | IllegalArgumentException | ||
| 98 | | InvocationTargetException e) { | ||
| 99 | break; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | return null; | ||
| 93 | } | 104 | } |
| 94 | 105 | ||
| 95 | @Override | 106 | @Override |
| 96 | public Object elevateMethodAndInvoke(String name, Class<?>[] parametersTypes, Object... args) { | 107 | public Object elevateMethodAndInvoke(String name, Class<?>[] parametersTypes, Object... args) { |
| 97 | // TODO Auto-generated method stub | 108 | try { |
| 98 | throw new UnsupportedOperationException("Unimplemented method 'elevateMethodAndInvoke'"); | 109 | var method = classInfo.getMethod(name); |
| 110 | method.setAccessible(true); | ||
| 111 | return method.invoke(subjectInstance, args); | ||
| 112 | } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | ||
| 113 | return null; | ||
| 114 | } | ||
| 99 | } | 115 | } |
| 100 | 116 | ||
| 101 | @Override | 117 | @Override |
| 102 | public String getInheritanceChain(String delimiter) { | 118 | public String getInheritanceChain(String delimiter) { |
| 103 | // TODO Auto-generated method stub | 119 | var sb = new StringBuilder(); |
| 104 | throw new UnsupportedOperationException("Unimplemented method 'getInheritanceChain'"); | 120 | var cursor = classInfo; |
| 121 | while (cursor != null) { | ||
| 122 | sb.append(cursor.getName() + " -> "); | ||
| 123 | cursor = cursor.getSuperclass(); | ||
| 124 | } | ||
| 125 | return sb.toString(); | ||
| 105 | } | 126 | } |
| 106 | } | 127 | } |
diff --git a/ex1/target/classes/reflection/api/ReflectionInvestigator.class b/ex1/target/classes/reflection/api/ReflectionInvestigator.class index f4c2476..429f479 100644 --- a/ex1/target/classes/reflection/api/ReflectionInvestigator.class +++ b/ex1/target/classes/reflection/api/ReflectionInvestigator.class | |||
| Binary files differ | |||