You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2020/08/28 07:57:49 UTC

[GitHub] [netbeans] singh-akhilesh opened a new pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

singh-akhilesh opened a new pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334


   https://issues.apache.org/jira/browse/NETBEANS-3986


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#issuecomment-704729031


   @lahodaj Thanks for reviewing and giving the good comments. I'll address all these.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r501089530



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            try {
+                fileManager.close();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+
+        if (publicFirstClassName != null && !publicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(publicFirstClassName, packageName);
+        } else if (nonPublicFirstClassName != null && !nonPublicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(nonPublicFirstClassName, packageName);
+        } else {
+            return null;
+        }
+
+    }
+
+    private String removePackage(String copiedCode, String oldPacageName) {

Review comment:
       Comment Addressed.
   Getting start and end offset of package using below:
   
   JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
   
   SourcePositions sourcePositions = Trees.instance(task).getSourcePositions();
               for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
                   packageStartOffset = sourcePositions.getStartPosition(compilationUnitTree, compilationUnitTree.getPackage());
                   packageEndOffset = sourcePositions.getEndPosition(compilationUnitTree, compilationUnitTree.getPackage());




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r501083494



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N

Review comment:
       Addressed code comment. 
   Used  _@NbBundle.Messages({
           "ERR_NotValidClass=Code is not a valid class",
           "ERR_ClassAlreadyPresent=Class {0} already present",
           "ERR_UnableToCreateFile=Unable to create file {0}",})_ for message bundling.
   and  _DialogDisplayer.getDefault().notifyLater(notifyMsg);_ for displaying error dialog.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r501089530



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            try {
+                fileManager.close();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+
+        if (publicFirstClassName != null && !publicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(publicFirstClassName, packageName);
+        } else if (nonPublicFirstClassName != null && !nonPublicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(nonPublicFirstClassName, packageName);
+        } else {
+            return null;
+        }
+
+    }
+
+    private String removePackage(String copiedCode, String oldPacageName) {

Review comment:
       Comment Addressed.
   Getting start and end offset of package using below:
   
   _**JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
   
   SourcePositions sourcePositions = Trees.instance(task).getSourcePositions();
               for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
                   packageStartOffset = sourcePositions.getStartPosition(compilationUnitTree, compilationUnitTree.getPackage());
                   packageEndOffset = sourcePositions.getEndPosition(compilationUnitTree, compilationUnitTree.getPackage());**_




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r501087686



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {

Review comment:
       Expecting IOExection here, changed this to IOException.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#issuecomment-707523407


   @lahodaj I have addressed all the comments, please relook it once.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] arvindaprameya commented on pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
arvindaprameya commented on pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#issuecomment-694736869


   Thanks for getting it done Akhilesh , approved.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] lahodaj commented on pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
lahodaj commented on pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#issuecomment-707656878


   Looks OK.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] arusinha commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
arusinha commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r491781294



