Skip to content

Commit

Permalink
Use JavaParserUtil instead of StaticJavaParser (#4499)
Browse files Browse the repository at this point in the history
  • Loading branch information
mernst authored Apr 1, 2021
1 parent b79ec90 commit abdbea3
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.checkerframework.common.basetype;

import com.github.javaparser.ParseProblemException;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.printer.PrettyPrinter;
import com.sun.source.tree.AnnotatedTypeTree;
Expand Down Expand Up @@ -131,6 +130,7 @@
import org.checkerframework.framework.util.ContractsFromMethod;
import org.checkerframework.framework.util.FieldInvariants;
import org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException;
import org.checkerframework.framework.util.JavaParserUtil;
import org.checkerframework.framework.util.StringToJavaExpression;
import org.checkerframework.javacutil.AnnotationBuilder;
import org.checkerframework.javacutil.AnnotationUtils;
Expand Down Expand Up @@ -338,10 +338,8 @@ protected void testJointJavacJavaParserVisitor() {
}

Map<Tree, com.github.javaparser.ast.Node> treePairs = new HashMap<>();
try {
java.io.InputStream reader = root.getSourceFile().openInputStream();
com.github.javaparser.ast.CompilationUnit javaParserRoot = StaticJavaParser.parse(reader);
reader.close();
try (InputStream reader = root.getSourceFile().openInputStream()) {
CompilationUnit javaParserRoot = JavaParserUtil.parseCompilationUnit(reader);
JavaParserUtils.concatenateAddedStringLiterals(javaParserRoot);
new JointVisitorWithDefaultAction() {
@Override
Expand Down Expand Up @@ -383,7 +381,7 @@ protected void testAnnotationInsertion() {

CompilationUnit originalAst;
try (InputStream originalInputStream = root.getSourceFile().openInputStream()) {
originalAst = StaticJavaParser.parse(originalInputStream);
originalAst = JavaParserUtil.parseCompilationUnit(originalInputStream);
} catch (IOException e) {
throw new BugInCF("Error while reading Java file: " + root.getSourceFile().toUri(), e);
}
Expand All @@ -406,7 +404,7 @@ protected void testAnnotationInsertion() {

CompilationUnit modifiedAst = null;
try {
modifiedAst = StaticJavaParser.parse(withAnnotations);
modifiedAst = JavaParserUtil.parseCompilationUnit(withAnnotations);
} catch (ParseProblemException e) {
throw new BugInCF("Failed to parse annotation insertion:\n" + withAnnotations, e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.checkerframework.common.wholeprograminference;

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.CallableDeclaration;
Expand Down Expand Up @@ -58,6 +57,7 @@
import org.checkerframework.framework.type.AnnotatedTypeMirror;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType;
import org.checkerframework.framework.type.GenericAnnotatedTypeFactory;
import org.checkerframework.framework.util.JavaParserUtil;
import org.checkerframework.javacutil.AnnotationUtils;
import org.checkerframework.javacutil.BugInCF;
import org.checkerframework.javacutil.ElementUtils;
Expand Down Expand Up @@ -351,7 +351,7 @@ private void addSourceFile(String path) {

CompilationUnit root;
try {
root = StaticJavaParser.parse(new File(path));
root = JavaParserUtil.parseCompilationUnit(new File(path));
} catch (FileNotFoundException e) {
throw new BugInCF("Failed to read Java file " + path, e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.checkerframework.framework.ajava;

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.TypeDeclaration;
import java.io.File;
Expand All @@ -10,6 +9,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.checkerframework.framework.util.JavaParserUtil;
import org.checkerframework.javacutil.BugInCF;

/**
Expand Down Expand Up @@ -45,7 +45,7 @@ public void addFileOrDirectory(File location) {

if (location.isFile() && location.getName().endsWith(".ajava")) {
try {
CompilationUnit root = StaticJavaParser.parse(location);
CompilationUnit root = JavaParserUtil.parseCompilationUnit(location);
for (TypeDeclaration<?> type : root.getTypes()) {
String name = JavaParserUtils.getFullyQualifiedName(type, root);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.checkerframework.framework.ajava;

import com.github.javaparser.Position;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.Node;
Expand Down Expand Up @@ -50,6 +49,7 @@
import org.checkerframework.checker.signature.qual.DotSeparatedIdentifiers;
import org.checkerframework.checker.signature.qual.FullyQualifiedName;
import org.checkerframework.framework.stub.AnnotationFileParser;
import org.checkerframework.framework.util.JavaParserUtil;
import org.plumelib.util.FilesPlume;

/** This program inserts annotations from an ajava file into a Java file. See {@link #main}. */
Expand Down Expand Up @@ -460,8 +460,8 @@ private Map<String, TypeElement> getImportedAnnotations(CompilationUnit cu) {
*/
public String insertAnnotations(
InputStream annotationFile, String javaFileContents, String lineSeparator) {
CompilationUnit annotationCu = StaticJavaParser.parse(annotationFile);
CompilationUnit javaCu = StaticJavaParser.parse(javaFileContents);
CompilationUnit annotationCu = JavaParserUtil.parseCompilationUnit(annotationFile);
CompilationUnit javaCu = JavaParserUtil.parseCompilationUnit(javaFileContents);
BuildInsertionsVisitor insertionVisitor =
new BuildInsertionsVisitor(javaFileContents, lineSeparator);
annotationCu.accept(insertionVisitor, javaCu);
Expand Down Expand Up @@ -568,7 +568,7 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {

CompilationUnit root = null;
try {
root = StaticJavaParser.parse(path);
root = JavaParserUtil.parseCompilationUnit(path.toFile());
} catch (IOException e) {
System.err.println("Failed to read file: " + path);
System.exit(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.javaparser.ParseProblemException;
import com.github.javaparser.Position;
import com.github.javaparser.Problem;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
Expand Down Expand Up @@ -98,6 +97,7 @@
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType;
import org.checkerframework.framework.util.JavaParserUtil;
import org.checkerframework.framework.util.element.ElementAnnotationUtil.ErrorTypeKindException;
import org.checkerframework.javacutil.AnnotationBuilder;
import org.checkerframework.javacutil.AnnotationUtils;
Expand Down Expand Up @@ -607,7 +607,7 @@ private void parseStubUnit(InputStream inputStream) {
if (debugAnnotationFileParser) {
stubDebug(String.format("parsing stub file %s", filename));
}
stubUnit = StaticJavaParser.parseStubUnit(inputStream);
stubUnit = JavaParserUtil.parseStubUnit(inputStream);

// getAllAnnotations() also modifies importedConstants and importedTypes. This should
// be refactored to be nicer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.javaparser.ParseException;
import com.github.javaparser.ParseProblemException;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.NodeList;
Expand Down Expand Up @@ -54,6 +53,7 @@
import org.checkerframework.checker.signature.qual.ClassGetName;
import org.checkerframework.checker.signature.qual.DotSeparatedIdentifiers;
import org.checkerframework.checker.signature.qual.FullyQualifiedName;
import org.checkerframework.framework.util.JavaParserUtil;
import org.checkerframework.javacutil.BugInCF;
import org.plumelib.reflection.Signatures;
import scenelib.annotations.Annotation;
Expand Down Expand Up @@ -180,7 +180,7 @@ private static void convert(AScene scene, InputStream in, OutputStream out)
throws IOException, DefException, ParseException {
StubUnit iu;
try {
iu = StaticJavaParser.parseStubUnit(in);
iu = JavaParserUtil.parseStubUnit(in);
} catch (ParseProblemException e) {
iu = null;
throw new BugInCF(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.checkerframework.framework.util;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseProblemException;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.StubUnit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

/** A replacement for StaticJavaParser that does not leak memory. */
public class JavaParserUtil {

/**
* Parses the Java code contained in the {@code InputStream} and returns a {@code CompilationUnit}
* that represents it.
*
* <p>This is like {@code StaticJavaParser.parse}, but it does not lead to memory leaks because it
* creates a new instance of JavaParser each time it is invoked. Re-using {@code StaticJavaParser}
* causes memory problems because it retains too much memory.
*
* @param inputStream the Java source code
* @return CompilationUnit representing the Java source code
* @throws ParseProblemException if the source code has parser errors
*/
public static CompilationUnit parseCompilationUnit(InputStream inputStream) {
JavaParser javaParser = new JavaParser(new ParserConfiguration());
ParseResult<CompilationUnit> parseResult = javaParser.parse(inputStream);
if (parseResult.isSuccessful() && parseResult.getResult().isPresent()) {
return parseResult.getResult().get();
} else {
throw new ParseProblemException(parseResult.getProblems());
}
}

/**
* Parses the Java code contained in the {@code File} and returns a {@code CompilationUnit} that
* represents it.
*
* <p>This is like {@code StaticJavaParser.parse}, but it does not lead to memory leaks because it
* creates a new instance of JavaParser each time it is invoked. Re-using {@code StaticJavaParser}
* causes memory problems because it retains too much memory.
*
* @param file the Java source code
* @return CompilationUnit representing the Java source code
* @throws ParseProblemException if the source code has parser errors
* @throws FileNotFoundException if the file was not found
*/
public static CompilationUnit parseCompilationUnit(File file) throws FileNotFoundException {
JavaParser javaParser = new JavaParser(new ParserConfiguration());
ParseResult<CompilationUnit> parseResult = javaParser.parse(file);
if (parseResult.isSuccessful() && parseResult.getResult().isPresent()) {
return parseResult.getResult().get();
} else {
throw new ParseProblemException(parseResult.getProblems());
}
}

/**
* Parses the Java code contained in the {@code String} and returns a {@code CompilationUnit} that
* represents it.
*
* <p>This is like {@code StaticJavaParser.parse}, but it does not lead to memory leaks because it
* creates a new instance of JavaParser each time it is invoked. Re-using {@code StaticJavaParser}
* causes memory problems because it retains too much memory.
*
* @param javaSource the Java source code
* @return CompilationUnit representing the Java source code
* @throws ParseProblemException if the source code has parser errors
*/
public static CompilationUnit parseCompilationUnit(String javaSource) {
JavaParser javaParser = new JavaParser(new ParserConfiguration());
ParseResult<CompilationUnit> parseResult = javaParser.parse(javaSource);
if (parseResult.isSuccessful() && parseResult.getResult().isPresent()) {
return parseResult.getResult().get();
} else {
throw new ParseProblemException(parseResult.getProblems());
}
}

/**
* Parses the stub file contained in the {@code InputStream} and returns a {@code StubUnit} that
* represents it.
*
* <p>This is like {@code StaticJavaParser.parse}, but it does not lead to memory leaks because it
* creates a new instance of JavaParser each time it is invoked. Re-using {@code StaticJavaParser}
* causes memory problems because it retains too much memory.
*
* @param inputStream the stub file
* @return StubUnit representing the stub file
* @throws ParseProblemException if the source code has parser errors
*/
public static StubUnit parseStubUnit(InputStream inputStream) {
JavaParser javaParser = new JavaParser(new ParserConfiguration());
ParseResult<StubUnit> parseResult = javaParser.parseStubUnit(inputStream);
if (parseResult.isSuccessful() && parseResult.getResult().isPresent()) {
return parseResult.getResult().get();
} else {
throw new ParseProblemException(parseResult.getProblems());
}
}
}

0 comments on commit abdbea3

Please sign in to comment.