A bugfix and new general utility functions used by the Eclipse plugin.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6861 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Brian Remedios committed 2009-02-22 07:45:52 +00:00
1 parent 50e9be7153
commit bf4843d9bc
3 files changed
+81 -6

No files matched your search

@@ -2,7 +2,9 @@ package net.sourceforge.pmd.util;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@@ -82,7 +84,7 @@ public final class ClassUtil {
* @param type
* @return String
*/
public static String asShortestName(Class type) {
public static String asShortestName(Class<?> type) {
String name = SHORT_NAMES_BY_TYPE.get(type);
return name == null ? type.getName() : name;
@@ -110,9 +112,9 @@ public final class ClassUtil {
* @param paramTypes Class[]
* @return Method
*/
public static Method methodFor(Class clasz, String methodName, Class[] paramTypes) {
public static Method methodFor(Class<?> clasz, String methodName, Class<?>[] paramTypes) {
Method method = null;
Class current = clasz;
Class<?> current = clasz;
while (current != Object.class) {
try {
method = current.getDeclaredMethod(methodName, paramTypes);
@@ -125,4 +127,24 @@ public final class ClassUtil {
}
return null;
}
/**
* Return the methods as a map keyed by their common declaration types.
*
* @param methods
* @return Map<String, List<Method>>
*/
public static Map<String, List<Method>> asMethodGroupsByTypeName(Method[] methods) {
Map<String, List<Method>> methodGroups = new HashMap<String, List<Method>>(methods.length);
for (int i=0; i<methods.length; i++) {
String clsName = ClassUtil.asShortestName(methods[i].getDeclaringClass());
if (!methodGroups.containsKey(clsName)) {
methodGroups.put(clsName, new ArrayList<Method>());
}
methodGroups.get(clsName).add(methods[i]);
}
return methodGroups;
}
}
@@ -1,8 +1,11 @@
package net.sourceforge.pmd.util;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -212,4 +215,52 @@ public final class CollectionUtil {
if (b == null) { return isEmpty(a); }
return a.equals(b);
}
/**
* If the newValue is already held within the values array then the values array
* is returned, otherwise a new array is created appending the newValue to the
* end.
*
* @param <T>
* @param values
* @param newValue
* @return
*/
public static <T> T[] addWithoutDuplicates(T[] values, T newValue) {
for (T value : values) {
if (value.equals(newValue)) {
return values;
}
}
T[] largerOne = (T[])Array.newInstance(values.getClass().getComponentType(), values.length + 1);
System.arraycopy(values, 0, largerOne, 0, values.length);
largerOne[values.length] = newValue;
return largerOne;
}
/**
* Returns an array of values as a union set of the two input arrays.
*
* @param <T>
* @param values
* @param newValues
* @return
*/
public static <T> T[] addWithoutDuplicates(T[] values, T[] newValues) {
Set<T> originals = new HashSet<T>(values.length);
for (T value : values) { originals.add(value); }
List<T> newOnes = new ArrayList<T>(newValues.length);
for (T value : newValues) {
if (originals.contains(value)) { continue; }
newOnes.add(value);
}
T[] largerOne = (T[])Array.newInstance(values.getClass().getComponentType(), values.length + newOnes.size());
System.arraycopy(values, 0, largerOne, 0, values.length);
for (int i=values.length; i<largerOne.length; i++) { largerOne[i] = newOnes.get(i-values.length); }
return largerOne;
}
}
@@ -240,13 +240,16 @@ public final class StringUtil {
}
/**
* Return the length of the shortest string in the array.
* If any one of them is null then it returns 0.
* If the collection is empty or any one of them is
* null then it returns 0.
*
* @param strings String[]
* @return int
*/
public static int lengthOfShortestIn(String[] strings) {
if (CollectionUtil.isEmpty(strings)) { return 0; }
int minLength = Integer.MAX_VALUE;
for (int i=0; i<strings.length; i++) {
@@ -380,8 +383,7 @@ public final class StringUtil {
StringBuilder sb = new StringBuilder(items[0].toString());
for (int i=1; i<items.length; i++) {
sb.append(separator);
sb.append(items[i]);
sb.append(separator).append(items[i]);
}
return sb.toString();