"์๋ , ์๋ฏธ๊ณ ."
"์๋ , ๋ฆฌ์."
"์ค๋ ์ ๋ ์๋กญ๊ณ ๋งค์ฐ ํฅ๋ฏธ๋ก์ด ์ฃผ์ ์ธ ๋์ ํ๋ก์์ ๋ํด ์ค๋ช ํ๊ฒ ์ต๋๋ค." .
"Java์๋ ํน์ ํด๋์ค์ ๊ธฐ๋ฅ์ ๋ณ๊ฒฝํ๋ ์ฌ๋ฌ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋คโฆ"
"์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ์ ์์์ ๋๋ค."
"ํด๋์ค์ ๋์์ ๋ณ๊ฒฝํ๋ ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ ์๋(๊ธฐ๋ณธ) ํด๋์ค๋ฅผ ์์ํ๋ ์ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ํด๋น ๋ฉ์๋๋ฅผ ์ฌ์ ์ํ๋ ๊ฒ์ ๋๋ค. ๊ทธ๋ฐ ๋ค์ ์๋ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ๋์ ํ์ ํด๋์ค๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค."
Reader reader = new UserCustomReader();
"๋ ๋ฒ์งธ ๋ฐฉ๋ฒ์ ๋ํผ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค."
" BufferedReader ๋ ์ด๋ฌํ ์ ํ์ ํด๋์ค์ ์์ ๋๋ค. ์ฒซ์งธ, Reader๋ฅผ ์์ ํฉ๋๋ค. ์ฆ, Reader ๋์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋์งธ, BufferedReader ๊ฐ์ฒด์ ์์ฑ์๋ก ์ ๋ฌ๋์ด์ผ ํ๋ ์๋ Reader ๊ฐ์ฒด์ ๋ํ ๋ชจ๋ ํธ์ถ์ ๋ฆฌ๋๋ ์ ํฉ๋๋ค. . ์๋ฅผ ๋ค์ด:"
Reader readerOriginal = new UserCustomReader();
Reader reader = new BufferedReader(readerOriginal);
"์ธ ๋ฒ์งธ ๋ฐฉ๋ฒ์ ๋์ ํ๋ก์(Proxy)๋ฅผ ๋ง๋๋ ๊ฒ์ ๋๋ค."
"Java(java.lang.reflect.Proxy)์๋ ๋ณ๋์ ํด๋์ค๋ฅผ ๋ง๋ค์ง ์๊ณ ๋ ํ๋ก๊ทธ๋จ ์คํ ์ค์(๋์ ์ผ๋ก) ๊ฐ์ฒด๋ฅผ ๊ตฌ์ฑํ ์ ์๋ ํน์ ํด๋์ค๊ฐ ์์ต๋๋ค ."
"์ด ์์ ์ ๋งค์ฐ ์ฝ์ต๋๋ค."
Reader reader = (Reader)Proxy.newProxyInstance();
"๊ทธ๊ฒ์ ์ด๋ฏธ ๋์๊ฒ ์๋ก์ด ๊ฒ์ ๋๋ค!"
"ํ์ง๋ง ๋ฌผ๋ก ๋ฉ์๋๊ฐ ์๋ ๊ฐ์ฒด๋ ํ์ํ์ง ์์ต๋๋ค. ๋ฉ์๋๊ฐ ์๋ ๊ฐ์ฒด๊ฐ ํ์ํ๊ณ ์ํ๋ ์์ ์ ์ํํ๋ ค๋ฉด ๊ฐ์ฒด๊ฐ ํ์ํฉ๋๋ค. Java๋ ๋ชจ๋ ๋ฉ์๋ ํธ์ถ์ ๊ฐ๋ก์ฑ ์ ์๋ InvocationHandler๋ผ๋ ํน์ ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉ ํฉ๋๋ค . ํ๋ก์ ๊ฐ์ฒด์ ์ฐ๊ฒฐ๋์ด ์์ต๋๋ค . ํ๋ก์ ๊ฐ์ฒด๋ ์ธํฐํ์ด์ค๋ฅผ ํตํด์๋ง ๋ง๋ค ์ ์์ต๋๋ค."
" Invoke โ ์ฃผ์ ์์ ์ด ๋จ์ํ ์ผ๋ถ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๊ฒ์ธ ๋ฉ์๋ ๋๋ ํด๋์ค์ ํ์ค ์ด๋ฆ์ ๋๋ค."
" ํธ๋ค๋ฌ โ ์ผ๋ถ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๋ ํด๋์ค์ ํ์ค ์ด๋ฆ์ ๋๋ค. ์๋ฅผ ๋ค์ด ๋ง์ฐ์ค ํด๋ฆญ์ ์ฒ๋ฆฌํ๋ ํด๋์ค๋ MouseClickHandler ๋ฑ์ผ๋ก ๋ถ๋ฆฝ๋๋ค."
"InvocationHandler ์ธํฐํ์ด์ค์๋ ํ๋ก์ ๊ฐ์ฒด์ ๋ํ ๋ชจ๋ ํธ์ถ์ด ์ ๋ฌ๋๋ ๋จ์ผ ํธ์ถ ๋ฉ์๋๊ฐ ์์ต๋๋ค . ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค."
Reader reader = (Reader)Proxy.newProxyInstance(new CustomInvocationHandler());
reader.close();
class CustomInvocationHandler implements InvocationHandler
{
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
System.out.println("yes!");
return null;
}
}
" reader . close () ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด invoke ๋ฉ์๋๊ฐ ํธ์ถ๋๊ณ ํ๋ฉด์ 'yes!'๊ฐ ํ์๋ฉ๋๋ค."
"๊ทธ๋์ CustomInvocationHandler ํด๋์ค๋ฅผ ์ ์ธํ๊ณ InvocationHandler ์ธํฐํ์ด์ค์ ํด๋น invoke ๋ฉ์๋๋ฅผ ๊ตฌํํ์ต๋๋ค. invoke ๋ฉ์๋๊ฐ ํธ์ถ๋๋ฉด 'yes!'๊ฐ ํ์๋ฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ CustomInvocationHandler ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์์ฑ ์ newProxyInstance ๋ฉ์๋ ์ ์ ๋ฌํ์ต๋๋ค. ํ๋ก์ ๊ฐ์ฒด์ ๋๋ค."
"๋ค, ๋ชจ๋ ๋ง์ต๋๋ค."
"์ด๊ฒ์ ๋งค์ฐ ๊ฐ๋ ฅํ ๋๊ตฌ์ ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ์ด๋ฌํ ํ๋ก์๋ ๋ค๋ฅธ ์ปดํจํฐ์์ ์ค์ ๋ก ์คํ๋๋ ํ๋ก๊ทธ๋จ์ ๊ฐ์ฒด๋ฅผ ์๋ฎฌ๋ ์ดํธํ๊ฑฐ๋ ์ก์ธ์ค๋ฅผ ์ ์ดํ๊ธฐ ์ํด ์์ฑ๋ฉ๋๋ค ."
"์ด ๋ฐฉ๋ฒ์์ ํ์ฌ ์ฌ์ฉ์์ ๊ถํ์ ํ์ธํ๊ณ , ์ค๋ฅ๋ฅผ ์ฒ๋ฆฌํ๊ณ , ์ค๋ฅ๋ฅผ ๊ธฐ๋กํ๋ ๋ฑ์ ์์ ์ ์ํํ ์ ์์ต๋๋ค."
"๋ค์์ invoke ๋ฉ์๋๊ฐ ์๋ ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ์์ ๋๋ค."
Reader original = new UserCustomReader();
Reader reader = (Reader)Proxy.newProxyInstance(new CustomInvocationHandler(original));
reader.close();
class CustomInvocationHandler implements InvocationHandler
{
private Reader readerOriginal;
CustomInvocationHandler(Reader readerOriginal)
{
this.readerOriginal = readerOriginal;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
if (method.getName().equals("close"))
{
System.out.println("Reader closed!");
}
// This calls the readerOriginal object's close method.
// The method's name and a description of its parameters are stored in the method variable.
return method.invoke(readerOriginal, args);
}
}
"์ด ์์๋ ๋ ๊ฐ์ง ํน๋ณํ ๊ธฐ๋ฅ์ด ์์ต๋๋ค."
"๋จผ์ ยซ์๋ณธยป Reader ๊ฐ์ฒด๊ฐ ์์ฑ์์ ์ ๋ฌ๋๊ณ ์ด์ ๋ํ ์ฐธ์กฐ๊ฐ CustomInvocationHandler ๋ด๋ถ์ ์ ์ฅ๋ฉ๋๋ค . "
"๋ ๋ฒ์งธ๋ก, invoke ๋ฉ์๋์์ ์ด ๋์ผํ ๋ฉ์๋๋ฅผ ๋ค์ ํธ์ถํ์ง๋ง ์ด๋ฒ์๋ ยซ์๋ณธยป ๊ฐ์ฒด์์ ํธ์ถํฉ๋๋ค."
"์. ์ฆ, ์ด ๋ง์ง๋ง ์ค์ ๋์ผํ ๋ฉ์๋๋ฅผ ํธ์ถํ์ง๋ง ์๋ ๊ฐ์ฒด์ ๋ํด ๋ค์๊ณผ ๊ฐ์ด ํธ์ถํฉ๋๋ค."
return method.invoke(readerOriginal, args);
"๋ค."
"๋งค์ฐ ๋ช ๋ฐฑํ๋ค๊ณ ๋งํ ์๋ ์์ง๋ง ์ฌ์ ํ ์ดํดํ ์ ์์ต๋๋ค. ๋๋ ๊ทธ๋ ๊ฒ ๋ณด์ ๋๋ค."
"์ข์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ํ ๊ฐ์ง ๋. newProxyInstance ๋ฉ์๋์์ ํ๋ก์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ค๋ฉด ๊ด๋ฆฌ ์ ๋ณด๋ฅผ ์กฐ๊ธ ๋ ์ ๋ฌํด์ผ ํฉ๋๋ค. ํ์ง๋ง ์ฐ๋ฆฌ๋ ์์ฒญ๋ ํ๋ก์ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๊ธฐ ๋๋ฌธ์ ์ด ์ ๋ณด๋ ์๋ ํด๋์ค ์์ฒด์์ ์ฝ๊ฒ ์ป์ ์ ์์ต๋๋ค. "
"์ฌ๊ธฐ ๋ ๋ค๋ฅธ ์๊ฐ ์์ต๋๋ค."
Reader original = new UserCustomReader();
ClassLoader classLoader = original.getClass().getClassLoader();
Class<?>[] interfaces = original.getClass().getInterfaces();
CustomInvocationHandler invocationHandler = new CustomInvocationHandler(original);
Reader reader = (Reader)Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
class CustomInvocationHandler implements InvocationHandler
{
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
return null;
}
}
"์. ClassLoader์ ์ธํฐํ์ด์ค ๋ชฉ๋ก. ์ด๊ฒ์ Reflection์์ ๊ฐ์ ธ์จ ๊ฒ ์๋๊ฐ์?"
"๋ค."
"๊ทธ๋ ๊ตฐ์. ์, ํ์ํ๋ค๋ฉด ์์์ ์ด๊ณ ๋งค์ฐ ๊ฐ๋จํ ํ๋ก์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค."
"๊ทธ๋ผ ๋์๊ณ ์๊ฒ ๊ฐ์ ํ์ธํด๋ด."
GO TO FULL VERSION