Community
Participate
Working Groups
I am trying to use AST.lineNumber() to return the line number. However, when the class didn't have a newline at the end of file, the AST parser seems not working and lineNumber() always return 1. ie. public class NO_WORKING { public void method() { } } <--- no new line, "}" is the last character -------------------------- public class WORKING { public void method() { } } <--- new line here ------------------------------
I could not reproduce with latest. Could you please let me know what code you used and which build? I tried the following code: assertTrue("not a compilation unit", result.getNodeType() == ASTNode.COMPILATION_UNIT); //$NON-NLS-1$ CompilationUnit compilationUnit = (CompilationUnit) result; ASTNode node = getASTNode(compilationUnit, 0, 0); assertNotNull("No node", node); assertTrue("not a method declaration", node.getNodeType() == ASTNode.METHOD_DECLARATION); //$NON-NLS-1$ MethodDeclaration methodDeclaration = (MethodDeclaration) node; SimpleName name = methodDeclaration.getName(); assertEquals("wrong line number", 3, compilationUnit.lineNumber(name.getStartPosition())); with your first test case (the one without any line break at the end). I got the proper answer. I tried with the binding resolution, it worked as well. I need more information in order to reproduce. Do you have any errors in your code?
Add CC.
Without further information, I will close it as WORKSFORME.
Close as WORKSFORME. Please reopen if you have more information or better steps to reproduce.