Search in sources :

Example 1 with CanConnect

use of org.kie.workbench.common.stunner.core.rule.annotation.CanConnect in project kie-wb-common by kiegroup.

the class ConnectionRuleGenerator method generate.

@Override
public StringBuffer generate(final String packageName, final PackageElement packageElement, final String className, final Element element, final ProcessingEnvironment processingEnvironment) throws GenerationException {
    final Messager messager = processingEnvironment.getMessager();
    messager.printMessage(Diagnostic.Kind.NOTE, "Starting code adf for [" + className + "]");
    final Elements elementUtils = processingEnvironment.getElementUtils();
    // Extract required information
    final TypeElement classElement = (TypeElement) element;
    final boolean isInterface = classElement.getKind().isInterface();
    final String ruleId = MainProcessor.toValidId(className);
    final String ruleDefinitionId = classElement.getQualifiedName().toString();
    CanConnect[] pcs = classElement.getAnnotationsByType(CanConnect.class);
    List<ConnectionRuleEntry> ruleEntries = new ArrayList<>();
    if (null != pcs) {
        for (final CanConnect pc : pcs) {
            String startRole = pc.startRole();
            String endRole = pc.endRole();
            ruleEntries.add(new ConnectionRuleEntry(startRole, endRole));
        }
    }
    Map<String, Object> root = new HashMap<String, Object>();
    root.put("ruleId", ruleId);
    root.put("ruleDefinitionId", ruleDefinitionId);
    root.put("connectionsSize", ruleEntries.size());
    root.put("connections", ruleEntries);
    // Generate code
    final StringWriter sw = new StringWriter();
    final BufferedWriter bw = new BufferedWriter(sw);
    try {
        final Template template = config.getTemplate("ConnectionRule.ftl");
        template.process(root, bw);
    } catch (IOException ioe) {
        throw new GenerationException(ioe);
    } catch (TemplateException te) {
        throw new GenerationException(te);
    } finally {
        try {
            bw.close();
            sw.close();
        } catch (IOException ioe) {
            throw new GenerationException(ioe);
        }
    }
    messager.printMessage(Diagnostic.Kind.NOTE, "Successfully generated code for [" + className + "]");
    processingContext.addRule(ruleId, ProcessingRule.TYPE.CONNECTION, sw.getBuffer());
    return null;
}
Also used : Messager(javax.annotation.processing.Messager) HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Elements(javax.lang.model.util.Elements) CanConnect(org.kie.workbench.common.stunner.core.rule.annotation.CanConnect) BufferedWriter(java.io.BufferedWriter) Template(freemarker.template.Template) StringWriter(java.io.StringWriter) GenerationException(org.uberfire.annotations.processors.exceptions.GenerationException)

Aggregations

Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Messager (javax.annotation.processing.Messager)1 TypeElement (javax.lang.model.element.TypeElement)1 Elements (javax.lang.model.util.Elements)1 CanConnect (org.kie.workbench.common.stunner.core.rule.annotation.CanConnect)1 GenerationException (org.uberfire.annotations.processors.exceptions.GenerationException)1