From 33e827ee0778639ac708acfdd5aac2db00577dc5 Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Tue, 6 Sep 2011 21:26:56 +1000 Subject: [PATCH] Add support for Java 7 - patch #3403265 --- .../tools/checkstyle/grammars/java.g | 86 ++++++++++++------- .../grammars/InputJava7Diamond.java | 10 +++ .../grammars/InputJava7MultiCatch.java | 30 +++++++ .../grammars/InputJava7NumericalLiterals.java | 84 ++++++++++++++++++ .../grammars/InputJava7StringSwitch.java | 20 +++++ .../grammars/InputJava7TryWithResources.java | 29 +++++++ .../checkstyle/grammars/Java7DiamondTest.java | 43 ++++++++++ .../grammars/Java7MultiCatchTest.java | 43 ++++++++++ .../grammars/Java7NumericalLiteralsTest.java | 42 +++++++++ .../grammars/Java7StringSwitchTest.java | 42 +++++++++ .../grammars/Java7TryWithResourcesTest.java | 43 ++++++++++ 11 files changed, 439 insertions(+), 33 deletions(-) create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7Diamond.java create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7MultiCatch.java create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7NumericalLiterals.java create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7StringSwitch.java create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7TryWithResources.java create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7DiamondTest.java create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7MultiCatchTest.java create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7NumericalLiteralsTest.java create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7StringSwitchTest.java create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7TryWithResourcesTest.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/grammars/java.g b/src/checkstyle/com/puppycrawl/tools/checkstyle/grammars/java.g index 9852e3def..a3fa5be66 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/grammars/java.g +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/grammars/java.g @@ -278,7 +278,8 @@ typeArguments[boolean addImagNode] : {currentLtLevel = ltCounter;} lt:LT {#lt.setType(GENERIC_START); ;ltCounter++;} - typeArgument[addImagNode] + // (Dinesh Bolkensteyn) Added support for Java 7 diamond notation (disabled ambiguous warnings since generated code seems to work) + (options{generateAmbigWarnings=false;}:typeArgument[addImagNode] (options{greedy=true;}: // match as many as possible // If there are any '>' to reconcile // (i.e. we've recently encountered a DT, SR or BSR @@ -286,7 +287,7 @@ typeArguments[boolean addImagNode] // possibly an enclosing type parameter) // then further type arguments are not possible {gtToReconcile == 0}? COMMA typeArgument[addImagNode] - )* + )*)? ( // turn warning off since Antlr generates the right code, // plus we have our semantic predicate below @@ -934,12 +935,16 @@ parameterModifier ; // A formal parameter. +// (Dinesh Bolkensteyn) Extended to support Java7's "multi-catch", several types seperated by '|' parameterDeclaration! - : pm:parameterModifier t:typeSpec[false] id:IDENT + : pm:parameterModifier t:typeSpec[false] (mct:multiCatchTypes { #t.setNextSibling(#mct); })? id:IDENT pd:declaratorBrackets[#t] {#parameterDeclaration = #(#[PARAMETER_DEF,"PARAMETER_DEF"], pm, #([TYPE,"TYPE"],pd), id);} ; + +multiCatchTypes + : (BOR! typeSpec[false])+; // Compound statement. This is used in many contexts: // Inside a class definition prefixed with "static": @@ -1116,12 +1121,21 @@ forIter ; // an exception handler try/catch block +// (Dinesh Bolkensteyn): Added support for Java 7 try-with-resources tryBlock - : "try"^ compoundStatement + : "try"^ + + // try-with-resources + (tryWithResources)? + + compoundStatement (handler)* ( finallyHandler )? ; +tryWithResources + : LPAREN^ modifiers typeSpec[true] IDENT ASSIGN expression RPAREN + ; // an exception handler handler @@ -1696,6 +1710,12 @@ protected HEX_DIGIT : ('0'..'9'|'A'..'F'|'a'..'f') ; + +// binary digit (again, note it's protected!) +protected +BINARY_DIGIT + : ('0'|'1') + ; // a dummy rule to force vocabulary to be all characters (except special @@ -1788,46 +1808,46 @@ NUM_INT protected INT_LITERAL : ( '0' - ( ('x'|'X') (HEX_DIGIT)+ - | ('0'..'9')* + ( ('x'|'X')(HEX_DIGIT)((HEX_DIGIT|'_')*(HEX_DIGIT))? // Hexa + | ('b'|'B')(BINARY_DIGIT)((BINARY_DIGIT|'_')*(BINARY_DIGIT))? // Binary + | ((('0'..'7')|'_')*('0'..'7'))? // If empty 0, otherwise octal (which may start with an underscore) ) - // non-zero decimal - | ('1'..'9') ('0'..'9')* + | ('1'..'9') (('0'..'9'|'_')*('0'..'9'))? // Non-zero decimal ) ; protected LONG_LITERAL - : ( '0' - ( ('x'|'X') (HEX_DIGIT)+ - | ('0'..'9')* + : ( '0' + ( ('x'|'X')(HEX_DIGIT)((HEX_DIGIT|'_')*(HEX_DIGIT))? // Hexa + | ('b'|'B')(BINARY_DIGIT)((BINARY_DIGIT|'_')*(BINARY_DIGIT))? // Binary + | ((('0'..'7')|'_')*('0'..'7'))? // If empty 0, otherwise octal (which may start with an underscore) ) - // non-zero decimal - | ('1'..'9') ('0'..'9')* - ) - // long signifier - ('l'|'L') + | ('1'..'9') (('0'..'9'|'_')*('0'..'9'))? // Non-zero decimal + ) + // long signifier + ('l'|'L') ; protected FLOAT_LITERAL : ( - (('0'..'9')* '.')=> - ( ('0'..'9')+ '.' ('0'..'9')* - | '.' ('0'..'9')+ + ((('0'..'9')(('0'..'9'|'_')*('0'..'9'))?)? '.')=> + ( (('0'..'9')(('0'..'9'|'_')*('0'..'9'))?) '.' (('0'..'9')(('0'..'9'|'_')*('0'..'9'))?)? + | '.' (('0'..'9')(('0'..'9'|'_')*('0'..'9'))?) ) (EXPONENT)? ('f'|'F')? | - ('0'..'9')+ ((EXPONENT ('f'|'F')?) | ('f'|'F')) + (('0'..'9')(('0'..'9'|'_')*('0'..'9'))?) ((EXPONENT ('f'|'F')?) | ('f'|'F')) ) ; protected DOUBLE_LITERAL : ( - (('0'..'9')* '.')=> - ( ('0'..'9')+ '.' ('0'..'9')* - | '.' ('0'..'9')+ + ((('0'..'9')(('0'..'9'|'_')*('0'..'9'))?)? '.')=> + ( (('0'..'9')(('0'..'9'|'_')*('0'..'9'))?) '.' (('0'..'9')(('0'..'9'|'_')*('0'..'9'))?)? + | '.' (('0'..'9')(('0'..'9'|'_')*('0'..'9'))?) ) | - ('0'..'9')+ + (('0'..'9')(('0'..'9'|'_')*('0'..'9'))?) ) (EXPONENT)? ('d'|'D') ; @@ -1835,12 +1855,12 @@ protected DOUBLE_LITERAL protected HEX_FLOAT_LITERAL : '0' ('x'|'X') ( - ((HEX_DIGIT)* '.')=> - ( (HEX_DIGIT)+ '.' (HEX_DIGIT)* - | '.' (HEX_DIGIT)+ + (((HEX_DIGIT)((HEX_DIGIT|'_')*(HEX_DIGIT))?)? '.')=> + ( ((HEX_DIGIT)((HEX_DIGIT|'_')*(HEX_DIGIT))?) '.' ((HEX_DIGIT)((HEX_DIGIT|'_')*(HEX_DIGIT))?)? + | '.' ((HEX_DIGIT)((HEX_DIGIT|'_')*(HEX_DIGIT))?) ) | - (HEX_DIGIT)+ + ((HEX_DIGIT)((HEX_DIGIT|'_')*(HEX_DIGIT))?) ) BINARY_EXPONENT ('f'|'F')? ; @@ -1848,12 +1868,12 @@ protected HEX_FLOAT_LITERAL protected HEX_DOUBLE_LITERAL : '0' ('x'|'X') ( - ((HEX_DIGIT)* '.')=> - ( (HEX_DIGIT)+ '.' (HEX_DIGIT)* - | '.' (HEX_DIGIT)+ + (((HEX_DIGIT)((HEX_DIGIT|'_')*(HEX_DIGIT))?)? '.')=> + ( ((HEX_DIGIT)((HEX_DIGIT|'_')*(HEX_DIGIT))?) '.' ((HEX_DIGIT)((HEX_DIGIT|'_')*(HEX_DIGIT))?)? + | '.' ((HEX_DIGIT)((HEX_DIGIT|'_')*(HEX_DIGIT))?) ) | - (HEX_DIGIT)+ + ((HEX_DIGIT)((HEX_DIGIT|'_')*(HEX_DIGIT))?) ) BINARY_EXPONENT ('d'|'D') ; @@ -1874,7 +1894,7 @@ EXPONENT protected SIGNED_INTEGER - : ('+'|'-')? ('0'..'9')+ + : ('+'|'-')? (('0'..'9')(('0'..'9'|'_')*('0'..'9'))?) ; protected diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7Diamond.java b/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7Diamond.java new file mode 100644 index 000000000..8b220bc4f --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7Diamond.java @@ -0,0 +1,10 @@ +package com.puppycrawl.tools.checkstyle.grammars; + +import java.util.*; + +public class InputJava7Diamond { + HashMap map = new HashMap(); + HashMap map2 = new HashMap<>(); + HashMap> map3 = new HashMap<>(); + ArrayList list = new ArrayList<>(); +} diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7MultiCatch.java b/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7MultiCatch.java new file mode 100644 index 000000000..3162c8886 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7MultiCatch.java @@ -0,0 +1,30 @@ +package com.puppycrawl.tools.checkstyle.grammars; + +import java.io.*; + +/** + * Input for Java 7 multi-catch. + */ +public class InputJava7MultiCatch +{ + public static class CustomException extends Exception { } + public static class AnotherCustomException extends Exception { } + + public static void logException(Exception e) { } + + public static void main(String[] args) { + try { + FileInputStream in = new FileInputStream("InputJava7MultiCatch.java"); + throw new CustomException(); + } catch (FileNotFoundException | CustomException e) { + logException(e); + } + + try { + FileInputStream in = new FileInputStream("InputJava7MultiCatch.java"); + throw new CustomException(); + } catch (final FileNotFoundException | CustomException | test.foo.AnotherCustomException e) { + logException(e); + } + } +} diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7NumericalLiterals.java b/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7NumericalLiterals.java new file mode 100644 index 000000000..46d96f30b --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7NumericalLiterals.java @@ -0,0 +1,84 @@ +package com.puppycrawl.tools.checkstyle.grammars; + +/** + * Input for Java 7 numerical literals. + */ +public class InputJava7NumericalLiterals +{ + int i1 = 0b00011110; + int i2 = 0B00011110; + int i3 = 0xA; + int i4 = 0x1___A_F; + int i5 = 0b1; + int i6 = 0b1___1_0; + int i7 = 0; + int i8 = 02; + int i9 = 0_123; + int i10 = 1; + int i11 = 1___3; + int i12 = 1_43_43598_7; + + long l1 = 0b00011110L; + long l2 = 0B00011110l; + long l3 = 0xAL; + long l4 = 0x1___A_FL; + long l5 = 0b1L; + long l6 = 0b1___1_0L; + long l7 = 0l; + long l8 = 02L; + long l9 = 0_123l; + long l10 = 1l; + long l11 = 1___3l; + long l12 = 1_43_43598_7L; + long l13 = 1_43_43598_7; // int promoted to long + + // the grammar considers floating point values to be of type "float" by default which is wrong, it should be "double". + + float f1 = .1f; + float f2 = 1.; // double "downgraded" to float + float f3 = 0f; + float f4 = 1e0; // double "downgraded" to float + float f5 = 1e0f; + float f6 = 12.345F; + float f7 = .5____2_1; // double "downgraded" to float + float f8 = 1__42__3.; // double "downgraded" to float + float f9 = 0__2_4__324f; + float f10 = 1_34e0; // double "downgraded" to float + float f11 = 1__1_2e0f; + float f12 = 2_1___2.3__4_5F; + float f13 = 1_34e0__4__3; // double "downgraded" to float + float f14 = 1__1_2e00__000_4f; + float f15 = 2_1___2.3__4_5e00______0_5F; + + double d1 = .1d; + double d2 = 1.D; + double d3 = 0d; + double d4 = 1e0D; + double d5 = 1e0d; + double d6 = 12.345D; + double d7 = .5____2_1d; + double d8 = 1__42__3.D; + double d9 = 0__2_4__324d; + double d10 = 1_34e0d; + double d11 = 1__1_2e0d; + double d12 = 2_1___2.3__4_5D; + double d13 = 1_34e0__4__3d; + double d14 = 1__1_2e00__000_4d; + double d15 = 2_1___2.3__4_5e00______0_5D; + double d16 = 0.12___34; // "float" promoted to double + + float hf1 = 0x.1___AFp1; // double "downgraded" to float + float hf2 = 0x.1___AFp0__0__0f; + float hf3 = 0x2__3_34.4___AFP00_00f; + + double hd1 = 0x.1___AFp1; + double hd2 = 0x.1___AFp0__0__0d; + double hd3 = 0x2__3_34.4___AFP00_00d; + + int doc1 = 1234_5678; + long doc2 = 1_2_3_4__5_6_7_8L; + int doc3 = 0b0001_0010_0100_1000; + double doc4 = 3.141_592_653_589_793d; + double doc5 = 0x1.ffff_ffff_ffff_fP1_023; // Double.MAX_VALUE + +} diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7StringSwitch.java b/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7StringSwitch.java new file mode 100644 index 000000000..e4cbf3680 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7StringSwitch.java @@ -0,0 +1,20 @@ +package com.puppycrawl.tools.checkstyle.grammars; + +/** + * Input for Java 7 String in Switch. + */ +public class InputJava7StringSwitch +{ + public static void main(String[] args) { + String mystr = "value" + "2"; + + switch (mystr) { + case "value1": + break; + case "value2": + break; + default: + break; + } + } +} diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7TryWithResources.java b/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7TryWithResources.java new file mode 100644 index 000000000..e5dee0536 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputJava7TryWithResources.java @@ -0,0 +1,29 @@ +package com.puppycrawl.tools.checkstyle.grammars; + +/** + * Input for Java 7 try-with-resources. + */ +public class InputJava7TryWithResources +{ + public static class MyResource implements AutoCloseable { + @Override + public void close() throws Exception { } + } + + public static void main(String[] args) throws Exception { + try (MyResource resource = new MyResource()) { } + + try (MyResource resource = new MyResource()) { } + finally { } + + try (MyResource resource = new MyResource()) { } + catch (Exception e) { } + + try (MyResource resource = new MyResource()) { } + catch (Exception e) { } + catch (Throwable t) { } + finally { } + + try (@SuppressWarnings("all") final MyResource resource = new MyResource()) { } + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7DiamondTest.java b/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7DiamondTest.java new file mode 100644 index 000000000..d0d32e177 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7DiamondTest.java @@ -0,0 +1,43 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2011 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.grammars; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck; + +import org.junit.Test; + +/** + * Tests Java 7 diamond can be parsed. + * @author Dinesh Bolkensteyn (SonarSource) + */ +public class Java7DiamondTest + extends BaseCheckTestSupport +{ + @Test + public void testCanParse() + throws Exception + { + final DefaultConfiguration checkConfig = + createCheckConfig(MemberNameCheck.class); + final String[] expected = {}; + verify(checkConfig, getPath("grammars/InputJava7Diamond.java"), expected); + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7MultiCatchTest.java b/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7MultiCatchTest.java new file mode 100644 index 000000000..4ee7007a7 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7MultiCatchTest.java @@ -0,0 +1,43 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2011 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.grammars; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck; + +import org.junit.Test; + +/** + * Tests Java 7 multi-catch can be parsed. + * @author Dinesh Bolkensteyn (SonarSource) + */ +public class Java7MultiCatchTest + extends BaseCheckTestSupport +{ + @Test + public void testCanParse() + throws Exception + { + final DefaultConfiguration checkConfig = + createCheckConfig(MemberNameCheck.class); + final String[] expected = {}; + verify(checkConfig, getPath("grammars/InputJava7MultiCatch.java"), expected); + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7NumericalLiteralsTest.java b/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7NumericalLiteralsTest.java new file mode 100644 index 000000000..6af3b96f9 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7NumericalLiteralsTest.java @@ -0,0 +1,42 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2011 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.grammars; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck; +import org.junit.Test; + +/** + * Tests Java 7 numerical literals can be parsed. + * @author Dinesh Bolkensteyn (SonarSource) + */ +public class Java7NumericalLiteralsTest + extends BaseCheckTestSupport +{ + @Test + public void testCanParse() + throws Exception + { + final DefaultConfiguration checkConfig = + createCheckConfig(MemberNameCheck.class); + final String[] expected = {}; + verify(checkConfig, getPath("grammars/InputJava7NumericalLiterals.java"), expected); + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7StringSwitchTest.java b/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7StringSwitchTest.java new file mode 100644 index 000000000..fb4e1f8d5 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7StringSwitchTest.java @@ -0,0 +1,42 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2011 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.grammars; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck; +import org.junit.Test; + +/** + * Tests Java 7 String in switch can be parsed. + * @author Dinesh Bolkensteyn (SonarSource) + */ +public class Java7StringSwitchTest + extends BaseCheckTestSupport +{ + @Test + public void testCanParse() + throws Exception + { + final DefaultConfiguration checkConfig = + createCheckConfig(MemberNameCheck.class); + final String[] expected = {}; + verify(checkConfig, getPath("grammars/InputJava7StringSwitch.java"), expected); + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7TryWithResourcesTest.java b/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7TryWithResourcesTest.java new file mode 100644 index 000000000..960731093 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/grammars/Java7TryWithResourcesTest.java @@ -0,0 +1,43 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2011 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.grammars; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck; + +import org.junit.Test; + +/** + * Tests Java 7 try-with-resources can be parsed. + * @author Dinesh Bolkensteyn (SonarSource) + */ +public class Java7TryWithResourcesTest + extends BaseCheckTestSupport +{ + @Test + public void testCanParse() + throws Exception + { + final DefaultConfiguration checkConfig = + createCheckConfig(MemberNameCheck.class); + final String[] expected = {}; + verify(checkConfig, getPath("grammars/InputJava7TryWithResources.java"), expected); + } +}