-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
76 lines (51 loc) · 2.73 KB
/
Solution.java
File metadata and controls
76 lines (51 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//And I took the little book out of the angel's hand, and ate it up; and it was in my mouth sweet as honey: and as soon as I had eaten it, my belly was bitter. (Revelation 10:10)
package com.javarush.task.task36.task3602;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.List;
/*
Найти класс по описанию
*/
public class Solution {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
System.out.println(getExpectedClass());
}
public static Class getExpectedClass() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Class[] classes = Collections.class.getDeclaredClasses();
for (Class c : classes){
if(Modifier.isPrivate(c.getModifiers()))
if(Modifier.isStatic(c.getModifiers()))
{
if(List.class.isAssignableFrom(c))
{
try{
Constructor constructor = c.getDeclaredConstructor();
constructor.setAccessible(true);
List list = (List) constructor.newInstance();
list.get(0);
}catch (IndexOutOfBoundsException e){
return c;
} catch (NoSuchMethodException e) {
} catch (InvocationTargetException e) {
}
}
}
}
return null;
}
}
/*
Найти класс по описанию
Описание класса:
1. Реализует интерфейс List;
2. Является приватным статическим классом внутри популярного утилитного класса;
3. Доступ по индексу запрещен - кидается исключение IndexOutOfBoundsException.
Используя рефлекшн (метод getDeclaredClasses), верни подходящий тип в методе getExpectedClass.
Требования:
1. Метод getExpectedClass должен использовать метод getDeclaredClasses подходящего утилитного класса.
2. Метод getExpectedClass должен вернуть правильный тип.
3. Метод main должен вызывать метод getExpectedClass.
4. Метод main должен вывести полученный класс на экран.
*/