A j
A j

Reputation: 1087

How to get the actual type in javax.xml.ws.Holder using Java reflection?

I want to do the Following.
1) I run wsimport on a WSDL to generate Java code.
2) wsimport generates a Java interface that represents the WSDL interface.
3) I want to use reflection to discover the parameter classes and return type for the methods in the interface.

Shown below is the interface that wsimport has generated.

public interface Sample2 {
@WebMethod(action = "http://www.example.org/sample2/getMeaning2")
public void getMeaning2(
    @WebParam(name = "getMeaning21", targetNamespace = "http://www.example.org/sample2/", partName = "parameters1")
    GetMeaning21 parameters1,
    @WebParam(name = "getMeaning2", targetNamespace = "http://www.example.org/sample2/", partName = "parameters2")
    GetMeaning2 parameters2,
    @WebParam(name = "getMeaningResponse21", targetNamespace = "http://www.example.org/sample2/", mode = WebParam.Mode.OUT, partName = "parameters3")
    Holder<GetMeaningResponse21> parameters3,
    @WebParam(name = "getMeaningResponse2", targetNamespace = "http://www.example.org/sample2/", mode = WebParam.Mode.OUT, partName = "parameters4")
    Holder<GetMeaningResponse2> parameters4);
}

My question is specific to the 2nd and 3rd parameters to the method getMeaning2. I am able to the get the parameters for the method using the reflection api. Method.getParameterTypes (note that this is actually a instance method).

Method declaredMethods[] = Sample2.class.getDeclaredMethods();
method = declaredMethods[0];
Class<?>[] parameterTypes = method.getParameterTypes();

The 2nd and the 3rd elements of the array are of type javax.xml.ws.Holder class.

My question is how do I get the actual type (in this case GetMeaningResponse21.class and GetMeaningResponse2.class) from the parameters3 and parameters4?

Upvotes: 0

Views: 711

Answers (1)

A j
A j

Reputation: 1087

I figured out a way of getting the Actual Type of the value in the Holder class.

Type[] genericParameterTypes = method.getGenericParameterTypes();
String typeName = ((ParameterizedType)genericParameterTypes[2]).getActualTypeArguments()[0].toString();
typeName = typeName.substring("class ".length());
Class<?> actualParamClass = Class.forName(typeName);

The actualParamClass is the Class of the actual type held by the Holder class.

Upvotes: 0

Related Questions