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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:29:34+00:00 2026-06-15T01:29:34+00:00

I am writing a java code analyzing snippet which will find out the use

  • 0

I am writing a java code analyzing snippet which will find out the use of variables in a method. (to be specific how many times a global class variable is read and written in a method). Can this be done using JavaParser? Would anyone have any other recommendations? Does any one know how class metrics are calculated? They probably deal with similar things.

  • 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-15T01:29:36+00:00Added an answer on June 15, 2026 at 1:29 am

    Thanks guys. Both your answers lead me in a direction to solution to this problem using the AST implementation in JAVAPARSER. Here’s a code snippet to help others

    class CatchNameExpr extends VoidVisitorAdapter {
        HashMap<String, ArrayList<Integer>> variableLineNumMap;``
        ArrayList<String> variableList;
        boolean functionParsing = false;
        public CatchNameExpr(ArrayList<String> classVariables) {
            variableList=classVariables;
        }
        public void visit(MethodDeclaration method, Object arg) {
            System.out.println("---------------");
            System.out.println(method.getName());
            System.out.println("---------------");
            variableLineNumMap = new HashMap<String, ArrayList<Integer>>();
            System.out.println();
            functionParsing = true;
            visit(method.getBody(),arg);
            // Analyze lines for variable usage. Add to list of vars after checking if its read or written or unknown.
    
            functionParsing = false;
        }
    
        public void visit(NameExpr n, Object arg) {
    
            if(!functionParsing)
                return;
            //TODO: check if this var was declared above it, as a local var to the func. if yes, return
            ArrayList<Integer> setOfLineNum;
    
            System.out.println(n.getBeginLine()+" NameExpr " + n.getName());
    
            if(!variableList.contains(n.getName()) || n.getName().length()==0)
                return;
            if (!variableLineNumMap.containsKey(n.getName()))
            {
                setOfLineNum =   new ArrayList<Integer>();
                setOfLineNum.add(n.getBeginLine());
                variableLineNumMap.put(n.getName(), setOfLineNum);
            }
            else
            {
                setOfLineNum = variableLineNumMap.get(n.getName());
                setOfLineNum.add(n.getBeginLine());
                variableLineNumMap.put(n.getName(), setOfLineNum);
    
            }
        }
    }
    

    Instantiate the class —>

    CatchNameExpr nameExp = new CatchNameExpr(classVariables);
            nameExp.visit(classCompilationUnit, null);
    

    In a similar manner you can visit the AST for the following expressions, statements, condition etc

    http://www.jarvana.com/jarvana/view/com/google/code/javaparser/javaparser/1.0.8/javaparser-1.0.8-javadoc.jar!/japa/parser/ast/visitor/VoidVisitorAdapter.html

    I am well aware that byte-code processor will be more efficient, and will do the job better than i can hope for. But given the time limit, this option fitted me the best.

    Thanks guys,
    Jasmeet

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing java code. which will work on windows and linux. some code
I am having trouble writing some Java code, which will create a container/folder in
I'm writing a Groovy script which uses third party java code that I can't
Having been writing Java code for many years, I was amazed when I saw
I am writing a program that will change java code. It changes the next-line
I am writing java code for EC2 that will consume messages from SQS. My
So I am writing a Java code to represent a heap sort and to
I am writing some Java code that authenticates to Active Directory using SASL GSSAPI.
I'm writing some Java code that deals with Chinese characters, and I got some
I'm writing some java code that's supposed to parse csv files with different column

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.