finding dead code...

Discussion in 'Java' started by secret, Oct 4, 2003.

  1. secret

    secret Guest

    I've recently taken responsibility for a large java application and I'd love
    to be able to use a tool to help me discover what classes and methods are no
    longer being used so I could refactor them out.

    Basically there are a small and finite number of places for users to enter
    the code. What I would ideally like to have is a tool that I could enter in
    those class/method entry points and the root of the project directory. The
    tool would then parse my code and make a graph of the classes and methods
    that could ever be reached from those starting points and would then return
    to me a list of the rest of the classes/methods that are never reached.

    Is there such a tool?

    alan
     
    secret, Oct 4, 2003
    #1
    1. Advertising

  2. secret

    VisionSet Guest

    "secret" <> wrote in message
    news:3zEfb.7345$...
    > I've recently taken responsibility for a large java application and I'd

    love
    > to be able to use a tool to help me discover what classes and methods are

    no
    > longer being used so I could refactor them out.
    >
    > Basically there are a small and finite number of places for users to enter
    > the code. What I would ideally like to have is a tool that I could enter

    in
    > those class/method entry points and the root of the project directory.

    The
    > tool would then parse my code and make a graph of the classes and methods
    > that could ever be reached from those starting points and would then

    return
    > to me a list of the rest of the classes/methods that are never reached.
    >


    Have a look at http://jgrasp.org & http://www.quest.com/jprobe/
    Or a good ide such as eclipse can help.

    --
    Mike W
     
    VisionSet, Oct 4, 2003
    #2
    1. Advertising

  3. secret () wrote:
    : tool would then parse my code and make a graph of the classes and methods
    : that could ever be reached from those starting points and would then return
    : to me a list of the rest of the classes/methods that are never reached.
    : Is there such a tool?

    That depends on what you really want. There are a few tools that can
    do code coverage. Basically this means that they tell you which
    classes/methods/lines are used during tests (you have automated tests
    dont you?). I havent used them but I know of them:
    jprobe, jcoverage...

    Profilers can most probably dump a list of methods/classes that have
    been used during tests.

    Ok, do you have tests for 100% of your application? if so
    congratulations, I dont find that too common so Ill assume that you
    dont. Then the coverage lists can not be used directly to tell you
    what classes are not used.

    So how do you do then? grep for "new <classname>" in the code?
    what about dynamic class loading? what about islands of unused code?

    For work I wrote a perl program that can read our java code and our
    config files and db and figure out which classes are used.
    This tool is highly specialized and will not do you any good
    (and since its work I cant share without asking my boss).

    So I would say that no, no such tool exists for you, but some tool
    that tries to tell you that actually do exist.

    Someone please tell me Im wrong.

    /robo
     
    Robert Olofsson, Oct 4, 2003
    #3
  4. secret

    Roedy Green Guest

    On Sat, 04 Oct 2003 18:45:19 GMT, "secret" <> wrote
    or quoted :

    >Basically there are a small and finite number of places for users to enter
    >the code. What I would ideally like to have is a tool that I could enter in
    >those class/method entry points and the root of the project directory. The
    >tool would then parse my code and make a graph of the classes and methods
    >that could ever be reached from those starting points and would then return
    >to me a list of the rest of the classes/methods that are never reached.


    Optimising compilers do this, but they don't modify the original
    source. Perhaps the Jet folk would be interested in exporting that
    information in a usable way. You might be able to figure out enough
    as it is from the verbose Jet compile listings. See
    http://mindprod.com/jet.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 4, 2003
    #4
  5. secret

    Roedy Green Guest

    On 4 Oct 2003 19:57:02 GMT, (Robert
    Olofsson) wrote or quoted :

    >Profilers can most probably dump a list of methods/classes that have
    >been used during tests.


    Of course just because it was not exercised in a given test does not
    mean it is safe to remove the code. It is just indicative it MIGHT be
    truly dead code.


    On the other hand there can be code that in theory can be reached, but
    in practice will never be.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 4, 2003
    #5
  6. secret

    Andrew Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    VisionSet wrote:
    | "secret" <> wrote in message
    | news:3zEfb.7345$...
    |
    |>I've recently taken responsibility for a large java application and I'd
    |
    | love
    |
    |>to be able to use a tool to help me discover what classes and methods are
    |
    | no
    |
    |>longer being used so I could refactor them out.
    |>
    |>Basically there are a small and finite number of places for users to enter
    |>the code. What I would ideally like to have is a tool that I could enter
    |
    | in
    |
    |>those class/method entry points and the root of the project directory.
    |
    | The
    |
    |>tool would then parse my code and make a graph of the classes and methods
    |>that could ever be reached from those starting points and would then
    |
    | return
    |
    |>to me a list of the rest of the classes/methods that are never reached.
    |>
    |
    |
    | Have a look at http://jgrasp.org & http://www.quest.com/jprobe/
    | Or a good ide such as eclipse can help.
    |

    Eclipse can easily do what you have specified. You can turn these
    preferences on and off, as well as many many more that I'm sure you'd
    find handy.

    - --
    [A n d r e w]
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.3 (GNU/Linux)

    iD8DBQE/fzhOx4nopx2ZsVURArS9AKDT/Kx3kW6/NPDoyl32xPaGtNepPACdHhO1
    LHkTd3pSMJP/9RwEeNfCqoE=
    =MxSB
    -----END PGP SIGNATURE-----
     
    Andrew, Oct 4, 2003
    #6
  7. secret

    Paul Tomblin Guest

    In a previous article, see mindprod.com said:
    >On 4 Oct 2003 19:57:02 GMT, (Robert
    >Olofsson) wrote or quoted :
    >>Profilers can most probably dump a list of methods/classes that have
    >>been used during tests.

    >
    >Of course just because it was not exercised in a given test does not
    >mean it is safe to remove the code. It is just indicative it MIGHT be
    >truly dead code.


    Unless you're using Class and ClassLoader to load classes by name, You can
    tell by code parsing if a method is never called.

    In one job, we used a code obfuscator that as well as obfuscating code,
    also did that analysis and removed unused methods. It made some pretty
    small jar files because of that. I think there was a way to tell it to
    just tell you which methods and classes were unused. I know there was a
    way to tell it what methods and classes you needed to access by name so it
    wouldn't obfuscate those.


    --
    Paul Tomblin <> http://xcski.com/blogs/pt/
    "Towards thee I roll, thou all-destroying but unconquering wanker; to the
    last I grapple with thee; from hell's heart I killfile thee; for hate's
    sake I spit my last post at thee! All your base are belong to us!"
     
    Paul Tomblin, Oct 4, 2003
    #7
  8. secret

    evernat

    Joined:
    Sep 9, 2009
    Messages:
    1
    Dead Code Detector (dcd)

    Dead Code Detector (dcd.dev.java.net) is exactly such a tool to find dead code without code coverage or unit tests.

    The tool does static analysis of compiled classes to search unused code, including private and package private methods and/or protected and public methods. But it does not start with entry points: so these entry points could be detected as suspects, which you can filter.

    If you have any problem with DCD you can reach me with evernat at free.fr

    JGrasp noted above has recently included a plugin for DCD.
     
    evernat, Sep 9, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Geronimo W. Christ Esq

    Removing dead code and unused functions

    Geronimo W. Christ Esq, Jun 19, 2005, in forum: C Programming
    Replies:
    39
    Views:
    2,272
    Dave Thompson
    Jul 4, 2005
  2. Dan Henry
    Replies:
    0
    Views:
    388
    Dan Henry
    Jun 21, 2005
  3. Greg
    Replies:
    10
    Views:
    1,524
    Dave Thompson
    Jul 4, 2005
  4. jaysome

    Dead Code?

    jaysome, Oct 10, 2007, in forum: C Programming
    Replies:
    4
    Views:
    365
    Alan Curry
    Oct 10, 2007
  5. sanjay
    Replies:
    5
    Views:
    663
    James Kanze
    Aug 1, 2008
Loading...

Share This Page