-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use JavaParserUtil instead of StaticJavaParser (#4499)
- Loading branch information
Showing
7 changed files
with
121 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
framework/src/main/java/org/checkerframework/framework/util/JavaParserUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |