Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8329645

javax.lang.model support for Derived Record Creation (Preview)

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 23
    • core-libs
    • None
    • minimal
    • Add methods to support a preview language feature.
    • Java API
    • SE

      Summary

      Update javax.land.model API to support component local variables as part of a preview language feature.

      Problem

      The Derived Record Creation feature contains new language structures to be modeled.

      Solution

      Use the preview visitors to provide the necessary language model.

      Specification

      diff --git a/src/java.compiler/share/classes/javax/lang/model/element/ElementKind.java b/src/java.compiler/share/classes/javax/lang/model/element/ElementKind.java
      index 4190e2acdab..91382fe6e90 100644
      --- a/src/java.compiler/share/classes/javax/lang/model/element/ElementKind.java
      +++ b/src/java.compiler/share/classes/javax/lang/model/element/ElementKind.java
      @@ -25,6 +25,8 @@
      
       package javax.lang.model.element;
      
      +import jdk.internal.javac.PreviewFeature;
      +
       /**
        * The {@code kind} of an element.
        *
      @@ -121,7 +123,14 @@ public enum ElementKind {
            * A binding variable in a pattern.
            * @since 16
            */
      -    BINDING_VARIABLE;
      +    BINDING_VARIABLE,
      +
      +    /**
      +     * A local component variable in a derived record creation expression.
      +     * @since 23
      +     */
      +    @PreviewFeature(feature=PreviewFeature.Feature.DERIVED_RECORD_CREATION, reflective=true)
      +    COMPONENT_LOCAL_VARIABLE;
      
           // Maintenance note: check if the default implementation of
           // Elements.getOutermostTypeElement needs updating when new kind
      @@ -200,7 +209,8 @@ public boolean isInitializer() {
            * Returns {@code true} if this is a kind of variable: including
            * {@code ENUM_CONSTANT}, {@code FIELD}, {@code PARAMETER},
            * {@code LOCAL_VARIABLE}, {@code EXCEPTION_PARAMETER},
      -     * {@code RESOURCE_VARIABLE}, and {@code BINDING_VARIABLE}.
      +     * {@code RESOURCE_VARIABLE}, {@code BINDING_VARIABLE}, and
      +     * {@code COMPONENT_LOCAL_VARIABLE}.
            *
            * @return {@code true} if this is a kind of variable
            * @since 19
      @@ -209,7 +219,7 @@ public boolean isVariable() {
               return switch(this) {
               case ENUM_CONSTANT, FIELD, PARAMETER,
                    LOCAL_VARIABLE, EXCEPTION_PARAMETER, RESOURCE_VARIABLE,
      -             BINDING_VARIABLE -> true;
      +             BINDING_VARIABLE, COMPONENT_LOCAL_VARIABLE -> true;
               default -> false;
               };
           }
      
      --- a/src/java.compiler/share/classes/javax/lang/model/util/ElementKindVisitor6.java
      +++ b/src/java.compiler/share/classes/javax/lang/model/util/ElementKindVisitor6.java
      @@ -1,5 +1,5 @@
       /*
      - * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
      + * Copyright (c) 2005, 2024, 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
      @@ -25,6 +25,8 @@
      
       package javax.lang.model.util;
      
      +import jdk.internal.javac.PreviewFeature;
      +
       import javax.lang.model.element.*;
       import javax.annotation.processing.SupportedSourceVersion;
       import javax.lang.model.SourceVersion;
      @@ -264,6 +266,9 @@
               case BINDING_VARIABLE:
                   return visitVariableAsBindingVariable(e, p);
      
      +        case COMPONENT_LOCAL_VARIABLE:
      +            return visitVariableAsComponentLocalVariable(e, p);
      +
               default:
                   throw new AssertionError("Bad kind " + k + " for VariableElement" + e);
               }
      @@ -364,6 +369,22 @@
               return visitUnknown(e, p);
           }
      
      +    /**
      +     * Visits a {@code COMPONENT_LOCAL_VARIABLE} variable element.
      +     *
      +     * @implSpec This implementation calls {@code visitUnknown}.
      +     *
      +     * @param e the element to visit
      +     * @param p a visitor-specified parameter
      +     * @return  the result of {@code visitUnknown}
      +     *
      +     * @since 23
      +     */
      +    @PreviewFeature(feature=PreviewFeature.Feature.DERIVED_RECORD_CREATION, reflective=true)
      +    public R visitVariableAsComponentLocalVariable(VariableElement e, P p) {
      +        return visitUnknown(e, p);
      +    }
      +
           /**
            * {@inheritDoc ElementVisitor}
            *
      
      --- a/src/java.compiler/share/classes/javax/lang/model/util/ElementKindVisitorPreview.java
      +++ b/src/java.compiler/share/classes/javax/lang/model/util/ElementKindVisitorPreview.java
      @@ -86,4 +86,21 @@ protected ElementKindVisitorPreview() {
           protected ElementKindVisitorPreview(R defaultValue) {
               super(defaultValue);
           }
      +
      +    /**
      +     * {@inheritDoc ElementKindVisitor6}
      +     *
      +     * @implSpec This implementation calls {@code defaultAction}.
      +     *
      +     * @param e {@inheritDoc ElementKindVisitor6}
      +     * @param p {@inheritDoc ElementKindVisitor6}
      +     * @return  the result of {@code defaultAction}
      +     *
      +     * @since 23
      +     */
      +    @Override
      +    @PreviewFeature(feature=PreviewFeature.Feature.DERIVED_RECORD_CREATION, reflective=true)
      +    public R visitVariableAsComponentLocalVariable(VariableElement e, P p) {
      +        return defaultAction(e, p);
      +     }
       }

            darcy Joe Darcy
            darcy Joe Darcy
            Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: