Search in sources :

Example 1 with RestModelExtension

use of org.glassfish.admin.rest.composite.RestModelExtension in project Payara by payara.

the class RestModelExtensionProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> elements, RoundEnvironment env) {
    Messager messager = processingEnv.getMessager();
    BufferedWriter bw = null;
    try {
        Map<String, List<String>> classes = new HashMap<String, List<String>>();
        for (TypeElement te : elements) {
            for (Element e : env.getElementsAnnotatedWith(te)) {
                final RestModelExtension annotation = e.getAnnotation(RestModelExtension.class);
                final String parent = annotation.parent();
                List<String> list = classes.get(parent);
                if (list == null) {
                    list = new ArrayList<String>();
                    classes.put(parent, list);
                }
                list.add(e.toString());
            }
        }
        if (!classes.isEmpty()) {
            final Filer filer = processingEnv.getFiler();
            FileObject fo = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/restmodelextensions");
            bw = new BufferedWriter(fo.openWriter());
            // parent model:model extension
            for (Map.Entry<String, List<String>> entry : classes.entrySet()) {
                final String key = entry.getKey();
                for (String ext : entry.getValue()) {
                    bw.write(key + ":" + ext + "\n");
                }
            }
            bw.close();
        }
    } catch (IOException ex) {
        messager.printMessage(Kind.ERROR, ex.getLocalizedMessage());
        if (bw != null) {
            try {
                bw.close();
            } catch (Exception e) {
            }
        }
    }
    return true;
}
Also used : Messager(javax.annotation.processing.Messager) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) RestModelExtension(org.glassfish.admin.rest.composite.RestModelExtension) IOException(java.io.IOException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) ArrayList(java.util.ArrayList) List(java.util.List) FileObject(javax.tools.FileObject) Filer(javax.annotation.processing.Filer) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Filer (javax.annotation.processing.Filer)1 Messager (javax.annotation.processing.Messager)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1 FileObject (javax.tools.FileObject)1 RestModelExtension (org.glassfish.admin.rest.composite.RestModelExtension)1