All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.codemodder.javaparser.DefaultCachingJavaParser Maven / Gradle / Ivy

There is a newer version: 0.98.6
Show newest version
package io.codemodder.javaparser;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

final class DefaultCachingJavaParser implements CachingJavaParser {

  private final JavaParser parser;
  private final Map cache;

  DefaultCachingJavaParser(final JavaParser parser) {
    this.parser = Objects.requireNonNull(parser);
    this.cache = new HashMap<>();
  }

  @Override
  public CompilationUnit parseJavaFile(final Path file) throws IOException {
    if (cache.containsKey(file)) {
      return cache.get(file);
    }
    try (InputStream in = Files.newInputStream(file)) {
      final ParseResult result = parser.parse(in);
      if (!result.isSuccessful()) {
        throw new RuntimeException("can't parse file");
      }
      CompilationUnit cu = result.getResult().orElseThrow();
      LexicalPreservingPrinter.setup(cu);
      cache.put(file, cu);
      return cu;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy