Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 515337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:37:04+00:00 2026-05-13T07:37:04+00:00

I have a line number of a Java source file and want to get

  • 0

I have a line number of a Java source file and want to get the sourounding method for that line number programatically.

I looked into ANTLR which didn’t help me much.

Janino (http://www.janino.net) seems promising, I would scan and parse (and if necessary compile) the code. Then I could use JDI and

ReferenceType.locationsOfLine(int lineNumber)

Still I don’t know how to use JDI for doing this and didn’t find a tutorial that goes anywhere in this direction.

Maybe there is some other way that I am completely missing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:37:04+00:00Added an answer on May 13, 2026 at 7:37 am

    If you’re using Java 6, and if you don’t mind using Sun’s APIs, then you can use the javac API. You’ll need to add tools.jar to your classpath.

    import java.io.IOException;
    import javax.tools.DiagnosticCollector;
    import javax.tools.JavaCompiler;
    import javax.tools.JavaFileObject;
    import javax.tools.StandardJavaFileManager;
    import javax.tools.ToolProvider;
    import javax.tools.JavaCompiler.CompilationTask;
    import com.sun.source.tree.CompilationUnitTree;
    import com.sun.source.tree.LineMap;
    import com.sun.source.tree.MethodTree;
    import com.sun.source.util.JavacTask;
    import com.sun.source.util.SourcePositions;
    import com.sun.source.util.TreeScanner;
    import com.sun.source.util.Trees;
    
    public class MethodFinder {
    
        public static void main(String[] args) {
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<JavaFileObject>();
            StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticsCollector, null, null);
            Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjects("path/to/Source.java");
            CompilationTask task = compiler.getTask(null, fileManager, diagnosticsCollector, null, null, fileObjects);
    
            // Here we switch to Sun-specific APIs
            JavacTask javacTask = (JavacTask) task;
            SourcePositions sourcePositions = Trees.instance(javacTask).getSourcePositions();
            Iterable<? extends CompilationUnitTree> parseResult = null;
            try {
                parseResult = javacTask.parse();
            } catch (IOException e) {
    
                // Parsing failed
                e.printStackTrace();
                System.exit(0);
            }
            for (CompilationUnitTree compilationUnitTree : parseResult) {
                compilationUnitTree.accept(new MethodLineLogger(compilationUnitTree, sourcePositions), null);
            }
        }
    
        private static class MethodLineLogger extends TreeScanner<Void, Void> {
            private final CompilationUnitTree compilationUnitTree;
            private final SourcePositions sourcePositions;
            private final LineMap lineMap;
    
            private MethodLineLogger(CompilationUnitTree compilationUnitTree, SourcePositions sourcePositions) {
                this.compilationUnitTree = compilationUnitTree;
                this.sourcePositions = sourcePositions;
                this.lineMap = compilationUnitTree.getLineMap();
            }
    
            @Override
            public Void visitMethod(MethodTree arg0, Void arg1) {
                long startPosition = sourcePositions.getStartPosition(compilationUnitTree, arg0);
                long startLine = lineMap.getLineNumber(startPosition);
                long endPosition = sourcePositions.getEndPosition(compilationUnitTree, arg0);
                long endLine = lineMap.getLineNumber(endPosition);
    
                // Voila!
                System.out.println("Found method " + arg0.getName() + " from line " + startLine + " to line "  + endLine + ".");
    
                return super.visitMethod(arg0, arg1);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a line number of a particular class source code (Java/C#) - is there
I have a Java project which will include a number of large SQL statements
I am trying to create a simple 3-D app for android that will have
UPDATE Some answers so far have suggested using an adjacency list. How would an
I recently started using Eclipse at work for my Java servlet projects. I've been
I was wandering if it's possible to open a source code in text editor
I recently started reading Paul Grahams 'On Lisp', and learning learning clojure along with
I've finally managed to create a Netbeans project out of an old standalone (not
I'm implementing a face tracker on Android, and as a literature study, would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.