При выполнении выдаёт следующее:
...тут Connected to the target VM и т.д....
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.javarush.task.task36.task3602.Solution (file:/C:/JavaRushTasks/out/production/4.JavaCollections/) to method java.util.Collections$CopiesList.get(int)
WARNING: Please consider reporting this to the maintainers of com.javarush.task.task36.task3602.Solution
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Есть идеи?
package com.javarush.task.task36.task3602;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
/*
Найти класс по описанию
*/
public class Solution {
public static void main(String[] args) {
System.out.println(getExpectedClass());
}
public static Class getExpectedClass() {
Class<?>[] classes = Collections.class.getDeclaredClasses();
for (Class<?> clazz : classes){
if (isImplementingList(clazz) && Modifier.isStatic(clazz.getModifiers()) && Modifier.isPrivate(clazz.getModifiers())){
try {
Method method = clazz.getDeclaredMethod("get", int.class);
method.setAccessible(true);
Constructor<?> constructor = clazz.getConstructor();
constructor.setAccessible(true);
method.invoke(constructor.newInstance(), 2);
}
catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {}
catch (InvocationTargetException e){
if (e.getCause().toString().contains("IndexOutOfBoundsException")) return clazz;
}
}
}
return null;
}
public static boolean isImplementingList(Class<?> clazz) {
ArrayList<Class<?>> classInterfaces = new ArrayList<>(Arrays.asList(clazz.getInterfaces()));
ArrayList<Class<?>> parentInterfaces = new ArrayList<>(Arrays.asList(clazz.getSuperclass().getInterfaces()));
return classInterfaces.contains(List.class) || parentInterfaces.contains(List.class);
}
}
/*
*/