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

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

commit 3e7423256e9bca69fb7d8fb25a9939b611cbc35b
Author: Arthur Sadykov <[email protected]>
AuthorDate: Fri Oct 9 10:21:22 2020 +0500

    Add support for static imports in code templates
---
 .../editor/java/JavaCodeTemplateProcessor.java     | 40 ++++++++++-
 .../editor/java/JavaCodeTemplateProcessorTest.java | 79 ++++++++++++++++++++--
 2 files changed, 112 insertions(+), 7 deletions(-)

diff --git 
a/java/java.editor/src/org/netbeans/modules/editor/java/JavaCodeTemplateProcessor.java
 
b/java/java.editor/src/org/netbeans/modules/editor/java/JavaCodeTemplateProcessor.java
index a6e8cde..8ea8627 100644
--- 
a/java/java.editor/src/org/netbeans/modules/editor/java/JavaCodeTemplateProcessor.java
+++ 
b/java/java.editor/src/org/netbeans/modules/editor/java/JavaCodeTemplateProcessor.java
@@ -70,6 +70,7 @@ public class JavaCodeTemplateProcessor implements 
CodeTemplateProcessor {
     public static final String CURRENT_CLASS_FULLY_QUALIFIED_NAME = 
"currClassFQName"; //NOI18N
     public static final String CURRENT_PACKAGE_NAME = "currPackageName"; 
//NOI18N
     public static final String CURRENT_METHOD_NAME = "currMethodName"; //NOI18N
+    public static final String STATIC_IMPORT = "staticImport"; //NOI18N
 
     private static final String TRUE = "true"; //NOI18N
     private static final String NULL = "null"; //NOI18N
@@ -122,7 +123,8 @@ public class JavaCodeTemplateProcessor implements 
CodeTemplateProcessor {
                         || CURRENT_PACKAGE_NAME.equals(hint)
                         || CURRENT_METHOD_NAME.equals(hint)
                         || ITERABLE_ELEMENT_TYPE.equals(hint)
-                        || UNCAUGHT_EXCEPTION_TYPE.equals(hint)) {
+                        || UNCAUGHT_EXCEPTION_TYPE.equals(hint)
+                        || STATIC_IMPORT.equals(hint)) {
                     needsParsing = true;
                 }
             }
@@ -539,11 +541,47 @@ public class JavaCodeTemplateProcessor implements 
CodeTemplateProcessor {
                         return value;
                     }
                 }
+            } else if (STATIC_IMPORT.equals(entry.getKey())) {
+                String qualifiedIdentifier = (String) entry.getValue();
+                param2hints.put(param, STATIC_IMPORT);
+                if (!qualifiedIdentifier.contains(".")) { //NOI18N
+                    return qualifiedIdentifier;
+                }
+                staticImport(qualifiedIdentifier);
+                return 
qualifiedIdentifier.substring(qualifiedIdentifier.lastIndexOf('.') + 1);
             }
         }
         return name;
     }
     
