code cleanup: javadoc warnings removed
reformatting to use only spaces and not tabs git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6397 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
1 parent
5dcf77bd74
commit
fa89131b76
3 files changed
+441
-438
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -7,106 +7,102 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* Various class-related utility methods.
|
||||
*
|
||||
*
|
||||
* @author Brian Remedios
|
||||
*/
|
||||
public final class ClassUtil {
|
||||
|
||||
public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
|
||||
public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
|
||||
|
||||
private ClassUtil() {
|
||||
};
|
||||
private ClassUtil() {
|
||||
};
|
||||
|
||||
@SuppressWarnings("PMD.AvoidUsingShortType")
|
||||
private static final TypeMap PRIMITIVE_TYPE_NAMES = new TypeMap(
|
||||
new Class[] { int.class, byte.class, long.class, short.class,
|
||||
float.class, double.class, char.class, boolean.class, });
|
||||
@SuppressWarnings("PMD.AvoidUsingShortType")
|
||||
private static final TypeMap PRIMITIVE_TYPE_NAMES = new TypeMap(new Class[] { int.class, byte.class, long.class,
|
||||
short.class, float.class, double.class, char.class, boolean.class, });
|
||||
|
||||
private static final TypeMap TYPES_BY_NAME = new TypeMap(new Class[] {
|
||||
Integer.class, Byte.class, Long.class, Short.class, Float.class,
|
||||
Double.class, Character.class, Boolean.class, BigDecimal.class,
|
||||
String.class, Object.class, });
|
||||
private static final TypeMap TYPES_BY_NAME = new TypeMap(new Class[] { Integer.class, Byte.class, Long.class,
|
||||
Short.class, Float.class, Double.class, Character.class, Boolean.class, BigDecimal.class, String.class,
|
||||
Object.class, });
|
||||
|
||||
/**
|
||||
* Returns the type(class) for the name specified or null if not found.
|
||||
*
|
||||
* @param name String
|
||||
* @return Class
|
||||
*/
|
||||
public static Class<?> getPrimitiveTypeFor(String name) {
|
||||
return PRIMITIVE_TYPE_NAMES.typeFor(name);
|
||||
}
|
||||
/**
|
||||
* Returns the type(class) for the name specified or null if not found.
|
||||
*
|
||||
* @param name String
|
||||
* @return Class
|
||||
*/
|
||||
public static Class<?> getPrimitiveTypeFor(String name) {
|
||||
return PRIMITIVE_TYPE_NAMES.typeFor(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a map of all the short names of classes we maintain mappings for.
|
||||
* The names are keyed by the classes themselves.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Map<Class, String> getClassShortNames() {
|
||||
/**
|
||||
* Return a map of all the short names of classes we maintain mappings for.
|
||||
* The names are keyed by the classes themselves.
|
||||
*
|
||||
* @return Map<Class, String>
|
||||
*/
|
||||
public static Map<Class, String> getClassShortNames() {
|
||||
Map<Class, String> map = new HashMap<Class, String>();
|
||||
map.putAll(PRIMITIVE_TYPE_NAMES.asInverseWithShortName());
|
||||
map.putAll(TYPES_BY_NAME.asInverseWithShortName());
|
||||
return map;
|
||||
}
|
||||
|
||||
Map<Class, String> map = new HashMap<Class, String>();
|
||||
map.putAll(PRIMITIVE_TYPE_NAMES.asInverseWithShortName());
|
||||
map.putAll(TYPES_BY_NAME.asInverseWithShortName());
|
||||
return map;
|
||||
}
|
||||
/**
|
||||
* Attempt to determine the actual class given the short name.
|
||||
*
|
||||
* @param shortName String
|
||||
* @return Class
|
||||
*/
|
||||
public static Class<?> getTypeFor(String shortName) {
|
||||
Class<?> type = TYPES_BY_NAME.typeFor(shortName);
|
||||
if (type != null) {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to determine the actual class given the short name.
|
||||
*
|
||||
* @param shortName String
|
||||
* @return Class
|
||||
*/
|
||||
public static Class<?> getTypeFor(String shortName) {
|
||||
type = PRIMITIVE_TYPE_NAMES.typeFor(shortName);
|
||||
if (type != null) {
|
||||
return type;
|
||||
}
|
||||
|
||||
Class<?> type = TYPES_BY_NAME.typeFor(shortName);
|
||||
if (type != null) {
|
||||
return type;
|
||||
}
|
||||
return CollectionUtil.getCollectionTypeFor(shortName);
|
||||
}
|
||||
|
||||
type = PRIMITIVE_TYPE_NAMES.typeFor(shortName);
|
||||
if (type != null) {
|
||||
return type;
|
||||
}
|
||||
/**
|
||||
* Returns the abbreviated name of the type, without the package name
|
||||
*
|
||||
* @param fullTypeName
|
||||
* @return String
|
||||
*/
|
||||
|
||||
return CollectionUtil.getCollectionTypeFor(shortName);
|
||||
}
|
||||
public static String withoutPackageName(String fullTypeName) {
|
||||
int dotPos = fullTypeName.lastIndexOf('.');
|
||||
return dotPos > 0 ? fullTypeName.substring(dotPos + 1) : fullTypeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the abbreviated name of the type, without the package name
|
||||
*
|
||||
* @param fullTypeName
|
||||
* @return String
|
||||
*/
|
||||
|
||||
public static String withoutPackageName(String fullTypeName) {
|
||||
|
||||
int dotPos = fullTypeName.lastIndexOf('.');
|
||||
return dotPos > 0 ? fullTypeName.substring(dotPos + 1) : fullTypeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to return the specified method from the class provided but will
|
||||
* walk up its superclasses until it finds a match. Returns null if it
|
||||
* doesn't.
|
||||
*
|
||||
* @param clasz Class
|
||||
* @param methodName String
|
||||
* @param paramTypes Class[]
|
||||
* @return Method
|
||||
*/
|
||||
public static Method methodFor(Class clasz, String methodName, Class[] paramTypes) {
|
||||
|
||||
Method method = null;
|
||||
Class current = clasz;
|
||||
while (current != Object.class) {
|
||||
try {
|
||||
method = current.getDeclaredMethod(methodName, paramTypes);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
current = current.getSuperclass();
|
||||
}
|
||||
if (method != null) { return method; }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Attempts to return the specified method from the class provided but will
|
||||
* walk up its superclasses until it finds a match. Returns null if it
|
||||
* doesn't.
|
||||
*
|
||||
* @param clasz Class
|
||||
* @param methodName String
|
||||
* @param paramTypes Class[]
|
||||
* @return Method
|
||||
*/
|
||||
public static Method methodFor(Class clasz, String methodName, Class[] paramTypes) {
|
||||
Method method = null;
|
||||
Class current = clasz;
|
||||
while (current != Object.class) {
|
||||
try {
|
||||
method = current.getDeclaredMethod(methodName, paramTypes);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
current = current.getSuperclass();
|
||||
}
|
||||
if (method != null) {
|
||||
return method;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -6,153 +6,148 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* A specialized map that stores types by both their full and short (without package prefixes) names.
|
||||
* If an incoming type shares the same name (but different package/prefix) with a type already in the
|
||||
* If an incoming type shares the same name (but different package/prefix) with a type already in the
|
||||
* map then an IllegalArgumentException will be thrown since any subsequent retrievals by said short
|
||||
* name could be in error.
|
||||
*
|
||||
*
|
||||
* @author Brian Remedios
|
||||
*/
|
||||
public class TypeMap {
|
||||
|
||||
private Map<String, Class<?>> typesByName;
|
||||
private Map<String, Class<?>> typesByName;
|
||||
|
||||
/**
|
||||
* Constructor for TypeMap.
|
||||
*
|
||||
* @param initialSize int
|
||||
*/
|
||||
public TypeMap(int initialSize) {
|
||||
typesByName = new HashMap<String, Class<?>>(initialSize);
|
||||
}
|
||||
/**
|
||||
* Constructor for TypeMap.
|
||||
*
|
||||
* @param initialSize int
|
||||
*/
|
||||
public TypeMap(int initialSize) {
|
||||
typesByName = new HashMap<String, Class<?>>(initialSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for TypeMap that takes in an initial set of types.
|
||||
*
|
||||
* @param types Class[]
|
||||
*/
|
||||
public TypeMap(Class<?>... types) {
|
||||
this(types.length);
|
||||
add(types);
|
||||
}
|
||||
/**
|
||||
* Constructor for TypeMap that takes in an initial set of types.
|
||||
*
|
||||
* @param types Class[]
|
||||
*/
|
||||
public TypeMap(Class<?>... types) {
|
||||
this(types.length);
|
||||
add(types);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a type to the receiver and stores it keyed by both its full and
|
||||
* short names. Throws an exception if the short name of the argument
|
||||
* matches an existing one already in the map for a different class.
|
||||
*
|
||||
* @param type Class
|
||||
* @throws IllegalArgumentException
|
||||
*/
|
||||
@SuppressWarnings("PMD.CompareObjectsWithEquals")
|
||||
public void add(Class<?> type) {
|
||||
/**
|
||||
* Adds a type to the receiver and stores it keyed by both its full and
|
||||
* short names. Throws an exception if the short name of the argument
|
||||
* matches an existing one already in the map for a different class.
|
||||
*
|
||||
* @param type Class
|
||||
* @throws IllegalArgumentException
|
||||
*/
|
||||
@SuppressWarnings("PMD.CompareObjectsWithEquals")
|
||||
public void add(Class<?> type) {
|
||||
final String shortName = ClassUtil.withoutPackageName(type.getName());
|
||||
Class<?> existingType = typesByName.get(shortName);
|
||||
if (existingType == null) {
|
||||
typesByName.put(type.getName(), type);
|
||||
typesByName.put(shortName, type);
|
||||
return;
|
||||
}
|
||||
|
||||
final String shortName = ClassUtil.withoutPackageName(type.getName());
|
||||
Class<?> existingType = typesByName.get(shortName);
|
||||
if (existingType == null) {
|
||||
typesByName.put(type.getName(), type);
|
||||
typesByName.put(shortName, type);
|
||||
return;
|
||||
}
|
||||
if (existingType != type) {
|
||||
throw new IllegalArgumentException("Short name collision between existing " + existingType + " and new "
|
||||
+ type);
|
||||
}
|
||||
}
|
||||
|
||||
if (existingType != type) {
|
||||
throw new IllegalArgumentException(
|
||||
"Short name collision between existing " +
|
||||
existingType + " and new " + type
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns whether the type is known to the receiver.
|
||||
*
|
||||
* @param type Class
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(Class<?> type) {
|
||||
return typesByName.containsValue(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the type is known to the receiver.
|
||||
*
|
||||
* @param type Class
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(Class<?> type) {
|
||||
return typesByName.containsValue(type);
|
||||
}
|
||||
/**
|
||||
* Returns whether the typeName is known to the receiver.
|
||||
*
|
||||
* @param typeName String
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(String typeName) {
|
||||
return typesByName.containsKey(typeName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the typeName is known to the receiver.
|
||||
*
|
||||
* @param typeName String
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(String typeName) {
|
||||
return typesByName.containsKey(typeName);
|
||||
}
|
||||
/**
|
||||
* Returns the type for the typeName specified.
|
||||
*
|
||||
* @param typeName String
|
||||
* @return Class
|
||||
*/
|
||||
public Class<?> typeFor(String typeName) {
|
||||
return typesByName.get(typeName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type for the typeName specified.
|
||||
*
|
||||
* @param typeName String
|
||||
* @return Class
|
||||
*/
|
||||
public Class<?> typeFor(String typeName) {
|
||||
return typesByName.get(typeName);
|
||||
}
|
||||
/**
|
||||
* Adds an array of types to the receiver at once.
|
||||
*
|
||||
* @param types Class[]
|
||||
*/
|
||||
public void add(Class<?>... types) {
|
||||
for (Class<?> element : types) {
|
||||
add(element);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an array of types to the receiver at once.
|
||||
*
|
||||
* @param types Class[]
|
||||
*/
|
||||
public void add(Class<?>... types) {
|
||||
for (Class<?> element : types) {
|
||||
add(element);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Creates and returns a map of short type names (without the package
|
||||
* prefixes) keyed by the classes themselves.
|
||||
*
|
||||
* @return Map
|
||||
*/
|
||||
public Map<Class<?>, String> asInverseWithShortName() {
|
||||
Map<Class<?>, String> inverseMap = new HashMap<Class<?>, String>(typesByName.size() / 2);
|
||||
|
||||
/**
|
||||
* Creates and returns a map of short type names (without the package
|
||||
* prefixes) keyed by the classes themselves.
|
||||
*
|
||||
* @return Map
|
||||
*/
|
||||
public Map<Class<?>, String> asInverseWithShortName() {
|
||||
Iterator iter = typesByName.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
Map.Entry entry = (Map.Entry) iter.next();
|
||||
storeShortest(inverseMap, entry.getValue(), (String) entry.getKey());
|
||||
}
|
||||
|
||||
Map<Class<?>, String> inverseMap = new HashMap<Class<?>, String>(typesByName.size() / 2);
|
||||
return inverseMap;
|
||||
}
|
||||
|
||||
Iterator iter = typesByName.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
Map.Entry entry = (Map.Entry) iter.next();
|
||||
storeShortest(inverseMap, entry.getValue(), (String) entry.getKey());
|
||||
}
|
||||
/**
|
||||
* Returns the total number of entries in the receiver. This will be exactly
|
||||
* twice the number of types added.
|
||||
*
|
||||
* @return the total number of entries in the receiver
|
||||
*/
|
||||
public int size() {
|
||||
return typesByName.size();
|
||||
}
|
||||
|
||||
return inverseMap;
|
||||
}
|
||||
/**
|
||||
* Store the shorter of the incoming value or the existing value in the map
|
||||
* at the key specified.
|
||||
*
|
||||
* @param map
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
private void storeShortest(Map map, Object key, String value) {
|
||||
String existingValue = (String) map.get(key);
|
||||
|
||||
/**
|
||||
* Returns the total number of entries in the receiver. This will be exactly
|
||||
* twice the number of types added.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int size() {
|
||||
return typesByName.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the shorter of the incoming value or the existing value in the map
|
||||
* at the key specified.
|
||||
*
|
||||
* @param map
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
private void storeShortest(Map map, Object key, String value) {
|
||||
if (existingValue == null) {
|
||||
map.put(key, value);
|
||||
return;
|
||||
}
|
||||
|
||||
String existingValue = (String) map.get(key);
|
||||
if (existingValue.length() < value.length()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (existingValue == null) {
|
||||
map.put(key, value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (existingValue.length() < value.length()) {
|
||||
return;
|
||||
}
|
||||
|
||||
map.put(key, value);
|
||||
}
|
||||
map.put(key, value);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user