summaryrefslogtreecommitdiffstats
path: root/ex1/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'ex1/src/main/java')
-rw-r--r--ex1/src/main/java/reflection/api/ReflectionInvestigator.java55
1 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 @@
1package reflection.api; 1package reflection.api;
2 2
3import java.lang.reflect.InvocationTargetException;
3import java.lang.reflect.Modifier; 4import java.lang.reflect.Modifier;
4import java.util.Set; 5import java.util.Set;
5import java.util.stream.Collectors; 6import 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}