src/share/classes/com/sun/tools/javac/comp/Attr.java

Print this page

        

@@ -979,18 +979,45 @@
         attribStat(tree.body, env);
         result = null;
     }
 
     public void visitTry(JCTry tree) {
+        // Create a new local environment with a local scope.
+        Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
+        // Attribute resource declarations
+        for (JCTree resource : tree.resources) {
+            if (resource instanceof JCVariableDecl) {
+                JCVariableDecl var = (JCVariableDecl) resource;
+                Type resourceType = types.upperBound(attribStat(var, localEnv));
+                if (var.type.tsym.kind == Kinds.VAR) {
+                    var.sym.setData(ElementKind.RESOURCE_VARIABLE);
+                }
+                if (types.asSuper(resourceType, syms.autoCloseableType.tsym) == null) {
+                    log.error(var.pos(), "arm.not.applicable.to.type");
+                    resourceType = types.createErrorType(resourceType);
+                }
+                chk.checkType(var.vartype.pos(), resourceType, var.sym.type);
+            } else if (resource instanceof JCExpression) {
+                JCExpression expr = (JCExpression)resource;
+                Type resourceType = types.upperBound(attribExpr(expr, localEnv));
+                if (types.asSuper(resourceType, syms.autoCloseableType.tsym) == null) {
+                    log.error(expr.pos(), "arm.not.applicable.to.type");
+                    resourceType = types.createErrorType(resourceType);
+                }
+            } else {
+                // Parser error.
+                throw new AssertionError(tree);
+            }
+        }
         // Attribute body
-        attribStat(tree.body, env.dup(tree, env.info.dup()));
+        attribStat(tree.body, localEnv.dup(tree, localEnv.info.dup()));
 
         // Attribute catch clauses
         for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
             JCCatch c = l.head;
             Env<AttrContext> catchEnv =
-                env.dup(c, env.info.dup(env.info.scope.dup()));
+                localEnv.dup(c, localEnv.info.dup(localEnv.info.scope.dup()));
             Type ctype = attribStat(c.param, catchEnv);
             if (TreeInfo.isMultiCatch(c)) {
                 //check that multi-catch parameter is marked as final
                 if ((c.param.sym.flags() & FINAL) == 0) {
                     log.error(c.param.pos(), "multicatch.param.must.be.final", c.param.sym);

@@ -1006,11 +1033,13 @@
             attribStat(c.body, catchEnv);
             catchEnv.info.scope.leave();
         }
 
         // Attribute finalizer
-        if (tree.finalizer != null) attribStat(tree.finalizer, env);
+        if (tree.finalizer != null) attribStat(tree.finalizer, localEnv);
+
+        localEnv.info.scope.leave();
         result = null;
     }
 
     public void visitConditional(JCConditional tree) {
         attribExpr(tree.cond, env, syms.booleanType);