diff --git a/java/debugger.jpda.projects/nbproject/project.xml b/java/debugger.jpda.projects/nbproject/project.xml index d1bacdc7971f..d668a669286b 100644 --- a/java/debugger.jpda.projects/nbproject/project.xml +++ b/java/debugger.jpda.projects/nbproject/project.xml @@ -181,6 +181,10 @@ org.netbeans.modules.classfile + + org.netbeans.modules.debugger.jpda + + org.netbeans.modules.editor.mimelookup diff --git a/java/debugger.jpda.projects/src/org/netbeans/modules/debugger/jpda/projects/IntroduceClass.java b/java/debugger.jpda.projects/src/org/netbeans/modules/debugger/jpda/projects/IntroduceClass.java index 737826ec7f29..7196077abbb4 100644 --- a/java/debugger.jpda.projects/src/org/netbeans/modules/debugger/jpda/projects/IntroduceClass.java +++ b/java/debugger.jpda.projects/src/org/netbeans/modules/debugger/jpda/projects/IntroduceClass.java @@ -94,6 +94,9 @@ boolean computeIntroduceMethod(TreePathHandle h, CompilationInfo info, TreePath String returnType = scanner.getReturnType(); if (returnType == null || !scanner.hasReturns()) { TypeMirror type = scanner.getReturnTypeMirror(); + if (type == null) { + type = info.getTrees().getTypeMirror(block); + } if (type == null) { Element lastStatementElement; if (lastStatement != null && (lastStatementElement = getElement(info, new TreePath(treePath, lastStatement))) != null) { @@ -120,6 +123,9 @@ boolean computeIntroduceMethod(TreePathHandle h, CompilationInfo info, TreePath returnType = type.toString(); // Prepend a return statement: long lsBegin = sourcePositions.getStartPosition(lastStatement); + if (lsBegin < 0) { + lsBegin = 0; + } // Make it relative to the beginning of the code snippet: lsBegin -= codeOffset; StringBuilder code = new StringBuilder(this.snippetCode) diff --git a/java/debugger.jpda.projects/src/org/netbeans/modules/debugger/jpda/projects/PreferredCCParser.java b/java/debugger.jpda.projects/src/org/netbeans/modules/debugger/jpda/projects/PreferredCCParser.java index 57b11b5d0a9f..26f587a06b78 100644 --- a/java/debugger.jpda.projects/src/org/netbeans/modules/debugger/jpda/projects/PreferredCCParser.java +++ b/java/debugger.jpda.projects/src/org/netbeans/modules/debugger/jpda/projects/PreferredCCParser.java @@ -434,15 +434,13 @@ public R interpretOrCompileCode(final Expression expression, //t1 = System.nanoTime(); try { final CompilationController ci = getPreferredCompilationController(fo, js); - //t2 = System.nanoTime(); - ParseExpressionTask task = new ParseExpressionTask<>(code, line, context); - boolean parsed = task.parse(fo, js, ci); - if (!parsed) { + ParseResult parsed = parse(fo, js, ci, code, line, context); + if (parsed == null) { return null; } - treePath = task.getTreePath(); - tree = task.getTree(); - trees = task.getTrees(); + treePath = parsed.treePath(); + tree = parsed.tree(); + trees = parsed.trees(); //t3 = System.nanoTime(); Boolean canIntrpt; if (treePath != null) { @@ -456,7 +454,7 @@ public R interpretOrCompileCode(final Expression expression, if (Boolean.FALSE.equals(canIntrpt)) { // Can not interpret, compile: ClassToInvoke compiledClass = - CodeSnippetCompiler.compileToClass(ci, code, task.getCodeOffset(), + CodeSnippetCompiler.compileToClass(ci, code, parsed.codeOffset(), js, fo, line, treePath, tree, staticContext); LOG.log(Level.FINE, "Compiled to: {0}", compiledClass); @@ -472,14 +470,13 @@ public R interpretOrCompileCode(final Expression expression, } if (success) { // Class is uploaded, interpret the class' method invocation: - task = new ParseExpressionTask<>(compiledClass.methodInvoke, line, context); - parsed = task.parse(fo, js, ci); - if (!parsed) { + parsed = parse(fo, js, ci, compiledClass.methodInvoke, line, context); + if (parsed == null) { return null; } - treePath = task.getTreePath(); - tree = task.getTree(); - trees = task.getTrees(); + treePath = parsed.treePath(); + tree = parsed.tree(); + trees = parsed.trees(); } } // else when compiledClass == null, try to interpret the original anyway... } @@ -520,7 +517,18 @@ public R interpretOrCompileCode(final Expression expression, //System.err.println("PARSE TIMES 1: "+(t2-t1)/1000000+", "+(t3-t2)/1000000+", "+(t4-t3)/1000000+" TOTAL: "+(t4-t1)/1000000+" ms."); return retValue; } - + + ParseResult parse(FileObject fo, JavaSource js, CompilationController ci, String code, int line, D context) throws IOException { + ParseExpressionTask task = new ParseExpressionTask<>(code, line, context); + boolean parsed = task.parse(fo, js, ci); + if (!parsed) { + return null; + } + return new ParseResult(task.getTreePath(), task.getTree(), task.getTrees(), task.getCodeOffset()); + } + + record ParseResult(TreePath treePath, Tree tree, Trees trees, int codeOffset) {} + private static class ParseExpressionTask implements Task { private final int line; diff --git a/java/debugger.jpda.projects/test/unit/src/org/netbeans/modules/debugger/jpda/projects/CodeSnippetCompilerTest.java b/java/debugger.jpda.projects/test/unit/src/org/netbeans/modules/debugger/jpda/projects/CodeSnippetCompilerTest.java index 4efc81839ea6..5e2dddabe1ca 100644 --- a/java/debugger.jpda.projects/test/unit/src/org/netbeans/modules/debugger/jpda/projects/CodeSnippetCompilerTest.java +++ b/java/debugger.jpda.projects/test/unit/src/org/netbeans/modules/debugger/jpda/projects/CodeSnippetCompilerTest.java @@ -19,6 +19,7 @@ package org.netbeans.modules.debugger.jpda.projects; import com.sun.source.tree.StatementTree; +import com.sun.source.tree.Tree; import com.sun.source.util.SourcePositions; import com.sun.source.util.TreePath; import java.io.ByteArrayInputStream; @@ -85,30 +86,44 @@ private static FileObject createFile( } public void testInferResultType() throws Exception { - String code = "package test;\n class A { public void test(java.util.List l) { } }"; - int pos = code.indexOf("{", code.indexOf("{") + 1); + String code = """ + package test; + class A { + public void test(java.util.List l) { + } + } + """; + + int pos = code.indexOf("}"); FileObject java = createFile(root, "test/A.java", code); //NOI18N - JavaSource.forFileObject(java).runUserActionTask((CompilationController cc) -> { + JavaSource js = JavaSource.forFileObject(java); + js.runUserActionTask((CompilationController cc) -> { cc.toPhase(Phase.RESOLVED); - TreePath posPath = cc.getTreeUtilities().pathFor(pos); - for (String watch : new String[] { - "l.stream().map(s -> s.length()).collect(java.util.stream.Collectors.toList());", - "l.stream().map(s -> s.length()).collect(java.util.stream.Collectors.toList())", - "l.stream().map(s -> s.length()).collect(java.util.stream.Collectors.toList()); \t\n", - }) { - StatementTree tree = cc.getTreeUtilities().parseStatement( - watch, - new SourcePositions[1] - ); - cc.getTreeUtilities().attributeTree(tree, cc.getTrees().getScope(posPath)); - TreePath tp = new TreePath(posPath, tree); - ClassToInvoke cti = CodeSnippetCompiler.compileToClass(cc, watch, 0, cc.getJavaSource(), java, -1, tp, tree, false); + int line = (int) cc.getCompilationUnit().getLineMap().getLineNumber(pos); + record TestCase(String snippet, String expectedDescriptor) {} + TestCase[] testCases = new TestCase[] { + new TestCase("l.stream().map(s -> s.length()).collect(java.util.stream.Collectors.toList());", + "(Ljava/util/List;)Ljava/util/List;"), + new TestCase("l.stream().map(s -> s.length()).collect(java.util.stream.Collectors.toList())", + "(Ljava/util/List;)Ljava/util/List;"), + new TestCase("l.stream().map(s -> s.length()).collect(java.util.stream.Collectors.toList()); \t\n", + "(Ljava/util/List;)Ljava/util/List;"), + new TestCase("l.isEmpty() || l.stream().anyMatch(s -> s.isEmpty())", + "(Ljava/util/List;)Z"), + }; + for (TestCase testCase : testCases) { + PreferredCCParser.ParseResult parsed = new PreferredCCParser().parse(java, js, cc, testCase.snippet(), line, new Object() { + public void setTreePath(TreePath tp) {} + }); + Tree tree = parsed.tree(); + TreePath tp = parsed.treePath(); + ClassToInvoke cti = CodeSnippetCompiler.compileToClass(cc, testCase.snippet(), parsed.codeOffset(), cc.getJavaSource(), java, line, tp, tree, false); ClassFile cf = new ClassFile(new ByteArrayInputStream(cti.bytecode)); for (Method m : cf.getMethods()) { if (m.getName().equals("invoke")) { - assertEquals("(Ljava/util/List;)Ljava/util/List;", m.getDescriptor()); + assertEquals(testCase.expectedDescriptor(), m.getDescriptor()); } } }