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/10/06 07:19:27 UTC

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

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