예제 #1
0
 private void addResources() throws Abort {
   HashSet<String> written = new HashSet<String>();
   try {
     for (JavaFileObject fo : resourceFileObjects) {
       CeyloncFileManager dfm = (CeyloncFileManager) fileManager;
       String jarFileName =
           JarUtils.toPlatformIndependentPath(
               dfm.getLocation(CeylonLocation.RESOURCE_PATH), fo.getName());
       if (!written.contains(jarFileName)) {
         dfm.setModule(modelLoader.findModuleForFile(new File(jarFileName)));
         FileObject outFile =
             dfm.getFileForOutput(StandardLocation.CLASS_OUTPUT, "", jarFileName, null);
         OutputStream out = outFile.openOutputStream();
         try {
           InputStream in = new FileInputStream(new File(fo.getName()));
           try {
             JarUtils.copy(in, out);
           } finally {
             in.close();
           }
         } finally {
           out.close();
         }
         written.add(jarFileName);
       }
     }
   } catch (IOException ex) {
     throw new Abort(ex);
   }
 }
예제 #2
0
 // This is a bit of a hack, but if we got passed a list of resources
 // without any accompaning source files we'll not be able to determine
 // the module to which the resource files belong. So to try to fix that
 // we see if a module file exists in the source folders and add it to
 // the list of source files
 private List<JavaFileObject> addModuleDescriptors(
     List<JavaFileObject> sourceFiles, List<JavaFileObject> resourceFiles) {
   List<JavaFileObject> result = sourceFiles;
   JavacFileManager dfm = (JavacFileManager) fileManager;
   for (JavaFileObject fo : resourceFiles) {
     String resName =
         JarUtils.toPlatformIndependentPath(
             dfm.getLocation(CeylonLocation.RESOURCE_PATH), fo.getName());
     JavaFileObject moduleFile = findModuleDescriptorForFile(new File(resName));
     if (moduleFile != null && !result.contains(moduleFile)) {
       result = result.append(moduleFile);
     }
   }
   return result;
 }
예제 #3
0
  private String getPackage(JavaFileObject file) throws IOException {
    Iterable<? extends File> prefixes =
        ((JavacFileManager) fileManager).getLocation(StandardLocation.SOURCE_PATH);

    // Figure out the package name by stripping the "-src" prefix and
    // extracting
    // the package part of the fullname.

    String filePath = file.toUri().getPath();
    // go absolute
    filePath = new File(filePath).getCanonicalPath();

    int srcDirLength = 0;
    for (File prefixFile : prefixes) {
      String prefix = prefixFile.getCanonicalPath();
      if (filePath.startsWith(prefix) && prefix.length() > srcDirLength) {
        srcDirLength = prefix.length();
      }
    }

    if (srcDirLength > 0) {
      String fullname = filePath.substring(srcDirLength);
      assert fullname.endsWith(".ceylon");
      fullname = fullname.substring(0, fullname.length() - ".ceylon".length());
      fullname = fullname.replace(File.separator, ".");
      if (fullname.startsWith(".")) fullname = fullname.substring(1);
      String packageName = Convert.packagePart(fullname);
      if (!packageName.equals("")) return packageName;
    }
    return null;
  }
예제 #4
0
 private boolean isResource(JavaFileObject fo) {
   // make sure we get a proper normalized abslute path
   String fileName = FileUtil.absoluteFile(new File(fo.toUri().getPath())).getPath();
   // now see if it's in any of the resource paths
   JavacFileManager dfm = (JavacFileManager) fileManager;
   for (File dir : dfm.getLocation(CeylonLocation.RESOURCE_PATH)) {
     String prefix = FileUtil.absoluteFile(dir).getPath();
     if (fileName.startsWith(prefix)) {
       return true;
     }
   }
   return false;
 }
예제 #5
0
  /**
   * Parse contents of file.
   *
   * @param filename The name of the file to be parsed.
   */
  public JCTree.JCCompilationUnit parse(JavaFileObject filename) {
    JavaFileObject prev = log.useSource(filename);
    try {
      JCTree.JCCompilationUnit t;
      if (filename.getName().endsWith(".java")) {

        t = parse(filename, readSource(filename));
      } else {
        t = ceylonParse(filename, readSource(filename));
        t.endPositions = new JavacParser.EmptyEndPosTable(null);
      }
      if (t.endPositions != null) log.setEndPosTable(filename, t.endPositions);
      return t;
    } finally {
      log.useSource(prev);
    }
  }
