-- 입력받는 파라미터가 없는 메서드


import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;

import java.lang.reflect.Method;

import org.junit.Test;

public class ReflectTest {

    @Test
    public void testReflect() throws Exception {
        Method method = Sample.class.getMethod("getSample");
        assertThat((String) method.invoke(Sample.class.newInstance()), is("Sample"));
    }

    public static class Sample {
        public String getSample() {
            return "Sample";
        }

        public String getSample(String append) {
            return "Sample " + append;
        }
    }
}


-- 파라미터를 입력받는 메서드

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;

import java.lang.reflect.Method;

import org.junit.Test;

public class ReflectTest {

    @Test
    public void testReflect() throws Exception {
        Method method = Sample.class.getMethod("getSample");
        assertThat((String) method.invoke(new Sample()), is("Sample"));
       
        method = Sample.class.getMethod("getSample", String.class);
        assertThat((String)method.invoke(new Sample(), "Test"), is("Sample Test"));
    }

    public class Sample {
        public String getSample() {
            return "Sample";
        }

        public String getSample(String append) {
            return "Sample " + append;
        }
    }
}

-- 다수의 파라미터를 받는 메서드

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;

import java.lang.reflect.Method;

import org.junit.Test;

public class ReflectTest {

    @Test
    public void testReflect() throws Exception {
        Method method = Sample.class.getMethod("getSample");
        assertThat((String) method.invoke(new Sample()), is("Sample"));
       
        method = Sample.class.getMethod("getSample", String.class);
        assertThat((String)method.invoke(new Sample(), "Test"), is("Sample Test"));
       
        method = Sample.class.getMethod("getSample", String.class, int.class);
        assertThat((String)method.invoke(new Sample(), "Cut", 7), is("Cut"));
    }

    public class Sample {
        public String getSample() {
            return "Sample";
        }

        public String getSample(String append) {
            return "Sample " + append;
        }
       
        public String getSample(String append, int beginIndex){
            return getSample(append).substring(beginIndex);
        }
    }
}
Posted by KENSIN
,


가격을 가져온다.

한국 돈으로 책은 6500원 이며,

일본 돈으로 책은 650엔 이다.

다른 물건도 단순히 '원 / 10 = 엔' 의 공식으로 값이 나오도록 적용한다.


우선 테스트를 작성한다.

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;

import java.lang.reflect.Proxy;

import org.junit.Test;

public class PriceTest {
    @Test
    public void getPrice(){
        Price koreanPrice = new KoreanPrice();
        assertThat(koreanPrice.getBookPrice(), is(6500));
        assertThat(koreanPrice.getCarPrice(), is(26000000));
        assertThat(koreanPrice.getPenPrice(), is(1200));
       
        Price japanPrice = (Price)Proxy.newProxyInstance(
                getClass().getClassLoader()
                , new Class[]{Price.class}
                , new JapanPrice(new KoreanPrice()));
        assertThat(japanPrice.getBookPrice(), is(650));
        assertThat(japanPrice.getCarPrice(), is(2600000));
        assertThat(japanPrice.getPenPrice(), is(120));
    }
}

Price 인터페이스

public interface Price{
    int getBookPrice();
    int getCarPrice();
    int getPenPrice();
}

원(W)화로 계산해주는 클래스

public class KoreanPrice implements Price {

    @Override
    public int getBookPrice() {
        return 6500;
    }

    @Override
    public int getCarPrice() {
        return 26000000;
    }

    @Override
    public int getPenPrice() {
        return 1200;
    }
}

Proxy를 이용해 엔화로 계산해 주기 위한 클래스

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class JapanPrice implements InvocationHandler {
    Object price;

    public JapanPrice(Object price) {
        this.price = price;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object obj = method.invoke(price, args);
        return ((Integer) obj) / 10;//단순히 10 나누기
    }
}

책값만 엔화로 계산해 주기 테스트

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class JapanPrice implements InvocationHandler {
	Object price;

	public JapanPrice(Object price) {
		this.price = price;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Object obj = method.invoke(price, args);
		if(obj instanceof Integer && method.getName().indexOf("Book") != -1) {
			return ((Integer)obj) / 10;
		}
		return (Integer) obj;
	}
}


import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;

import java.lang.reflect.Proxy;

import org.junit.Test;

public class PriceTest {
	@Test
	public void getPrice(){
		Price koreanPrice = new KoreanPrice();
		assertThat(koreanPrice.getBookPrice(), is(6500));
		assertThat(koreanPrice.getCarPrice(), is(26000000));
		assertThat(koreanPrice.getPenPrice(), is(1200));
		
		Price japanPrice = (Price)Proxy.newProxyInstance(
				getClass().getClassLoader()
				, new Class[]{Price.class}
				, new JapanPrice(new KoreanPrice()));
		assertThat(japanPrice.getBookPrice(), is(650));
		assertThat(japanPrice.getCarPrice(), is(26000000));
		assertThat(japanPrice.getPenPrice(), is(1200));
	}
}

Posted by KENSIN
,