src/share/classes/com/sun/tools/javac/parser/JavacParser.java

Print this page

        

*** 129,138 **** --- 129,139 ---- 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,193 **** --- 185,198 ---- /** 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?
*** 1910,1932 **** accept(SEMI); return t; } case TRY: { S.nextToken(); 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 { log.error(pos, "try.without.catch.or.finally"); } ! return F.at(pos).Try(body, catchers.toList(), finalizer); } case SWITCH: { S.nextToken(); JCExpression selector = parExpression(); accept(LBRACE); --- 1915,1948 ---- 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, resources); } case SWITCH: { S.nextToken(); JCExpression selector = parExpression(); accept(LBRACE);
*** 2383,2392 **** --- 2399,2438 ---- 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,3221 **** --- 3262,3273 ---- 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; + } + } }