예제 #6
0
  private JCCompilationUnit ceylonParse(JavaFileObject filename, CharSequence readSource) {
    if (ceylonEnter.hasRun())
      throw new RunTwiceException(
          "Trying to load new source file after CeylonEnter has been called: " + filename);
    try {
      ModuleManager moduleManager = phasedUnits.getModuleManager();
      ModuleSourceMapper moduleSourceMapper = phasedUnits.getModuleSourceMapper();
      File sourceFile = new File(filename.getName());
      // FIXME: temporary solution
      VirtualFile file = vfs.getFromFile(sourceFile);
      VirtualFile srcDir = vfs.getFromFile(getSrcDir(sourceFile));

      String source = readSource.toString();
      char[] chars = source.toCharArray();
      LineMap map = Position.makeLineMap(chars, chars.length, false);

      PhasedUnit phasedUnit = null;

      PhasedUnit externalPhasedUnit = compilerDelegate.getExternalSourcePhasedUnit(srcDir, file);

      String suppressWarnings = options.get(Option.CEYLONSUPPRESSWARNINGS);
      final EnumSet<Warning> suppressedWarnings;
      if (suppressWarnings != null) {
        if (suppressWarnings.trim().isEmpty()) {
          suppressedWarnings = EnumSet.allOf(Warning.class);
        } else {
          suppressedWarnings = EnumSet.noneOf(Warning.class);
          for (String name : suppressWarnings.trim().split(" *, *")) {
            suppressedWarnings.add(Warning.valueOf(name));
          }
        }
      } else {
        suppressedWarnings = EnumSet.noneOf(Warning.class);
      }

      if (externalPhasedUnit != null) {
        phasedUnit = new CeylonPhasedUnit(externalPhasedUnit, filename, map);
        phasedUnit.setSuppressedWarnings(suppressedWarnings);
        phasedUnits.addPhasedUnit(externalPhasedUnit.getUnitFile(), phasedUnit);
        gen.setMap(map);

        String pkgName = phasedUnit.getPackage().getQualifiedNameString();
        if ("".equals(pkgName)) {
          pkgName = null;
        }
        return gen.makeJCCompilationUnitPlaceholder(
            phasedUnit.getCompilationUnit(), filename, pkgName, phasedUnit);
      }
      if (phasedUnit == null) {
        ANTLRStringStream input = new NewlineFixingStringStream(source);
        CeylonLexer lexer = new CeylonLexer(input);

        CommonTokenStream tokens = new CommonTokenStream(lexer);

        CeylonParser parser = new CeylonParser(tokens);
        CompilationUnit cu = parser.compilationUnit();

        java.util.List<LexError> lexerErrors = lexer.getErrors();
        for (LexError le : lexerErrors) {
          printError(le, le.getMessage(), "ceylon.lexer", map);
        }

        java.util.List<ParseError> parserErrors = parser.getErrors();
        for (ParseError pe : parserErrors) {
          printError(pe, pe.getMessage(), "ceylon.parser", map);
        }

        // if we continue and it's not a descriptor, we don't care about errors
        if ((options.get(Option.CEYLONCONTINUE) != null
                && !ModuleManager.MODULE_FILE.equals(sourceFile.getName())
                && !ModuleManager.PACKAGE_FILE.equals(sourceFile.getName()))
            // otherwise we care about errors
            || (lexerErrors.size() == 0 && parserErrors.size() == 0)) {
          // FIXME: this is bad in many ways
          String pkgName = getPackage(filename);
          // make a Package with no module yet, we will resolve them later
          /*
           * Stef: see javadoc for findOrCreateModulelessPackage() for why this is here.
           */
          com.redhat.ceylon.model.typechecker.model.Package p =
              modelLoader.findOrCreateModulelessPackage(pkgName == null ? "" : pkgName);
          phasedUnit =
              new CeylonPhasedUnit(
                  file,
                  srcDir,
                  cu,
                  p,
                  moduleManager,
                  moduleSourceMapper,
                  ceylonContext,
                  filename,
                  map);
          phasedUnit.setSuppressedWarnings(suppressedWarnings);
          phasedUnits.addPhasedUnit(file, phasedUnit);
          gen.setMap(map);

          return gen.makeJCCompilationUnitPlaceholder(cu, filename, pkgName, phasedUnit);
        }
      }
    } catch (RuntimeException e) {
      throw e;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    JCCompilationUnit result =
        make.TopLevel(List.<JCAnnotation>nil(), null, List.<JCTree>of(make.Erroneous()));
    result.sourcefile = filename;
    return result;
  }