Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to find unused variables in Java program?

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an assignment to write a parser which will find unused variables in our code. I am totally dumbstruck since I don't have any approach. Could you please provide me any pointers to proceed with my assignment
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to use either a class which implements Map or a class which implements Dictionary to find all of the variables in the source code.
Once you have a collection of all of the variables then you want to count the number of occurrences of each variable.
Now that you have the count you can see which variables are used and which are not.

Here is some extra reading which may help out
Collections Documentation
Collections Tutorial By Oracle
 
Saloon Keeper
Posts: 9267
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting problem. This could be either quick and dirty, or complex and complete. @Pete's approach is one simple way and may suffice for an assignment.

The ultimate and non-trivial way would be to have a Java parser implemented with something like Antlr. Then analyze the parse tree. A Java language definition suitable to use as a spec that can be fed into Antlr can be found online but I've found those to sometimes be incomplete so you may end  up searching for one that works. Then the analysis involves traversing the parse tree.

A middle of the road approach would involve first writing a filter that removed all comments and string literals. You don't want to be counting occurrences that appear in those. This is trickier than it sounds but once written can be used in creating a variety of analysis tools.

Then find all words that begin with a lower case letter. This is not fool proof as Java compilers will accept variable names that begin with upper case letters. It is only by convention that variables begin with lower case letters.

Eliminate keywords.

Next, eliminate words that are immediately followed by an open parenthesis, those would be methods, not variables.

Next, keep track of scope. Variables defined within blocks ({...}), variables defined as method parameters, and class level variables.

See if you can detect assignment to a variable vs a declaration vs being used.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic