2013-04-15 4 views
1

메서드 호출에 인터페이스를 제공하고 싶습니다. 주어진 인터페이스에 따라 메소드는 인스턴스를 작성해야합니다. 이 목적을 위해 나는 generics를 사용하여 메소드에 다른 종류의 인터페이스를 제공합니다. 여기에 예 :메서드 매개 변수로 inteface

static <T> T createClient(T, String endpointAddress) { 
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean() 
    factory.setServiceClass(T.class) 
    factory.setAddress(endpointAddress) 
    (T) factory.create() // error -> java.lang.IllegalArgumentException: java.lang.Class is not an interface 
} 

// AccessibleClient is an interface. call method 
createClient(AccessibleClient, "http://localhost/service") 

나는 내 접근 방식이 적절한 해결책이라는 것을 모른다.

+1

shouldn' 그것은'createClient (T param, String endpointAddress)'가 될 것인가? – sanbhat

+0

인터페이스에서 인스턴스를 어떻게 만들 계획입니까? – Apurv

+2

'createClient (AccessibleClient.class, "http : // localhost/service")' – gontard

답변

1

T.class - Java에서는 런타임에 정보를 사용할 수 없다고 말할 수 없습니다.

으로 gontard 당 '의 comment, 당신은 아마 이런 걸 원하는 : (? 또한, 자바 당신이 필요로하는 세미콜론과 return 키워드 -이 그루비 또는 무언가)

static <T> T createClient (Class<T> t, String endpointAddress) 
{ 
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); 
    factory.setServiceClass(t); 
    factory.setAddress(endpointAddress); 
    return (T) factory.create(); 
} 

createClient(AccessibleClient.class, "..."); 

+0

나는 groovy와 plain java를 사용하지 않았다는 것을 잊어 버렸다. Groovy에서이 정보는 런타임에 매우 유용하며 세미콜론과 리턴을 사용할 필요가 없습니다. 자바의 컨텍스트에서 올바른 때문에 귀하의 답변을 수락하십시오. – hitty5

관련 문제