You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@netbeans.apache.org by GitBox <gi...@apache.org> on 2017/12/05 07:20:11 UTC

[GitHub] sdedic closed pull request #292: Two fixes for the "Create Subclass" suggestion

sdedic closed pull request #292: Two fixes for the "Create Subclass" suggestion
URL: https://github.com/apache/incubator-netbeans/pull/292
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/java.hints/src/org/netbeans/modules/java/hints/suggestions/CreateSubclass.java b/java.hints/src/org/netbeans/modules/java/hints/suggestions/CreateSubclass.java
index 53669b443..1b4ec422d 100644
--- a/java.hints/src/org/netbeans/modules/java/hints/suggestions/CreateSubclass.java
+++ b/java.hints/src/org/netbeans/modules/java/hints/suggestions/CreateSubclass.java
@@ -26,6 +26,7 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.concurrent.Callable;
 
 import javax.lang.model.element.Element;
 import javax.lang.model.element.ElementKind;
@@ -33,9 +34,11 @@
 import javax.lang.model.element.Modifier;
 import javax.lang.model.element.PackageElement;
 import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.TypeParameterElement;
 import javax.lang.model.type.TypeMirror;
 import javax.lang.model.util.ElementFilter;
 import javax.swing.text.JTextComponent;
+import javax.tools.Diagnostic;
 
 import com.sun.source.tree.ClassTree;
 import com.sun.source.tree.CompilationUnitTree;
@@ -44,8 +47,6 @@
 import com.sun.source.tree.TypeParameterTree;
 import com.sun.source.util.SourcePositions;
 import com.sun.source.util.TreePath;
-import java.util.concurrent.Callable;
-import javax.lang.model.element.TypeParameterElement;
 
 import org.netbeans.api.editor.EditorRegistry;
 import org.netbeans.api.java.classpath.ClassPath;
@@ -97,17 +98,36 @@ public static ErrorDescription check(HintContext context) {
         ClassTree cls = (ClassTree) tp.getLeaf();
         CompilationInfo info = context.getInfo();
         SourcePositions sourcePositions = info.getTrees().getSourcePositions();
-        int startPos = (int) sourcePositions.getStartPosition(tp.getCompilationUnit(), cls);
+        long startPos = sourcePositions.getStartPosition(tp.getCompilationUnit(), cls);
+        if (startPos > Integer.MAX_VALUE) {
+            return null;
+        }
+        int[] bodySpan = info.getTreeUtilities().findBodySpan(cls);
+        if (bodySpan == null || bodySpan[0] <= startPos) {
+            return null;
+        }
         int caret = context.getCaretLocation();
-        String code = context.getInfo().getText();
-        if (startPos < 0 || caret < 0 || caret < startPos || caret >= code.length()) {
+        if (startPos < 0 || caret < 0 || caret < startPos || caret >= bodySpan[0]) {
             return null;
         }
 
-        String headerText = code.substring(startPos, caret);
-        int idx = headerText.indexOf('{'); //NOI18N
-        if (idx >= 0) {
-            return null;
+        // #222487
+        // If there is a compile-time error on the class, then don't offer to
+        // create a subclass.
+        List<Diagnostic> errors = info.getDiagnostics();
+        if (!errors.isEmpty()) {
+            for (Diagnostic d : errors) {
+                if (d.getKind() != Diagnostic.Kind.ERROR) {
+                    continue;
+                }
+                // Check that the error's start position is within the class header
+                // Note: d.getEndPosition() is not used because, for example,
+                // a "compiler.err.does.not.override.abstract" error ends at
+                // the end of the class tree.
+                if (startPos <= d.getStartPosition() && d.getStartPosition() <= bodySpan[0]) {
+                    return null;
+                }
+            }
         }
 
         TypeElement typeElement = (TypeElement) info.getTrees().getElement(tp);
diff --git a/java.hints/test/unit/src/org/netbeans/modules/java/hints/suggestions/CreateSubclassTest.java b/java.hints/test/unit/src/org/netbeans/modules/java/hints/suggestions/CreateSubclassTest.java
index a6eac3770..5538c084a 100644
--- a/java.hints/test/unit/src/org/netbeans/modules/java/hints/suggestions/CreateSubclassTest.java
+++ b/java.hints/test/unit/src/org/netbeans/modules/java/hints/suggestions/CreateSubclassTest.java
@@ -73,5 +73,73 @@ public void testTypeParams226791b() throws Exception {
                           "package test;\n" +
                           "public class NewTest<F, S extends CharSequence> extends Test<F, S> {}\n");
     }
-    
+
+    @Test
+    public void testNotConfusedByCommentedLBRACE() throws Exception {
+        CreateSubclass.overrideNameAndPackage = new String[] {
+            "NewTest",
+            "test"
+        };
+        HintTest test = HintTest.create();
+        try (OutputStream out = FileUtil.createData(FileUtil.getConfigRoot(), "Templates/Classes/Class.java").getOutputStream()) {
+            out.write("public class New {}\n".getBytes("UTF-8"));
+        }
+        test.setCaretMarker('^')
+            .input("package test;\n" +
+                   "public/*{*/ class Te^st {}\n")
+            .run(CreateSubclass.class)
+            .findWarning("1:20-1:20:hint:ERR_CreateSubclass")
+            .applyFix()
+            .assertCompilable("test/NewTest.java")
+            .assertOutput("test/NewTest.java",
+                          "package test;\n" +
+                          "public class NewTest extends Test {}\n");
+    }
+
+    @Test
+    public void testDontSuggestIfNonCompilable222487() throws Exception {
+        HintTest.create()
+            .setCaretMarker('^')
+            .input("package test;\n" +
+                   "import base.Base;\n" +
+                   "public class Te^st extends Base {}\n",
+                   false)
+            .input("base/Base.java",
+                   "package base;\n" +
+                   "public class Base {\n" +
+                   "    private Base() {\n" +
+                   "    }\n" +
+                   "}\n")
+            .run(CreateSubclass.class)
+            .assertWarnings();
+    }
+
+    @Test
+    public void testDontSuggestIfNonCompilable222487b() throws Exception {
+        HintTest.create()
+            .setCaretMarker('^')
+            .input("package test;\n" +
+                   "import base.Base;\n" +
+                   "public class Te^st extends Base {}\n",
+                   false)
+            .input("base/Base.java",
+                   "package base;\n" +
+                   "public class Base {\n" +
+                   "    protected Base(String value) {\n" +
+                   "    }\n" +
+                   "}\n")
+            .run(CreateSubclass.class)
+            .assertWarnings();
+    }
+
+    @Test
+    public void testDontSuggestIfNonCompilable222487c() throws Exception {
+        HintTest.create()
+            .setCaretMarker('^')
+            .input("package test;\n" +
+                   "public class Te^st implements Runnable {}\n",
+                   false)
+            .run(CreateSubclass.class)
+            .assertWarnings();
+    }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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