Keeping ability to specify resource as a variable inside try-catch patterns (porting from NetBeans proper)
authorJan Lahoda <jlahoda@netbeans.org>
Wed, 09 Feb 2011 18:10:03 +0100
changeset 542af54c39d11ba
parent 541 bcaf48a2e220
child 545 224ff3deee5d
Keeping ability to specify resource as a variable inside try-catch patterns (porting from NetBeans proper)
api/src/org/netbeans/modules/jackpot30/impl/Utilities.java
api/test/unit/src/org/netbeans/modules/jackpot30/impl/UtilitiesTest.java
     1.1 --- a/api/src/org/netbeans/modules/jackpot30/impl/Utilities.java	Tue Feb 08 23:16:16 2011 +0100
     1.2 +++ b/api/src/org/netbeans/modules/jackpot30/impl/Utilities.java	Wed Feb 09 18:10:03 2011 +0100
     1.3 @@ -1263,6 +1263,25 @@
     1.4              return super.switchBlockStatementGroup();
     1.5          }
     1.6  
     1.7 +
     1.8 +        @Override
     1.9 +        protected JCTree resource() {
    1.10 +            if (S.token() == Token.IDENTIFIER && S.stringVal().startsWith("$")) {
    1.11 +                //XXX: should inspect the next token, not next character:
    1.12 +                char[] maybeSemicolon = S.getRawCharacters(S.endPos(), S.endPos() + 1);
    1.13 +
    1.14 +                if (maybeSemicolon[0] == ';' || maybeSemicolon[0] == ')') {
    1.15 +                    int pos = S.pos();
    1.16 +                    com.sun.tools.javac.util.Name name = S.name();
    1.17 +
    1.18 +                    S.nextToken();
    1.19 +
    1.20 +                    return F.at(pos).Ident(name);
    1.21 +                }
    1.22 +            }
    1.23 +            return super.resource();
    1.24 +        }
    1.25 +
    1.26      }
    1.27  
    1.28      private static final class PushbackLexer implements Lexer {
     2.1 --- a/api/test/unit/src/org/netbeans/modules/jackpot30/impl/UtilitiesTest.java	Tue Feb 08 23:16:16 2011 +0100
     2.2 +++ b/api/test/unit/src/org/netbeans/modules/jackpot30/impl/UtilitiesTest.java	Wed Feb 09 18:10:03 2011 +0100
     2.3 @@ -222,6 +222,42 @@
     2.4          String golden = "$1.isDirectory()";
     2.5          assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " "));
     2.6      }
     2.7 +    
     2.8 +    public void testARMResourceVariable1() throws Exception {
     2.9 +        prepareTest("test/Test.java", "package test; public class Test{}");
    2.10 +
    2.11 +        Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap());
    2.12 +        Tree result = Utilities.parseAndAttribute(info, "try ($t$) { $stmts$; } catch $catches$", s);
    2.13 +
    2.14 +        assertTrue(result.getKind().name(), result.getKind() == Kind.TRY);
    2.15 +
    2.16 +        String golden = "try ($t$) { $stmts$; }$catches$";
    2.17 +        assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " "));
    2.18 +    }
    2.19 +    
    2.20 +    public void testARMResourceVariable2() throws Exception {
    2.21 +        prepareTest("test/Test.java", "package test; public class Test{}");
    2.22 +
    2.23 +        Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap());
    2.24 +        Tree result = Utilities.parseAndAttribute(info, "try ($t$; $type $name = $init) { $stmts$; } catch $catches$", s);
    2.25 +
    2.26 +        assertTrue(result.getKind().name(), result.getKind() == Kind.TRY);
    2.27 +
    2.28 +        String golden = "try ($t$ final $type $name = $init;) { $stmts$; }$catches$";
    2.29 +        assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " "));
    2.30 +    }
    2.31 +    
    2.32 +    public void testARMResourceNotVariable() throws Exception {
    2.33 +        prepareTest("test/Test.java", "package test; public class Test{}");
    2.34 +
    2.35 +        Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap());
    2.36 +        Tree result = Utilities.parseAndAttribute(info, "try ($t $n = $init$) { $stmts$; } catch $catches$", s);
    2.37 +
    2.38 +        assertTrue(result.getKind().name(), result.getKind() == Kind.TRY);
    2.39 +
    2.40 +        String golden = "try (final $t $n = $init$;) { $stmts$; }$catches$";
    2.41 +        assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " "));
    2.42 +    }
    2.43  
    2.44      public void testParseAndAttributeType() throws Exception {
    2.45          prepareTest("test/Test.java", "package test; public class Test{}");