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/03/16 13:10:38 UTC
[GitHub] [netbeans] lahodaj commented on a change in pull request #2020:
[NETBEANS-3986] Creating New Class On Pasting Class Text
lahodaj commented on a change in pull request #2020: [NETBEANS-3986] Creating New Class On Pasting Class Text
URL: https://github.com/apache/netbeans/pull/2020#discussion_r393007526
##########
File path: java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateClassAndCopy.java
##########
@@ -0,0 +1,172 @@
+/*
+ * 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 java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Set;
+import java.util.StringTokenizer;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.lang.model.SourceVersion;
+import javax.swing.JOptionPane;
+import org.openide.actions.PasteAction;
+import org.openide.awt.ActionID;
+import org.openide.awt.ActionReference;
+import org.openide.awt.ActionRegistration;
+import org.openide.explorer.ExplorerManager;
+import org.openide.filesystems.FileObject;
+import org.openide.loaders.DataFolder;
+import org.openide.nodes.Node;
+import org.openide.util.Exceptions;
+import org.openide.util.NbBundle.Messages;
+
+@ActionID(
+ category = "Build",
+ id = "org.netbeans.spi.java.project.support.ui.CreateClassAndCopy"
+)
+@ActionRegistration(
+ displayName = "#CTL_CreateClassAndCopy"
+)
+@ActionReference(path = "Loaders/folder/any/Actions", position = 750)
+@Messages("CTL_CreateClassAndCopy=PasteClass")
+public final class CreateClassAndCopy implements ActionListener {
+
+ private final DataFolder context;
+
+ public CreateClassAndCopy(DataFolder context) {
+ this.context = context;
+ }
+
+ static ExplorerManager findExplorerManager() {
+ Throwable t = null;
+
+ try {
+ Class c = Class.forName("org.openide.windows.TopComponent"); // NOI18N
+
+ // use reflection now
+ Method m = c.getMethod("getRegistry"); // NOI18N
+ Object o = m.invoke(null);
+
+ c = Class.forName("org.openide.windows.TopComponent$Registry"); // NOI18N
+
+ // use reflection now
+ m = c.getMethod("getActivated"); // NOI18N
+ o = m.invoke(o);
+
+ if (o instanceof ExplorerManager.Provider) {
+ return ((ExplorerManager.Provider) o).getExplorerManager();
+ }
+ } // exceptions from forName:
+ catch (ClassNotFoundException x) {
+ } catch (ExceptionInInitializerError x) {
+ } catch (LinkageError x) {
+ } // exceptions from getMethod:
+ catch (SecurityException x) {
+ t = x;
+ } catch (NoSuchMethodException x) {
+ t = x;
+ } // exceptions from invoke
+ catch (IllegalAccessException x) {
+ t = x;
+ } catch (IllegalArgumentException x) {
+ t = x;
+ } catch (InvocationTargetException x) {
+ t = x;
+ }
+
+ if (t != null) {
+ Logger.getLogger(PasteAction.class.getName()).log(Level.WARNING, null, t);
+ }
+
+ return null;
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent ev) {
+ try {
+ Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+ if(!c.isDataFlavorAvailable(DataFlavor.stringFlavor))return;
+ boolean containsClass = false;
+ String data = (String) c.getData(DataFlavor.stringFlavor);
+ String className = "";
Review comment:
I wonder if it wouldn't be easier to let some (Java) parser parse the class, and analyze the output. Something as simple as:
((JavacTask) javax.tools.ToolProvider.getSystemJavaCompiler().getTask(...)).parse()
might give much better outcomes, and is unlikely to be problematic w.r.t. performance (parse is typically very fast). The only parameter that really needs to be filled in for getTask is the last one, and it may be a singleton list with a very simple JavaFileObject, like this one:
https://github.com/apache/netbeans/blob/5f675e88a3ae3fee652332f9faf2f1985508c272/java/lib.nbjavac/test/unit/src/org/netbeans/lib/nbjavac/services/NBClassWriterTest.java#L61
(Passing the "out" Writer and passing the output to "/dev/null" is probably a good idea, as well, but not needed for the functionality to work.)
As far as I can tell, the code here won't work if:
-there are any imports
-or annotations
-or the class is non-public
-or if the thing that is being pasted is an interface, enum, annotation type (or record)
-or if there's any other modifier for the class (not a big factor right now, as probably only "strictfp" could be here).
This feels unnecessarily limited.
----------------------------------------------------------------
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
With regards,
Apache Git Services
---------------------------------------------------------------------
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