Search in sources :

Example 1 with MemberDoc

use of com.sun.javadoc.MemberDoc in project jersey by jersey.

the class ResourceDoclet method getSerializedLinkFromTag.

private static String getSerializedLinkFromTag(final SeeTag linkTag) {
    final MemberDoc referencedMember = linkTag.referencedMember();
    if (referencedMember == null) {
        throw new NullPointerException("Referenced member of @link " + print(linkTag) + " cannot be resolved.");
    }
    if (!referencedMember.isStatic()) {
        LOG.warning("Referenced member of @link " + print(linkTag) + " is not static." + " Right now only references to static members are supported.");
        return null;
    }
    /* Get referenced example bean
         */
    final ClassDoc containingClass = referencedMember.containingClass();
    final Object object;
    try {
        final Field declaredField = Class.forName(containingClass.qualifiedName(), false, Thread.currentThread().getContextClassLoader()).getDeclaredField(referencedMember.name());
        if (referencedMember.isFinal()) {
            declaredField.setAccessible(true);
        }
        object = declaredField.get(null);
        LOG.log(Level.FINE, "Got object " + object);
    } catch (final Exception e) {
        LOG.info("Have classloader: " + ResourceDoclet.class.getClassLoader().getClass());
        LOG.info("Have thread classloader " + Thread.currentThread().getContextClassLoader().getClass());
        LOG.info("Have system classloader " + ClassLoader.getSystemClassLoader().getClass());
        LOG.log(Level.SEVERE, "Could not get field " + referencedMember.qualifiedName(), e);
        return null;
    }
    /* marshal the bean to xml
         */
    try {
        final JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        final StringWriter stringWriter = new StringWriter();
        final Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(object, stringWriter);
        final String result = stringWriter.getBuffer().toString();
        LOG.log(Level.FINE, "Got marshalled output:\n" + result);
        return result;
    } catch (final Exception e) {
        LOG.log(Level.SEVERE, "Could serialize bean to xml: " + object, e);
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBContext(javax.xml.bind.JAXBContext) MemberDoc(com.sun.javadoc.MemberDoc) MalformedURLException(java.net.MalformedURLException) ClassDoc(com.sun.javadoc.ClassDoc)

Aggregations

ClassDoc (com.sun.javadoc.ClassDoc)1 MemberDoc (com.sun.javadoc.MemberDoc)1 StringWriter (java.io.StringWriter)1 Field (java.lang.reflect.Field)1 MalformedURLException (java.net.MalformedURLException)1 JAXBContext (javax.xml.bind.JAXBContext)1 Marshaller (javax.xml.bind.Marshaller)1