Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions java/debugger.jpda.projects/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@
<code-name-base>org.netbeans.modules.classfile</code-name-base>
<compile-dependency/>
</test-dependency>
<test-dependency>
<code-name-base>org.netbeans.modules.debugger.jpda</code-name-base>
<compile-dependency/>
</test-dependency>
<test-dependency>
<code-name-base>org.netbeans.modules.editor.mimelookup</code-name-base>
<compile-dependency/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,13 @@ public <R,D> R interpretOrCompileCode(final Expression<Object> expression,
//t1 = System.nanoTime();
try {
final CompilationController ci = getPreferredCompilationController(fo, js);
//t2 = System.nanoTime();
ParseExpressionTask<D> 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) {
Expand All @@ -456,7 +454,7 @@ public <R,D> R interpretOrCompileCode(final Expression<Object> 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);
Expand All @@ -472,14 +470,13 @@ public <R,D> R interpretOrCompileCode(final Expression<Object> 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...
}
Expand Down Expand Up @@ -520,7 +517,18 @@ public <R,D> R interpretOrCompileCode(final Expression<Object> expression,
//System.err.println("PARSE TIMES 1: "+(t2-t1)/1000000+", "+(t3-t2)/1000000+", "+(t4-t3)/1000000+" TOTAL: "+(t4-t1)/1000000+" ms.");
return retValue;
}


<D> ParseResult parse(FileObject fo, JavaSource js, CompilationController ci, String code, int line, D context) throws IOException {
ParseExpressionTask<D> 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<D> implements Task<CompilationController> {

private final int line;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> l) { } }";
int pos = code.indexOf("{", code.indexOf("{") + 1);
String code = """
package test;
class A {
public void test(java.util.List<String> 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());
}
}
}
Expand Down
Loading