Exemplo n.º 1
0
  @Override
  public CompilationUnit[] getCompilationUnits() {
    if (this.compilationUnits == null) return EclipseCompilerImpl.NO_UNITS;
    ArrayList<CompilationUnit> units = new ArrayList<CompilationUnit>();
    for (final JavaFileObject javaFileObject : this.compilationUnits) {
      if (javaFileObject.getKind() != JavaFileObject.Kind.SOURCE) {
        throw new IllegalArgumentException();
      }
      String name = javaFileObject.getName();
      name = name.replace('\\', '/');
      CompilationUnit compilationUnit =
          new CompilationUnit(null, name, null) {

            @Override
            public char[] getContents() {
              try {
                return javaFileObject.getCharContent(true).toString().toCharArray();
              } catch (IOException e) {
                e.printStackTrace();
                throw new AbortCompilationUnit(null, e, null);
              }
            }
          };
      units.add(compilationUnit);
      this.javaFileObjectMap.put(compilationUnit, javaFileObject);
    }
    CompilationUnit[] result = new CompilationUnit[units.size()];
    units.toArray(result);
    return result;
  }
  private OutputFileObject getFileForOutput(
      Location location,
      JavaFileObject.Kind kind,
      String fileName,
      @Nullable String className,
      FileObject sibling)
      throws IOException {
    JavaFileObject src = null;
    if (sibling instanceof JavaFileObject) {
      final JavaFileObject javaFileObject = (JavaFileObject) sibling;
      if (javaFileObject.getKind() == JavaFileObject.Kind.SOURCE) {
        src = javaFileObject;
      }
    }

    File dir = getSingleOutputDirectory(location, src);

    if (location == StandardLocation.CLASS_OUTPUT) {
      if (dir == null) {
        throw new IOException("Output directory is not specified");
      }
    } else if (location == StandardLocation.SOURCE_OUTPUT) {
      if (dir == null) {
        dir = getSingleOutputDirectory(StandardLocation.CLASS_OUTPUT, src);
        if (dir == null) {
          throw new IOException("Neither class output directory nor source output are specified");
        }
      }
    }
    final File file =
        (dir == null ? new File(fileName).getAbsoluteFile() : new File(dir, fileName));
    return new OutputFileObject(myContext, file, kind, className, src);
  }
Exemplo n.º 3
0
  public ModuleSymbol findSingleModule() {
    try {
      JavaFileObject src_fo = getModuleInfoFromLocation(StandardLocation.SOURCE_PATH, Kind.SOURCE);
      JavaFileObject class_fo =
          getModuleInfoFromLocation(StandardLocation.CLASS_OUTPUT, Kind.CLASS);
      JavaFileObject fo =
          (src_fo == null)
              ? class_fo
              : (class_fo == null) ? src_fo : classFinder.preferredFileObject(src_fo, class_fo);

      ModuleSymbol msym;
      if (fo == null) {
        msym = syms.unnamedModule;
      } else {
        switch (fo.getKind()) {
          case SOURCE:
            if (!inFindSingleModule) {
              try {
                inFindSingleModule = true;
                // Note: the following will trigger a re-entrant call to Modules.enter
                msym = sourceFileCompleter.complete(fo);
                msym.module_info.classfile = fo;
              } finally {
                inFindSingleModule = false;
              }
            } else {
              // the module-info.java does not contain a module declaration,
              // avoid infinite recursion:
              msym = syms.unnamedModule;
            }
            break;
          case CLASS:
            Name name;
            try {
              name = names.fromString(readModuleName(fo));
            } catch (BadClassFile | IOException ex) {
              // fillIn will report proper errors:
              name = names.error;
            }
            msym = syms.enterModule(name);
            msym.module_info.classfile = fo;
            msym.completer = Completer.NULL_COMPLETER;
            classFinder.fillIn(msym.module_info);
            break;
          default:
            Assert.error();
            msym = syms.unnamedModule;
            break;
        }
      }

      msym.classLocation = StandardLocation.CLASS_OUTPUT;
      return msym;

    } catch (IOException e) {
      throw new Error(e); // FIXME
    }
  }
 /* (non-Javadoc)
  * @see javax.tools.JavaFileManager#inferBinaryName(javax.tools.JavaFileManager.Location, javax.tools.JavaFileObject)
  */
 @Override
 public String inferBinaryName(Location location, JavaFileObject file) {
   String name = file.getName();
   JavaFileObject javaFileObject = null;
   int index = name.lastIndexOf('.');
   if (index != -1) {
     name = name.substring(0, index);
   }
   try {
     javaFileObject = getJavaFileForInput(location, name, file.getKind());
   } catch (IOException e) {
     // ignore
   } catch (IllegalArgumentException iae) {
     return null; // Either unknown kind or location not present
   }
   if (javaFileObject == null) {
     return null;
   }
   return name.replace('/', '.');
 }
    /** Enter a set of generated class files. */
    private List<ClassSymbol> enterClassFiles(Map<String, JavaFileObject> classFiles) {
      ClassReader reader = ClassReader.instance(context);
      Names names = Names.instance(context);
      List<ClassSymbol> list = List.nil();

      for (Map.Entry<String, JavaFileObject> entry : classFiles.entrySet()) {
        Name name = names.fromString(entry.getKey());
        JavaFileObject file = entry.getValue();
        if (file.getKind() != JavaFileObject.Kind.CLASS) throw new AssertionError(file);
        ClassSymbol cs;
        if (isPkgInfo(file, JavaFileObject.Kind.CLASS)) {
          Name packageName = Convert.packagePart(name);
          PackageSymbol p = reader.enterPackage(packageName);
          if (p.package_info == null)
            p.package_info = reader.enterClass(Convert.shortName(name), p);
          cs = p.package_info;
          if (cs.classfile == null) cs.classfile = file;
        } else cs = reader.enterClass(name, file);
        list = list.prepend(cs);
      }
      return list.reverse();
    }