Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions TAP4/src/dynamic/DynamicProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
* Created by milax on 20/10/15.
*/
public class DynamicProxy implements InvocationHandler {
private Object target = null;
private final Object target;
public static Object newInstance(Object target){
Class targetClass = target.getClass();
Class interfaces[] = targetClass.getInterfaces();
Class<?> targetClass = target.getClass();
Class<?>[] interfaces = targetClass.getInterfaces();
return Proxy. newProxyInstance(targetClass.getClassLoader(),
interfaces, new DynamicProxy(target));
}
private DynamicProxy(Object target) {
this.target = target;
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object invocationResult = null;
try
{
Expand All @@ -36,9 +36,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
{
System.err.println("Invocation of " + method.getName() + " failed");
}
finally{
return invocationResult;
}
return invocationResult;
}

}
12 changes: 6 additions & 6 deletions TAP4/src/reflection/InspectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ public class InspectTest {

public static void main(String[] args)
throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class aClass =Class.forName("reflection.Person");
Method nameMethod=aClass.getDeclaredMethod("getName");
Object newObject = aClass.newInstance();
String output =(String) nameMethod.invoke(newObject);
Class<?> aClass = Class.forName("reflection.Person");
Method nameMethod = aClass.getDeclaredMethod("getName");
Object newObject = aClass.getDeclaredConstructor().newInstance();
String output = (String) nameMethod.invoke(newObject);
System.out.println(output);

Person p1 = (Person)newObject;
Person p1 = (Person) newObject;
System.out.println(p1.getName());


for (Method method:aClass.getDeclaredMethods())
for (Method method : aClass.getDeclaredMethods())
System.out.println(method.getName());

}
Expand Down