xbn.programs
Class ReportBadImports

java.lang.Object
  |
  +--xbn.XBNObject
        |
        +--xbn.programs.XBNStatic
              |
              +--xbn.programs.ReportBadImports

public class ReportBadImports
extends XBNStatic

An application to analyze a directory of Java code, reporting on all unneeded/redundant import statements. It detects imports that are not used, duplicate import statements, and also needless self-referencing star ('.*;') imports.

Source code:  ReportBadImports.java.

Notes

This application does the same thing as PMD's rulesets/imports.xml ruleset, but has more specific error messages.

This application uses string analysis only, no reflection is involved. Specifically, it searches for the last word in each specific import statement ('AssertException' is the last word in 'import xbn.AssertException;'), to see that it exists elsewhere in the code (ignoring comments). It can tell the difference between 'MyAssertException' and 'AssertException'.

'.*;' import statements (star imports, the opposite of specific imports) are also analyzed for duplicates, and also that the package it is importing is one other than the package in which the containing class exists.

In order for this code to work properly...

This application will only work (as expected!) on Java code that can be successfully compiled.

Although the compiler knows that // contained in a string is not the beginning of a comment, you must denote this with

'/' + '/'

and not

'//'

The same is true with the multi-line comment delimiter:

'/' + '*' and '*' + '/'

instead of

'/*' and '*/'

Why? Because I don't want to have to write complicated string parsing, which can tell the difference between true comment delimiters, and those existing inside strings. :'  )

Error codes

Key:

Precedence: Duplicate state is always reported. Self-referencing trumps unneeded, and unneeded trumps sub-import. When one error type trumps another, the trumped error condition is not analyzed, and therefore unknown (denoted by a ?).

Code for import type Error type
Star Specific Self-referencing Unneeded Sub-import Duplicate
f f Yes ? ? no
F F Yes ? ? Yes
n/a i no no Yes no
n/a I no no Yes Yes
n/a x no Yes ? no
n/a X no Yes ? Yes
d d no no no Yes

Import statements for testing

Adding these import statements into VWChar (right below existing import statements), and then running this application (or PMD) against it, will result in every possible error. There is no need to compile the code.

      //To test xbn.programs.ReportBadImports...START
         import xbn.array.*;
         import xbn.array.*;
         import xbn.array.UNNEEDED_and_SUB_IMPORT_and_SELF_REFERENCE;
         import xbn.array.UNNEEDED_and_SUB_IMPORT_and_SELF_REFERENCE_and_DUPLICATE;
         import xbn.array.UNNEEDED_and_SUB_IMPORT_and_SELF_REFERENCE_and_DUPLICATE;

         import java.util.UNNEEDED;

         //Duplicate, because of the actual import statement above
         //this testing section.
         import java.util.Arrays;

         import java.util.UNNEEDED_and_DUPLICATE;
         import java.util.UNNEEDED_and_DUPLICATE;

         //No errors.
         import java.bla.*;

         import java.bla.UNNEEDED_and_SUB_IMPORT;
         import java.bla.UNNEEDED_and_SUB_IMPORT_and_DUPLICATE;
         import java.bla.UNNEEDED_and_SUB_IMPORT_and_DUPLICATE;
      //To test xbn.programs.ReportBadImports...END


Fields inherited from class xbn.XBNObject
bFALSE_IN_PRODUCTION, bTRUE_IN_PRODUCTION, sCNSTR, sES, sLINE_SEP
 
Constructor Summary
ReportBadImports()
          Create a ReportBadImports.
 
Method Summary
static void main(String[] as_cmdLineParams)
          Run the application.
 
Methods inherited from class xbn.programs.XBNStatic
throwAXS
 
Methods inherited from class xbn.XBNObject
getXMsgPrefix, sop, sopl, sopl, throwAX, throwAXIfBadStr, throwAXIfNull, throwAXSpoof
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReportBadImports

public ReportBadImports()

Create a ReportBadImports. This constructor does nothing.

Method Detail

main

public static final void main(String[] as_cmdLineParams)

Run the application.




Copyright 1997-2003, Jeff Epstein, All Rights Reserved. See top of source code files for copyright notice.

 

http://sourceforge.net/projects/xbnjava

 

SourceForge.net Logo