Skip to content
Draft
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 @@ -72,6 +72,8 @@ public enum Feature {
status="Second Preview")
PEM_API,
LANGUAGE_MODEL,
@JEP(number=999, title="Enhanced Local Variable Declarations", status="First Preview")
ENHANCED_LOCAL_VARIABLE_DECLARATIONS(),
/**
* A key for testing.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,6 +25,8 @@

package com.sun.source.tree;

import jdk.internal.javac.PreviewFeature;

/**
* A tree node for an "enhanced" {@code for} loop statement.
*
Expand All @@ -41,12 +43,37 @@
* @since 1.6
*/
public interface EnhancedForLoopTree extends StatementTree {
/**
* "Enhanced" {@code for} declarations come in two forms:
* <ul>
* <li> local variable declarations and
* <li> record patterns
* </ul>
*
* @since 27
*/
@PreviewFeature(feature=PreviewFeature.Feature.ENHANCED_LOCAL_VARIABLE_DECLARATIONS, reflective=true)
public enum DeclarationKind {
/** enum constant for local variable declarations */
VARIABLE,
/** enum constant for record pattern declarations */
PATTERN
}

/**
* Returns the control variable for the loop.
* @return the control variable
* @return the control variable, or {@code null} if this "enhanced" {@code for} uses a pattern
*/
VariableTree getVariable();

/**
* Returns the record pattern for the loop.
* @return the record pattern, or {@code null} if this "enhanced" {@code for} uses a local variable declaration
* @since 27
*/
@PreviewFeature(feature=PreviewFeature.Feature.ENHANCED_LOCAL_VARIABLE_DECLARATIONS, reflective=true)
PatternTree getRecordPattern();

/**
* Returns the expression yielding the values for the control variable.
* @return the expression
Expand All @@ -58,4 +85,12 @@ public interface EnhancedForLoopTree extends StatementTree {
* @return the body of the loop
*/
StatementTree getStatement();

/**
* Returns the kind of the declaration of the "enhanced" {@code for}.
* @return the kind of the declaration
* @since 27
*/
@PreviewFeature(feature=PreviewFeature.Feature.ENHANCED_LOCAL_VARIABLE_DECLARATIONS, reflective=true)
DeclarationKind getDeclarationKind();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.sun.source.tree;

import jdk.internal.javac.PreviewFeature;

/**
* A tree node for an Enhanced Local Variable Declaration Statement.
*
* For example:
* <pre>
* <em>record pattern</em> = <em>expression</em>;
* </pre>
*
* @jls 14.23.1 Enhanced Local Variable Declaration Statements
*
* @author Angelos Bimpoudis
* @since 27
*/
@PreviewFeature(feature=PreviewFeature.Feature.ENHANCED_LOCAL_VARIABLE_DECLARATIONS, reflective=true)
public interface EnhancedVariableDeclTree extends StatementTree {
/**
* Returns the pattern for the enhanced local variable declaration statement.
* @return pattern
*/
Tree getPattern();

/**
* Returns the expression to be matched.
* @return the expression
*/
ExpressionTree getExpression();
}
11 changes: 10 additions & 1 deletion src/jdk.compiler/share/classes/com/sun/source/tree/Tree.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,6 +25,8 @@

package com.sun.source.tree;

import jdk.internal.javac.PreviewFeature;

/**
* Common interface for all nodes in an abstract syntax tree.
*
Expand Down Expand Up @@ -78,6 +80,13 @@ public enum Kind {
*/
ASSERT(AssertTree.class),

/**
* Used for instances of {@link EnhancedVariableDeclTree}.
* @since 27
*/
@PreviewFeature(feature=PreviewFeature.Feature.ENHANCED_LOCAL_VARIABLE_DECLARATIONS, reflective=true)
ENHANCED_VARIABLE_DECL(EnhancedVariableDeclTree.class),

/**
* Used for instances of {@link AssignmentTree}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,6 +25,8 @@

package com.sun.source.tree;

import jdk.internal.javac.PreviewFeature;

/**
* A visitor of trees, in the style of the visitor design pattern.
* Classes implementing this interface are used to operate
Expand Down Expand Up @@ -89,6 +91,17 @@ public interface TreeVisitor<R,P> {
*/
R visitAssert(AssertTree node, P p);

/**
* Visits an {@code EnhancedVariableDeclTree} node.
* @param node the node being visited
* @param p a parameter value
* @return a result value
*
* @since 27
*/
@PreviewFeature(feature=PreviewFeature.Feature.ENHANCED_LOCAL_VARIABLE_DECLARATIONS, reflective=true)
R visitEnhancedVariableDecl(EnhancedVariableDeclTree node, P p);

/**
* Visits an {@code AssignmentTree} node.
* @param node the node being visited
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,6 +26,7 @@
package com.sun.source.util;

import com.sun.source.tree.*;
import jdk.internal.javac.PreviewFeature;

/**
* A simple visitor for tree nodes.
Expand Down Expand Up @@ -475,6 +476,21 @@ public R visitAssert(AssertTree node, P p) {
return defaultAction(node, p);
}

/**
* {@inheritDoc}
*
* @implSpec This implementation calls {@code defaultAction}.
*
* @param node {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of {@code defaultAction}
*/
@Override
@PreviewFeature(feature=PreviewFeature.Feature.ENHANCED_LOCAL_VARIABLE_DECLARATIONS, reflective=true)
public R visitEnhancedVariableDecl(EnhancedVariableDeclTree node, P p) {
return defaultAction(node, p);
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,6 +26,7 @@
package com.sun.source.util;

import com.sun.source.tree.*;
import jdk.internal.javac.PreviewFeature;

/**
* A TreeVisitor that visits all the child tree nodes.
Expand Down Expand Up @@ -333,11 +334,29 @@ public R visitForLoop(ForLoopTree node, P p) {
@Override
public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
R r = scan(node.getVariable(), p);
r = scanAndReduce(node.getRecordPattern(), p, r);
r = scanAndReduce(node.getExpression(), p, r);
r = scanAndReduce(node.getStatement(), p, r);
return r;
}

/**
* {@inheritDoc}
*
* @implSpec This implementation scans the children in left to right order.
*
* @param node {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of scanning
*/
@Override
@PreviewFeature(feature=PreviewFeature.Feature.ENHANCED_LOCAL_VARIABLE_DECLARATIONS, reflective=true)
public R visitEnhancedVariableDecl(EnhancedVariableDeclTree node, P p) {
R r = scan(node.getPattern(), p);
r = scanAndReduce(node.getExpression(), p, r);
return r;
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -31,14 +31,11 @@
import com.sun.tools.javac.jvm.Target;
import com.sun.tools.javac.resources.CompilerProperties.Errors;
import com.sun.tools.javac.resources.CompilerProperties.LintWarnings;
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
import com.sun.tools.javac.util.Assert;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.JCDiagnostic.Error;
import com.sun.tools.javac.util.JCDiagnostic.LintWarning;
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
import com.sun.tools.javac.util.JCDiagnostic.Warning;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Names;
import com.sun.tools.javac.util.Options;
Expand All @@ -50,7 +47,6 @@
import java.util.Set;

import static com.sun.tools.javac.main.Option.PREVIEW;
import com.sun.tools.javac.util.JCDiagnostic;

/**
* Helper class to handle preview language features. This class maps certain language features
Expand Down Expand Up @@ -212,6 +208,7 @@ public boolean isEnabled() {
public boolean isPreview(Feature feature) {
return switch (feature) {
case PRIMITIVE_PATTERNS -> true;
case ENHANCED_VARIABLE_DECLS -> true;
//Note: this is a backdoor which allows to optionally treat all features as 'preview' (for testing).
//When real preview features will be added, this method can be implemented to return 'true'
//for those selected features, and 'false' for all the others.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ public enum Feature {
PRIVATE_MEMBERS_IN_PERMITS_CLAUSE(JDK19),
ERASE_POLY_SIG_RETURN_TYPE(JDK24),
CAPTURE_MREF_RETURN_TYPE(JDK26),
ENHANCED_VARIABLE_DECLS(JDK26, Fragments.FeatureEnhancedVariableDecls, DiagKind.PLURAL),
;

enum DiagKind {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -191,6 +191,7 @@ public static Symtab instance(Context context) {
public final Type incompatibleClassChangeErrorType;
public final Type cloneNotSupportedExceptionType;
public final Type matchExceptionType;
public final Type nullPointerExceptionType;
public final Type annotationType;
public final TypeSymbol enumSym;
public final Type listType;
Expand Down Expand Up @@ -570,6 +571,7 @@ public <R, P> R accept(ElementVisitor<R, P> v, P p) {
incompatibleClassChangeErrorType = enterClass("java.lang.IncompatibleClassChangeError");
cloneNotSupportedExceptionType = enterClass("java.lang.CloneNotSupportedException");
matchExceptionType = enterClass("java.lang.MatchException");
nullPointerExceptionType = enterClass("java.lang.NullPointerException");
annotationType = enterClass("java.lang.annotation.Annotation");
classLoaderType = enterClass("java.lang.ClassLoader");
enumSym = enterClass(java_base, names.java_lang_Enum);
Expand Down
Loading