I can't seem to figure out a fix for the ClassCastException that my code throws.
package com.codegym.task.task32.task3206;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
/*
Generics for creating a proxy object
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
test(solution.getProxy(Item.class)); // true false false
test(solution.getProxy(Item.class, Small.class)); // true false true
test(solution.getProxy(Item.class, Big.class, Small.class));// true true true
test(solution.getProxy(Big.class, Small.class)); // true true true, because Big inherits Item
test(solution.getProxy(Big.class)); // true true false, because Big inherits Item
}
private static void test(Object proxy) {
boolean isItem = proxy instanceof Item;
boolean isBig = proxy instanceof Big;
boolean isSmall = proxy instanceof Small;
System.out.format("%b %b %b\n", isItem, isBig, isSmall);
}
public <T extends Item> T getProxy(Class<T> first, Class<?>... second) {
Class[] interfaces = first.getClass().getInterfaces();
List<Class> list = new ArrayList<>();
for (Class c: second) {
list.add(c);
}
for (Class s: interfaces) {
list.add(s);
}
Class[] allInterfaces = list.toArray(new Class[0]);
return (T)(Proxy.newProxyInstance(this.getClass().getClassLoader(), allInterfaces, new ItemInvocationHandler()));
}
}