Checking in some Java 5 changes

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4994 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2007-01-28 04:56:11 +00:00
parent 0eaac2fbdb
commit e14bf0ee5f
10 changed files with 73 additions and 77 deletions

View File

@ -10,13 +10,20 @@ import net.sourceforge.pmd.properties.EnumeratedProperty;
*/
public class EnumeratedPropertyTest extends AbstractPropertyDescriptorTester {
private static final Object[][] mixedItems = new Object[][] {
{"map", new HashMap()},
{"emptyArray", new Object[0]},
{"list", new ArrayList()},
{"string", "Hello World!"},
private static final String[] keys = new String[] {
"map",
"emptyArray",
"list",
"string",
};
private static final Object[] values = new Object[] {
new HashMap(),
new Object[0],
new ArrayList(),
"Hello World!",
};
public EnumeratedPropertyTest() {
super();
}
@ -28,7 +35,7 @@ public class EnumeratedPropertyTest extends AbstractPropertyDescriptorTester {
*/
protected Object createValue(int count) {
if (count == 1) return ((Object[])randomChoice(mixedItems))[1];
if (count == 1) return randomChoice(values);
Object[] values = new Object[count];
for (int i=0; i<values.length; i++) values[i] = createValue(1);
@ -43,8 +50,8 @@ public class EnumeratedPropertyTest extends AbstractPropertyDescriptorTester {
protected PropertyDescriptor createProperty(int maxCount) {
return maxCount == 1 ?
new EnumeratedProperty("testEnumerations", "Test enumerations with complex types", mixedItems, 1.0f) :
new EnumeratedProperty("testEnumerations", "Test enumerations with complex types", mixedItems, 1.0f, 3);
new EnumeratedProperty<Object>("testEnumerations", "Test enumerations with complex types", keys, values, 1.0f) :
new EnumeratedProperty<Object>("testEnumerations", "Test enumerations with complex types", keys, values, 1.0f, 3);
}
}

View File

@ -33,7 +33,7 @@ class NonRuleWithAllPropertyTypes extends AbstractRule {
public static final PropertyDescriptor singleType = new TypeProperty("singleType", "Property with a single type value", String.class, 5.0f);
public static final PropertyDescriptor multiType = new TypeProperty("multiType", "Property with multiple type values", new Class[] {Integer.class, Object.class}, 6.0f);
public static final PropertyDescriptor enumType = new EnumeratedProperty("enumType", "Property with a enumerated choices", new Object[][] {{"String", String.class},{"Object", Object.class}}, 5.0f);
public static final PropertyDescriptor enumType = new EnumeratedProperty<Class>("enumType", "Property with a enumerated choices", new String[] {"String", "Object"}, new Class[] {String.class, Object.class}, 5.0f);
private static final Map<String, PropertyDescriptor> propertyDescriptorsByName = asFixedMap(new PropertyDescriptor[] {

View File

@ -13,22 +13,21 @@ public class SourceTypeToRuleLanguageMapper {
/**
* Map of SourceType on RuleLanguage.
*/
private static Map mapSourceTypeOnRuleLanguage = CollectionUtil.mapFrom( new Object[][] {
{ SourceType.JAVA_13, Language.JAVA },
{ SourceType.JAVA_14, Language.JAVA },
{ SourceType.JAVA_15, Language.JAVA },
{ SourceType.JAVA_16, Language.JAVA },
{ SourceType.JSP, Language.JSP },
});
private static Map<SourceType, Language> mapSourceTypeOnRuleLanguage = CollectionUtil.mapFrom(
new SourceType[] { SourceType.JAVA_13, SourceType.JAVA_14,
SourceType.JAVA_15, SourceType.JAVA_16, SourceType.JSP, },
new Language[] { Language.JAVA, Language.JAVA, Language.JAVA,
Language.JAVA, Language.JSP, });
private SourceTypeToRuleLanguageMapper() {};
/**
* Get the RuleLanguage that corresponds to the given SourceType.
*
* @param sourceType the SourceType
*
* @param sourceType
* the SourceType
* @return a RuleLanguage
*/
public static Language getMappedLanguage(SourceType sourceType) {
return (Language) mapSourceTypeOnRuleLanguage.get(sourceType);
return mapSourceTypeOnRuleLanguage.get(sourceType);
}
}

View File

@ -19,14 +19,14 @@ public class NoInlineStyleInformation extends AbstractJspRule {
/**
* List of HTML element-names that define style.
*/
private static final Set STYLE_ELEMENT_NAMES = CollectionUtil.asSet(
private static final Set<String> STYLE_ELEMENT_NAMES = CollectionUtil.asSet(
new String[]{"B", "I", "FONT", "BASEFONT", "U", "CENTER"}
);
/**
* List of HTML element-names that can have attributes defining style.
*/
private static final Set ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES = CollectionUtil.asSet(
private static final Set<String> ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES = CollectionUtil.asSet(
new String[]{"P", "TABLE", "THEAD", "TBODY", "TFOOT", "TR", "TD", "COL", "COLGROUP"}
);
@ -34,7 +34,7 @@ public class NoInlineStyleInformation extends AbstractJspRule {
* List of attributes that define style when they are attributes of HTML elements with
* names in ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES.
*/
private static final Set STYLE_ATTRIBUTES = CollectionUtil.asSet(
private static final Set<String> STYLE_ATTRIBUTES = CollectionUtil.asSet(
new String[]{"STYLE", "FONT", "SIZE", "COLOR", "FACE", "ALIGN", "VALIGN", "BGCOLOR"}
);

View File

@ -13,36 +13,36 @@ import net.sourceforge.pmd.util.StringUtil;
* @author Brian Remedios
* @version $Revision$
*/
public class EnumeratedProperty extends AbstractPMDProperty {
public class EnumeratedProperty<E> extends AbstractPMDProperty {
private Object[][] choiceTuples;
private Map choicesByLabel;
private Map labelsByChoice;
private Map<String, E> choicesByLabel;
private Map<E, String> labelsByChoice;
/**
* Constructor for EnumeratedProperty.
* @param theName String
* @param theDescription String
* @param theChoices Object[][]
* @param theLabels String[]
* @param theChoices E[]
* @param theUIOrder float
*/
public EnumeratedProperty(String theName, String theDescription, Object[][] theChoices, float theUIOrder) {
this(theName, theDescription, theChoices, theUIOrder, 1);
public EnumeratedProperty(String theName, String theDescription, String[] theLabels, E[] theChoices, float theUIOrder) {
this(theName, theDescription, theLabels, theChoices, theUIOrder, 1);
}
/**
* Constructor for EnumeratedProperty.
* @param theName String
* @param theDescription String
* @param theChoices Object[][]
* @param theLabels String[]
* @param theChoices E[]
* @param theUIOrder float
* @param maxValues int
*/
public EnumeratedProperty(String theName, String theDescription, Object[][] theChoices, float theUIOrder, int maxValues) {
super(theName, theDescription, theChoices[0][1], theUIOrder);
choiceTuples = theChoices;
choicesByLabel = CollectionUtil.mapFrom(theChoices);
public EnumeratedProperty(String theName, String theDescription, String[] theLabels, E[] theChoices, float theUIOrder, int maxValues) {
super(theName, theDescription, theChoices[0], theUIOrder);
choicesByLabel = CollectionUtil.mapFrom(theLabels, theChoices);
labelsByChoice = CollectionUtil.invertedMapFrom(choicesByLabel);
maxValueCount(maxValues);
@ -57,15 +57,6 @@ public class EnumeratedProperty extends AbstractPMDProperty {
return Object.class;
}
/**
* Method choices.
* @return Object[][]
* @see net.sourceforge.pmd.PropertyDescriptor#choices()
*/
public Object[][] choices() {
return choiceTuples;
}
private String nonLegalValueMsgFor(Object value) {
return "" + value + " is not a legal value";
}
@ -94,10 +85,10 @@ public class EnumeratedProperty extends AbstractPMDProperty {
/**
* Method choiceFrom.
* @param label String
* @return Object
* @return E
*/
private Object choiceFrom(String label) {
Object result = choicesByLabel.get(label);
private E choiceFrom(String label) {
E result = choicesByLabel.get(label);
if (result != null) return result;
throw new IllegalArgumentException(label);
}
@ -128,7 +119,7 @@ public class EnumeratedProperty extends AbstractPMDProperty {
*/
public String asDelimitedString(Object value) {
if (maxValueCount() == 1) return (String)labelsByChoice.get(value);
if (maxValueCount() == 1) return labelsByChoice.get(value);
Object[] choices = (Object[])value;

View File

@ -6,7 +6,6 @@ package net.sourceforge.pmd.rules;
import java.util.Set;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.ast.ASTAllocationExpression;
import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.ast.ASTPrimaryExpression;
@ -20,7 +19,7 @@ public class UnnecessaryConversionTemporary extends AbstractRule {
private ASTPrimaryExpression primary;
private boolean usingPrimitiveWrapperAllocation;
private static final Set primitiveWrappers = CollectionUtil.asSet(
private static final Set<String> primitiveWrappers = CollectionUtil.asSet(
new String[] {"Integer", "Boolean", "Double", "Long", "Short", "Byte", "Float"}
);

View File

@ -22,12 +22,12 @@ public class UselessOperationOnImmutable extends AbstractRule {
/**
* These are the methods which are immutable
*/
private static final Set targetMethods = CollectionUtil.asSet(new String[] { ".add", ".multiply", ".divide", ".subtract", ".setScale", ".negate", ".movePointLeft", ".movePointRight", ".pow", ".shiftLeft", ".shiftRight" });
private static final Set<String> targetMethods = CollectionUtil.asSet(new String[] { ".add", ".multiply", ".divide", ".subtract", ".setScale", ".negate", ".movePointLeft", ".movePointRight", ".pow", ".shiftLeft", ".shiftRight" });
/**
* These are the classes that the rule can apply to
*/
private static final Set targetClasses = CollectionUtil.asSet(new String[] { "java.math.BigDecimal", "BigDecimal", "java.math.BigInteger", "BigInteger" });
private static final Set<String> targetClasses = CollectionUtil.asSet(new String[] { "java.math.BigDecimal", "BigDecimal", "java.math.BigInteger", "BigInteger" });
public Object visit(ASTLocalVariableDeclaration node, Object data) {

View File

@ -14,7 +14,7 @@ import net.sourceforge.pmd.util.CollectionUtil;
public class UnnecessaryWrapperObjectCreation extends AbstractRule {
private static final Set prefixSet = CollectionUtil.asSet(new String[] {
private static final Set<String> prefixSet = CollectionUtil.asSet(new String[] {
"Byte.valueOf",
"Short.valueOf",
"Integer.valueOf",
@ -24,7 +24,7 @@ public class UnnecessaryWrapperObjectCreation extends AbstractRule {
"Character.valueOf"
});
private static final Set suffixSet = CollectionUtil.asSet(new String[] {
private static final Set<String> suffixSet = CollectionUtil.asSet(new String[] {
"byteValue",
"shortValue",
"intValue",

View File

@ -12,13 +12,13 @@ import net.sourceforge.pmd.util.CollectionUtil;
*/
public class SourceTypeHandlerBroker {
private static final Map mapSourceTypeOnSourceTypeHandler = CollectionUtil.mapFrom( new Object[][] {
{ SourceType.JAVA_13, new Java13Handler()},
{ SourceType.JAVA_14, new Java14Handler()},
{ SourceType.JAVA_15, new Java15Handler()},
{ SourceType.JAVA_16, new Java16Handler()},
{ SourceType.JSP, new JspTypeHandler()},
});
private static final Map<SourceType, SourceTypeHandler> mapSourceTypeOnSourceTypeHandler = CollectionUtil
.mapFrom(new SourceType[] { SourceType.JAVA_13, SourceType.JAVA_14,
SourceType.JAVA_15, SourceType.JAVA_16, SourceType.JSP, },
new SourceTypeHandler[] { new Java13Handler(), new Java14Handler(),
new Java15Handler(), new Java16Handler(),
new JspTypeHandler(), });
/**
* Never create one
@ -26,7 +26,7 @@ public class SourceTypeHandlerBroker {
private SourceTypeHandlerBroker() { }
public static SourceTypeHandler getVisitorsFactoryForSourceType(SourceType sourceType) {
SourceTypeHandler handler = (SourceTypeHandler) mapSourceTypeOnSourceTypeHandler.get(sourceType);
SourceTypeHandler handler = mapSourceTypeOnSourceTypeHandler.get(sourceType);
if (handler == null) {
throw new IllegalArgumentException("No VisitorsFactory is registered for SourceType [" + sourceType + "].");

View File

@ -87,9 +87,9 @@ public class CollectionUtil {
* @param items Object[]
* @return Set
*/
public static Set asSet(Object[] items) {
public static <T> Set<T> asSet(T[] items) {
Set set = new HashSet(items.length);
Set<T> set = new HashSet<T>(items.length);
for (int i=0; i<items.length; i++) {
set.add(items[i]);
}
@ -103,10 +103,13 @@ public class CollectionUtil {
* @param keyValueSets Object[][]
* @return Map
*/
public static Map mapFrom(Object[][] keyValueSets) {
Map map = new HashMap(keyValueSets.length);
for (int i=0; i<keyValueSets.length; i++) {
map.put(keyValueSets[i][0], keyValueSets[i][1]);
public static <K, V> Map<K, V> mapFrom(K[] keys, V[] values) {
if (keys.length != values.length) {
throw new RuntimeException("mapFrom keys and values arrays have different sizes");
}
Map<K, V> map = new HashMap<K, V>(keys.length);
for (int i=0; i<keys.length; i++) {
map.put(keys[i], values[i]);
}
return map;
}
@ -117,14 +120,11 @@ public class CollectionUtil {
* @param source Map
* @return Map
*/
public static Map invertedMapFrom(Map source) {
Map map = new HashMap(source.size());
Iterator iter = source.entrySet().iterator();
Entry entry;
while (iter.hasNext()) {
entry = (Entry)iter.next();
map.put(entry.getValue(), entry.getKey());
}
public static <K, V> Map<V, K> invertedMapFrom(Map<K, V> source) {
Map<V, K> map = new HashMap<V, K>(source.size());
for (Map.Entry<K, V> entry: source.entrySet()) {
map.put(entry.getValue(), entry.getKey());
}
return map;
}