Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 7 and Java 8 improvements #3

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 117 additions & 3 deletions src/main/javacc/Java1.1.jj
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public class JavaParser implements JavaParserInterface
public static final int TRANSIENT = 0x0100;
public static final int VOLATILE = 0x0200;
public static final int STRICTFP = 0x1000;
public static final int DEFAULT = 0x2000;

/** A set of accessors that indicate whether the specified modifier
is in the set. */
Expand All @@ -228,6 +229,11 @@ public class JavaParser implements JavaParserInterface
return (modifiers & PRIVATE) != 0;
}

public boolean isDefault(int modifiers)
{
return (modifiers & DEFAULT) != 0;
}

public boolean isStatic(int modifiers)
{
return (modifiers & STATIC) != 0;
Expand Down Expand Up @@ -276,6 +282,36 @@ public class JavaParser implements JavaParserInterface
return modifiers & ~mod;
}
}

public static void main(String args[]) {
JavaParser parser;
if (args.length == 0) {
System.out.println("Java Parser Version 1.1: Reading from standard input . . .");
parser = new JavaParser(System.in);
} else if (args.length == 1) {
System.out.println("Java Parser Version 1.1: Reading from file " + args[0] + " . . .");
try {
parser = new JavaParser(new java.io.FileInputStream(args[0]));
} catch (java.io.FileNotFoundException e) {
System.out.println("Java Parser Version 1.1: File " + args[0] + " not found.");
return;
}
} else {
System.out.println("Java Parser Version 1.1: Usage is one of:");
System.out.println(" java javancss.parser.JavaParser < inputfile");
System.out.println("OR");
System.out.println(" java javancss.parser.JavaParser inputfile");
return;
}
try {
parser.CompilationUnit();
System.out.println("Java Parser Version 1.1: Java program parsed successfully.");
} catch (ParseException e) {
System.out.println(e.getMessage());
System.out.println("Java Parser Version 1.1: Encountered errors during parse.");
}
}

}

PARSER_END(JavaParser)
Expand Down Expand Up @@ -801,6 +837,8 @@ TOKEN :
| < COMMA: "," >
| < DOT: "." >
| < AT: "@" >
| < LAMBDA: "->" >
| < DOUBLECOLON: "::" >
}

/* OPERATORS */
Expand Down Expand Up @@ -2281,7 +2319,14 @@ void Expression() :
//System.out.println( "Expression start" );
}
{
LOOKAHEAD(LambdaExpression()) LambdaExpression()
|
AssignmentExpression()
}

void AssignmentExpression() :
{}
{
LOOKAHEAD( PrimaryExpression() AssignmentOperator() )
//{ System.out.println( "Expression" ); }
Assignment()
Expand Down Expand Up @@ -2463,6 +2508,8 @@ void CastExpression() :
{
LOOKAHEAD("(" PrimitiveType())
"(" Type() ")" UnaryExpression()
|
LOOKAHEAD("(" Type() ")" LambdaExpression()) "(" Type() ")" LambdaExpression()
|
"(" Type() ")" UnaryExpressionNotPlusMinus()
}
Expand All @@ -2472,6 +2519,8 @@ void PrimaryExpression() :
// { System.out.println( "Before PrimaryExpression" ); }
}
{
LOOKAHEAD(MethodReference()) MethodReference()
|
PrimaryPrefix() ( LOOKAHEAD(2) PrimarySuffix() )*
}

Expand Down Expand Up @@ -2522,7 +2571,7 @@ void PrimarySuffix() :
|
"::" Identifier()
|
Arguments()
Arguments() [ "{" MethodDeclaration() "}"]
}

void Literal() :
Expand Down Expand Up @@ -2782,11 +2831,27 @@ void SwitchStatement() :
_localCases = 0;
}
"switch" "(" Expression() ")" "{"
( SwitchLabel() ( BlockStatement() )* )*
(SwitchCase())*
"}"
{ _ncss++; log.finer( "_ncss++" );}
}

void SwitchCase() :
{}
{
SwitchLabel() SwitchBlockStatements()
}

void SwitchBlockStatements() :
{}
{
LOOKAHEAD(SwitchLabel()) {}
|
BlockStatement() SwitchBlockStatements()
|
{}
}

void SwitchLabel() :
{}
{
Expand Down Expand Up @@ -2934,7 +2999,7 @@ void TryWithResources() :
{}
{
// LocalVariableDeclaration() [ ";" ]
LocalVariableDeclaration() ( ";" LocalVariableDeclaration() )*
LocalVariableDeclaration() ( LOOKAHEAD(2) ";" LocalVariableDeclaration() )* [ ";" ]
}

void Identifier() :
Expand Down Expand Up @@ -3108,6 +3173,11 @@ int Modifiers():
}
}
|
"default" { modifiers |= ModifierSet.DEFAULT; if ( _tmpToken == null ) {
_tmpToken = getToken( 0 );
}
}
|
"final" { modifiers |= ModifierSet.FINAL; if ( _tmpToken == null ) {
_tmpToken = getToken( 0 );
}
Expand Down Expand Up @@ -3444,3 +3514,47 @@ void MemberSelector():
"." TypeArguments() <IDENTIFIER>
}

void LambdaExpression():
{}
{
LambdaParameters() "->" LambdaBody()
}

void LambdaParameters():
{}
{
<IDENTIFIER>
|
LOOKAHEAD("(" InferredFormalParameterList() ")") "(" InferredFormalParameterList() ")"
|
LOOKAHEAD( [FormalParameters()] ) [FormalParameters()]
}

