[ClassLoader 퀴즈 3] Whiteship은 Whiteship일까 아닐까?
Java : 2010. 2. 19. 16:46
퀴즈 2번 문제의 정답을 맞추신 분이라면 클래스패스와 클래스로더의 관계에 대해 잘 알고 계신 것 같습니다. temp 폴더는 클래스패스에 들어있지 않다고 했었죠. 따라서 URLClassLoader.loadClass를 실행했을 때 그것의 상위 클래스로더인 AppClassLoader가 읽어오지 못하고 자기 자신이 가져오게 됩니다.
따라서 결국 제가 원하던대로 whiteshipClass1과 whtieshipClass2는 각각 다른 클래스로더(uCL1, uCL2)들이 가져오게 됐습니다.
이쯤 말씀드렸으니.. 정답은 뭐... 당연한 거니까 패스.
@Test(expected = ClassCastException.class)
public void classCastException() throws Exception {
URL url = new URL("file:C:/intellij9-workspace/springsprout2/temp/");
URLClassLoader uCL1 = new URLClassLoader(new URL[]{url});
Class whiteshipClass1 = uCL1.loadClass("Whiteship");
Object whiteship1 = whiteshipClass1.newInstance();
URLClassLoader uCL2 = new URLClassLoader(new URL[]{url});
Class whiteshipClass2 = uCL2.loadClass("Whiteship");
Object whiteship2 = whiteshipClass2.newInstance();
assertThat(whiteshipClass1, is(not(whiteshipClass2)));
whiteshipClass1.cast(whiteship2);
}
이번에는 주관식입니다.
위 테스트는 통과 합니다.왜 통과하는 걸까요?
즉, 왜 ClassCastException이 발생하는 걸까요?
'Java' 카테고리의 다른 글
자바 클래스로더 입문 퀴즈 정리 (2) | 2010.02.23 |
---|---|
[ClassLoader 퀴즈 끝] SpringSprout와 WhiteshipFactory가 참조하는 Whiteship은 누구인가. (0) | 2010.02.23 |
[ClassLoader 퀴즈 6] SpringSprout는 과연 Whiteship의 이름을 알 수 있을까? (4) | 2010.02.22 |
[ClassLoader 퀴즈 5] Whiteship은 언제 로딩 될까? (2) | 2010.02.22 |
[ClassLoader 퀴즈 4] SpringSprout가 알고 있는 Whiteship은 누구인가? (10) | 2010.02.21 |
[ClassLoader 퀴즈 3] Whiteship은 Whiteship일까 아닐까? (2) | 2010.02.19 |
[ClassLoader 퀴즈 2] Whiteship은 내가 데려왔다!! (5) | 2010.02.19 |
[ClassLoader 퀴즈 1] Whiteship은 대체 누가 데려온 것일까? (8) | 2010.02.19 |
[Java] ClassLoader API (2) | 2010.02.17 |
[NullPE] SpEL 때문에 고민 해결 (0) | 2010.02.10 |
[NullPE] NullPointerException 때문에 고민 1 (2) | 2010.02.10 |
테스트가 성공하는건.. 머 다른 클래스 로더에 올라와있으니... 새로 각기 다를 것이고..
예외가 나는건.. whiteshipClass1 class객체를 whiteship2로 캐스팅 하는데..
uCL1 이곳에서는 whiteship2 이 어떤 놈인지 알수가 없는거죠 ...uCL2에서 살고 있는 놈이고 Object이긴 허나.. whiteship2가 먼지 알수가 없는 상황이죠 .. 여기서 캐스팅 하려니까 난 몰라~ 이러고 Exception을 내 뱉는듯;;;
아님.. 오라이;; ㅋㅋ
이것도 맞췄네;;
근데 저기서 cast() 안쓰고 CCE 나오게 하는 방법도 알아?