# HG changeset patch # Parent 60b43521af570d5840d367d3101328ccf5a983a5 diff --git a/java.source/apichanges.xml b/java.source/apichanges.xml --- a/java.source/apichanges.xml +++ b/java.source/apichanges.xml @@ -108,6 +108,19 @@ + + + Find span of the MethodTree#getParameters() parameter list in the source. + + + + + + TreeUtilities has a new findMethodParameterSpan(MethodTree method) method. + + + + TreePathHandle can be created from ElementHandle diff --git a/java.source/nbproject/project.properties b/java.source/nbproject/project.properties --- a/java.source/nbproject/project.properties +++ b/java.source/nbproject/project.properties @@ -46,7 +46,7 @@ javadoc.title=Java Source javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml -spec.version.base=0.80.0 +spec.version.base=0.81.0 test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/javac-api-nb-7.0-b07.jar test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\ ${o.n.core.dir}/lib/boot.jar:\ diff --git a/java.source/src/org/netbeans/api/java/source/TreeUtilities.java b/java.source/src/org/netbeans/api/java/source/TreeUtilities.java --- a/java.source/src/org/netbeans/api/java/source/TreeUtilities.java +++ b/java.source/src/org/netbeans/api/java/source/TreeUtilities.java @@ -69,6 +69,7 @@ import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Types; +import org.netbeans.api.annotations.common.CheckForNull; import org.netbeans.api.annotations.common.NonNull; import org.netbeans.api.java.lexer.JavaTokenId; import org.netbeans.api.java.source.JavaSource.Phase; @@ -644,6 +645,50 @@ return findNameSpan(var.getName().toString(), var); } + /**Find span of the {@link MethodTree#getParameters()} parameter list in the source. + * Returns the position of the opening and closing parentheses of the parameter list + * in the source code that was parsed (ie. {@link CompilationInfo.getText()}, which + * may differ from the positions in the source document if it has been already altered. + * + * @param method method which parameter list should be searched for + * @return the span of the parameter list, or null if cannot be found + * @since 0.81 + */ + public @CheckForNull int[] findMethodParameterSpan(@NonNull MethodTree method) { + JCTree jcTree = (JCTree) method; + int pos = jcTree.pos; + + if (pos < 0) + return null; + + TokenSequence tokenSequence = info.getTokenHierarchy().tokenSequence(JavaTokenId.language()); + tokenSequence.move(pos); + + int startPos = -1; + int endPos = -1; + while(tokenSequence.moveNext()) { + if(tokenSequence.token().id() == JavaTokenId.LPAREN) { + startPos = tokenSequence.offset(); + } + if(tokenSequence.token().id() == JavaTokenId.RPAREN) { + endPos = tokenSequence.offset(); + break; + } + if(tokenSequence.token().id() == JavaTokenId.LBRACE) { + return null; + } + } + + if(startPos == -1 || endPos == -1) { + return null; + } + + return new int[] { + startPos, + endPos + }; + } + /**Find span of the {@link MemberSelectTree#getIdentifier()} identifier in the source. * Returns starting and ending offset of the name in the source code that was parsed * (ie. {@link CompilationInfo.getText()}, which may differ from the positions in the source diff --git a/java.source/test/unit/src/org/netbeans/api/java/source/TreeUtilitiesTest.java b/java.source/test/unit/src/org/netbeans/api/java/source/TreeUtilitiesTest.java --- a/java.source/test/unit/src/org/netbeans/api/java/source/TreeUtilitiesTest.java +++ b/java.source/test/unit/src/org/netbeans/api/java/source/TreeUtilitiesTest.java @@ -307,6 +307,28 @@ assertNull(span); } + public void testFindMethodParameterSpan1() throws Exception { + prepareTest("Test", "package test; public class Test {public void test() {}}"); + + TreePath tp = info.getTreeUtilities().pathFor(77 - 30); + MethodTree ct = (MethodTree) tp.getLeaf(); + + int[] span = info.getTreeUtilities().findMethodParameterSpan(ct); + + assertTrue(Arrays.toString(span), Arrays.equals(span, new int[] {79 - 30, 80 - 30})); + } + + public void testFindMethodParameterSpan2() throws Exception { + prepareTest("Test", "package test; public class Test {public void test(String name) {}}"); + + TreePath tp = info.getTreeUtilities().pathFor(77 - 30); + MethodTree ct = (MethodTree) tp.getLeaf(); + + int[] span = info.getTreeUtilities().findMethodParameterSpan(ct); + + assertTrue(Arrays.toString(span), Arrays.equals(span, new int[] {79 - 30, 91 - 30})); + } + public void testTreePath124760a() throws Exception { prepareTest("Test", "package test; public class Test {public Test(int iii[]){}}");