This is an automated email from the ASF dual-hosted git repository.

akhileshsingh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 8328d7c  [NETBEANS-5008] : Support for finalization of 
pattern-matching-instance-of feature (#2804)
8328d7c is described below

commit 8328d7c0c07fe8326eb8d19a451d88cbdfb642e8
Author: Akhilesh Singh <akhilesh.s.si...@oracle.com>
AuthorDate: Tue Mar 30 18:03:27 2021 +0530

    [NETBEANS-5008] : Support for finalization of pattern-matching-instance-of 
feature (#2804)
    
    * [NETBEANS-5008] : Support for finalization of 
pattern-matching-instance-of feature
---
 .../java/hints/jdk/ConvertToPatternInstanceOf.java |  7 +++---
 java/java.source.base/apichanges.xml               | 16 +++++++++++++
 .../org/netbeans/api/java/source/TreeMaker.java    | 16 ++++++++++++-
 .../netbeans/modules/java/source/TreeShims.java    | 27 ++++++++++++++--------
 .../modules/java/source/builder/TreeFactory.java   |  8 +++++++
 .../modules/java/source/pretty/VeryPretty.java     |  9 +++-----
 6 files changed, 64 insertions(+), 19 deletions(-)

diff --git 
a/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToPatternInstanceOf.java
 
b/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToPatternInstanceOf.java
index e397ed8..488c36c 100644
--- 
a/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToPatternInstanceOf.java
+++ 
b/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToPatternInstanceOf.java
@@ -43,6 +43,7 @@ import org.netbeans.api.java.source.CompilationInfo;
 import org.netbeans.api.java.source.TreePathHandle;
 import org.netbeans.api.java.source.WorkingCopy;
 import org.netbeans.modules.java.hints.errors.Utilities;
+import org.netbeans.modules.java.source.TreeShims;
 import org.netbeans.spi.editor.hints.ErrorDescription;
 import org.netbeans.spi.editor.hints.Fix;
 import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
@@ -138,9 +139,9 @@ public class ConvertToPatternInstanceOf {
             IfTree it = (IfTree) main.getLeaf();
             InstanceOfTree iot = (InstanceOfTree) ((ParenthesizedTree) 
it.getCondition()).getExpression();
             StatementTree bt = it.getThenStatement();
-//            wc.rewrite(iot.getType(), 
wc.getTreeMaker().BindingPattern(var.getName(), iot.getType()));
-//            wc.rewrite(bt, wc.getTreeMaker().removeBlockStatement(bt, 0));
-            InstanceOfTree cond = 
wc.getTreeMaker().InstanceOf(iot.getExpression(), 
wc.getTreeMaker().BindingPattern(varName, iot.getType()));
+            InstanceOfTree cond = 
wc.getTreeMaker().InstanceOf(iot.getExpression(), 
TreeShims.isJDKVersionSupportEnablePreview()
+                    ? wc.getTreeMaker().BindingPattern(varName, iot.getType())
+                    : 
wc.getTreeMaker().BindingPattern(wc.getTreeMaker().Variable(wc.getTreeMaker().Modifiers(EnumSet.noneOf(Modifier.class)),
 varName, iot.getType(), null)));
             StatementTree thenBlock = removeFirst ? 
wc.getTreeMaker().removeBlockStatement((BlockTree) bt, 0) : bt;
             wc.rewrite(it, 
wc.getTreeMaker().If(wc.getTreeMaker().Parenthesized(cond), thenBlock, 
it.getElseStatement()));
             replaceOccurrences.stream().map(tph -> tph.resolve(wc)).forEach(tp 
-> {
diff --git a/java/java.source.base/apichanges.xml 
b/java/java.source.base/apichanges.xml
index 29772fc..5d0fbb8 100644
--- a/java/java.source.base/apichanges.xml
+++ b/java/java.source.base/apichanges.xml
@@ -348,6 +348,22 @@
         </description>
         <class name="TreeUtilities" package="org.netbeans.api.java.source"/>
     </change>
+    
+    <change id="TreeMaker.BindingPattern">
+        <api name="javasource_base" />
+        <summary>Added new method for getting Tree using VariableTree 
type</summary>
+        <version major="12" minor="4"/>
+        <date day="23" month="3" year="2021"/>
+        <author login="singh-akhilesh"/>
+        <compatibility addition="yes" binary="compatible" source="compatible"/>
+        <description>
+            <p>
+                Regarding the JEP 394 (finalization of pattern matching for 
instanceof in JDK 16).
+                The shape of the BindingPatternTree has changed; the 
BindingPatternTree does not have a name and type, but rather a VariableTree.
+            </p>
+        </description>
+        <class name="TreeMaker" package="org.netbeans.api.java.source"/>
+    </change>
 </changes>
 <htmlcontents>
 <head>
diff --git 
a/java/java.source.base/src/org/netbeans/api/java/source/TreeMaker.java 
b/java/java.source.base/src/org/netbeans/api/java/source/TreeMaker.java
index baef5da..f513299 100644
--- a/java/java.source.base/src/org/netbeans/api/java/source/TreeMaker.java
+++ b/java/java.source.base/src/org/netbeans/api/java/source/TreeMaker.java
@@ -111,6 +111,7 @@ import org.openide.util.Parameters;
  * 
  * @since 0.44.0
  */
+
 public final class TreeMaker {
     
     private TreeFactory delegate;
@@ -1213,17 +1214,30 @@ public final class TreeMaker {
     
     /**
      * Creates a new BindingPatternTree.
-     *
+     * @deprecated
      * @param name name of the binding variable
      * @param type the type of the pattern
      * @return the newly created BindingPatternTree
      * @throws NoSuchMethodException if the used javac does not support
      *                               BindingPatternTree.
      */
+    @Deprecated
     public Tree BindingPattern(CharSequence name,
                                Tree type) {
         return delegate.BindingPattern(name, type);
     }
+    
+      /**
+     * Creates a new Tree for a given VariableTree
+     * @specication : 15.20.2
+     * @param vt the VariableTree of the pattern
+     * @see com.sun.source.tree.BindingPatternTree
+     * @return the newly created BindingPatternTree
+     * @since 16
+     */
+    public Tree BindingPattern(VariableTree vt) {
+        return delegate.BindingPattern(vt);
+    }
 
     /**
      * Creates a new VariableTree from a VariableElement.
diff --git 
a/java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java 
b/java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java
index db4abc9..4131643 100644
--- a/java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java
+++ b/java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java
@@ -26,6 +26,7 @@ import com.sun.source.tree.ExpressionTree;
 import com.sun.source.tree.InstanceOfTree;
 import com.sun.source.tree.SwitchTree;
 import com.sun.source.tree.Tree;
+import com.sun.source.tree.VariableTree;
 import com.sun.tools.javac.tree.DocTreeMaker;
 import com.sun.tools.javac.tree.JCTree;
 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
@@ -38,9 +39,8 @@ import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import javax.lang.model.SourceVersion;
 import javax.lang.model.element.Name;
-import org.openide.util.Exceptions;
-
 import javax.lang.model.element.Element;
 import javax.lang.model.element.ElementKind;
 import javax.lang.model.element.TypeElement;
@@ -52,7 +52,8 @@ public class TreeShims {
     public static final String YIELD = "YIELD"; //NOI18N
     public static final String BINDING_VARIABLE = "BINDING_VARIABLE"; //NOI18N
     public static final String RECORD = "RECORD"; //NOI18N
-
+    public static final int PATTERN_MATCHING_INSTANCEOF_PREVIEW_JDK_VERSION = 
15; //NOI18N
+    
     public static List<? extends ExpressionTree> getExpressions(CaseTree node) 
{
         try {
             Method getExpressions = 
CaseTree.class.getDeclaredMethod("getExpressions");
@@ -156,9 +157,11 @@ public class TreeShims {
 
     public static Name getBinding(Tree node) {
         try {
-            Class bpt = 
Class.forName("com.sun.source.tree.BindingPatternTree");
-            Method getBinding = bpt.getDeclaredMethod("getBinding");
-            return (Name) getBinding.invoke(node);
+            Class bpt = 
Class.forName("com.sun.source.tree.BindingPatternTree"); //NOI18N
+            return isJDKVersionSupportEnablePreview() 
+                    ? (Name)bpt.getDeclaredMethod("getBinding").invoke(node)  
//NOI18N
+                    : 
((VariableTree)bpt.getDeclaredMethod("getVariable").invoke(node)).getName(); 
//NOI18N
+
         } catch (NoSuchMethodException | ClassNotFoundException ex) {
             return null;
         } catch (IllegalAccessException | IllegalArgumentException | 
InvocationTargetException ex) {
@@ -238,9 +241,11 @@ public class TreeShims {
             return null;
         }
         try {
-            Class bindingPatternTreeClass = 
Class.forName("com.sun.source.tree.BindingPatternTree"); //NOI18N
-            Method getType = 
bindingPatternTreeClass.getDeclaredMethod("getType");  //NOI18N
-            return (Tree) getType.invoke(node);
+            Class bpt = 
Class.forName("com.sun.source.tree.BindingPatternTree"); //NOI18N
+            return isJDKVersionSupportEnablePreview()
+                    ? (Tree) bpt.getDeclaredMethod("getType").invoke(node) 
//NOI18N
+                    : ((VariableTree) 
bpt.getDeclaredMethod("getVariable").invoke(node)).getType(); //NOI18N
+
         } catch (NoSuchMethodException ex) {
             return null;
         } catch (IllegalAccessException | IllegalArgumentException | 
InvocationTargetException | ClassNotFoundException ex) {
@@ -306,4 +311,8 @@ public class TreeShims {
             throw TreeShims.<RuntimeException>throwAny(ex);
         }
     }
+    
+    public static boolean isJDKVersionSupportEnablePreview() {
+        return 
Integer.valueOf(SourceVersion.latest().name().split("_")[1]).compareTo(PATTERN_MATCHING_INSTANCEOF_PREVIEW_JDK_VERSION)
 <= 0;
+    }
 }
diff --git 
a/java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java
 
b/java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java
index d59a4fb..32758b5 100644
--- 
a/java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java
+++ 
b/java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java
@@ -921,6 +921,14 @@ public class TreeFactory {
             throw throwAny(t);
         }
     }
+    
+    public Tree BindingPattern(VariableTree vt) {
+        try {
+            return (Tree) 
make.getClass().getMethod("BindingPattern",JCVariableDecl.class).invoke(make.at(NOPOS),
 vt);
+        } catch (Throwable t) {
+            throw throwAny(t);
+        }
+    }
 
     public VariableTree Variable(VariableElement variable, ExpressionTree 
initializer) {
         return make.at(NOPOS).VarDef((Symbol.VarSymbol)variable, 
(JCExpression)initializer);
diff --git 
a/java/java.source.base/src/org/netbeans/modules/java/source/pretty/VeryPretty.java
 
b/java/java.source.base/src/org/netbeans/modules/java/source/pretty/VeryPretty.java
index 34aecf6..1470510 100644
--- 
a/java/java.source.base/src/org/netbeans/modules/java/source/pretty/VeryPretty.java
+++ 
b/java/java.source.base/src/org/netbeans/modules/java/source/pretty/VeryPretty.java
@@ -2077,14 +2077,11 @@ public final class VeryPretty extends JCTree.Visitor 
implements DocTreeVisitor<V
     public void visitTree(JCTree tree) {
         if ("BINDING_PATTERN".equals(tree.getKind().name())) {
             try {
-                Class bindingPatternClass = 
Class.forName("com.sun.source.tree.BindingPatternTree");
-                Method getBinding = 
bindingPatternClass.getMethod("getBinding");
-                Method getType = bindingPatternClass.getMethod("getType");
-                print((JCTree) getType.invoke(tree));
+                print((JCTree) TreeShims.getBindingPatternType(tree));
                 print(' ');
-                print((Name) getBinding.invoke(tree));
+                print((Name) TreeShims.getBinding(tree));
                 return ;
-            } catch (NoSuchMethodException | SecurityException | 
IllegalAccessException | IllegalArgumentException | InvocationTargetException | 
ClassNotFoundException ex) {
+            } catch (RuntimeException ex) {
                 Exceptions.printStackTrace(ex);
             }
         }

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to