I've been stuck there for a while now trying to figure out what should be the utility class to call the method getDeclaredClasses on.
package com.codegym.task.task36.task3602;
import java.util.*;
/*
Find the class by its description
*/
public class Solution {
public static void main(String[] args) {
/*for(Class clazz : Collections.class.getDeclaredClasses()){
System.out.println(clazz.getSimpleName());
}*/
System.out.println(getExpectedClass());
}
public static <T extends List> Class<T> getExpectedClass() {
String className = "";
Class clazz = null;
Class<?>[] classes = Collections.class.getDeclaredClasses();
for (Class<?> c : classes){
try {
List<String> list = (List)c.newInstance();
list.add("Test1");
list.add("Test2");
list.add("Test3");
list.get(1);
}
catch (InstantiationException | IllegalAccessException ignored){}
catch (IndexOutOfBoundsException e){
className = c.getName();
break;
}
}
try{
clazz = Class.forName(className);
}
catch (ClassNotFoundException ignored){}
return clazz;
}
}