JavaSourceUtil { static public SourcePosition findSourcePosition(File javaFile, int lineNumber, int columnNumber) throws IOException { String[] lines = new String[3]; int currentLineNumber = 1; try (BufferedReader br = new BufferedReader(new FileReader(javaFile))) { for (String line; (line = br.readLine()) != null; ) { lines[0] = lines[1]; lines[1] = lines[2]; lines[2] = line; if (lineNumber == currentLineNumber) { break; } currentLineNumber++; } } if (lineNumber != currentLineNumber) { return null; } SourcePosition pos0 = findSourcePositionInComment(lines[1]); if (pos0 != null) { return pos0; } SourcePosition pos1 = findSourcePositionInComment(lines[0]); return pos1; }  static SourcePosition findSourcePosition(File javaFile, int lineNumber, int columnNumber); static SourcePosition findSourcePositionInComment(String line);  }
@Test public void findSourcePosition() throws URISyntaxException, IOException { URL url = this.getClass().getResource("/generated/Args.java.txt"); File f = new File(url.toURI()); SourcePosition pos; pos = JavaSourceUtil.findSourcePosition(f, 1, 1); Assert.assertNull(pos); pos = JavaSourceUtil.findSourcePosition(f, 24, 13); Assert.assertEquals(4, pos.getLineNumber()); Assert.assertEquals(6, pos.getPosInLine()); pos = JavaSourceUtil.findSourcePosition(f, 90, 36); Assert.assertEquals(5, pos.getLineNumber()); Assert.assertEquals(1, pos.getPosInLine()); }