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


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
,
인터넷 익스플로러의 언어 설정 옵션이다.



위 화면에서 ko_KR 이 IE9, FF 등에서 기본 설정 되어있는 언어 옵션이다.

IE8은 ko 가 기본값으로 되어있다. (위 화면의 옵션은 IE9 화면이며 ko 는 테스트를 위해 추가한 부분이다, 위아래로 이동하여 우선권 설정이 가능하다.)


URLEncoder.encode("한글", encoding_charset) 로 인코딩 후 GET 주소를 보내면 서버에서 request 에 캐릭터 셋을 euc-kr 로 설정한후 request.getParameter 로 받을 경우ko 는 바로 제대로 된 한글이 보여지고 ko_KR 은 깨진 문자열로 보여진다.

따라서 request.getLocale() 을 이용해 ko 인지 ko_KR인지 판단하여 ko_KR일 경우 파라미터 값을 다시 getByte("ISO-8859-1"), "euc-kr" 로 인코딩 변경하여주면 정상적인 한글 값을 받아 올수 있다.


-- 사이트 페이지 링크

<a href="?name=<%=URLEncoder.encode("홍길동", "euc-kr")%>" >

-- 서버에서의 처리

String name = request.getParameter("name");
// [ko] 로 넘어온 경우 출력해 보면 "홍길동" 으로 찍힌다. 인코딩할 필요 없음.

// [ko_KR] 인 경우 깨져서 보인다. 인코딩이 필요하다.
if("get".equalsIgnoreCase(request.getMethod()) && !"".equals(name)){
    String localeName = String.valueOf(request.getLocale()).trim();
    if("ko_KR".equals(localeName)){
        name = new String(name.getBytes("iso-8859-1"), "euc-kr");
        // [ko_KR] 로 넘어온 경우 인코딩 후 여기서 "홍길동" 으로 찍힌다.
    }
}



Posted by KENSIN
,