+    private void staticImport(String qualifiedIdentifier) {
+        JTextComponent component = request.getComponent();
+        Document document = component.getDocument();
+        JavaSource javaSource = JavaSource.forDocument(document);
+        if (javaSource != null) {
+            try {
+                javaSource.runModificationTask(copy  -> {
+                    JavaSource.Phase phase = 
copy.toPhase(JavaSource.Phase.RESOLVED);
+                    if (phase.compareTo(JavaSource.Phase.RESOLVED) == 0) {
+                        TreeMaker make = copy.getTreeMaker();
+                        CompilationUnitTree compilationUnit = 
copy.getCompilationUnit();
+                        List<? extends ImportTree> imports = 
compilationUnit.getImports();
+                        for (ImportTree importTree : imports) {
+                            if 
(importTree.getQualifiedIdentifier().toString().equals(qualifiedIdentifier)) {
+                                return;
+                            }
+                        }
+                        CompilationUnitTree newCompilationUnit = 
make.addCompUnitImport(
+                                compilationUnit, 
make.Import(make.Identifier(qualifiedIdentifier), true));
+                        copy.rewrite(compilationUnit, newCompilationUnit);
+                    }
+                }).commit();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+    }
+    
     private VariableElement instanceOf(String typeName, String name) {
         try {
             if (cInfo != null) {
diff --git 
a/java/java.editor/test/unit/src/org/netbeans/modules/editor/java/JavaCodeTemplateProcessorTest.java
 
b/java/java.editor/test/unit/src/org/netbeans/modules/editor/java/JavaCodeTemplateProcessorTest.java
index e53666c..08b5d41 100644
--- 
a/java/java.editor/test/unit/src/org/netbeans/modules/editor/java/JavaCodeTemplateProcessorTest.java
+++ 
b/java/java.editor/test/unit/src/org/netbeans/modules/editor/java/JavaCodeTemplateProcessorTest.java
@@ -18,11 +18,13 @@
  */
 package org.netbeans.modules.editor.java;
 
+import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.util.Arrays;
 import javax.swing.JEditorPane;
+import javax.swing.SwingUtilities;
 import javax.swing.text.Document;
 import javax.swing.text.EditorKit;
 import org.junit.Test;
@@ -38,6 +40,8 @@ import org.openide.filesystems.FileUtil;
 
 public class JavaCodeTemplateProcessorTest extends NbTestCase {
 
+    private FileObject testFile;
+    
     public JavaCodeTemplateProcessorTest(String name) {
         super(name);
     }
@@ -63,10 +67,12 @@ public class JavaCodeTemplateProcessorTest extends 
NbTestCase {
 
     private void doTestTemplateInsert(String template, String code, String 
expected) throws Exception {
         clearWorkDir();
-        FileObject testFile = 
FileUtil.toFileObject(getWorkDir()).createData("Test.java");
+        testFile = FileUtil.toFileObject(getWorkDir()).createData("Test.java");
         EditorKit kit = new JavaKit();
         JEditorPane pane = new JEditorPane();
-        pane.setEditorKit(kit);
+        SwingUtilities.invokeAndWait(() -> {
+            pane.setEditorKit(kit);
+        });
         Document doc = pane.getDocument();
         doc.putProperty(Document.StreamDescriptionProperty, testFile);
         doc.putProperty(Language.class, JavaTokenId.language());
@@ -90,10 +96,71 @@ public class JavaCodeTemplateProcessorTest extends 
NbTestCase {
         assertEquals(expectedText, doc.getText(0, doc.getLength()));
         assertEquals(resultCaretOffset, pane.getCaretPosition());
     }
-
-    @Override
-    protected boolean runInEQ() {
-        return true;
+    
+    @Test
+    public void 
testShouldAddStaticImportForTemplateParameterWithStaticImportHint() throws 
Exception {
+        doTestTemplateInsert("int max = ${param 
staticImport=\"java.lang.Math.max\" editable=false}(0, 1);",
+                             "public class Test {\n" +
+                             "    private void t(String... args) {\n" +
+                             "        |\n" +
+                             "    }\n" +
+                             "}",
+                             "public class Test {\n" +
+                             "    private void t(String... args) {\n" +
+                             "        int max = max(0, 1);|\n" +
+                             "    }\n" +
+                             "}");
+        assertFileObjectTextMatchesRegex("(?s)\\s*?import static 
java\\.lang\\.Math\\.max;\\s*?public class Test.*?");
+    }
+    
+    @Test
+    public void 
testShouldNotAddDuplicatesOfStaticImportForTemplateParameterWithStaticImportHint()
 throws Exception {
+        doTestTemplateInsert("int max = ${param 
staticImport=\"java.lang.Math.max\" editable=false}(0, 1);",
+                             "public class Test {\n" +
+                             "    private void t(String... args) {\n" +
+                             "        |\n" +
+                             "    }\n" +
+                             "}",
+                             "public class Test {\n" +
+                             "    private void t(String... args) {\n" +
+                             "        int max = max(0, 1);|\n" +
+                             "    }\n" +
+                             "}");
+        doTestTemplateInsert("int max = ${param 
staticImport=\"java.lang.Math.max\" editable=false}(0, 1);",
+                             "public class Test {\n" +
+                             "    private void t(String... args) {\n" +
+                             "        int max = max(0, 1);\n" +
+                             "        |\n"+
+                             "    }\n" +
+                             "}",
+                             "public class Test {\n" +
+                             "    private void t(String... args) {\n" +
+                             "        int max = max(0, 1);\n" +
+                             "        int max = max(0, 1);|\n" +
+                             "    }\n" +
+                             "}");
+        assertFileObjectTextMatchesRegex("(?s)\\s*?import static 
java\\.lang\\.Math\\.max;\\s*?public class Test.*?");
+    }
+    
+    @Test
+    public void 
testWhenOnlyIdentifierWithoutTypeIsSpecifiedThenDoNotAddStaticImport() throws 
Exception {
+        doTestTemplateInsert("int max = ${param staticImport=\"max\" 
editable=false}(0, 1);",
+                             "public class Test {\n" +
+                             "    private void t(String... args) {\n" +
+                             "        |\n" +
+                             "    }\n" +
+                             "}",
+                             "public class Test {\n" +
+                             "    private void t(String... args) {\n" +
+                             "        int max = max(0, 1);|\n" +
+                             "    }\n" +
+                             "}");
+        assertFileObjectTextMatchesRegex("(?s)\\s*?public class Test.*?");
+    }
+    
+    private void assertFileObjectTextMatchesRegex(String regex) throws 
IOException {
+        String text = testFile.asText();
+        assertTrue("The file text must match the regular expression", 
text.matches(regex));
     }
 
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

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

Reply via email to