@Override protected Object addValueIn(Control widget, PropertyDescriptor<?> desc, PropertySource source) { int idx = ((Combo) widget).getSelectionIndex(); if (idx < 0) return null; String newValue = ((Combo) widget).getItem(idx); String[] currentValues = (String[]) valueFor(source, desc); String[] newValues = CollectionUtil.addWithoutDuplicates(currentValues, newValue); if (currentValues.length == newValues.length) return null; source.setProperty((EnumeratedMultiProperty<?>) desc, newValues); return newValue; }
public class UnnecessaryConversionTemporaryRule extends AbstractJavaRule { private boolean inPrimaryExpressionContext; private ASTPrimaryExpression primary; private boolean usingPrimitiveWrapperAllocation; private static final Set<String> PRIMITIVE_WRAPPERS = CollectionUtil.asSet( new String[] {"Integer", "Boolean", "Double", "Long", "Short", "Byte", "Float"}); public Object visit(ASTPrimaryExpression node, Object data) { if (node.jjtGetNumChildren() == 0 || (node.jjtGetChild(0)).jjtGetNumChildren() == 0 || !(node.jjtGetChild(0).jjtGetChild(0) instanceof ASTAllocationExpression)) { return super.visit(node, data); } // TODO... hmmm... is this inPrimaryExpressionContext gibberish necessary? inPrimaryExpressionContext = true; primary = node; super.visit(node, data); inPrimaryExpressionContext = false; usingPrimitiveWrapperAllocation = false; return data; } public Object visit(ASTAllocationExpression node, Object data) { if (!inPrimaryExpressionContext || !(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) { return super.visit(node, data); } if (!PRIMITIVE_WRAPPERS.contains(node.jjtGetChild(0).getImage())) { return super.visit(node, data); } usingPrimitiveWrapperAllocation = true; return super.visit(node, data); } public Object visit(ASTPrimarySuffix node, Object data) { if (inPrimaryExpressionContext && usingPrimitiveWrapperAllocation) { if (node.hasImageEqualTo("toString")) { if (node.jjtGetParent() == primary) { addViolation(data, node); } } } return super.visit(node, data); } }
/** {@inheritDoc} */ @Override public boolean usesDefaultValues() { Map<PropertyDescriptor<?>, Object> valuesByProperty = getPropertiesByPropertyDescriptor(); if (valuesByProperty.isEmpty()) { return true; } Iterator<Map.Entry<PropertyDescriptor<?>, Object>> iter = valuesByProperty.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<PropertyDescriptor<?>, Object> entry = iter.next(); if (!CollectionUtil.areEqual(entry.getKey().defaultValue(), entry.getValue())) { return false; } } return true; }
public class UnnecessaryWrapperObjectCreationRule extends AbstractJavaRule { private static final Set<String> PREFIX_SET = CollectionUtil.asSet( new String[] { "Byte.valueOf", "Short.valueOf", "Integer.valueOf", "Long.valueOf", "Float.valueOf", "Double.valueOf", "Character.valueOf" }); private static final Set<String> SUFFIX_SET = CollectionUtil.asSet( new String[] { "toString", "byteValue", "shortValue", "intValue", "longValue", "floatValue", "doubleValue", "charValue" }); public Object visit(ASTPrimaryPrefix node, Object data) { if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) { return super.visit(node, data); } String image = ((ASTName) node.jjtGetChild(0)).getImage(); if (image.startsWith("java.lang.")) { image = image.substring(10); } boolean checkBoolean = ((RuleContext) data) .getLanguageVersion() .compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5")) >= 0; if (PREFIX_SET.contains(image) || (checkBoolean && "Boolean.valueOf".equals(image))) { ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent(); if (parent.jjtGetNumChildren() >= 3) { Node n = parent.jjtGetChild(2); if (n instanceof ASTPrimarySuffix) { ASTPrimarySuffix suffix = (ASTPrimarySuffix) n; image = suffix.getImage(); if (SUFFIX_SET.contains(image) || (checkBoolean && "booleanValue".equals(image))) { super.addViolation(data, node); return data; } } } } return super.visit(node, data); } }
public boolean appliesToClassName(String name) { return CollectionUtil.isCollectionType(name, true); }