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 9236071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:12:04+00:00 2026-06-18T07:12:04+00:00

I have selected a piece of code and I want to parse with AST

  • 0

I have selected a piece of code and I want to parse with AST with the purpose of identify methods in the selected code.

public void run(IAction action) {
    SelectedText selectedText;      
    IEditorPart editor = getActiveEditor();

    if (editor instanceof AbstractTextEditor) {
        selectedText = getSelectedText(editor);
        creteAST(selectedText);
    }           
}

private void creteAST(SelectedText selectedText) {
    CompilationUnit parse = parse(selectedText);
    MethodVisitor visitor = new MethodVisitor();
    parse.accept(visitor);

    System.out.println("Printing methods from the selected code");
    for (MethodDeclaration method : visitor.getMethods()) {
        System.out.println("Method name: " + method.getName()+ ". Return type: " + method.getReturnType2());
        System.out.println(method);
    }
}

private static CompilationUnit parse(SelectedText selectedText) {
    String s_text = selectedText.getSelectedText();
    char[] c_text = s_text.toCharArray();
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(c_text);
    parser.setResolveBindings(true);
    return (CompilationUnit) parser.createAST(null);
  }

As you can see, I have to change the type from SelectedText to char[] before parsing.

I am doing something wrong because the parser does not find any method.

What I am doing wrong?

  • 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-06-18T07:12:05+00:00Added an answer on June 18, 2026 at 7:12 am

    javadoc for ASTParser.setResolveBindings() says:

    Binding information is obtained from the Java model. This means that
    the compilation unit must be located relative to the Java model. This
    happens automatically when the source code comes from either
    setSource(ICompilationUnit) or setSource(IClassFile). When source is
    supplied by setSource(char[]), the location must be extablished
    explicitly by calling setProject(IJavaProject) and
    setUnitName(String). Note that the compiler options that affect doc
    comment checking may also affect whether any bindings are resolved for
    nodes within doc comments.

    IJavaProject javaProject = ...obtain it somehow, possibly through getAdapter() on editor?
    String name = ...from editor?...
    
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(c_text);
    parser.setResolveBindings(true);
    parser.setProject(javaProject);
    parser.setUnitName(name);
    return (CompilationUnit) parser.createAST(null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code: listTable.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) {
I have a piece of code that removes and add class 'selected' when the
I have this piece of code: foreach (ListItem item in lbUnassigned.Items) { if (item.Selected)
I have this piece of code $('ul.fonts li', '#wizard').live('click', function(e){ $('ul.fonts li', '#wizard').removeClass('selected'); $(this).addClass('selected');
I have a piece of code like below: var selected = A; bool isSelected
I have selected text by visual mode and pressed :. I run the following
After I have selected a range containing numerical values, I want to, via VBA,
I want to make it so the item I have selected in the list
I want the user to click the select box and when have selected any
Hi Below I have piece of code where it displays a message stored in

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.