Skip to content

Commit

Permalink
More clean up of streams
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Jul 7, 2016
1 parent e81f9d1 commit 72d0d95
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 147 deletions.
Expand Up @@ -123,8 +123,7 @@ public static Parameter createParameter(Type type, String name) {
public static FieldDeclaration createFieldDeclaration(int modifiers, Type type, VariableDeclarator variable) {
List<VariableDeclarator> variables = new ArrayList<VariableDeclarator>();
variables.add(variable);
FieldDeclaration ret = new FieldDeclaration(modifiers, type, variables);
return ret;
return new FieldDeclaration(modifiers, type, variables);
}

/**
Expand Down
Expand Up @@ -21,32 +21,34 @@

package com.github.javaparser;

import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.CommentsCollection;
import com.github.javaparser.ast.comments.CommentsParser;
import com.github.javaparser.ast.comments.LineComment;

import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
* Assigns comments to nodes of the AST.
*
* @author Sebastian Kuerten
* @author Júlio Vilmar Gesser
*/
public class CommentsInserter
{

class CommentsInserter {
private boolean doNotAssignCommentsPreceedingEmptyLines = true;
private boolean doNotConsiderAnnotationsAsNodeStartForCodeAttribution = false;

CommentsInserter() {
}

public void insertComments(CompilationUnit cu, String code)
throws IOException {
public void insertComments(CompilationUnit cu, String comments) throws IOException {
CommentsParser commentsParser = new CommentsParser();
CommentsCollection allComments = commentsParser.parse(code);
CommentsCollection allComments = commentsParser.parse(comments);

insertCommentsInCu(cu, allComments);
}
Expand All @@ -55,18 +57,16 @@ public boolean getDoNotConsiderAnnotationsAsNodeStartForCodeAttribution() {
return doNotConsiderAnnotationsAsNodeStartForCodeAttribution;
}

public void setDoNotConsiderAnnotationsAsNodeStartForCodeAttribution(
boolean doNotConsiderAnnotationsAsNodeStartForCodeAttribution) {
this.doNotConsiderAnnotationsAsNodeStartForCodeAttribution = doNotConsiderAnnotationsAsNodeStartForCodeAttribution;
public void setDoNotConsiderAnnotationsAsNodeStartForCodeAttribution(boolean newValue) {
this.doNotConsiderAnnotationsAsNodeStartForCodeAttribution = newValue;
}

public boolean getDoNotAssignCommentsPreceedingEmptyLines() {
return doNotAssignCommentsPreceedingEmptyLines;
}

public void setDoNotAssignCommentsPreceedingEmptyLines(
boolean doNotAssignCommentsPreceedingEmptyLines) {
this.doNotAssignCommentsPreceedingEmptyLines = doNotAssignCommentsPreceedingEmptyLines;
public void setDoNotAssignCommentsPreceedingEmptyLines(boolean newValue) {
this.doNotAssignCommentsPreceedingEmptyLines = newValue;
}

/**
Expand Down
Expand Up @@ -32,33 +32,41 @@
import java.util.List;

/**
* A thin wrapper around ASTParser with a few convenience methods for parsing CompilationUnits, Blocks, Statements, etc.
*
* @author Sebastian Kuerten
*/
public class InstanceJavaParser {
private final ASTParser astParser;
private final Provider provider;

public InstanceJavaParser(Provider provider) {
this.provider=provider;
astParser = new ASTParser(provider);
}

private ASTParser astParser;
public InstanceJavaParser(Reader reader) {
this(new StreamProvider(reader));
}

public InstanceJavaParser(InputStream input) throws IOException {
astParser = new ASTParser(new StreamProvider(input));
this(new StreamProvider(input));
}

public InstanceJavaParser(InputStream input, String encoding) throws IOException {
astParser = new ASTParser(new StreamProvider(input, encoding));
this(new StreamProvider(input, encoding));
}

public InstanceJavaParser(File file) throws IOException {
InputStream input = new BufferedInputStream(new FileInputStream(file));
astParser = new ASTParser(new StreamProvider(input));
this(new FileInputStream(file));
}

public InstanceJavaParser(File file, String encoding)
throws IOException {
InputStream input = new BufferedInputStream(new FileInputStream(file));
astParser = new ASTParser(new StreamProvider(input, encoding));
public InstanceJavaParser(File file, String encoding) throws IOException {
this(new FileInputStream(file), encoding);
}

public InstanceJavaParser(Reader reader) {
astParser = new ASTParser(new StreamProvider(reader));
public InstanceJavaParser(String source) {
this(new StringReader(source));
}

/**
Expand All @@ -80,7 +88,19 @@ public List<Token> getTokens() {
* if the source code has parser errors
*/
public CompilationUnit parse() throws ParseException {
return astParser.CompilationUnit();
try {
return astParser.CompilationUnit();
}finally {
closeProvider();
}
}

private void closeProvider() {
try {
provider.close();
} catch (IOException e) {
// Since we're done parsing and have our result, we don't care about any errors.
}
}

/**
Expand All @@ -91,7 +111,11 @@ public CompilationUnit parse() throws ParseException {
* if the source code has parser errors
*/
public BlockStmt parseBlock() throws ParseException {
return astParser.Block();
try{
return astParser.Block();
}finally {
closeProvider();
}
}

/**
Expand All @@ -103,7 +127,11 @@ public BlockStmt parseBlock() throws ParseException {
* if the source code has parser errors
*/
public List<?> parseStatements() throws ParseException {
return astParser.Statements();
try {
return astParser.Statements();
}finally {
closeProvider();
}
}

/**
Expand All @@ -115,7 +143,11 @@ public List<?> parseStatements() throws ParseException {
* if the source code has parser errors
*/
public Statement parseStatement() throws ParseException {
return astParser.Statement();
try {
return astParser.Statement();
}finally {
closeProvider();
}
}

/**
Expand All @@ -127,7 +159,11 @@ public Statement parseStatement() throws ParseException {
* if the source code has parser errors
*/
public ImportDeclaration parseImport() throws ParseException {
return astParser.ImportDeclaration();
try {
return astParser.ImportDeclaration();
} finally {
closeProvider();
}
}

/**
Expand All @@ -139,7 +175,11 @@ public ImportDeclaration parseImport() throws ParseException {
* if the source code has parser errors
*/
public Expression parseExpression() throws ParseException {
return astParser.Expression();
try {
return astParser.Expression();
} finally {
closeProvider();
}
}

/**
Expand All @@ -151,7 +191,11 @@ public Expression parseExpression() throws ParseException {
* if the source code has parser errors
*/
public AnnotationExpr parseAnnotation() throws ParseException {
return astParser.Annotation();
try {
return astParser.Annotation();
} finally {
closeProvider();
}
}

/**
Expand All @@ -163,7 +207,11 @@ public AnnotationExpr parseAnnotation() throws ParseException {
* if the source code has parser errors
*/
public BodyDeclaration parseBodyDeclaration() throws ParseException {
return astParser.AnnotationBodyDeclaration();
try {
return astParser.AnnotationBodyDeclaration();
} finally {
closeProvider();
}
}

/**
Expand All @@ -179,7 +227,10 @@ public BodyDeclaration parseBodyDeclaration() throws ParseException {
*/
public BodyDeclaration parseClassOrInterfaceBodyDeclaration(
boolean isInterface) throws ParseException {
return astParser.ClassOrInterfaceBodyDeclaration(isInterface);
try {
return astParser.ClassOrInterfaceBodyDeclaration(isInterface);
} finally {
closeProvider();
}
}

}

0 comments on commit 72d0d95

Please sign in to comment.