简体   繁体   中英

Generifying java methods

I have methods like below

public InstitutionsType toInstitutionPOJO(String xml) throws Exception {
        InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        JAXBContext jaxbContext = JAXBContext.newInstance(InstitutionsType.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader someSource = factory.createXMLEventReader(stream);
        JAXBElement<InstitutionsType> userElement = jaxbUnmarshaller.unmarshal(someSource, InstitutionsType.class);
        return userElement.getValue();
    }

public ErrorType toErrorPOJO(String xml) throws Exception {
    InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
    JAXBContext jaxbContext = JAXBContext.newInstance(ErrorType.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader someSource = factory.createXMLEventReader(stream);
    JAXBElement<ErrorType> userElement = jaxbUnmarshaller.unmarshal(someSource, ErrorType.class);
    return userElement.getValue();
}

I have to create around 14 similar methods which are exactly same except for the output type. Can we generify this?

Something like this should do the trick..

public <T> T  toPOJO(String xml, Class<T> type) throws Exception {
        InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        JAXBContext jaxbContext = JAXBContext.newInstance(type);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader someSource = factory.createXMLEventReader(stream);
        JAXBElement<T> userElement = jaxbUnmarshaller.unmarshal(someSource, type);
        return userElement.getValue();
}

You can create a Interface which is then implemented by these output type classes. Then you can simply return the interface

public interface Outputs{
    // declare functions here
}

public class InstitutionsType implements Outputs{

    // Override the methods here   
}

And then these functions could be re-written as:

public Outputs toInstitutionPOJO(String xml, Class<Outputs> type) throws Exception {
        InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        JAXBContext jaxbContext = JAXBContext.newInstance(InstitutionsType.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader someSource = factory.createXMLEventReader(stream);
        JAXBElement<Outputs> userElement = jaxbUnmarshaller.unmarshal(someSource, type);
        return userElement.getValue();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM