diff --git a/src/jdk.compiler/share/classes/com/sun/source/tree/EnhancedForLoopTree.java b/src/jdk.compiler/share/classes/com/sun/source/tree/EnhancedForLoopTree.java
index 05f53542846..5545c2be93e 100644
--- a/src/jdk.compiler/share/classes/com/sun/source/tree/EnhancedForLoopTree.java
+++ b/src/jdk.compiler/share/classes/com/sun/source/tree/EnhancedForLoopTree.java
@@ -25,6 +25,8 @@
package com.sun.source.tree;
+import jdk.internal.javac.PreviewFeature;
+
/**
* A tree node for an "enhanced" {@code for} loop statement.
*
@@ -47,7 +49,10 @@ public interface EnhancedForLoopTree extends StatementTree {
*
local variable declarations and
* record patterns
*
+ *
+ * @since 20
*/
+ @PreviewFeature(feature=PreviewFeature.Feature.RECORD_PATTERNS, reflective=true)
public enum DeclarationKind {
/** enum constant for local variable declarations */
VARDECL,
@@ -57,8 +62,16 @@ public enum DeclarationKind {
/**
* Returns the control variable for the loop.
- * @return the control variable
+ * @return the control variable, or {@code null} if this enhanced for uses a pattern
+ */
+ Tree getVariable();
+
+ /**
+ * Returns the control variable or pattern for the loop.
+ * @return the control variable or pattern
+ * @since 20
*/
+ @PreviewFeature(feature=PreviewFeature.Feature.RECORD_PATTERNS, reflective=true)
Tree getVariableOrRecordPattern();
/**
@@ -76,6 +89,8 @@ public enum DeclarationKind {
/**
* Returns the kind of the declaration of the enhanced for.
* @return the kind of the declaration
+ * @since 20
*/
- EnhancedForLoopTree.DeclarationKind getDeclarationKind();
+ @PreviewFeature(feature=PreviewFeature.Feature.RECORD_PATTERNS, reflective=true)
+ DeclarationKind getDeclarationKind();
}
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java
index 895d24d0b91..b8858e08010 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java
@@ -1225,6 +1225,10 @@ protected JCEnhancedForLoop(JCTree varOrRecordPattern, JCExpression expr, JCStat
@DefinedBy(Api.COMPILER_TREE)
public Kind getKind() { return Kind.ENHANCED_FOR_LOOP; }
@DefinedBy(Api.COMPILER_TREE)
+ public JCVariableDecl getVariable() {
+ return varOrRecordPattern instanceof JCVariableDecl var ? var : null;
+ }
+ @DefinedBy(Api.COMPILER_TREE)
public JCTree getVariableOrRecordPattern() { return varOrRecordPattern; }
@DefinedBy(Api.COMPILER_TREE)
public JCExpression getExpression() { return expr; }