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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:22:02+00:00 2026-05-13T20:22:02+00:00

i have a variable of type VariableDeclarationStatement. I want to get the name of

  • 0

i have a variable of type VariableDeclarationStatement. I want to get the name of the method from this variable . How can i do that?Help

  • 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-13T20:22:03+00:00Added an answer on May 13, 2026 at 8:22 pm

    You could use the code in this thread and use an VariableDeclarationFragment:

        public boolean visit(VariableDeclarationStatement node)
        {
            System.out.println("Visiting variable declaration statement.");
            for(int i = 0; i < node.fragments().size(); ++i)
            {
                VariableDeclarationFragment frag = (VariableDeclarationFragment)node.fragments().get(i);
                System.out.println("Fragment: " + node.getType() + " " + frag.getName());
            }
    
            System.out.println("");        
            return true;
        }
    

    To get the method in which this (variable) is defined, I would use a visitor of the CompilationUnit, looking for that VariableDeclarationFragment while memorizing the IMethod I am currently parsing:

            IJavaElement element = delta.getElement();
    
            if(element.getElementType() != IJavaElement.COMPILATION_UNIT)
                return;
    
            ICompilationUnit compilationUnit = (ICompilationUnit)element;
    
            try
            {
    
                IType type = compilationUnit.findPrimaryType();
                IMethod[] methods = type.getMethods();
                for(IMethod method : methods)
                {                
                    ASTParser parser = ASTParser.newParser(AST.JLS3);                
                    parser.setSource(compilationUnit);
                    parser.setSourceRange(method.getSourceRange().getOffset(), method.getSourceRange().getLength());
                    //parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
                    //parser.setSource(method.getSource().toCharArray());
                    //parser.setProject(method.getJavaProject());                
                    parser.setResolveBindings(true);
                    CompilationUnit cu = (CompilationUnit)parser.createAST(null);
                    cu.accept(new ASTMethodVisitor());
    
                    // If the visitor visit the right VariableDeclarationFragment,
                    // then the right IMethod is the current 'method' variable
    
                }                        
            }
            catch(JavaModelException e)
            {         
                e.printStackTrace();
            }         
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable which is datetime type. How can i get the shortdatetostring()
I have a Type variable t passed into a method, and I want to
I have a variable of type Hashmap <String,Integer >. In this, the Integer value
I have a variable of type size_t , and I want to print it
Suppose I have a variable of type Int = 08, how can I convert
I have a reference-type variable that is readonly , because the reference never change,
I need your help, For example I have a decimal type variable and I
I have a variable of type List<RelationHeader> . Now I want to copy all
I have a variable of type Dictionary<MyType, List<MyOtherType>> I want to convert it to
I have a stupid question. I got this variable: var type:int; and this function:

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.