src/share/classes/com/sun/tools/javac/parser/JavacParser.java
Print this page
@@ -129,10 +129,11 @@
this.allowAsserts = source.allowAsserts();
this.allowEnums = source.allowEnums();
this.allowForeach = source.allowForeach();
this.allowStaticImport = source.allowStaticImport();
this.allowAnnotations = source.allowAnnotations();
+ this.allowARM = source.allowAutomaticResourceManagement();
this.allowDiamond = source.allowDiamond();
this.allowMulticatch = source.allowMulticatch();
this.allowTypeAnnotations = source.allowTypeAnnotations();
this.keepDocComments = keepDocComments;
if (keepDocComments)
@@ -184,10 +185,14 @@
/** Switch: should we recognize type annotations?
*/
boolean allowTypeAnnotations;
+ /** Switch: should we recognize automatic resource management?
+ */
+ boolean allowARM;
+
/** Switch: should we keep docComments?
*/
boolean keepDocComments;
/** Switch: should we keep line table?
@@ -1840,10 +1845,11 @@
* | FOR "(" ForInitOpt ";" [Expression] ";" ForUpdateOpt ")" Statement
* | FOR "(" FormalParameter : Expression ")" Statement
* | WHILE ParExpression Statement
* | DO Statement WHILE ParExpression ";"
* | TRY Block ( Catches | [Catches] FinallyPart )
+ * | TRY "(" ResourceSpecification ")" Block [Catches] [FinallyPart]
* | SWITCH ParExpression "{" SwitchBlockStatementGroups "}"
* | SYNCHRONIZED ParExpression Block
* | RETURN [Expression] ";"
* | THROW Expression ";"
* | BREAK [Ident] ";"
@@ -1910,23 +1916,34 @@
accept(SEMI);
return t;
}
case TRY: {
S.nextToken();
+ List<JCTree> resources = List.<JCTree>nil();
+ if (S.token() == LPAREN) {
+ checkAutomaticResourceManagement();
+ S.nextToken();
+ resources = resourceDeclarators();
+ accept(RPAREN);
+ }
JCBlock body = block();
ListBuffer<JCCatch> catchers = new ListBuffer<JCCatch>();
JCBlock finalizer = null;
if (S.token() == CATCH || S.token() == FINALLY) {
while (S.token() == CATCH) catchers.append(catchClause());
if (S.token() == FINALLY) {
S.nextToken();
finalizer = block();
}
} else {
+ if (allowARM) {
+ if (resources.isEmpty())
+ log.error(pos, "try.without.catch.finally.or.resource.decls");
+ } else
log.error(pos, "try.without.catch.or.finally");
}
- return F.at(pos).Try(body, catchers.toList(), finalizer);
+ return F.at(pos).Try(resources, body, catchers.toList(), finalizer);
}
case SWITCH: {
S.nextToken();
JCExpression selector = parExpression();
accept(LBRACE);
@@ -2383,10 +2400,40 @@
if ((mods.flags & Flags.VARARGS) == 0)
type = bracketsOpt(type);
return toP(F.at(pos).VarDef(mods, name, type, null));
}
+ /** ResourceDeclarators = ResourceDeclarator { ";" ResourceDeclarator }
+ */
+ List<JCTree> resourceDeclarators() {
+ ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
+ defs.append(resourceDeclarator());
+ while (S.token() == SEMI) {
+ // All but last of multiple declarators subsume a semicolon
+ storeEnd(defs.elems.last(), S.endPos());
+ S.nextToken();
+ defs.append(resourceDeclarator());
+ }
+ return defs.toList();
+ }
+
+ /** ResourceDeclarator = LocalVariableDeclaration */
+ JCTree resourceDeclarator() {
+ int pos = S.pos();
+ if (S.token() == FINAL || S.token() == MONKEYS_AT) {
+ return variableDeclaratorRest(pos, optFinal(0), parseType(),
+ ident(), true, null);
+ } else {
+ JCExpression t = term(EXPR | TYPE);
+ if ((lastmode & TYPE) != 0 && S.token() == IDENTIFIER)
+ return variableDeclaratorRest(pos, toP(F.at(pos).Modifiers(Flags.FINAL)), t,
+ ident(), true, null);
+ else
+ return t;
+ }
+ }
+
/** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration}
*/
public JCTree.JCCompilationUnit parseCompilationUnit() {
int pos = S.pos();
JCExpression pid = null;
@@ -3216,6 +3263,12 @@
if (!allowMulticatch) {
log.error(S.pos(), "multicatch.not.supported.in.source", source.name);
allowMulticatch = true;
}
}
+ void checkAutomaticResourceManagement() {
+ if (!allowARM) {
+ log.error(S.pos(), "automatic.resource.management.not.supported.in.source", source.name);
+ allowARM = true;
+ }
+ }
}