void InferredFormalParameterList():
{}
{
<IDENTIFIER> ( "," <IDENTIFIER> )*
}


void LambdaBody():
{}
{
Expression()
|
Block()
}

void MethodReference():
{}
{
"super" "::" [TypeArguments()] <IDENTIFIER>
|
LOOKAHEAD( 4 ) Name() "." "super" "::" [TypeArguments()] <IDENTIFIER>
|
// ClassType() "::" [TypeArguments()] "new" and ArrayType() "::" "new" is included in below
LOOKAHEAD(ReferenceType() "::" [TypeArguments()] ( <IDENTIFIER> | "new" )) ReferenceType() "::" [TypeArguments()] ( <IDENTIFIER> | "new" )
|
// Name() "::" [TypeArguments()] <IDENTIFIER> is included in below
PrimaryPrefix() ( LOOKAHEAD(2) PrimarySuffix() )* "::" [TypeArguments()] <IDENTIFIER>
}
133 changes: 67 additions & 66 deletions src/test/java/javancss/ParseTest.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
/*
Copyright (C) 2000 Chr. Clemens Lee <clemens@kclee.com>.

This file is part of JavaNCSS
(http://www.kclee.de/clemens/java/javancss/).

JavaNCSS is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

JavaNCSS 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 General Public License
for more details.

You should have received a copy of the GNU General Public License
along with JavaNCSS; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */

package javancss;

public class ParseTest extends AbstractTestCase
{
public void testParse()
{
checkParse( 31 ); // java.net.Socket, why?
checkParse( 33 ); // java.text.Decimalformat, why?
checkParse( 34 ); // java.text.TextBoundaryData, why?

checkParse( 48 );

checkParse( 50 );

checkParse( 71 ); // class declared in method
checkParse( 133 ); // char.class
checkParse( 135 ); // annotation

checkParse( 142 ); // JAVANCSS-12
checkParse( 143 ); // JAVANCSS-9
checkParse( 144 ); // JAVANCSS-13
checkParse( 145 ); // JAVANCSS-14
checkParse( 146 ); // JAVANCSS-17
checkParse( 147 ); // anonymous subcluss
checkParse( 148 ); // JAVANCSS-49
checkParse( 149 ); // JAVANCSS-46
checkParse( 150 ); // JAVANCSS-53
checkParse( 151 ); // JAVANCSS-45
checkParse( 152 ); // JAVANCSS-57
checkParse( 153 ); // JAVANCSS-54
checkParse( 154 ); // JAVANCSS-52
checkParse( 155 ); // JAVANCSS-28
checkParse( 156 ); // hexadecimal floating-point literals
checkParse( 157 ); // Java 7 literals
checkParse( 158 ); // JAVANCSS-48
checkParse( 159 ); // default interface methods
checkParse( 160 ); // method references
}

private void checkParse( int testFile )
{
Javancss pJavancss = measureTestFile( testFile );
assertFalse( "Parsing file Test" + testFile + ".java failed!", pJavancss.getNcss() <= 0 );
}
}
/*
Copyright (C) 2000 Chr. Clemens Lee <clemens@kclee.com>.

This file is part of JavaNCSS
(http://www.kclee.de/clemens/java/javancss/).

JavaNCSS is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

JavaNCSS 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 General Public License
for more details.

You should have received a copy of the GNU General Public License
along with JavaNCSS; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */

package javancss;

public class ParseTest extends AbstractTestCase
{
public void testParse()
{
checkParse( 31 ); // java.net.Socket, why?
checkParse( 33 ); // java.text.Decimalformat, why?
checkParse( 34 ); // java.text.TextBoundaryData, why?

checkParse( 48 );

checkParse( 50 );

checkParse( 71 ); // class declared in method
checkParse( 133 ); // char.class
checkParse( 135 ); // annotation

checkParse( 142 ); // JAVANCSS-12
checkParse( 143 ); // JAVANCSS-9
checkParse( 144 ); // JAVANCSS-13
checkParse( 145 ); // JAVANCSS-14
checkParse( 146 ); // JAVANCSS-17
checkParse( 147 ); // anonymous subcluss
checkParse( 148 ); // JAVANCSS-49
checkParse( 149 ); // JAVANCSS-46
checkParse( 150 ); // JAVANCSS-53
checkParse( 151 ); // JAVANCSS-45
checkParse( 152 ); // JAVANCSS-57
checkParse( 153 ); // JAVANCSS-54
checkParse( 154 ); // JAVANCSS-52
checkParse( 155 ); // JAVANCSS-28
checkParse( 156 ); // hexadecimal floating-point literals
checkParse( 157 ); // Java 7 literals
checkParse( 158 ); // JAVANCSS-48
checkParse( 159 ); // default interface methods
checkParse( 160 ); // java 8 lambda and method reference
checkParse( 161 ); // found by cobertura
}

private void checkParse( int testFile )
{
Javancss pJavancss = measureTestFile( testFile );
assertFalse( "Parsing file Test" + testFile + ".java failed!", pJavancss.getNcss() <= 0 );
}
}
17 changes: 17 additions & 0 deletions src/test/resources/Test148.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ public void baz() {
// do nothing
}
}

public void baz2() {
String zipFileName = "";
String outputFileName = "";
java.nio.charset.Charset charset = java.nio.charset.Charset.forName("US-ASCII");
java.nio.file.Path outputFilePath = java.nio.file.Paths.get(outputFileName);

try (
java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName);
java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset);
) {
// do nothing
}
catch (java.beans.PropertyVetoException e) {
// do nothing
}
}

}

Loading