コード例 #1
0
  /**
   * Finds a {@code imagebundler.properties} file.
   *
   * <p>This uses a heuristic because in Eclipse we will not know what our working directory is (it
   * is wherever Eclipse was started from), so project/workspace-relative paths will not work.
   *
   * <p>As far as passing in a imagebundler.properties path as a {@code -Afile=path} setting,
   * Eclipse also lacks any {@code $ basepath} -type interpolation in its APT key/value pairs (like
   * Ant would be able to do). So only fixed values are accepted, meaning an absolute path, which
   * would be too tied to any one developer's particular machine.
   *
   * <p>The one thing the APT API gives us is the CLASS_OUTPUT (e.g. bin/apt). So we start there and
   * walk up parent directories looking for {@code imagebundler.properties} files.
   */
  private File resolveImageBundlerPropertiesIfExists(Location location) {
    // Find a dummy /bin/apt/dummy.txt path to start at
    final String dummyPath;
    try {
      // We don't actually create this, we just want its URI
      FileObject dummyFileObject = CurrentEnv.getFiler().getResource(location, "", "dummy.txt");
      dummyPath = dummyFileObject.toUri().toString().replaceAll("file:", "");
    } catch (IOException e1) {
      return null;
    }

    // Walk up looking for a imagebundler.properties
    File current = new File(dummyPath).getParentFile();
    while (current != null) {
      File possible = new File(current, "imagebundler.properties");
      if (possible.exists()) {
        return possible;
      }
      current = current.getParentFile();
    }

    // Before giving up, try just grabbing it from the current directory
    File possible = new File("imagebundler.properties");
    if (possible.exists()) {
      return possible;
    }

    // No imagebundler.properties found
    return null;
  }
コード例 #2
0
 /* (non-Javadoc)
  * @see javax.tools.JavaFileManager#getJavaFileForOutput(javax.tools.JavaFileManager.Location, java.lang.String, javax.tools.JavaFileObject.Kind, javax.tools.FileObject)
  */
 @Override
 public JavaFileObject getJavaFileForOutput(
     Location location, String className, Kind kind, FileObject sibling) throws IOException {
   if (kind != Kind.CLASS && kind != Kind.SOURCE) {
     throw new IllegalArgumentException("Invalid kind : " + kind); // $NON-NLS-1$
   }
   Iterable<? extends File> files = getLocation(location);
   if (files == null) {
     if (!location.equals(StandardLocation.CLASS_OUTPUT)
         && !location.equals(StandardLocation.SOURCE_OUTPUT))
       throw new IllegalArgumentException("Unknown location : " + location); // $NON-NLS-1$
     // we will use either the sibling or user.dir
     if (sibling != null) {
       String normalizedFileName = normalized(className);
       int index = normalizedFileName.lastIndexOf('/');
       if (index != -1) {
         normalizedFileName = normalizedFileName.substring(index + 1);
       }
       normalizedFileName += kind.extension;
       URI uri = sibling.toUri();
       URI uri2 = null;
       try {
         String path = uri.getPath();
         index = path.lastIndexOf('/');
         if (index != -1) {
           path = path.substring(0, index + 1);
           path += normalizedFileName;
         }
         uri2 = new URI(uri.getScheme(), uri.getHost(), path, uri.getFragment());
       } catch (URISyntaxException e) {
         throw new IllegalArgumentException("invalid sibling"); // $NON-NLS-1$
       }
       return new EclipseFileObject(className, uri2, kind, this.charset);
     } else {
       String normalizedFileName = normalized(className);
       normalizedFileName += kind.extension;
       File f = new File(System.getProperty("user.dir"), normalizedFileName); // $NON-NLS-1$
       return new EclipseFileObject(className, f.toURI(), kind, this.charset);
     }
   }
   final Iterator<? extends File> iterator = files.iterator();
   if (iterator.hasNext()) {
     File file = iterator.next();
     String normalizedFileName = normalized(className);
     normalizedFileName += kind.extension;
     File f = new File(file, normalizedFileName);
     return new EclipseFileObject(className, f.toURI(), kind, this.charset);
   } else {
     throw new IllegalArgumentException("location is empty : " + location); // $NON-NLS-1$
   }
 }
コード例 #3
0
  @Override
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {

    Set<? extends Element> elements = env.getElementsAnnotatedWith(Struct.class);

    for (Element element : elements) {

      String pakage = eltUtils.getPackageOf(element).toString();

      try {
        Struct struct = element.getAnnotation(Struct.class);
        String headerRelPath = struct.header();
        // yes, this is a hack but there are simple to many bugs in this api.
        // e.g.: 6647998
        FileObject header = filer.getResource(StandardLocation.SOURCE_PATH, pakage, headerRelPath);

        String root = basedir;
        File headerFile = null;
        if (header.toUri().isAbsolute()) {
          headerFile = new File(header.toUri());

          root = headerFile.getAbsolutePath();
          root = root.substring(0, root.length() - headerFile.getName().length() - 1);
          root = root.substring(0, root.length() - pakage.length()) + "../";
        } else {
          root = System.getProperty("user.dir");
          headerFile = new File(root + separator + header.toUri());
        }

        System.out.println("processing header: " + header.toUri());

        generateStructBinding(element, struct, root, pakage, headerFile);
      } catch (IOException ex) {
        throw new RuntimeException("IOException encountered, run if you can!", ex);
      }
    }
    return true;
  }
