Exemplo n.º 1
0
  public static CompilationUnit[] parseICompilationUnitStream(
      Stream<ICompilationUnit> stream, IJavaProject javaProject)
      throws IOException, JavaModelException {

    // Sneaky.<JavaModelException> Throw();
    // Sneaky.<IOException> Throw();
    return stream
        .map(ICompilationUnit::getResource)
        .map(IResource::getLocationURI)
        .map(File::new)
        .map(Sneaky.unchecked(f -> SharedUtils.parseJavaSource(f, javaProject)))
        .filter(Objects::nonNull)
        .toArray(CompilationUnit[]::new);
  }
Exemplo n.º 2
0
  /**
   * Parses the specified Java source file located in the given Java project.
   *
   * @param sourceFile The specified Java source file.
   * @param project The given Java project.
   * @return The parsed compilation unit.
   * @throws IOException Thrown when I/O error occurs during reading the file.
   * @throws JavaModelException
   */
  public static CompilationUnit parseJavaSource(File sourceFile, IJavaProject project)
      throws IOException, JavaModelException {
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    char[] content = SharedUtils.getFileContents(sourceFile);

    parser.setSource(content);
    parser.setProject(project);

    parser.setResolveBindings(true);
    parser.setBindingsRecovery(true);
    parser.setUnitName(sourceFile.getName());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);

    CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
    return compilationUnit;
  }