[java] Add versions 15 (new default) and 15-preview, remove 13-preview

This commit is contained in:
Andreas Dangel
2020-08-13 19:15:02 +02:00
parent 575d1262a9
commit 02a78f5bea
11 changed files with 28 additions and 257 deletions

View File

@ -185,7 +185,7 @@ Example:
* [apex](pmd_rules_apex.html) (Salesforce Apex)
* [java](pmd_rules_java.html)
* Supported Versions: 1.3, 1.4, 1.5, 5, 1.6, 6, 1.7, 7, 1.8, 8, 9, 1.9, 10, 1.10, 11, 12,
13, 13-preview, 14 (default), 14-preview
13, 14, 14-preview, 15 (default), 15-preview
* [ecmascript](pmd_rules_ecmascript.html) (JavaScript)
* [jsp](pmd_rules_jsp.html)
* [modelica](pmd_rules_modelica.html)

View File

@ -1,4 +1,7 @@
/**
* Remove support for Java 13 preview language features
* Andreas Dangel 08/2020
*====================================================================
* Add support for record types introduced as a preview language
* feature with Java 14. See JEP 359.
* Andreas Dangel 02/2020
@ -418,8 +421,8 @@ public class JavaParser {
}
}
private void checkForMultipleCaseLabels() {
if (!isJava13PreviewOr14()) {
throwParseException("Multiple case labels in switch statements are only supported with Java 13 Preview, or Java >= 14");
if (jdkVersion < 14) {
throwParseException("Multiple case labels in switch statements are only supported with Java >= 14");
}
}
/**
@ -432,30 +435,26 @@ public class JavaParser {
*/
private boolean inSwitchLabel = false;
private boolean isJava13PreviewOr14() {
return jdkVersion >= 14 || jdkVersion >= 13 && preview;
}
private void checkForSwitchRules() {
if (!isJava13PreviewOr14()) {
throwParseException("Switch rules in switch statements are only supported with Java 13 Preview, or Java >= 14");
if (jdkVersion < 14) {
throwParseException("Switch rules in switch statements are only supported with Java >= 14");
}
}
private void checkForSwitchExpression() {
if (!isJava13PreviewOr14()) {
throwParseException("Switch expressions are only supported with Java 13 Preview, or Java >= 14");
if (jdkVersion < 14) {
throwParseException("Switch expressions are only supported with Java >= 14");
}
}
private void checkForYieldStatement() {
if (!isJava13PreviewOr14()) {
throwParseException("Yield statements are only supported with Java 13 Preview or Java >= 14");
if (jdkVersion < 14) {
throwParseException("Yield statements are only supported with Java >= 14");
}
}
private void checkForTextBlockLiteral() {
if (jdkVersion != 13 && jdkVersion != 14 || !preview) {
throwParseException("Text block literals are only supported with Java 13 Preview or Java 14 Preview");
if (jdkVersion != 14 || !preview) {
throwParseException("Text block literals are only supported with Java 14 Preview");
}
}
@ -509,7 +508,7 @@ public class JavaParser {
private boolean inSwitchExprBlock = false;
private boolean isYieldStart() {
return inSwitchExprBlock && isJava13PreviewOr14()
return inSwitchExprBlock && jdkVersion >= 14
&& isKeyword("yield")
&& mayStartExprAfterYield(2);
}

View File

@ -27,9 +27,10 @@ public class JavaLanguageModule extends BaseLanguageModule {
addVersion("11", new JavaLanguageHandler(11));
addVersion("12", new JavaLanguageHandler(12));
addVersion("13", new JavaLanguageHandler(13));
addVersion("13-preview", new JavaLanguageHandler(13, true));
addDefaultVersion("14", new JavaLanguageHandler(14)); // 14 is the default
addVersion("14", new JavaLanguageHandler(14));
addVersion("14-preview", new JavaLanguageHandler(14, true));
addDefaultVersion("15", new JavaLanguageHandler(15)); // 15 is the default
addVersion("15-preview", new JavaLanguageHandler(15, true));
}
}

View File

@ -26,8 +26,8 @@ public class LanguageVersionDiscovererTest {
File javaFile = new File("/path/to/MyClass.java");
LanguageVersion languageVersion = discoverer.getDefaultLanguageVersionForFile(javaFile);
assertEquals("LanguageVersion must be Java 14 !",
LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("14"), languageVersion);
assertEquals("LanguageVersion must be Java 15 !",
LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("15"), languageVersion);
}
/**
@ -48,7 +48,7 @@ public class LanguageVersionDiscovererTest {
public void testLanguageVersionDiscoverer() {
PMDConfiguration configuration = new PMDConfiguration();
LanguageVersionDiscoverer languageVersionDiscoverer = configuration.getLanguageVersionDiscoverer();
assertEquals("Default Java version", LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("14"),
assertEquals("Default Java version", LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("15"),
languageVersionDiscoverer
.getDefaultLanguageVersion(LanguageRegistry.getLanguage(JavaLanguageModule.NAME)));
configuration

View File

@ -52,6 +52,10 @@ public class LanguageVersionTest extends AbstractLanguageVersionTest {
LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("14"), },
{ JavaLanguageModule.NAME, JavaLanguageModule.TERSE_NAME, "14-preview",
LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("14-preview"), },
{ JavaLanguageModule.NAME, JavaLanguageModule.TERSE_NAME, "15",
LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("15"), },
{ JavaLanguageModule.NAME, JavaLanguageModule.TERSE_NAME, "15-preview",
LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("15-preview"), },
// this one won't be found: case sensitive!
{ "JAVA", "JAVA", "1.7", null, },

View File

@ -1,91 +0,0 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.ast;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import net.sourceforge.pmd.lang.ast.ParseException;
import net.sourceforge.pmd.lang.java.JavaParsingHelper;
public class Java13Test {
private final JavaParsingHelper java12 =
JavaParsingHelper.WITH_PROCESSING.withDefaultVersion("12")
.withResourceContext(Java13Test.class, "jdkversiontests/java13/");
private final JavaParsingHelper java13p = java12.withDefaultVersion("13-preview");
@Test
public void testSwitchExpressions() {
ASTCompilationUnit compilationUnit = java13p.parseResource("SwitchExpressions.java");
ASTSwitchExpression switchExpression = compilationUnit.getFirstDescendantOfType(ASTSwitchExpression.class);
Assert.assertEquals(4, switchExpression.getNumChildren());
Assert.assertTrue(switchExpression.getChild(0) instanceof ASTExpression);
Assert.assertEquals(3, switchExpression.findChildrenOfType(ASTSwitchLabeledRule.class).size());
Assert.assertEquals(1, switchExpression.findChildrenOfType(ASTSwitchLabeledBlock.class).size());
Assert.assertEquals(1, switchExpression.findDescendantsOfType(ASTYieldStatement.class).size());
ASTYieldStatement yieldStatement = switchExpression.getFirstDescendantOfType(ASTYieldStatement.class);
Assert.assertEquals(Integer.TYPE, yieldStatement.getType());
}
@Test
public void testSwitchExpressionsYield() {
ASTCompilationUnit compilationUnit = java13p.parseResource("SwitchExpressionsYield.java");
ASTSwitchExpression switchExpression = compilationUnit.getFirstDescendantOfType(ASTSwitchExpression.class);
Assert.assertEquals(11, switchExpression.getNumChildren());
Assert.assertTrue(switchExpression.getChild(0) instanceof ASTExpression);
Assert.assertEquals(5, switchExpression.findChildrenOfType(ASTSwitchLabel.class).size());
ASTYieldStatement yieldStatement = switchExpression.getFirstDescendantOfType(ASTYieldStatement.class);
Assert.assertEquals("SwitchExpressionsBreak.SIX", yieldStatement.getImage());
Assert.assertTrue(yieldStatement.getChild(0) instanceof ASTExpression);
ASTLocalVariableDeclaration localVar = compilationUnit.findDescendantsOfType(ASTLocalVariableDeclaration.class)
.get(1);
ASTVariableDeclarator localVarDecl = localVar.getFirstChildOfType(ASTVariableDeclarator.class);
Assert.assertEquals(Integer.TYPE, localVarDecl.getType());
Assert.assertEquals(Integer.TYPE, switchExpression.getType());
}
@Test(expected = ParseException.class)
public void testSwitchExpressionsBeforeJava13() {
java12.parseResource("SwitchExpressions.java");
}
@Test
public void testTextBlocks() {
ASTCompilationUnit compilationUnit = java13p.parseResource("TextBlocks.java");
List<ASTLiteral> literals = compilationUnit.findDescendantsOfType(ASTLiteral.class);
Assert.assertEquals(10, literals.size());
for (int i = 0; i < 8; i++) {
ASTLiteral literal = literals.get(i);
Assert.assertTrue(literal.isTextBlock());
}
Assert.assertEquals("\"\"\"\n"
+ " <html>\n"
+ " <body>\n"
+ " <p>Hello, world</p>\n"
+ " </body>\n"
+ " </html>\n"
+ " \"\"\"",
literals.get(0).getImage());
Assert.assertFalse(literals.get(8).isTextBlock());
Assert.assertTrue(literals.get(9).isTextBlock());
}
@Test(expected = ParseException.class)
public void testTextBlocksBeforeJava13() {
java12.parseResource("TextBlocks.java");
}
}

View File

@ -26,15 +26,12 @@ public class Java14Test {
private final JavaParsingHelper java14p = java14.withDefaultVersion("14-preview");
private final JavaParsingHelper java13 = java14.withDefaultVersion("13");
private final JavaParsingHelper java13p = java14.withDefaultVersion("13-preview");
/**
* Tests switch expressions with yield.
* The switch expressions have no changed between java 13-preview and 14, so behave exactly the same.
*/
@Test
public void switchExpressions() {
parseAndCheckSwitchExpression(java13p);
parseAndCheckSwitchExpression(java14);
parseAndCheckSwitchExpression(java14p);
}
@ -84,11 +81,6 @@ public class Java14Test {
@Test
public void checkYieldConditionalBehaviour() {
checkYieldStatements(java13p);
}
@Test
public void checkYieldConditionalBehaviourJ14() {
checkYieldStatements(java14);
}
@ -138,7 +130,6 @@ public class Java14Test {
@Test
public void multipleCaseLabels() {
multipleCaseLabels(java13p);
multipleCaseLabels(java14);
multipleCaseLabels(java14p);
}
@ -154,7 +145,6 @@ public class Java14Test {
@Test
public void switchRules() {
switchRules(java13p);
switchRules(java14);
switchRules(java14p);
}
@ -182,7 +172,6 @@ public class Java14Test {
@Test
public void simpleSwitchExpressions() {
simpleSwitchExpressions(java13p);
simpleSwitchExpressions(java14);
simpleSwitchExpressions(java14p);
}

View File

@ -19,8 +19,9 @@ import net.sourceforge.pmd.lang.java.JavaParsingHelper
enum class JavaVersion : Comparable<JavaVersion> {
J1_3, J1_4, J1_5, J1_6, J1_7, J1_8, J9, J10, J11,
J12,
J13, J13__PREVIEW,
J14, J14__PREVIEW;
J13,
J14, J14__PREVIEW,
J15, J15__PREVIEW;
/** Name suitable for use with e.g. [JavaParsingHelper.parse] */
val pmdName: String = name.removePrefix("J").replaceFirst("__", "-").replace('_', '.').toLowerCase()

View File

@ -1,41 +0,0 @@
/**
*
*
* @see <a href="http://openjdk.java.net/jeps/354">JEP 354: Switch Expressions (Preview)</a>
*/
public class SwitchExpressions {
private enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
public static void main(String[] args) {
Day day = Day.FRIDAY;
int j = switch (day) {
case MONDAY -> 0;
case TUESDAY -> 1;
default -> {
int k = day.toString().length();
int result = f(k);
yield result;
}
};
System.out.printf("j = %d%n", j);
String s = "Bar";
int result = switch (s) {
case "Foo":
yield 1;
case "Bar":
yield 2;
default:
System.out.println("Neither Foo nor Bar, hmmm...");
yield 0;
};
System.out.printf("result = %d%n", result);
}
private static int f(int k) {
return k+1;
}
}

View File

@ -1,36 +0,0 @@
/**
*
* @see <a href="https://openjdk.java.net/jeps/325">JEP 325: Switch Expressions (Preview)</a>
*/
public class SwitchExpressionsYield {
private static final int MONDAY = 1;
private static final int TUESDAY = 2;
private static final int WEDNESDAY = 3;
private static final int THURSDAY = 4;
private static final int FRIDAY = 5;
private static final int SATURDAY = 6;
private static final int SUNDAY = 7;
private static final int SIX = 6;
public static void main(String[] args) {
int day = FRIDAY;
var numLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY: yield SwitchExpressionsBreak.SIX;
case TUESDAY : yield 7;
case THURSDAY, SATURDAY : yield 8;
case WEDNESDAY : yield 9;
default : {
int k = day * 2;
int result = f(k);
yield result;
}
};
System.out.printf("NumLetters: %d%n", numLetters);
}
private static int f(int k) {
return k*3;
}
}

View File

@ -1,55 +0,0 @@
/**
* @see <a href="http://openjdk.java.net/jeps/355">JEP 355: Text Blocks (Preview)</a>
*/
public class TextBlocks {
public static void main(String[] args) {
String html = """
<html>
<body>
<p>Hello, world</p>
</body>
</html>
""";
System.out.println(html);
String season = """
winter"""; // the six characters w i n t e r
String period = """
winter
"""; // the seven characters w i n t e r LF
String greeting =
"""
Hi, "Bob"
"""; // the ten characters H i , SP " B o b " LF
String salutation =
"""
Hi,
"Bob"
"""; // the eleven characters H i , LF SP " B o b " LF
String empty = """
"""; // the empty string (zero length)
String quote = """
"
"""; // the two characters " LF
String backslash = """
\\
"""; // the two characters \ LF
String normalStringLiteral = "test";
String code =
"""
String text = \"""
A text block inside a text block
\""";
""";
}
}