Skip to content

Commit

Permalink
Fix for resource leaks in AstBuilder and Antlr4PluginFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 16, 2018
1 parent 309021a commit 9a8ce13
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
import org.codehaus.groovy.control.CompilePhase;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
import org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport;
import org.codehaus.groovy.runtime.StringGroovyMethods;
import org.codehaus.groovy.syntax.Numbers;
import org.codehaus.groovy.syntax.SyntaxException;
Expand Down Expand Up @@ -355,7 +356,17 @@ public AstBuilder(SourceUnit sourceUnit) {
this.sourceUnit = sourceUnit;
this.moduleNode = new ModuleNode(sourceUnit);

CharStream charStream = createCharStream(sourceUnit);
// GRECLIPSE edit
//CharStream charStream = createCharStream(sourceUnit);
CharStream charStream;
try {
sourceUnitReader = new BufferedReader(sourceUnit.getSource().getReader());
} catch (IOException e) {
throw new RuntimeException("Error occurred reading the source code.", e);
}
try {
charStream = CharStreams.fromReader(sourceUnitReader, sourceUnit.getName());
// GRECLIPSE end

this.lexer = new GroovyLangLexer(charStream);
this.parser = new GroovyLangParser(new CommonTokenStream(this.lexer));
Expand All @@ -365,21 +376,24 @@ public AstBuilder(SourceUnit sourceUnit) {
this.groovydocManager = GroovydocManager.getInstance();

// GRECLIPSE add
try (BufferedReader reader = new BufferedReader(sourceUnit.getSource().getReader())) {
// TODO: Can this be done without boxing/unboxing offsets or juggling temp arrays?
int chr, off = 0; List<Integer> ends = new ArrayList<>(32); ends.add(0);
while ((chr = reader.read()) != -1) { off += 1;
if (chr == '\n') ends.add(off);
}
ends.add(off);
try (BufferedReader reader = new BufferedReader(sourceUnit.getSource().getReader())) {
// TODO: Can this be done without boxing/unboxing offsets or juggling temp arrays?
int chr, off = 0; List<Integer> ends = new ArrayList<>(32); ends.add(0);
while ((chr = reader.read()) != -1) { off += 1;
if (chr == '\n') ends.add(off);
}
ends.add(off);

int[] arr = new int[ends.size()];
for (int i = 0, n = arr.length; i < n; i += 1) {
arr[i] = ends.get(i);
int[] arr = new int[ends.size()];
for (int i = 0, n = arr.length; i < n; i += 1) {
arr[i] = ends.get(i);
}
this.locationSupport = new LocationSupport(arr);
}
this.locationSupport = new LocationSupport(arr);
} catch (Exception e) {
throw new RuntimeException("Error occurred reading the source code.", e);
} catch (Throwable t) {
DefaultGroovyMethodsSupport.closeQuietly(sourceUnitReader);
if (t instanceof Error) throw (Error) t; // preserve error semantics
throw new RuntimeException("Error occurred reading the source code.", t);
}
// GRECLIPSE end
}
Expand Down Expand Up @@ -424,6 +438,7 @@ private <T extends ASTNode> T configureAST(T astNode, GroovyParser.GroovyParserR
}
// GRECLIPSE end

/* GRECLIPSE edit
private CharStream createCharStream(SourceUnit sourceUnit) {
CharStream charStream;
Expand All @@ -437,6 +452,7 @@ private CharStream createCharStream(SourceUnit sourceUnit) {
return charStream;
}
*/

private GroovyParserRuleContext buildCST() throws CompilationFailedException {
GroovyParserRuleContext result;
Expand Down Expand Up @@ -496,6 +512,11 @@ public ModuleNode buildAST() {
} catch (Throwable t) {
throw convertException(t);
}
// GRECLIPSE add
finally {
DefaultGroovyMethodsSupport.closeQuietly(sourceUnitReader);
}
// GRECLIPSE end
}

@Override
Expand Down Expand Up @@ -5165,6 +5186,7 @@ public List<DeclarationExpression> getDeclarationExpressions() {
private final GroovydocManager groovydocManager;
// GRECLIPSE add
private final LocationSupport locationSupport;
private final BufferedReader sourceUnitReader;
// GRECLIPSE add
private final List<ClassNode> classNodeList = new LinkedList<>();
private final Deque<ClassNode> classNodeStack = new ArrayDeque<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,27 @@
*/
package org.codehaus.groovy.control;

import groovy.lang.GroovyRuntimeException;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.syntax.ParserException;
import org.codehaus.groovy.syntax.Reduction;

import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.io.Reader;

/**
* A factory of parser plugin instances
*
*/
public abstract class ParserPluginFactory {
/* GRECLIPSE edit
private static Class<?> ANTLR4_CLASS=null;
*/

/**
* creates the ANTLR 4 parser
* @return the factory for the parser
*/
public static ParserPluginFactory antlr4() {
/* GRECLIPSE edit
if (ANTLR4_CLASS==null) {
String name = "org.apache.groovy.parser.antlr4.Antlr4PluginFactory";
try {
Expand All @@ -54,6 +57,24 @@ public ParserPluginFactory run() throws Exception {
} catch (PrivilegedActionException e) {
throw new GroovyRuntimeException("Could not create instance of parser plugin factory for antlr4", e.getCause());
}
*/
return new ParserPluginFactory() {
@Override
public ParserPlugin createParserPlugin() {
return new ParserPlugin() {
@Override
public Reduction parseCST(SourceUnit sourceUnit, Reader reader) throws CompilationFailedException {
return null;
}
@Override
public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduction cst) throws ParserException {
assert sourceUnit.getSource() != null && sourceUnit.getSource().canReopenSource();
return new org.apache.groovy.parser.antlr4.AstBuilder(sourceUnit).buildAST();
}
};
}
};
// GRECLIPSE end
}

/**
Expand Down

0 comments on commit 9a8ce13

Please sign in to comment.