コード例 #4
0
 private void addEntry(JarOutputStream jar, FileObject file) throws IOException {
   assert jar != null;
   String path = file.toUri().getPath();
   while (path.startsWith("/")) {
     path = path.substring(1);
   }
   JarEntry entry = new JarEntry(path);
   jar.putNextEntry(entry);
   InputStream input = file.openInputStream();
   try {
     byte[] buffer = new byte[1024];
     while (true) {
       int read = input.read(buffer);
       if (read < 0) {
         break;
       }
       jar.write(buffer, 0, read);
     }
     jar.closeEntry();
   } finally {
     input.close();
   }
 }
コード例 #5
0
  @Override
  protected void outputResults() throws IOException {
    StringBuilder sb = new StringBuilder();

    sb.append('[');
    String separator = "";

    // Components are already sorted.
    for (Map.Entry<String, ComponentInfo> entry : components.entrySet()) {
      ComponentInfo component = entry.getValue();
      sb.append(separator);
      outputComponent(component, sb);
      separator = ",\n";
    }

    sb.append(']');

    FileObject src = createOutputFileObject(OUTPUT_FILE_NAME);
    Writer writer = src.openWriter();
    writer.write(sb.toString());
    writer.flush();
    writer.close();
    messager.printMessage(Diagnostic.Kind.NOTE, "Wrote file " + src.toUri());
  }
コード例 #6
0
  private <M extends Serializable> void resolvedQualified(
      TemplateProvider<?> provider, Template<M> template, Element[] elements) {

    //
    TemplatesMetaModel metaModel = application.getChild(TemplatesMetaModel.KEY);

    //
    Path.Relative path = template.getRelativePath();
    if (classCache.containsKey(path)) {
      log.log("Template class " + path + " was found in cache");
      return;
    }

    //
    Path.Absolute resolvedPath = metaModel.resolvePath(path);

    //
    Writer writer = null;
    try {
      // Template qualified class
      FileObject classFile =
          application.getProcessingContext().createSourceFile(resolvedPath.getName(), elements);
      writer = classFile.openWriter();
      writer.append("package ").append(resolvedPath.getDirs()).append(";\n");
      writer.append("import ").append(TemplateDescriptor.class.getCanonicalName()).append(";\n");
      writer.append("import ").append(TemplatePlugin.class.getCanonicalName()).append(";\n");
      writer.append("@").append(Generated.class.getName()).append("({})\n");
      writer
          .append("@")
          .append(juzu.Path.class.getName())
          .append("(\"")
          .append(path.getValue())
          .append("\")\n");
      writer
          .append("public class ")
          .append(path.getRawName())
          .append(" extends ")
          .append(juzu.template.Template.class.getName())
          .append("\n");
      writer.append("{\n");
      writer.append("@javax.inject.Inject\n");
      writer
          .append("public ")
          .append(path.getRawName())
          .append("(")
          .append(TemplatePlugin.class.getSimpleName())
          .append(" templatePlugin")
          .append(")\n");
      writer.append("{\n");
      writer
          .append("super(templatePlugin, \"")
          .append(path.getValue())
          .append("\"")
          .append(", ")
          .append(provider.getTemplateStubType().getName())
          .append(".class);\n");
      writer.append("}\n");

      //
      writer
          .append("public static final ")
          .append(TemplateDescriptor.class.getName())
          .append(" DESCRIPTOR = new ")
          .append(TemplateDescriptor.class.getName())
          .append("(")
          .append(resolvedPath.getName())
          .append(".class,")
          .append(provider.getTemplateStubType().getName())
          .append(".class")
          .append(");\n");

      //
      String baseBuilderName = juzu.template.Template.Builder.class.getCanonicalName();
      if (template.getParameters() != null) {
        // Implement abstract method with this class Builder covariant return type
        writer.append("public Builder builder() {\n");
        writer.append("return new Builder();\n");
        writer.append("}\n");

        // Covariant return type of with()
        writer.append("public Builder with() {\n");
        writer.append("return (Builder)super.with();\n");
        writer.append("}\n");

        // Setters on builders
        writer.append("public class Builder extends ").append(baseBuilderName).append("\n");
        writer.append("{\n");
        for (String paramName : template.getParameters()) {
          writer
              .append("public Builder ")
              .append(paramName)
              .append("(Object ")
              .append(paramName)
              .append(") {\n");
          writer.append("set(\"").append(paramName).append("\",").append(paramName).append(");\n");
          writer.append("return this;\n");
          writer.append(("}\n"));
        }
        writer.append("}\n");
      } else {
        // Implement abstract factory method
        writer.append("public ").append(baseBuilderName).append(" builder() {\n");
        writer.append("return new ").append(baseBuilderName).append("();\n");
        writer.append("}\n");
      }

      // Close class
      writer.append("}\n");

      //
      classCache.put(path, classFile);

      //
      log.log(
          "Generated template class "
              + path
              + " as "
              + classFile.toUri()
              + " with originating elements "
              + Arrays.asList(elements));
    } catch (IOException e) {
      throw TemplateMetaModel.CANNOT_WRITE_TEMPLATE_CLASS.failure(e, elements[0], path);
    } finally {
      Tools.safeClose(writer);
    }
  }