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

  • SEARCH
  • Home
  • 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

I have a macro that passes the line number and file name to an
I have found that call stack helps in finding the line number of a
I have number of line breaks in a string. But I only want it
I have created a line chart using Reporting Services that charts the number of
Say I have two files where there is one number per line File 1
I have a console application that uses a number of command line switches to
I have a file where each line starts with a number. The user can
I have try this code on DatabaseHelper.java Line Number 34 public DatabaseHelper(Context context) throws
I have a (java) program that prints a line of hex numbers to stdout
Given a line number of a particular class source code (Java/C#) - is there

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.