示例#1
0
 @Override
 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   // check if process was done on previous round
   if (OcelotProcessor.isDone()
       || roundEnv.processingOver()) { // Returns true if types generated by this round will not be
     // subject to a subsequent round of annotation processing; returns
     // false otherwise.
     return true; // if is it : stop
   }
   // Create provider of ocelot-services.js
   String js = createJSServicesProvider();
   // Create file ocelot-services.js
   try {
     FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT, "", js);
     try (Writer writer = resource.openWriter()) {
       ElementVisitor visitor = new DataServiceVisitor(processingEnv);
       for (Element element : roundEnv.getElementsAnnotatedWith(DataService.class)) {
         messager.printMessage(
             Diagnostic.Kind.MANDATORY_WARNING, " javascript generation class : " + element);
         element.accept(visitor, writer);
       }
     }
   } catch (IOException e) {
     messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage());
   }
   OcelotProcessor.setDone(true);
   return true;
 }
示例#2
0
  /**
   * Velocity の実行(リソース用)。出力先はパッケージ名とリソース名から自動生成。
   *
   * @param context VelocityContext
   * @param pkgName パッケージ名
   * @param resName リソース名
   * @param templ Velocityテンプレート名。
   * @throws Exception
   */
  public void applyTemplateText(
      VelocityContext context, String pkgName, String resName, String templ) throws Exception {
    context.put("packageName", pkgName);
    context.put("resourceName", resName);
    context.put("environment", environment);

    Template template = getVelocityTemplate(templ);

    String resFullPath = pkgName.replace('.', '/') + "/" + resName;
    Filer filer = environment.getFiler();
    FileObject file =
        filer.createResource(StandardLocation.SOURCE_OUTPUT, "", resFullPath, (Element[]) null);
    PrintWriter writer = new PrintWriter(file.openWriter());
    template.merge(context, writer);
    writer.close();
  }
示例#3
0
  @Override
  @SuppressWarnings("unchecked")
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (roundEnv.processingOver()) {
      return false;
    }

    Set<String> beans = new TreeSet<String>();

    // JavaBean is a type only annotation, so cast to TypeElement is safe
    for (TypeElement type : (Set<TypeElement>) roundEnv.getElementsAnnotatedWith(JavaBean.class)) {
      beans.add(type.getQualifiedName().toString());
    }

    // remove any existing values; we append to the file
    Filer filer = processingEnv.getFiler();

    FileObject manifest = null;

    try {
      processingEnv.getMessager().printMessage(Kind.NOTE, "Attempting to open manifest...");

      manifest = filer.getResource(StandardLocation.SOURCE_PATH, "", "META-INF/MANIFEST.MF");

      if (manifest != null) {
        processingEnv.getMessager().printMessage(Kind.NOTE, "Succeeded: " + manifest.getName());

        BufferedReader r = null;

        try {
          processingEnv
              .getMessager()
              .printMessage(Kind.NOTE, "Attempting to find previously defined Java beans");

          r = new BufferedReader(new InputStreamReader(manifest.openInputStream(), "UTF-8"));

          String possibleBean = null;

          for (String line = r.readLine(); line != null; line = r.readLine()) {
            if (possibleBean == null) {
              if (line.startsWith("Name: ") && line.endsWith(".class")) {
                possibleBean =
                    line.substring("Name: ".length(), line.length() - ".class".length())
                        .replace('/', '.');

                try {
                  Class.forName(possibleBean);
                } catch (ClassNotFoundException notABean) {
                  possibleBean = null;
                }
              }
            } else {
              if (line.equals("Java-Bean: True")) {
                processingEnv
                    .getMessager()
                    .printMessage(Kind.NOTE, possibleBean + " already defined");
                beans.remove(possibleBean);
              }

              possibleBean = null;
            }
          }

          r.close();

        } catch (FileNotFoundException ignore) {
          processingEnv.getMessager().printMessage(Kind.NOTE, "Manifest not found");
        } catch (IOException e) {
          throw new RuntimeException("Failed to read current Java-Bean information", e);
        } finally {
          if (r != null) {
            try {
              r.close();
            } catch (IOException ignore) {
            }
          }
        }
      }
    } catch (FileNotFoundException ignore) {
      // no file to process
      processingEnv.getMessager().printMessage(Kind.NOTE, "Manifest does not exist...");
    } catch (IOException e) {
      processingEnv
          .getMessager()
          .printMessage(
              Kind.ERROR, "Failed to load existing manifest for Java-Bean processing:\n" + e);

      return false;
    }

    try {
      processingEnv
          .getMessager()
          .printMessage(Kind.NOTE, "Attempting to create output manifest...");

      manifest = filer.createResource(StandardLocation.SOURCE_OUTPUT, "", "META-INF/MANIFEST.MF");

      processingEnv.getMessager().printMessage(Kind.NOTE, "Succeeded: " + manifest.getName());
    } catch (IOException e) {
      processingEnv
          .getMessager()
          .printMessage(Kind.ERROR, "Cannot create manifest for Java-Bean processing:\n" + e);

      return false;
    }

    processingEnv.getMessager().printMessage(Kind.NOTE, "Appending Java-Beans to MANIFEST.MF");
    processingEnv.getMessager().printMessage(Kind.NOTE, beans.toString());

    PrintWriter pw = null;

    try {
      pw = new PrintWriter(new OutputStreamWriter(manifest.openOutputStream(), "UTF-8"));

      pw.println();

      for (String value : beans) {
        pw.println("Name: " + value + ".class");
        pw.println("Java-Bean: True");
        pw.println();
      }
    } catch (IOException e) {
      throw new RuntimeException("Failed to write Java-Bean information", e);
    } finally {
      if (pw != null) {
        pw.close();
      }
    }

    return false;
  }