##########
File path: java/java.project.ui/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java
##########
@@ -721,6 +724,121 @@ public void testCopyPaste () throws Exception {
         }
     }
 
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("import java.util.*;"
+                + "class C{}"
+                + "public class PC{}"
+                + "interface I{}"
+                + "enum En{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "class C{}"
+                    + "public class PC{}"
+                    + "interface I{}"
+                    + "enum En{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_removeExistingPackageName() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-rm-package");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("package copy.paste     ."
+                + "java"
+                + ";"
+                + "import java.util.*;"
+                + "public class PC{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "public class PC{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_CompilationErrorInCode() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-error");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +
+                "\"C:\\Program Files\\NB\\Community Edition 201.3803.71\\lib\\");
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "Hello.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +

Review comment:
       plz avoid hardcoded path
   

##########
File path: java/java.project.ui/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java
##########
@@ -721,6 +724,121 @@ public void testCopyPaste () throws Exception {
         }
     }
 
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("import java.util.*;"
+                + "class C{}"
+                + "public class PC{}"
+                + "interface I{}"
+                + "enum En{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "class C{}"
+                    + "public class PC{}"
+                    + "interface I{}"
+                    + "enum En{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_removeExistingPackageName() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-rm-package");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("package copy.paste     ."
+                + "java"
+                + ";"
+                + "import java.util.*;"
+                + "public class PC{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "public class PC{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_CompilationErrorInCode() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-error");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +

Review comment:
       plz avoid hardcoded path

##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public";
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class");
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java");
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present");
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file");
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            try {
+                fileManager.close();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+
+        if (publicFirstClassName != null && !publicFirstClassName.equals("<error>")) {
+            return new ClassContent(publicFirstClassName, packageName);
+        } else if (nonPublicFirstClassName != null && !nonPublicFirstClassName.equals("<error>")) {
+            return new ClassContent(nonPublicFirstClassName, packageName);
+        } else {
+            return null;
+        }
+
+    }
+
+    private String removePackage(String copiedCode, String oldPacageName) {
+        String packageRegex = oldPacageName.replace(".", "\\s*.\\s*");

Review comment:
       plz use //NOI18N for string literal. need to correct wherever used string literal
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r501086380



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

Review comment:
       Addressed.
   You are correct file manager is not needed in this case. So removed file manager passed null in compiler.getTask.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#issuecomment-697618312


   @jlahoda Please review this PR.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r491839180



##########
File path: java/java.project.ui/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java
##########
@@ -721,6 +724,121 @@ public void testCopyPaste () throws Exception {
         }
     }
 
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("import java.util.*;"
+                + "class C{}"
+                + "public class PC{}"
+                + "interface I{}"
+                + "enum En{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "class C{}"
+                    + "public class PC{}"
+                    + "interface I{}"
+                    + "enum En{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_removeExistingPackageName() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-rm-package");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("package copy.paste     ."
+                + "java"
+                + ";"
+                + "import java.util.*;"
+                + "public class PC{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "public class PC{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_CompilationErrorInCode() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-error");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +

Review comment:
       addressed code review comments

##########
File path: java/java.project.ui/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java
##########
@@ -721,6 +724,121 @@ public void testCopyPaste () throws Exception {
         }
     }
 
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("import java.util.*;"
+                + "class C{}"
+                + "public class PC{}"
+                + "interface I{}"
+                + "enum En{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "class C{}"
+                    + "public class PC{}"
+                    + "interface I{}"
+                    + "enum En{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_removeExistingPackageName() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-rm-package");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("package copy.paste     ."
+                + "java"
+                + ";"
+                + "import java.util.*;"
+                + "public class PC{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "public class PC{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_CompilationErrorInCode() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-error");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +
+                "\"C:\\Program Files\\NB\\Community Edition 201.3803.71\\lib\\");
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "Hello.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +

Review comment:
       addressed code review comments

##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public";
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class");
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java");
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present");
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file");
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            try {
+                fileManager.close();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+
+        if (publicFirstClassName != null && !publicFirstClassName.equals("<error>")) {
+            return new ClassContent(publicFirstClassName, packageName);
+        } else if (nonPublicFirstClassName != null && !nonPublicFirstClassName.equals("<error>")) {
+            return new ClassContent(nonPublicFirstClassName, packageName);
+        } else {
+            return null;
+        }
+
+    }
+
+    private String removePackage(String copiedCode, String oldPacageName) {
+        String packageRegex = oldPacageName.replace(".", "\\s*.\\s*");

Review comment:
       addressed code review comments




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r501092904



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            try {
+                fileManager.close();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+
+        if (publicFirstClassName != null && !publicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(publicFirstClassName, packageName);
+        } else if (nonPublicFirstClassName != null && !nonPublicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(nonPublicFirstClassName, packageName);
+        } else {
+            return null;
+        }
+
+    }
+
+    private String removePackage(String copiedCode, String oldPacageName) {
+        String packageRegex = oldPacageName.replace(".", "\\s*.\\s*"); //NOI18N
+        packageRegex = "package\\s+" + packageRegex + "\\s*;"; //NOI18N
+        Pattern p = Pattern.compile(packageRegex);
+
+        Matcher m = p.matcher(copiedCode);
+        StringBuffer sb = new StringBuffer();
+        if (m.find()) {
+            m.appendReplacement(sb, ""); //NOI18N
+        }
+
+        m.appendTail(sb);
+        return sb.toString();
+    }
+
+    private String getPackageNameFromFile(File fileName) {
+        String packageLocation = null;
+        try {
+            FileObject data = FileUtil.createData(fileName);
+            JavaSource js = JavaSource.forFileObject(data);
+
+            final DeadlockTask bt = new DeadlockTask(JavaSource.Phase.RESOLVED);
+            js.runUserActionTask(bt, true);

Review comment:
       Comment addressed!
   Removed the previously written code for getting class path and use the suggested one : ** ClassPath.getClassPath(data, ClassPath.SOURCE);** 
   
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r501089530



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            try {
+                fileManager.close();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+
+        if (publicFirstClassName != null && !publicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(publicFirstClassName, packageName);
+        } else if (nonPublicFirstClassName != null && !nonPublicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(nonPublicFirstClassName, packageName);
+        } else {
+            return null;
+        }
+
+    }
+
+    private String removePackage(String copiedCode, String oldPacageName) {

Review comment:
       Comment Addressed.
   Getting start and end offset of package using below:
   
   **JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
   
   SourcePositions sourcePositions = Trees.instance(task).getSourcePositions();
               for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
                   packageStartOffset = sourcePositions.getStartPosition(compilationUnitTree, compilationUnitTree.getPackage());
                   packageEndOffset = sourcePositions.getEndPosition(compilationUnitTree, compilationUnitTree.getPackage());**




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r491839180



##########
File path: java/java.project.ui/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java
##########
@@ -721,6 +724,121 @@ public void testCopyPaste () throws Exception {
         }
     }
 
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("import java.util.*;"
+                + "class C{}"
+                + "public class PC{}"
+                + "interface I{}"
+                + "enum En{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "class C{}"
+                    + "public class PC{}"
+                    + "interface I{}"
+                    + "enum En{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_removeExistingPackageName() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-rm-package");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("package copy.paste     ."
+                + "java"
+                + ";"
+                + "import java.util.*;"
+                + "public class PC{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "public class PC{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_CompilationErrorInCode() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-error");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +

Review comment:
       addressed code review comments

##########
File path: java/java.project.ui/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java
##########
@@ -721,6 +724,121 @@ public void testCopyPaste () throws Exception {
         }
     }
 
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("import java.util.*;"
+                + "class C{}"
+                + "public class PC{}"
+                + "interface I{}"
+                + "enum En{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "class C{}"
+                    + "public class PC{}"
+                    + "interface I{}"
+                    + "enum En{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_removeExistingPackageName() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-rm-package");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("package copy.paste     ."
+                + "java"
+                + ";"
+                + "import java.util.*;"
+                + "public class PC{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "public class PC{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_CompilationErrorInCode() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-error");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +
+                "\"C:\\Program Files\\NB\\Community Edition 201.3803.71\\lib\\");
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "Hello.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +

Review comment:
       addressed code review comments

##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public";
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class");
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java");
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present");
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file");
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            try {
+                fileManager.close();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+
+        if (publicFirstClassName != null && !publicFirstClassName.equals("<error>")) {
+            return new ClassContent(publicFirstClassName, packageName);
+        } else if (nonPublicFirstClassName != null && !nonPublicFirstClassName.equals("<error>")) {
+            return new ClassContent(nonPublicFirstClassName, packageName);
+        } else {
+            return null;
+        }
+
+    }
+
+    private String removePackage(String copiedCode, String oldPacageName) {
+        String packageRegex = oldPacageName.replace(".", "\\s*.\\s*");

Review comment:
       addressed code review comments




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r501086875



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));

Review comment:
       Addressed.
   Verified with and without nb-javac, working as expected.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r501083494



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N

Review comment:
       Addressed code comment. 
   Used  _**@NbBundle.Messages({
           "ERR_NotValidClass=Code is not a valid class",
           "ERR_ClassAlreadyPresent=Class {0} already present",
           "ERR_UnableToCreateFile=Unable to create file {0}",})**_ for message bundling.
   and  _**DialogDisplayer.getDefault().notifyLater(notifyMsg);**_ for displaying error dialog.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] Akshay-Gupta-Oracle merged pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
Akshay-Gupta-Oracle merged pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r501084841



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {

Review comment:
       Addressed. 
   Removed this class **_DeadlockTask_** .




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r501084841



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {

Review comment:
       Addressed. 
   Removed this class.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] lahodaj commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
lahodaj commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r500050377



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

Review comment:
       Not sure if the file manager is needed here (passing null to getTask instead of a file manager should be fine). But if yes, please wrap it with try-with-resources, so that it is guaranteed to be closed.

##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));

Review comment:
       Please check this work both with and without nb-javac (there are some special cases for nb-javac, so to be sure).

##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {

Review comment:
       What exceptions are expected here?

##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            try {
+                fileManager.close();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+
+        if (publicFirstClassName != null && !publicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(publicFirstClassName, packageName);
+        } else if (nonPublicFirstClassName != null && !nonPublicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(nonPublicFirstClassName, packageName);
+        } else {
+            return null;
+        }
+
+    }
+
+    private String removePackage(String copiedCode, String oldPacageName) {
+        String packageRegex = oldPacageName.replace(".", "\\s*.\\s*"); //NOI18N
+        packageRegex = "package\\s+" + packageRegex + "\\s*;"; //NOI18N
+        Pattern p = Pattern.compile(packageRegex);
+
+        Matcher m = p.matcher(copiedCode);
+        StringBuffer sb = new StringBuffer();
+        if (m.find()) {
+            m.appendReplacement(sb, ""); //NOI18N
+        }
+
+        m.appendTail(sb);
+        return sb.toString();
+    }
+
+    private String getPackageNameFromFile(File fileName) {
+        String packageLocation = null;
+        try {
+            FileObject data = FileUtil.createData(fileName);
+            JavaSource js = JavaSource.forFileObject(data);
+
+            final DeadlockTask bt = new DeadlockTask(JavaSource.Phase.RESOLVED);
+            js.runUserActionTask(bt, true);

Review comment:
       Running a task seems unnecessarily complex and slow. If I read this correctly, the goal is to get the source ClassPath. There are multiple ways to achieve that:
   -ClassPath.getClassPath(ClassPath.SOURCE, data) //watch for null as a result, treat as ClassPath.EMPTY.
   -ClasspathInfo.create(data).getClassPath(...SOURCE)
   -js.getClasspathInfo()
   
   The first is likely to be the fastest in this context.

##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {

Review comment:
       I think this class can be removed, see below. Please note that CompilationInfo is only valid while the task is running, and "exporting" it outside of the task is likely to lead to bad effects.

##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            try {
+                fileManager.close();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+
+        if (publicFirstClassName != null && !publicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(publicFirstClassName, packageName);
+        } else if (nonPublicFirstClassName != null && !nonPublicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(nonPublicFirstClassName, packageName);
+        } else {
+            return null;
+        }
+
+    }
+
+    private String removePackage(String copiedCode, String oldPacageName) {

Review comment:
       I think I would rather record the start and end offset of the package clause in ClassContent, and removed it based on the offsets. Especially given we have access to the AST above, should be easier than trying to setup a regexp.

##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N

Review comment:
       It would be better to use DialogDisplayer.notify(Later) instead of JOptionPane (on all places):
   https://bits.netbeans.org/dev/javadoc/org-openide-dialogs/org/openide/DialogDisplayer.html#notifyLater-org.openide.NotifyDescriptor-
   
   (+something like @NbBundle.Messages("ERR_NotValidClass=Code is not a valid class") and Bundle.ERR_NotValidClass().




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] arusinha commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
arusinha commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r491781294



##########
File path: java/java.project.ui/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java
##########
@@ -721,6 +724,121 @@ public void testCopyPaste () throws Exception {
         }
     }
 
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("import java.util.*;"
+                + "class C{}"
+                + "public class PC{}"
+                + "interface I{}"
+                + "enum En{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "class C{}"
+                    + "public class PC{}"
+                    + "interface I{}"
+                    + "enum En{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_removeExistingPackageName() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-rm-package");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("package copy.paste     ."
+                + "java"
+                + ";"
+                + "import java.util.*;"
+                + "public class PC{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "public class PC{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_CompilationErrorInCode() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-error");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +
+                "\"C:\\Program Files\\NB\\Community Edition 201.3803.71\\lib\\");
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "Hello.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +

Review comment:
       plz avoid hardcoded path
   

##########
File path: java/java.project.ui/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java
##########
@@ -721,6 +724,121 @@ public void testCopyPaste () throws Exception {
         }
     }
 
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("import java.util.*;"
+                + "class C{}"
+                + "public class PC{}"
+                + "interface I{}"
+                + "enum En{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "class C{}"
+                    + "public class PC{}"
+                    + "interface I{}"
+                    + "enum En{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_removeExistingPackageName() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-rm-package");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("package copy.paste     ."
+                + "java"
+                + ";"
+                + "import java.util.*;"
+                + "public class PC{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "public class PC{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_CompilationErrorInCode() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-error");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +

Review comment:
       plz avoid hardcoded path

##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public";
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class");
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java");
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present");
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file");
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            try {
+                fileManager.close();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+
+        if (publicFirstClassName != null && !publicFirstClassName.equals("<error>")) {
+            return new ClassContent(publicFirstClassName, packageName);
+        } else if (nonPublicFirstClassName != null && !nonPublicFirstClassName.equals("<error>")) {
+            return new ClassContent(nonPublicFirstClassName, packageName);
+        } else {
+            return null;
+        }
+
+    }
+
+    private String removePackage(String copiedCode, String oldPacageName) {
+        String packageRegex = oldPacageName.replace(".", "\\s*.\\s*");

Review comment:
       plz use //NOI18N for string literal. need to correct wherever used string literal
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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


[GitHub] [netbeans] singh-akhilesh commented on a change in pull request #2334: [NETBEANS-3986] Create new Class/Interface/Enum when copy-paste raw text

Posted by GitBox <gi...@apache.org>.
singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r501089530



##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public"; //NOI18N
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create class"); //NOI18N
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + classContent.getClassName() + ".java"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); //NOI18N
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if (classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            try {
+                fileManager.close();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+
+        if (publicFirstClassName != null && !publicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(publicFirstClassName, packageName);
+        } else if (nonPublicFirstClassName != null && !nonPublicFirstClassName.equals("<error>")) { //NOI18N
+            return new ClassContent(nonPublicFirstClassName, packageName);
+        } else {
+            return null;
+        }
+
+    }
+
+    private String removePackage(String copiedCode, String oldPacageName) {

Review comment:
       Comment Addressed.
   Getting start and end offset of package using below:
   
   **_JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
   
   SourcePositions sourcePositions = Trees.instance(task).getSourcePositions();
               for (CompilationUnitTree compilationUnitTree : ((JavacTask) task).parse()) {
                   packageStartOffset = sourcePositions.getStartPosition(compilationUnitTree, compilationUnitTree.getPackage());
                   packageEndOffset = sourcePositions.getEndPosition(compilationUnitTree, compilationUnitTree.getPackage());_**




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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