Skip to content

Commit

Permalink
[java] Add Java8TreeDumpTest for UnnamedVariable
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed May 23, 2024
1 parent 4cfe90c commit a54dc1e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@Suite
@SelectClasses({
ParserCornersTest.class,
Java8TreeDumpTest.class,
Java9TreeDumpTest.class,
Java14TreeDumpTest.class,
Java15TreeDumpTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/

package net.sourceforge.pmd.lang.java.ast;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.junit.jupiter.api.Test;

import net.sourceforge.pmd.lang.java.BaseJavaTreeDumpTest;
import net.sourceforge.pmd.lang.java.JavaParsingHelper;
import net.sourceforge.pmd.lang.test.ast.BaseParsingHelper;

class Java8TreeDumpTest extends BaseJavaTreeDumpTest {

private final JavaParsingHelper java8 = JavaParsingHelper.DEFAULT
.withDefaultVersion("8")
.withResourceContext(Java8TreeDumpTest.class, "jdkversiontests/java8");

@Test
void unnamedVariablesAreAllowedWithJava8() {
doTest("UnnamedVariable");
}

@Override
public @NonNull BaseParsingHelper<?, ?> getParser() {
return java8;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/

/**
* See https://github.com/pmd/pmd/pull/5004: With Java 22, "_" are unnamed variables.
* In Java 9-21 "_" is a restricted keyword and cannot be used as a variable name.
* Before Java 9, "_" is still a valid name, but not a unnmaed variable.
*/
class UnnamedVariable {
void method() {
int _ = 1;
}

void method2(int _) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
+- CompilationUnit[@PackageName = ""]
+- ClassDeclaration[@Abstract = false, @Annotation = false, @Anonymous = false, @BinaryName = "UnnamedVariable", @CanonicalName = "UnnamedVariable", @EffectiveVisibility = Visibility.V_PACKAGE, @Enum = false, @Final = false, @Interface = false, @Local = false, @Nested = false, @PackageName = "", @Record = false, @RegularClass = true, @RegularInterface = false, @SimpleName = "UnnamedVariable", @Static = false, @TopLevel = true, @Visibility = Visibility.V_PACKAGE]
+- ModifierList[@EffectiveModifiers = (), @ExplicitModifiers = ()]
+- ClassBody[@Empty = false, @Size = 2]
+- MethodDeclaration[@Abstract = false, @Arity = 0, @EffectiveVisibility = Visibility.V_PACKAGE, @Final = false, @Name = "method", @Overridden = false, @Static = false, @Varargs = false, @Visibility = Visibility.V_PACKAGE, @Void = true]
| +- ModifierList[@EffectiveModifiers = (), @ExplicitModifiers = ()]
| +- VoidType[]
| +- FormalParameters[@Empty = true, @Size = 0]
| +- Block[@Empty = false, @Size = 1, @containsComment = false]
| +- LocalVariableDeclaration[@EffectiveVisibility = Visibility.V_LOCAL, @Final = false, @TypeInferred = false, @Visibility = Visibility.V_LOCAL]
| +- ModifierList[@EffectiveModifiers = (), @ExplicitModifiers = ()]
| +- PrimitiveType[@Kind = PrimitiveTypeKind.INT]
| +- VariableDeclarator[@Initializer = true, @Name = "_"]
| +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = false, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = false, @LambdaParameter = false, @LocalVariable = true, @Name = "_", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = false, @Static = false, @TypeInferred = false, @Unnamed = false, @Visibility = Visibility.V_LOCAL]
| +- NumericLiteral[@Base = 10, @CompileTimeConstant = true, @DoubleLiteral = false, @FloatLiteral = false, @Image = "1", @IntLiteral = true, @Integral = true, @LiteralText = "1", @LongLiteral = false, @ParenthesisDepth = 0, @Parenthesized = false, @ValueAsDouble = 1.0, @ValueAsFloat = 1.0, @ValueAsInt = 1, @ValueAsLong = 1]
+- MethodDeclaration[@Abstract = false, @Arity = 1, @EffectiveVisibility = Visibility.V_PACKAGE, @Final = false, @Name = "method2", @Overridden = false, @Static = false, @Varargs = false, @Visibility = Visibility.V_PACKAGE, @Void = true]
+- ModifierList[@EffectiveModifiers = (), @ExplicitModifiers = ()]
+- VoidType[]
+- FormalParameters[@Empty = false, @Size = 1]
| +- FormalParameter[@EffectiveVisibility = Visibility.V_LOCAL, @Final = false, @Varargs = false, @Visibility = Visibility.V_LOCAL]
| +- ModifierList[@EffectiveModifiers = (), @ExplicitModifiers = ()]
| +- PrimitiveType[@Kind = PrimitiveTypeKind.INT]
| +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = false, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = true, @LambdaParameter = false, @LocalVariable = false, @Name = "_", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = false, @Static = false, @TypeInferred = false, @Unnamed = false, @Visibility = Visibility.V_LOCAL]
+- Block[@Empty = true, @Size = 0, @containsComment = false]

0 comments on commit a54dc1e

Please sign in to comment.