# HG changeset patch # Parent e3144f6c9d2bb7e9aa1d4be1fdf2d2e826a4c7d5 diff --git a/beans/nbproject/project.xml b/beans/nbproject/project.xml --- a/beans/nbproject/project.xml +++ b/beans/nbproject/project.xml @@ -159,7 +159,7 @@ - 0.123 + 0.126 diff --git a/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyCodeGenerator.java b/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyCodeGenerator.java --- a/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyCodeGenerator.java +++ b/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyCodeGenerator.java @@ -203,7 +203,7 @@ } if(enumconstantEnd == -1) { - enumconstantEnd = (int) trees.getSourcePositions().getStartPosition(cut, clazz); + enumconstantEnd = treeUtils.findBodySpan(clazz)[0] + 1; } if(otherStart == -1) { 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,22 @@ + + + Added utility method to find span of a ClassTree's body in the source. + + + + + + Added utility method to find span of a ClassTree's body in the source. + Returns starting and ending offset of the body in the source code that was parsed + (ie. CompilationInfo.getText()), which may differ from the positions in the source + document if it has been already altered. + + + + Added support for DocTrees. 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.125.0 +spec.version.base=0.126.0 test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/nb-javac-api.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 @@ -707,6 +707,44 @@ return set; } + /**Find span of the {@link ClassTree}'s body in the source. + * Returns starting and ending offset of the body 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 clazz class which body should be searched for + * @return the span of the body, or null if cannot be found + * @since 0.126 + */ + public int[] findBodySpan(ClassTree clazz) { + JCTree jcTree = (JCTree) clazz; + int pos = jcTree.pos; + + if (pos < 0) + return null; + + TokenSequence tokenSequence = info.getTokenHierarchy().tokenSequence(JavaTokenId.language()); + tokenSequence.move(pos); + + int startPos = -1; + int endPos = (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), clazz); + while(tokenSequence.moveNext()) { + if(tokenSequence.token().id() == JavaTokenId.LBRACE) { + startPos = tokenSequence.offset(); + break; + } + } + + if(startPos == -1 || endPos == -1) { + return null; + } + + return new int[] { + startPos, + endPos + }; + } + /**Find span of the {@link ClassTree#getSimpleName()} 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 @@ -313,6 +313,17 @@ assertNull(span); } + public void testFindClassBodySpan1() throws Exception { + prepareTest("Test", "package test; public class Test {public void test() {}}"); + + TreePath tp = info.getTreeUtilities().pathFor(59 - 30); + ClassTree ct = (ClassTree) tp.getLeaf(); + + int[] span = info.getTreeUtilities().findBodySpan(ct); + + assertTrue(Arrays.toString(span), Arrays.equals(span, new int[] {62 - 30, 85 - 30})); + } + public void testFindMethodParameterSpan1() throws Exception { prepareTest("Test", "package test; public class Test {public void test() {}}");