# HG changeset patch # Parent 41fa459bcf1b81a92fa4c69cbe9d55b8e1439e22 diff --git a/java.source/apichanges.xml b/java.source/apichanges.xml --- a/java.source/apichanges.xml +++ b/java.source/apichanges.xml @@ -108,6 +108,20 @@ + + + TreeUtilities.parse{Statement,Expression,VariableInitializer,StaticBlock} has been extended to provide parse errors + + + + + + org.netbeans.api.java.source.TreeUtilities has been extended with variants + of parseStatement,parseExpression,parseVariableInitializer and parseStaticBlock that + return compilation errors produced during parsing. + + + DisjointTypeTree renamed to DisjunctiveTypeTree, AnnotationTypeTree has been removed diff --git a/java.source/nbproject/project.properties b/java.source/nbproject/project.properties --- a/java.source/nbproject/project.properties +++ b/java.source/nbproject/project.properties @@ -46,7 +46,7 @@ javadoc.title=Java Source javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml -spec.version.base=0.70.0 +spec.version.base=0.71.0 test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/javac-api-nb-7.0-b07.jar test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\ ${o.n.core.dir}/lib/boot.jar:\ diff --git a/java.source/src/org/netbeans/api/java/source/TreeUtilities.java b/java.source/src/org/netbeans/api/java/source/TreeUtilities.java --- a/java.source/src/org/netbeans/api/java/source/TreeUtilities.java +++ b/java.source/src/org/netbeans/api/java/source/TreeUtilities.java @@ -62,6 +62,7 @@ import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.util.Context; import java.util.*; +import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.lang.model.SourceVersion; @@ -69,6 +70,9 @@ import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Types; +import javax.tools.Diagnostic; +import javax.tools.DiagnosticListener; +import javax.tools.JavaFileObject; import org.netbeans.api.annotations.common.NonNull; import org.netbeans.api.java.lexer.JavaTokenId; import org.netbeans.api.java.source.JavaSource.Phase; @@ -388,17 +392,29 @@ } /**Parses given statement. - * + * * @param stmt statement code * @param sourcePositions return value - new SourcePositions for the new tree * @return parsed {@link StatementTree} or null? */ public StatementTree parseStatement(String stmt, SourcePositions[] sourcePositions) { + return parseStatement(stmt, sourcePositions, new ArrayList>()); + } + + /**Parses given statement. + * + * @param stmt statement code + * @param sourcePositions return value - new SourcePositions for the new tree + * @param errors return value - Diagnostics found while parsing the given code + * @return parsed {@link StatementTree} or null? + * @since 0.71 + */ + public StatementTree parseStatement(String stmt, SourcePositions[] sourcePositions, List> errors) { com.sun.tools.javac.tree.TreeMaker jcMaker = com.sun.tools.javac.tree.TreeMaker.instance(info.impl.getJavacTask().getContext()); int oldPos = jcMaker.pos; try { - return (StatementTree)info.impl.getJavacTask().parseStatement(stmt, sourcePositions); + return (StatementTree)info.impl.getJavacTask().parseStatement(stmt, sourcePositions, new CollectingDiagnosticListener(errors)); } finally { jcMaker.pos = oldPos; } @@ -411,11 +427,23 @@ * @return parsed {@link ExpressionTree} or null? */ public ExpressionTree parseExpression(String expr, SourcePositions[] sourcePositions) { + return parseExpression(expr, sourcePositions, new ArrayList>()); + } + + /**Parses given expression. + * + * @param expr expression code + * @param sourcePositions return value - new SourcePositions for the new tree + * @param errors return value - Diagnostics found while parsing the given code + * @return parsed {@link ExpressionTree} or null? + * @since 0.71 + */ + public ExpressionTree parseExpression(String expr, SourcePositions[] sourcePositions, List> errors) { com.sun.tools.javac.tree.TreeMaker jcMaker = com.sun.tools.javac.tree.TreeMaker.instance(info.impl.getJavacTask().getContext()); int oldPos = jcMaker.pos; try { - return (ExpressionTree) info.impl.getJavacTask().parseExpression(expr, sourcePositions); + return (ExpressionTree) info.impl.getJavacTask().parseExpression(expr, sourcePositions, new CollectingDiagnosticListener(errors)); } finally { jcMaker.pos = oldPos; } @@ -428,33 +456,68 @@ * @return parsed {@link ExpressionTree} or null? */ public ExpressionTree parseVariableInitializer(String init, SourcePositions[] sourcePositions) { + return parseVariableInitializer(init, sourcePositions, new ArrayList>()); + } + + /**Parses given variable initializer. + * + * @param init initializer code + * @param sourcePositions return value - new SourcePositions for the new tree + * @param errors return value - Diagnostics found while parsing the given code + * @return parsed {@link ExpressionTree} or null? + * @since 0.71 + */ + public ExpressionTree parseVariableInitializer(String init, SourcePositions[] sourcePositions, List> errors) { com.sun.tools.javac.tree.TreeMaker jcMaker = com.sun.tools.javac.tree.TreeMaker.instance(info.impl.getJavacTask().getContext()); int oldPos = jcMaker.pos; try { - return (ExpressionTree)info.impl.getJavacTask().parseVariableInitializer(init, sourcePositions); + return (ExpressionTree)info.impl.getJavacTask().parseVariableInitializer(init, sourcePositions, new CollectingDiagnosticListener(errors)); } finally { jcMaker.pos = oldPos; } } /**Parses given static block. - * + * * @param block block code * @param sourcePositions return value - new SourcePositions for the new tree * @return parsed {@link BlockTree} or null? */ public BlockTree parseStaticBlock(String block, SourcePositions[] sourcePositions) { + return parseStaticBlock(block, sourcePositions, new ArrayList>()); + } + + /**Parses given static block. + * + * @param block block code + * @param sourcePositions return value - new SourcePositions for the new tree + * @param errors return value - Diagnostics found while parsing the given code + * @return parsed {@link BlockTree} or null? + * @since 0.71 + */ + public BlockTree parseStaticBlock(String block, SourcePositions[] sourcePositions, List> errors) { com.sun.tools.javac.tree.TreeMaker jcMaker = com.sun.tools.javac.tree.TreeMaker.instance(info.impl.getJavacTask().getContext()); int oldPos = jcMaker.pos; try { - return (BlockTree)info.impl.getJavacTask().parseStaticBlock(block, sourcePositions); + return (BlockTree)info.impl.getJavacTask().parseStaticBlock(block, sourcePositions, new CollectingDiagnosticListener(errors)); } finally { jcMaker.pos = oldPos; } } + private static final class CollectingDiagnosticListener implements DiagnosticListener { + private final List> errors; + public CollectingDiagnosticListener(List> errors) { + this.errors = errors; + } + @Override + public void report(Diagnostic diagnostic) { + errors.add(diagnostic); + } + } + //XXX: parseAnnotationValue /**Computes {@link Scope} for the given position.