io.codemodder.javaparser.DefaultCachingJavaParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of codemodder-base Show documentation
Show all versions of codemodder-base Show documentation
Base framework for writing codemods in Java
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