diff --git a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/debug/BreakpointLocationTests.groovy b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/debug/BreakpointLocationTests.groovy index e78dd08391..9be22d0632 100644 --- a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/debug/BreakpointLocationTests.groovy +++ b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/debug/BreakpointLocationTests.groovy @@ -1,11 +1,11 @@ /* - * Copyright 2009-2019 the original author or authors. + * Copyright 2009-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -30,7 +30,7 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { private ASTNode findBreakpointLocation(String target, CharSequence source, String sourcePackage = '') { GroovyCompilationUnit unit = addGroovySource(source, nextUnitName(), sourcePackage) - Document document = new Document(String.valueOf(unit.contents)) + def document = new Document(String.valueOf(unit.contents)) int offset = document.get().indexOf(target) int line = document.getLineOfOffset(offset) @@ -43,9 +43,9 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript1() { def node = findBreakpointLocation 'comment', '''\ - // some comment text - def t = [ x:1, y:2 ] - '''.stripIndent() + |// some comment text + |def t = [ x:1, y:2 ] + |'''.stripMargin() assert node?.lineNumber == 2 } @@ -53,9 +53,9 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript2() { def node = findBreakpointLocation 'def t', '''\ - // some comment text - def t = [ x:1, y:2 ] - '''.stripIndent() + |// some comment text + |def t = [ x:1, y:2 ] + |'''.stripMargin() assert node?.lineNumber == 2 } @@ -63,9 +63,9 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript3() { def node = findBreakpointLocation 't.getX', '''\ - def t = [ x:1, y:2 ] - t.getX() - '''.stripIndent() + |def t = [ x:1, y:2 ] + |t.getX() + |'''.stripMargin() assert node?.lineNumber == 2 } @@ -73,12 +73,12 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript4() { def node = findBreakpointLocation 'it.x', '''\ - def shiftTriangle = { it -> - it.x += 1 - it.y += 1 - it.getX() - } - '''.stripIndent() + |def shiftTriangle = { it -> + | it.x += 1 + | it.y += 1 + | it.getX() + |} + |'''.stripMargin() assert node?.lineNumber == 2 } @@ -86,12 +86,12 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript5() { def node = findBreakpointLocation 'it.y', '''\ - def shiftTriangle = { it -> - it.x += 1 - it.y += 1 - it.getX() - } - '''.stripIndent() + |def shiftTriangle = { it -> + | it.x += 1 + | it.y += 1 + | it.getX() + |} + |'''.stripMargin() assert node?.lineNumber == 3 } @@ -99,12 +99,12 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript6() { def node = findBreakpointLocation 'it.getX', '''\ - def shiftTriangle = { it -> - it.x += 1 - it.y += 1 - it.getX() - } - '''.stripIndent() + |def shiftTriangle = { it -> + | it.x += 1 + | it.y += 1 + | it.getX() + |} + |'''.stripMargin() assert node?.lineNumber == 4 } @@ -112,11 +112,11 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript7() { def node = findBreakpointLocation 'y:', '''\ - def t - t = [ x: 1, - y: 2, - z: 4 ] - '''.stripIndent() + |def t + |t = [ x: 1, + | y: 2, + | z: 4 ] + |'''.stripMargin() assert node?.lineNumber == 3 } @@ -124,11 +124,11 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript8() { def node = findBreakpointLocation 'z:', '''\ - def t - t = [ x: 1, - y: 2, - z: 4 ] - '''.stripIndent() + |def t + |t = [ x: 1, + | y: 2, + | z: 4 ] + |'''.stripMargin() assert node?.lineNumber == 4 } @@ -136,11 +136,11 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript9() { def node = findBreakpointLocation '1', '''\ - def t - t = [ 1, - 2, - 3] - '''.stripIndent() + |def t + |t = [ 1, + | 2, + | 3] + |'''.stripMargin() assert node?.lineNumber == 2 } @@ -148,11 +148,11 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript10() { def node = findBreakpointLocation '2', '''\ - def t - t = [ 1, - 2, - 3] - '''.stripIndent() + |def t + |t = [ 1, + | 2, + | 3] + |'''.stripMargin() assert node?.lineNumber == 3 } @@ -160,11 +160,11 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript11() { def node = findBreakpointLocation '3', '''\ - def t - t = [ 1, - 2, - 3] - '''.stripIndent() + |def t + |t = [ 1, + | 2, + | 3] + |'''.stripMargin() assert node?.lineNumber == 4 } @@ -172,9 +172,9 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript12() { def node = findBreakpointLocation '[]', '''\ - def t - t = []; - '''.stripIndent() + |def t + |t = []; + |'''.stripMargin() assert node?.lineNumber == 2 } @@ -182,10 +182,10 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript13() { def node = findBreakpointLocation 'def x', '''\ - def x() { - print "Hi" - } - '''.stripIndent() + |def x() { + | print "Hi" + |} + |'''.stripMargin() assert node?.lineNumber == 1 } @@ -193,10 +193,10 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript14() { def node = findBreakpointLocation 'print', '''\ - def x() { - print "Hi" - } - '''.stripIndent() + |def x() { + | print "Hi" + |} + |'''.stripMargin() assert node?.lineNumber == 2 } @@ -204,8 +204,8 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInScript15() { def node = findBreakpointLocation 'print', '''\ - def p = { g -> print g } - '''.stripIndent() + |def p = { g -> print g } + |'''.stripMargin() assert node?.lineNumber == 1 } @@ -214,13 +214,13 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass1() { - def node = findBreakpointLocation 'Class', '''\ - class Class { - Class() { - super() - } - } - '''.stripIndent() + def node = findBreakpointLocation 'C', '''\ + |class C { + | C() { + | super() + | } + |} + |'''.stripMargin() assert node instanceof ClassNode assert node.lineNumber == 1 @@ -228,13 +228,13 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass2() { - def node = findBreakpointLocation 'Class()', '''\ - class Class { - Class() { - super() - } - } - '''.stripIndent() + def node = findBreakpointLocation 'C()', '''\ + |class C { + | C() { + | super() + | } + |} + |'''.stripMargin() assert node?.lineNumber == 2 } @@ -242,12 +242,12 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass3() { def node = findBreakpointLocation 'super()', '''\ - class Class { - Class() { - super() - } - } - '''.stripIndent() + |class C { + | C() { + | super() + | } + |} + |'''.stripMargin() assert node?.lineNumber == 3 } @@ -255,12 +255,12 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass4() { def node = findBreakpointLocation '}', '''\ - class Class { - Class() { - super() - } - } - '''.stripIndent() + |class C { + | C() { + | super() + | } + |} + |'''.stripMargin() assert node == null } @@ -268,12 +268,12 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass5() { def node = findBreakpointLocation 'here()', '''\ - class Class { - static { - here() - } - } - '''.stripIndent() + |class C { + | static { + | here() + | } + |} + |'''.stripMargin() assert node?.lineNumber == 3 } @@ -281,12 +281,12 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass6() { def node = findBreakpointLocation 'm()', '''\ - class Class { - def m() { - here() - } - } - '''.stripIndent() + |class C { + | void m() { + | here() + | } + |} + |'''.stripMargin() assert node?.lineNumber == 2 } @@ -294,12 +294,12 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass7() { def node = findBreakpointLocation 'here()', '''\ - class Class { - def m() { - here() - } - } - '''.stripIndent() + |class C { + | void m() { + | here() + | } + |} + |'''.stripMargin() assert node?.lineNumber == 3 } @@ -307,10 +307,10 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test // TODO: Should line breakpoint be created for closure body instead of watchpoint for field? void testBreakpointInClass8() { def node = findBreakpointLocation 'here()', '''\ - class Class { - def t = { here() } - } - '''.stripIndent() + |class C { + | def t = { here() } + |} + |'''.stripMargin() assert node instanceof FieldNode assert node.lineNumber == 2 @@ -319,10 +319,10 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass9() { def node = findBreakpointLocation 'property', '''\ - class Class { - String property - } - '''.stripIndent() + |class C { + | String property + |} + |'''.stripMargin() assert node instanceof FieldNode assert node.lineNumber == 2 @@ -331,11 +331,11 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass9a() { def node = findBreakpointLocation 'field', '''\ - import groovy.transform.PackageScope - class Class { - @PackageScope String field - } - '''.stripIndent() + |import groovy.transform.PackageScope + |class C { + | @PackageScope String field + |} + |'''.stripMargin() assert node instanceof FieldNode assert node.lineNumber == 3 @@ -344,15 +344,15 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass10() { def node = findBreakpointLocation '*', '''\ - /** - * - */ - class Class { - Class() { - super() - } - } - '''.stripIndent() + |/** + | * + | */ + |class C { + | C() { + | super() + | } + |} + |'''.stripMargin() assert node instanceof ClassNode assert node.lineNumber == 4 @@ -361,15 +361,15 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass11() { def node = findBreakpointLocation '*', '''\ - class Class { - /** - * - */ - Class() { - super() - } - } - '''.stripIndent() + |class C { + | /** + | * + | */ + | C() { + | super() + | } + |} + |'''.stripMargin() assert node instanceof ConstructorNode assert node.lineNumber == 5 @@ -378,15 +378,15 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass12() { def node = findBreakpointLocation '*', '''\ - class Class { - /** - * - */ - def m() { - here() - } - } - '''.stripIndent() + |class C { + | /** + | * + | */ + | void m() { + | here() + | } + |} + |'''.stripMargin() assert node instanceof MethodNode assert node.lineNumber == 5 @@ -394,14 +394,14 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test void testBreakpointInClass13() { - def node = findBreakpointLocation 'def m', '''\ - class Class { - @Deprecated - def m() { - here() - } - } - '''.stripIndent() + def node = findBreakpointLocation 'void m', '''\ + |class C { + | @Deprecated + | void m() { + | here() + | } + |} + |'''.stripMargin() assert node instanceof MethodNode } @@ -409,18 +409,39 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite { @Test // https://github.com/groovy/groovy-eclipse/issues/790 void testBreakpointInClass14() { def node = findBreakpointLocation 'println', '''\ - class One { - void meth() { - println 'blah blah' - } - } - class Two { - final Map map - Two(Map map = Collections.EMPTY_MAP) { - this.map = map - } - } - '''.stripIndent() + |class C { + | void m() { + | println 'blah blah' + | } + |} + |class D { + | final Map map + | D(Map map = Collections.EMPTY_MAP) { + | this.map = map + | } + |} + |'''.stripMargin() + + assert node.lineNumber == 3 + } + + @Test // https://github.com/groovy/groovy-eclipse/issues/1154 + void testBreakpointInClass15() { + addGroovySource '''\ + |public @interface A { + | Class value() + |} + |'''.stripMargin(), 'A' + + def node = findBreakpointLocation 'println', '''\ + |class C { + | @A({ + | println 'blah blah' + | }) + | void m() { + | } + |} + |'''.stripMargin() assert node.lineNumber == 3 } diff --git a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/debug/ui/BreakpointLocationFinder.java b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/debug/ui/BreakpointLocationFinder.java index a1afe1b404..3864737d12 100644 --- a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/debug/ui/BreakpointLocationFinder.java +++ b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/debug/ui/BreakpointLocationFinder.java @@ -1,11 +1,11 @@ /* - * Copyright 2009-2019 the original author or authors. + * Copyright 2009-2020 the original author or authors. * * Licensed 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 + * https://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, @@ -38,17 +38,23 @@ public class BreakpointLocationFinder { protected final LocationSupport locator; protected final Iterable nodes; - public BreakpointLocationFinder(ModuleNode module) { - TreeSet nodes = new TreeSet<>(Comparator.comparing(ASTNode::getLineNumber).thenComparing(ASTNode::getColumnNumber) + public BreakpointLocationFinder(final ModuleNode module) { + locator = module.getNodeMetaData(LocationSupport.class); + TreeSet nodes = new TreeSet<>(Comparator.comparing(this::lineNumber).thenComparing(ASTNode::getColumnNumber) .thenComparing(Comparator.comparing(ASTNode::getLastLineNumber).thenComparing(ASTNode::getLastColumnNumber).reversed())); new DepthFirstVisitor() { @Override - protected void visitAnnotation(AnnotationNode annotation) { + protected void visitAnnotation(final AnnotationNode annotation) { + for (Expression expression : annotation.getMembers().values()) { + if (expression instanceof ClosureExpression) { + expression.visit(this); + } + } } @Override - protected void visitExpression(Expression expression) { + protected void visitExpression(final Expression expression) { if (expression.getLineNumber() > 0 && !(expression instanceof ClosureExpression || expression instanceof TupleExpression)) { nodes.add(expression); @@ -57,7 +63,7 @@ protected void visitExpression(Expression expression) { } @Override - public void visitMethod(MethodNode node) { + public void visitMethod(final MethodNode node) { if (node.getLineNumber() > 0) { nodes.add(node); } @@ -65,7 +71,7 @@ public void visitMethod(MethodNode node) { } @Override - public void visitField(FieldNode node) { + public void visitField(final FieldNode node) { if (node.getLineNumber() > 0) { nodes.add(node); } @@ -73,7 +79,7 @@ public void visitField(FieldNode node) { } @Override - public void visitClass(ClassNode node) { + public void visitClass(final ClassNode node) { if (node.getLineNumber() > 0) { nodes.add(node); } @@ -82,10 +88,9 @@ public void visitClass(ClassNode node) { }.visitModule(module); this.nodes = Collections.unmodifiableSet(nodes); - this.locator = module.getNodeMetaData(LocationSupport.class); } - public ASTNode findBreakpointLocation(int lineNumber) { + public ASTNode findBreakpointLocation(final int lineNumber) { ASTNode bestMatch = null; boolean skipNext = false; for (ASTNode node : nodes) { @@ -105,7 +110,7 @@ public ASTNode findBreakpointLocation(int lineNumber) { return bestMatch; } - protected int lineNumber(ASTNode node) { + protected int lineNumber(final ASTNode node) { if (locator != null && (node instanceof AnnotatedNode && !(node instanceof Expression))) { // annotations, modifiers and generics may be on separate line(s) int[] row_col = locator.getRowCol(((AnnotatedNode) node).getNameStart());