Refactored out ugly copy/paste code.

This commit is contained in:
Oliver Burn 2003-03-14 03:22:53 +00:00
parent 4cddf4771c
commit 8dd44cbead
4 changed files with 15 additions and 37 deletions

View File

@ -173,4 +173,13 @@ public final class Utils
return retVal;
}
/**
* @return the base class name from a fully qualified name
* @param aType the fully qualified name. Cannot be null
*/
public static String baseClassname(String aType)
{
final int i = aType.lastIndexOf(".");
return (i == -1) ? aType : aType.substring(i + 1);
}
}

View File

@ -27,6 +27,7 @@ import java.util.StringTokenizer;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FullIdent;
import com.puppycrawl.tools.checkstyle.api.Utils;
import antlr.collections.AST;
// TODO: Clean up potential duplicate code here and in UnusedImportsCheck
@ -61,7 +62,7 @@ import antlr.collections.AST;
* @author lkuehne
*/
public class IllegalInstantiationCheck
extends AbstractImportCheck
extends AbstractImportCheck
{
/** Set of fully qualified classnames. E.g. "java.lang.Boolean" */
private final Set mIllegalClasses = new HashSet();
@ -238,7 +239,7 @@ public class IllegalInstantiationCheck
}
}
else {
if (basename(importArg).equals(aClassName)
if (Utils.baseClassname(importArg).equals(aClassName)
&& mIllegalClasses.contains(importArg))
{
return importArg;
@ -249,16 +250,6 @@ public class IllegalInstantiationCheck
return null;
}
/**
* @return the class name from a fully qualified name
* @param aType the fully qualified name
*/
private static String basename(String aType)
{
final int i = aType.lastIndexOf(".");
return (i == -1) ? aType : aType.substring(i + 1);
}
/**
* Sets the classes that are illegal to instantiate.
* @param aClassNames a comma seperate list of class names

View File

@ -559,7 +559,7 @@ public class JavadocMethodCheck
return false;
}
final String base = basename(aFullName);
final String base = Utils.baseClassname(aFullName);
if (aShortName.length() >= aFullName.length()
|| !base.equals(aShortName))
{
@ -705,16 +705,4 @@ public class JavadocMethodCheck
}
}
}
// TODO: clean up duplicate code in UnusedImports and IllegalInstantiation
/**
* @return the class name from a fully qualified name
* @param aType the fully qualified name
*/
private String basename(String aType)
{
final int i = aType.lastIndexOf(".");
return (i == -1) ? aType : aType.substring(i + 1);
}
}

View File

@ -21,6 +21,7 @@ package com.puppycrawl.tools.checkstyle.checks;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FullIdent;
import com.puppycrawl.tools.checkstyle.api.Utils;
import java.util.HashSet;
import java.util.Set;
@ -65,7 +66,7 @@ public class UnusedImportsCheck
while (it.hasNext()) {
final FullIdent imp = (FullIdent) it.next();
if (!mReferenced.contains(basename(imp.getText()))) {
if (!mReferenced.contains(Utils.baseClassname(imp.getText()))) {
log(imp.getLineNo(),
imp.getColumnNo(),
"import.unused", imp.getText());
@ -130,15 +131,4 @@ public class UnusedImportsCheck
mImports.add(name);
}
}
/**
* @return the class name from a fully qualified name
* @param aType the fully qualified name
*/
private String basename(String aType)
{
final int i = aType.lastIndexOf(".");
return (i == -1) ? aType : aType.substring(i + 1);
}
}