Example #1
0
  @Test
  public void testRemoveAnnotation() throws Exception {
    CompilerAssert<File, File> helper = compiler("metamodel.tag");
    helper.assertCompile();

    //
    JavaFile file = helper.assertSource("metamodel", "tag", "package-info.java");
    PackageDeclaration a = file.assertPackage();
    NormalAnnotationExpr applicationDecl = (NormalAnnotationExpr) a.getAnnotations().get(0);
    applicationDecl.getPairs().clear();
    file.assertSave();

    //
    File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser");
    MetaModelState unserialize = Tools.unserialize(MetaModelState.class, ser);
    ModuleMetaModel mm = (ModuleMetaModel) unserialize.metaModel;
    mm.getQueue().clear();
    Tools.serialize(unserialize, ser);

    //
    helper.addClassPath(helper.getClassOutput()).assertCompile();
    unserialize = Tools.unserialize(MetaModelState.class, ser);
    mm = (ModuleMetaModel) unserialize.metaModel;
    List<MetaModelEvent> events = mm.getQueue().clear();
    assertEquals(2, events.size());
    assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType());
    assertInstanceOf(TemplateMetaModel.class, events.get(0).getObject());
    assertEquals(
        Path.parse("/metamodel/tag/tags/foo.gtmpl"),
        ((TemplateMetaModel) events.get(0).getObject()).getPath());
    assertEquals(MetaModelEvent.UPDATED, events.get(1).getType());
    assertInstanceOf(ApplicationMetaModel.class, events.get(1).getObject());
  }
  @Override
  public void postProcessAnnotations(ModuleMetaModel metaModel) {

    // Resolve applications
    for (ApplicationMetaModel application : metaModel.getChildren(ApplicationMetaModel.class)) {
      if (application.modified) {
        metaModel.queue(MetaModelEvent.createUpdated(application));
        application.modified = false;
      }
    }

    //
    context.postProcessAnnotations();
  }
 private ApplicationMetaModel add(
     ModuleMetaModel metaModel, ElementHandle.Package handle, String applicationName) {
   ApplicationMetaModel application = new ApplicationMetaModel(handle, applicationName);
   metaModel.addChild(Key.of(handle, ApplicationMetaModel.class), application);
   context.add(application);
   return application;
 }
Example #4
0
  @Test
  public void testUpdateAnnotation() throws Exception {
    CompilerAssert<File, File> helper = compiler("metamodel.tag");
    helper.assertCompile();

    //
    JavaFile file = helper.assertSource("metamodel", "tag", "package-info.java");
    PackageDeclaration a = file.assertPackage();
    NormalAnnotationExpr applicationDecl = (NormalAnnotationExpr) a.getAnnotations().get(0);
    NormalAnnotationExpr tagDecl =
        (NormalAnnotationExpr) applicationDecl.getPairs().get(0).getValue();
    boolean changed = false;
    for (MemberValuePair pair : tagDecl.getPairs()) {
      if (pair.getName().equals("path")) {
        pair.setValue(new StringLiteralExpr("bar.gtmpl"));
        changed = true;
      }
    }
    assertTrue(changed);
    file.assertSave();

    //
    File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser");
    MetaModelState unserialize = Tools.unserialize(MetaModelState.class, ser);
    ModuleMetaModel mm = (ModuleMetaModel) unserialize.metaModel;
    mm.getQueue().clear();
    Tools.serialize(unserialize, ser);

    //
    helper.addClassPath(helper.getClassOutput()).assertCompile();
    unserialize = Tools.unserialize(MetaModelState.class, ser);
    mm = (ModuleMetaModel) unserialize.metaModel;
    List<MetaModelEvent> events = mm.getQueue().clear();
    assertEquals(3, events.size());
    assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType());
    assertInstanceOf(TemplateMetaModel.class, events.get(0).getObject());
    assertEquals(
        Path.parse("/metamodel/tag/tags/foo.gtmpl"),
        ((TemplateMetaModel) events.get(0).getObject()).getPath());
    assertEquals(MetaModelEvent.AFTER_ADD, events.get(1).getType());
    assertInstanceOf(TemplateMetaModel.class, events.get(1).getObject());
    assertEquals(
        Path.parse("/metamodel/tag/tags/bar.gtmpl"),
        ((TemplateMetaModel) events.get(1).getObject()).getPath());
    assertEquals(MetaModelEvent.UPDATED, events.get(2).getType());
    assertInstanceOf(ApplicationMetaModel.class, events.get(2).getObject());
  }
 @Override
 public JSON getDescriptor(ModuleMetaModel metaModel) {
   JSON json = new JSON();
   for (ApplicationMetaModel application : metaModel.getChildren(ApplicationMetaModel.class)) {
     json.set(application.getHandle().getPackage().toString(), new JSON());
   }
   return json;
 }
 @Override
 public void processAnnotationRemoved(
     ModuleMetaModel metaModel, AnnotationKey key, AnnotationState removed) {
   if (key.getType().equals(APPLICATION)) {
     ElementHandle.Package pkg = (ElementHandle.Package) key.getElement();
     ApplicationMetaModel mm = metaModel.getChild(Key.of(pkg, ApplicationMetaModel.class));
     if (mm != null) {
       context.remove(mm);
       mm.remove();
     }
   }
 }
 @Override
 public void processAnnotationUpdated(
     ModuleMetaModel metaModel,
     AnnotationKey key,
     AnnotationState removed,
     AnnotationState added) {
   if (key.getType().equals(APPLICATION)) {
     ElementHandle.Package pkg = (ElementHandle.Package) key.getElement();
     ApplicationMetaModel application =
         metaModel.getChild(Key.of(pkg, ApplicationMetaModel.class));
     application.modified = true;
   }
 }
  @Override
  public void postActivate(ModuleMetaModel metaModel) {
    for (ApplicationMetaModelPlugin plugin : context.getPlugins()) {
      plugin.postActivate(metaModel);
    }

    //
    context.postActivate(metaModel.processingContext);
    for (ApplicationMetaModel application : metaModel.getChildren(ApplicationMetaModel.class)) {
      for (ApplicationMetaModelPlugin plugin : context.getPlugins()) {
        plugin.postActivate(application);
      }
    }
  }
  private void emitConfig(ModuleMetaModel metaModel) {
    JSON descriptor = new JSON();

    // Application configs
    for (ApplicationMetaModel application : metaModel.getChildren(ApplicationMetaModel.class)) {

      //
      metaModel.processingContext.log(
          "Emitting application " + application.getHandle() + " config");

      // Recycle
      descriptor.clear();

      // Emit config
      for (ApplicationMetaModelPlugin plugin : context.getPlugins()) {
        JSON pluginDescriptor = plugin.getDescriptor(application);
        if (pluginDescriptor != null) {
          descriptor.set(plugin.getName(), pluginDescriptor);
        }
      }

      //
      Writer writer = null;
      try {
        FileObject fo =
            metaModel.processingContext.createResource(
                StandardLocation.CLASS_OUTPUT, application.getName(), "config.json");
        writer = fo.openWriter();
        descriptor.toString(writer, 2);
      } catch (IOException e) {
        throw ApplicationMetaModel.CANNOT_WRITE_APPLICATION_CONFIG.failure(
            e, metaModel.processingContext.get(application.getHandle()), application.getName());
      } finally {
        Tools.safeClose(writer);
      }
    }
  }