Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class InstanceOfExprTest {

@Test
void annotationsOnTheType_patternExpression() {
InstanceOfExpr expr = TestParser.parseExpression(LanguageLevel.JAVA_14, "obj instanceof @A @DA String s");
InstanceOfExpr expr = TestParser.parseExpression(LanguageLevel.JAVA_14_PREVIEW, "obj instanceof @A @DA String s");

assertThat(expr.getType().getAnnotations())
.containsExactly(
Expand All @@ -77,7 +77,7 @@ void annotationsOnTheType_referenceTypeExpression() {
@Test
void instanceOf_patternExpression() {
String x = "obj instanceof String s";
InstanceOfExpr expr = TestParser.parseExpression(LanguageLevel.JAVA_14, x);
InstanceOfExpr expr = TestParser.parseExpression(LanguageLevel.JAVA_14_PREVIEW, x);

assertEquals("obj", expr.getExpression().toString());
assertEquals("String", expr.getType().asString());
Expand All @@ -95,7 +95,7 @@ void instanceOf_patternExpression() {
@Test
void instanceOf_patternExpression_prettyPrinter() {
String x = "obj instanceof String s";
InstanceOfExpr expr = TestParser.parseExpression(LanguageLevel.JAVA_14, x);
InstanceOfExpr expr = TestParser.parseExpression(LanguageLevel.JAVA_14_PREVIEW, x);

assertEquals("obj instanceof String s", expr.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.validator.Java5Validator;
import com.github.javaparser.ast.validator.language_level_validations.Java5Validator;
import org.junit.jupiter.api.Test;

import static com.github.javaparser.ParseStart.VARIABLE_DECLARATION_EXPR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import org.junit.jupiter.api.Test;

import static com.github.javaparser.ParseStart.STATEMENT;
import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_12;
import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_12_PREVIEW;
import static com.github.javaparser.Providers.provider;
import static com.github.javaparser.utils.TestUtils.assertNoProblems;

class Java12ValidatorTest {
public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_12));
public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_12_PREVIEW));

@Test
void expressionsInLabelsNotAllowed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import org.junit.jupiter.api.Test;

import static com.github.javaparser.ParseStart.STATEMENT;
import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_13;
import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_13_PREVIEW;
import static com.github.javaparser.Providers.provider;
import static com.github.javaparser.utils.TestUtils.assertNoProblems;

class Java13ValidatorTest {
public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_13));
public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_13_PREVIEW));

@Test
void yieldAllowed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.type.PrimitiveType;
import com.github.javaparser.ast.validator.language_level_validations.Java1_0Validator;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.UnionType;
import com.github.javaparser.ast.validator.language_level_validations.Java7Validator;
import org.junit.jupiter.api.Test;

import java.util.*;
Expand Down Expand Up @@ -105,7 +106,7 @@ void multiCatchWithoutElements() {

List<Problem> problems = new ArrayList<>();
new Java7Validator().accept(unionType, new ProblemReporter(problems::add));

assertProblems(problems, "UnionType.elements can not be empty.");
}

Expand All @@ -116,7 +117,7 @@ void multiCatchWithOneElement() {

List<Problem> problems = new ArrayList<>();
new Java7Validator().accept(unionType, new ProblemReporter(problems::add));

assertProblems(problems, "Union type (multi catch) must have at least two elements.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

import java.util.Optional;

import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.utils.TestParser;
import org.junit.jupiter.api.Test;

import com.github.javaparser.ast.CompilationUnit;
Expand All @@ -51,8 +53,8 @@
import com.github.javaparser.printer.configuration.DefaultPrinterConfiguration.ConfigOption;
import com.github.javaparser.printer.configuration.PrinterConfiguration;

class PrettyPrintVisitorTest {
class PrettyPrintVisitorTest extends TestParser {

private Optional<ConfigurationOption> getOption(PrinterConfiguration config, ConfigOption cOption) {
return config.get(new DefaultConfigurationOption(cOption));
}
Expand Down Expand Up @@ -478,13 +480,16 @@ void printYield() {

@Test
void printTextBlock() {
CompilationUnit cu = parse("class X{String html = \"\"\"\n" +
CompilationUnit cu = parseCompilationUnit(
ParserConfiguration.LanguageLevel.JAVA_13_PREVIEW,
"class X{String html = \"\"\"\n" +
" <html>\n" +
" <body>\n" +
" <p>Hello, world</p>\n" +
" </body>\n" +
" </html>\n" +
" \"\"\";}");
" \"\"\";}"
);

assertEqualsStringIgnoringEol("String html = \"\"\"\n" +
" <html>\n" +
Expand All @@ -497,9 +502,12 @@ void printTextBlock() {

@Test
void printTextBlock2() {
CompilationUnit cu = parse("class X{String html = \"\"\"\n" +
CompilationUnit cu = parseCompilationUnit(
ParserConfiguration.LanguageLevel.JAVA_13_PREVIEW,
"class X{String html = \"\"\"\n" +
" <html>\n" +
" </html>\"\"\";}");
" </html>\"\"\";}"
);

assertEqualsStringIgnoringEol("String html = \"\"\"\n" +
" <html>\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,11 @@
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.validator.*;
import com.github.javaparser.ast.validator.language_level_validations.*;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
import com.github.javaparser.resolution.SymbolResolver;
import com.github.javaparser.utils.LineSeparator;
import com.github.javaparser.version.Java10PostProcessor;
import com.github.javaparser.version.Java11PostProcessor;
import com.github.javaparser.version.Java12PostProcessor;
import com.github.javaparser.version.Java13PostProcessor;
import com.github.javaparser.version.Java14PostProcessor;
import com.github.javaparser.version.Java15PostProcessor;
import com.github.javaparser.version.Java16PostProcessor;
import com.github.javaparser.ast.validator.postprocessors.Java10PostProcessor;

import java.nio.charset.Charset;
import java.util.ArrayList;
Expand Down Expand Up @@ -98,30 +93,71 @@ public enum LanguageLevel {
* Java 10
*/
JAVA_10(new Java10Validator(), new Java10PostProcessor()),
/**
* Java 10 -- including incubator/preview/second preview features.
* Note that preview features, unless otherwise specified, follow the grammar and behaviour of the latest released JEP for that feature.
*/
JAVA_10_PREVIEW(new Java10PreviewValidator(), new Java10PostProcessor()),
/**
* Java 11
*/
JAVA_11(new Java11Validator(), new Java11PostProcessor()),
JAVA_11(new Java11Validator(), null),
/**
* Java 11 -- including incubator/preview/second preview features.
* Note that preview features, unless otherwise specified, follow the grammar and behaviour of the latest released JEP for that feature.
*/
JAVA_11_PREVIEW(new Java11PreviewValidator(), null),
/**
* Java 12
*/
JAVA_12(new Java12Validator(), new Java12PostProcessor()),
JAVA_12(new Java12Validator(), null),
/**
* Java 12 -- including incubator/preview/second preview features.
* Note that preview features, unless otherwise specified, follow the grammar and behaviour of the latest released JEP for that feature.
* <ul>
* <li>Switch expressions are permitted, with a single label only and no yield.</li>
* </ul>
*/
JAVA_12_PREVIEW(new Java12PreviewValidator(), null),
/**
* Java 13
*/
JAVA_13(new Java13Validator(), new Java13PostProcessor()),
JAVA_13(new Java13Validator(), null),
/**
* Java 13 -- including incubator/preview/second preview features.
* Note that preview features, unless otherwise specified, follow the grammar and behaviour of the latest released JEP for that feature.
* <ul>
* <li>Switch expressions are permitted, with a single label only.</li>
* </ul>
*/
JAVA_13_PREVIEW(new Java13PreviewValidator(), null),
/**
* Java 14
*/
JAVA_14(new Java14Validator(), new Java14PostProcessor()),
JAVA_14(new Java14Validator(), null),
/**
* Java 14 -- including incubator/preview/second preview features.
* Note that preview features, unless otherwise specified, follow the grammar and behaviour of the latest released JEP for that feature.
*/
JAVA_14_PREVIEW(new Java14PreviewValidator(), null),
/**
* Java 15
*/
JAVA_15(new Java15Validator(), new Java15PostProcessor()),
JAVA_15(new Java15Validator(), null),
/**
* Java 15 -- including incubator/preview/second preview features.
* Note that preview features, unless otherwise specified, follow the grammar and behaviour of the latest released JEP for that feature.
*/
JAVA_15_PREVIEW(new Java15PreviewValidator(), null),
/**
* Java 16
*/
JAVA_16(new Java16Validator(), new Java16PostProcessor());
JAVA_16(new Java16Validator(), null),
/**
* Java 16 -- including incubator/preview/second preview features.
* Note that preview features, unless otherwise specified, follow the grammar and behaviour of the latest released JEP for that feature.
*/
JAVA_16_PREVIEW(new Java16PreviewValidator(), null);

/**
* Does no post processing or validation. Only for people wanting the fastest parsing.
Expand All @@ -138,12 +174,17 @@ public enum LanguageLevel {
/**
* The newest Java features supported.
*/
public static LanguageLevel BLEEDING_EDGE = JAVA_16;
public static LanguageLevel BLEEDING_EDGE = JAVA_16_PREVIEW;

final Validator validator;
final ParseResult.PostProcessor postProcessor;

private static final LanguageLevel[] yieldSupport = new LanguageLevel[]{JAVA_13, JAVA_14, JAVA_15, JAVA_16};
private static final LanguageLevel[] yieldSupport = new LanguageLevel[]{
JAVA_13, JAVA_13_PREVIEW,
JAVA_14, JAVA_14_PREVIEW,
JAVA_15, JAVA_15_PREVIEW,
JAVA_16, JAVA_16_PREVIEW
};

LanguageLevel(Validator validator, ParseResult.PostProcessor postProcessor) {
this.validator = validator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ReservedKeywordValidator extends VisitorValidator {
private final String keyword;
private final String error;

ReservedKeywordValidator(String keyword) {
public ReservedKeywordValidator(String keyword) {
this.keyword = keyword;
error = f("'%s' cannot be used as an identifier as it is a keyword.", keyword);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public interface TypedValidator<N extends Node> extends BiConsumer<N, ProblemRep
default ParseResult.PostProcessor postProcessor() {
return (result, configuration) ->
result.getResult().ifPresent(node ->
accept((N) node, new ProblemReporter(problem -> result.getProblems().add(problem))));
accept((N) node, new ProblemReporter(problem -> result.getProblems().add(problem)))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2020 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser 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.
*/

package com.github.javaparser.ast.validator.language_level_validations;

import com.github.javaparser.ast.type.VarType;
import com.github.javaparser.ast.validator.SingleNodeTypeValidator;
import com.github.javaparser.ast.validator.Validator;
import com.github.javaparser.ast.validator.language_level_validations.chunks.VarValidator;

/**
* This validator validates according to Java 10 syntax rules -- including incubator/preview/second preview features.
*
* @see <a href="https://openjdk.java.net/projects/jdk/10/">https://openjdk.java.net/projects/jdk/10/</a>
*/
public class Java10PreviewValidator extends Java10Validator {

public Java10PreviewValidator() {
super();

// Incubator
// No incubator language features added within Java 10

// Preview
// No preview language features added within Java 10

// 2nd Preview
// No 2nd preview language features added within Java 10

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
* GNU Lesser General Public License for more details.
*/

package com.github.javaparser.ast.validator;
package com.github.javaparser.ast.validator.language_level_validations;

import com.github.javaparser.ast.type.VarType;
import com.github.javaparser.ast.validator.chunks.VarValidator;
import com.github.javaparser.ast.validator.SingleNodeTypeValidator;
import com.github.javaparser.ast.validator.Validator;
import com.github.javaparser.ast.validator.language_level_validations.chunks.VarValidator;

/**
* This validator validates according to Java 10 syntax rules.
Expand All @@ -35,8 +37,16 @@ public class Java10Validator extends Java9Validator {

public Java10Validator() {
super();
add(varOnlyOnLocalVariableDefinitionAndForAndTry);
/* There is no validator that validates that "var" is not used in Java 9 and lower, since the parser will never create a VarType node,
because that is done by the Java10 postprocessor. You can add it by hand, but that is obscure enough to ignore. */

// Released Language Features

{
/*
* Java 10 released local variable type inference in for and try-with (JEP286).
* Java 11 released local variable type inference for lambda parameters also (JEP323)
*/
add(varOnlyOnLocalVariableDefinitionAndForAndTry);
}

}
}
Loading