Add support for Java 7 - patch #3403265

This commit is contained in:
Oliver Burn 2011-09-06 21:26:56 +10:00
parent b519429598
commit 33e827ee07
11 changed files with 439 additions and 33 deletions

View File

@ -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

View File

@ -0,0 +1,10 @@
package com.puppycrawl.tools.checkstyle.grammars;
import java.util.*;
public class InputJava7Diamond {
HashMap<String, Integer> map = new HashMap<String, Integer>();
HashMap<String, Integer> map2 = new HashMap<>();
HashMap<String, HashMap<Integer, Integer>> map3 = new HashMap<>();
ArrayList<String> list = new ArrayList<>();
}

View File

@ -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);
}
}
}

View File

@ -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
}

View File

@ -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;
}
}
}

View File

@ -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()) { }
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}