Java: get annotation attributes 

Joined:
04/09/2007
Posts:
753

October 08, 2012 11:56:29    Last update: October 08, 2012 11:56:29
This example gets the annotation attributes of of a web service client generated by JAX-WS RI.

The generated web service client looks like this:
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.7-b01-
 * Generated source version: 2.1
 * 
 */
@WebServiceClient(name = "MyTestWebService", targetNamespace = "http://service.ex.com/mytestwebservice/v1.0", wsdlLocation = "mytestwebservice.wsdl")
public class MyTestWebService
    extends Service
{
    .
    .
    .
}


This is how to get the attributes for annotation @WebServiceClient:
WebServiceClient wsc = MyTestWebService.class.getAnnotation(WebServiceClient.class);
String name = wsc.name();
String targetNamespace = wsc.targetNamespace();
String wsdlLocation = wsc.wsdlLocation();


Note that even though name, targetNamespace and wsdlLocation are attributes, you get them using a method call.

Also, annotations are available at runtime only when they have RetentionPolicy.RUNTIME.
Share |
| Comment  | Tags