Skip to content

Commit

Permalink
fixed bug #772832, Confusion about class named 'Boolean'
Browse files Browse the repository at this point in the history
  • Loading branch information
lkuehne committed Aug 3, 2003
1 parent 2d16874 commit bb1db49
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
10 changes: 7 additions & 3 deletions docs/releasenotes.html
Expand Up @@ -174,10 +174,14 @@ <h2>Release 3.2</h2>
handle all Unicode symbols (bug 755744)</li>

<li class="body">Corrected misspelling of &quot;precede&quot; (bug 744342)</li>
</ul>

<li class="body">EmptyForIteratorPadCheck errors when for statement
wraps at the iterator (bug 777471)</li>
<li class="body">EmptyForIteratorPadCheck errors when for statement
wraps at the iterator (bug 777471)</li>

<li class="body">False alarms from IllegalInstantiation for
user defined classes with the same names as those in
java.lang (bug 772832)</li>

</ul>


Expand Down
Expand Up @@ -72,13 +72,20 @@ public class IllegalInstantiationCheck
/** the imports for the file */
private final Set mImports = new HashSet();

/** the class names defined in the file */
private final Set mClassNames = new HashSet();

/** the instantiations in the file */
private final Set mInstantiations = new HashSet();

/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {
TokenTypes.IMPORT,
TokenTypes.LITERAL_NEW,
TokenTypes.PACKAGE_DEF,
TokenTypes.CLASS_DEF,
};
}

Expand Down Expand Up @@ -107,6 +114,8 @@ public void beginTree(DetailAST aRootAST)
super.beginTree(aRootAST);
mPkgName = null;
mImports.clear();
mInstantiations.clear();
mClassNames.clear();
}

/** @see com.puppycrawl.tools.checkstyle.api.Check */
Expand All @@ -122,11 +131,38 @@ public void visitToken(DetailAST aAST)
case TokenTypes.IMPORT:
processImport(aAST);
break;
case TokenTypes.CLASS_DEF:
processClassDef(aAST);
break;
default:
throw new IllegalArgumentException("Unknown type " + aAST);
}
}

/**
* @see com.puppycrawl.tools.checkstyle.api.Check#finishTree
*/
public void finishTree(DetailAST aRootAST)
{
for (Iterator it = mInstantiations.iterator(); it.hasNext();) {
DetailAST literalNewAST = (DetailAST) it.next();
postprocessLiteralNew(literalNewAST);
}
}

/**
* Collects classes defined in the source file. Required
* to avoid false alarms for local vs. java.lang classes.
*
* @param aAST the classdef token.
*/
private void processClassDef(DetailAST aAST)
{
final DetailAST identToken = aAST.findFirstToken(TokenTypes.IDENT);
String className = identToken.getText();
mClassNames.add(className);
}

/**
* Perform processing for an import token
* @param aAST the import token
Expand Down Expand Up @@ -154,13 +190,22 @@ private void processPackageDef(DetailAST aAST)
}

/**
* Perform processing for an "new" token
* Collects a "new" token.
* @param aAST the "new" token
*/
private void processLiteralNew(DetailAST aAST)
{
final DetailAST typeNameAST = (DetailAST) aAST.getFirstChild();
mInstantiations.add(aAST);
}

/**
* Processes one of the collected "new" tokens when treewalking
* has finished.
* @param aAST the "new" token.
*/
private void postprocessLiteralNew(DetailAST aAST)
{
final DetailAST typeNameAST = (DetailAST) aAST.getFirstChild();
final AST nameSibling = typeNameAST.getNextSibling();
if (nameSibling != null
&& nameSibling.getType() == TokenTypes.ARRAY_DECLARATOR)
Expand Down Expand Up @@ -206,7 +251,32 @@ private String getIllegalInstantiation(String aClassName)
&& illegal.endsWith(aClassName)
&& illegal.startsWith(javaLang))
{
return illegal;
// java.lang needs no import, but a class without import might
// also come from the same file or be in the same package.
// E.g. if a class defines an inner class "Boolean",
// the expression "new Boolean()" refers to that class,
// not to java.lang.Boolean

boolean isSameFile = mClassNames.contains(aClassName);

boolean isSamePackage = false;
try {
final ClassLoader classLoader = getClassLoader();
if (classLoader != null) {
final String fqName = mPkgName + "." + aClassName;
classLoader.loadClass(fqName);
// no ClassNotFoundException, fqName is a known class
isSamePackage = true;
}
}
catch (ClassNotFoundException ex) {
// not a class from the same package
isSamePackage = false;
}

if (!(isSameFile || isSamePackage)) {
return illegal;
}
}

// class from same package
Expand Down

0 comments on commit bb1db49

Please sign in to comment.