|
5 | 5 | import java.util.Arrays; |
6 | 6 | import java.util.Set; |
7 | 7 |
|
8 | | -// Reflective instantiaion demo (Page 283) |
| 8 | +// 리플렉션으로 활용한 인스턴스화 데모 |
9 | 9 | public class ReflectiveInstantiation { |
10 | | - // Reflective instantiation with interface access |
| 10 | + // 코드 65-1 리플렉션으로 생성하고 인터페이스로 참조해 활용한다. (372-373쪽) |
11 | 11 | public static void main(String[] args) { |
12 | | - // Translate the class name into a Class object |
| 12 | + // 클래스 이름을 Class 객체로 변환 |
13 | 13 | Class<? extends Set<String>> cl = null; |
14 | 14 | try { |
15 | | - cl = (Class<? extends Set<String>>) // Unchecked cast! |
| 15 | + cl = (Class<? extends Set<String>>) // 비검사 형변환! |
16 | 16 | Class.forName(args[0]); |
17 | 17 | } catch (ClassNotFoundException e) { |
18 | | - fatalError("Class not found."); |
| 18 | + fatalError("클래스를 찾을 수 없습니다."); |
19 | 19 | } |
20 | 20 |
|
21 | | - // Get the constructor |
| 21 | + // 생성자를 얻는다. |
22 | 22 | Constructor<? extends Set<String>> cons = null; |
23 | 23 | try { |
24 | 24 | cons = cl.getDeclaredConstructor(); |
25 | 25 | } catch (NoSuchMethodException e) { |
26 | | - fatalError("No parameterless constructor"); |
| 26 | + fatalError("매개변수 없는 생성자를 찾을 수 없습니다."); |
27 | 27 | } |
28 | 28 |
|
29 | | - // Instantiate the set |
| 29 | + // 집합의 인스턴스를 만든다. |
30 | 30 | Set<String> s = null; |
31 | 31 | try { |
32 | 32 | s = cons.newInstance(); |
33 | 33 | } catch (IllegalAccessException e) { |
34 | | - fatalError("Constructor not accessible"); |
| 34 | + fatalError("생성자에 접근할 수 없습니다."); |
35 | 35 | } catch (InstantiationException e) { |
36 | | - fatalError("Class not instantiable."); |
| 36 | + fatalError("클래스를 인스턴스화할 수 없습니다."); |
37 | 37 | } catch (InvocationTargetException e) { |
38 | | - fatalError("Constructor threw " + e.getCause()); |
| 38 | + fatalError("생성자가 예외를 던졌습니다: " + e.getCause()); |
39 | 39 | } catch (ClassCastException e) { |
40 | | - fatalError("Class doesn't implement Set"); |
| 40 | + fatalError("Set을 구현하지 않은 클래스입니다."); |
41 | 41 | } |
42 | 42 |
|
43 | | - // Exercise the set |
| 43 | + // 생성한 집합을 사용한다. |
44 | 44 | s.addAll(Arrays.asList(args).subList(1, args.length)); |
45 | 45 | System.out.println(s); |
46 | 46 | } |
|
0 commit comments