Checking in some Java 5 changes

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4991 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2007-01-28 00:59:32 +00:00
parent 45e310cfe4
commit 84bbe57a2a
7 changed files with 47 additions and 50 deletions

View File

@ -7,13 +7,12 @@ import net.sourceforge.pmd.jsp.ast.JspParser;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class AbstractJspNodesTst extends TestCase {
public void assertNumberOfNodes(Class clazz, String source, int number) {
Set nodes = getNodes(clazz, source);
public <T> void assertNumberOfNodes(Class<T> clazz, String source, int number) {
Set<T> nodes = getNodes(clazz, source);
assertEquals("Exactly " + number + " element(s) expected", number, nodes.size());
}
@ -24,10 +23,10 @@ public class AbstractJspNodesTst extends TestCase {
* @param source
* @return Set
*/
public Set getNodes(Class clazz, String source) {
public <T> Set<T> getNodes(Class<T> clazz, String source) {
JspParser parser = new JspParser(new JspCharStream(new StringReader(source)));
Node rootNode = parser.CompilationUnit();
Set nodes = new HashSet();
Set<T> nodes = new HashSet<T>();
addNodeAndSubnodes(rootNode, nodes, clazz);
return nodes;
}
@ -40,12 +39,11 @@ public class AbstractJspNodesTst extends TestCase {
* @param allNodes
* @return Set
*/
public Set getNodesOfType(Class clazz, Set allNodes) {
Set result = new HashSet();
for (Iterator i = allNodes.iterator(); i.hasNext();) {
Object node = i.next();
public <T> Set<T> getNodesOfType(Class<T> clazz, Set allNodes) {
Set<T> result = new HashSet<T>();
for (Object node: allNodes) {
if (clazz.equals(node.getClass())) {
result.add(node);
result.add((T)node);
}
}
return result;
@ -59,10 +57,10 @@ public class AbstractJspNodesTst extends TestCase {
* @param nodex
* @param clazz
*/
private void addNodeAndSubnodes(Node node, Set nodes, Class clazz) {
private <T> void addNodeAndSubnodes(Node node, Set<T> nodes, Class<T> clazz) {
if (null != node) {
if ((null == clazz) || (clazz.equals(node.getClass()))) {
nodes.add(node);
nodes.add((T)node);
}
}
for (int i = 0; i < node.jjtGetNumChildren(); i++) {

View File

@ -39,9 +39,9 @@ public class JspDocStyleTest extends AbstractJspNodesTst {
public void testElementAttributeAndNamespace() throws Throwable {
Set nodes = getNodes(null, TEST_ELEMENT_AND_NAMESPACE);
Set elementNodes = getNodesOfType(ASTElement.class, nodes);
Set<ASTElement> elementNodes = getNodesOfType(ASTElement.class, nodes);
assertEquals("One element node expected!", 1, elementNodes.size());
ASTElement element = (ASTElement) elementNodes.iterator().next();
ASTElement element = elementNodes.iterator().next();
assertEquals("Correct name expected!", "h:html", element.getName());
assertEquals("Has namespace prefix!", true, element.isHasNamespacePrefix());
assertEquals("Element is empty!", true, element.isEmpty());
@ -72,30 +72,29 @@ public class JspDocStyleTest extends AbstractJspNodesTst {
{
Set nodes = getNodes(null, TEST_ATTRIBUTE_VALUE_CONTAINING_HASH);
Set attributes = getNodesOfType(ASTAttribute.class, nodes);
Set<ASTAttribute> attributes = getNodesOfType(ASTAttribute.class, nodes);
assertEquals("Three attributes expected!", 3, attributes.size());
List attrsList = new ArrayList(attributes);
Collections.sort(attrsList, new Comparator() {
public int compare(Object arg0, Object arg1) {
return ((ASTAttribute)arg0).getName().compareTo(
((ASTAttribute)arg1).getName() );
List<ASTAttribute> attrsList = new ArrayList<ASTAttribute>(attributes);
Collections.sort(attrsList, new Comparator<ASTAttribute>() {
public int compare(ASTAttribute arg0, ASTAttribute arg1) {
return arg0.getName().compareTo(arg1.getName());
}
});
ASTAttribute attr = (ASTAttribute) attrsList.get(0);
ASTAttribute attr = attrsList.get(0);
assertEquals("Correct attribute name expected!",
"foo", attr.getName());
assertEquals("Correct attribute value expected!",
"CREATE", ((ASTAttributeValue) attr.getFirstChildOfType(ASTAttributeValue.class)).getImage());
attr = (ASTAttribute) attrsList.get(1);
attr = attrsList.get(1);
assertEquals("Correct attribute name expected!",
"href", attr.getName());
assertEquals("Correct attribute value expected!",
"#", ((ASTAttributeValue) attr.getFirstChildOfType(ASTAttributeValue.class)).getImage());
attr = (ASTAttribute) attrsList.get(2);
attr = attrsList.get(2);
assertEquals("Correct attribute name expected!",
"something", attr.getName());
assertEquals("Correct attribute value expected!",
@ -120,10 +119,10 @@ public class JspDocStyleTest extends AbstractJspNodesTst {
public void testDoctype() {
Set nodes = getNodes(null, TEST_DOCTYPE);
Set docTypeDeclarations = getNodesOfType(ASTDoctypeDeclaration.class, nodes);
Set<ASTDoctypeDeclaration> docTypeDeclarations = getNodesOfType(ASTDoctypeDeclaration.class, nodes);
assertEquals("One doctype declaration expected!", 1, docTypeDeclarations
.size());
ASTDoctypeDeclaration docTypeDecl = (ASTDoctypeDeclaration) docTypeDeclarations
ASTDoctypeDeclaration docTypeDecl = docTypeDeclarations
.iterator().next();
assertEquals("Correct doctype-name expected!", "html", docTypeDecl.getName());

View File

@ -34,29 +34,29 @@ public class JspPageStyleTest extends AbstractJspNodesTst {
public void testDirective() {
Set nodes = getNodes(null, JSP_DIRECTIVE);
Set directives = getNodesOfType(ASTJspDirective.class, nodes);
Set<ASTJspDirective> directives = getNodesOfType(ASTJspDirective.class, nodes);
assertEquals("One directive expected!", 1, directives.size());
ASTJspDirective directive = (ASTJspDirective) directives.iterator().next();
ASTJspDirective directive = directives.iterator().next();
assertEquals("Correct directive name expected!",
"page", directive.getName());
Set directiveAttrs = getNodesOfType(ASTJspDirectiveAttribute.class, nodes);
Set<ASTJspDirectiveAttribute> directiveAttrs = getNodesOfType(ASTJspDirectiveAttribute.class, nodes);
assertEquals("Two directive attributes expected!", 2, directiveAttrs.size());
List attrsList = new ArrayList(directiveAttrs);
Collections.sort(attrsList, new Comparator() {
public int compare(Object arg0, Object arg1) {
return ((ASTJspDirectiveAttribute) arg0).getName().compareTo(((ASTJspDirectiveAttribute) arg1).getName());
List<ASTJspDirectiveAttribute> attrsList = new ArrayList<ASTJspDirectiveAttribute>(directiveAttrs);
Collections.sort(attrsList, new Comparator<ASTJspDirectiveAttribute>() {
public int compare(ASTJspDirectiveAttribute arg0, ASTJspDirectiveAttribute arg1) {
return arg0.getName().compareTo(arg1.getName());
}
});
ASTJspDirectiveAttribute attr = (ASTJspDirectiveAttribute) attrsList.get(0);
ASTJspDirectiveAttribute attr = attrsList.get(0);
assertEquals("Correct directive attribute name expected!",
"language", attr.getName());
assertEquals("Correct directive attribute value expected!",
"java", attr.getValue());
attr = (ASTJspDirectiveAttribute) attrsList.get(1);
attr = attrsList.get(1);
assertEquals("Correct directive attribute name expected!",
"session", attr.getName());
assertEquals("Correct directive attribute value expected!",

View File

@ -18,9 +18,9 @@ import java.util.Stack;
*/
public class Structure {
private LinkedList dataFlow = new LinkedList();
private Stack braceStack = new Stack();
private Stack continueBreakReturnStack = new Stack();
private LinkedList<DataFlowNode> dataFlow = new LinkedList<DataFlowNode>();
private Stack<StackObject> braceStack = new Stack<StackObject>();
private Stack<StackObject> continueBreakReturnStack = new Stack<StackObject>();
/**
* This class encapsulates the access to the DataFlowNode class. Is this worthwhile?
@ -41,11 +41,11 @@ public class Structure {
}
public IDataFlowNode getLast() {
return (IDataFlowNode) this.dataFlow.getLast();
return this.dataFlow.getLast();
}
public IDataFlowNode getFirst() {
return (IDataFlowNode) this.dataFlow.getFirst();
return this.dataFlow.getFirst();
}
// ----------------------------------------------------------------------------

View File

@ -30,7 +30,7 @@ import java.util.Set;
*/
public class UnsynchronizedStaticDateFormatter extends AbstractRule {
private static Set targets = new HashSet();
private static Set<String> targets = new HashSet<String>();
static {
targets.add("DateFormat");
targets.add("SimpleDateFormat");

View File

@ -65,7 +65,7 @@ public class AvoidDuplicateLiteralsRule extends AbstractRule {
private static final String SEPARATOR_PROPERTY = "separator";
private static final String EXCEPTION_FILE_NAME_PROPERTY = "exceptionfile";
private Map<String, List> literals = new HashMap<String, List>();
private Map<String, List<ASTLiteral>> literals = new HashMap<String, List<ASTLiteral>>();
private Set<String> exceptions = new HashSet<String>();
public Object visit(ASTCompilationUnit node, Object data) {
@ -104,10 +104,10 @@ public class AvoidDuplicateLiteralsRule extends AbstractRule {
int threshold = getIntProperty("threshold");
for (String key: literals.keySet()) {
List occurrences = (List) literals.get(key);
List<ASTLiteral> occurrences = literals.get(key);
if (occurrences.size() >= threshold) {
Object[] args = new Object[]{key, Integer.valueOf(occurrences.size()), Integer.valueOf(((SimpleNode) occurrences.get(0)).getBeginLine())};
addViolation(data, (SimpleNode) occurrences.get(0), args);
Object[] args = new Object[]{key, Integer.valueOf(occurrences.size()), Integer.valueOf(occurrences.get(0).getBeginLine())};
addViolation(data, occurrences.get(0), args);
}
}
return data;
@ -125,10 +125,10 @@ public class AvoidDuplicateLiteralsRule extends AbstractRule {
}
if (literals.containsKey(node.getImage())) {
List occurrences = (List) literals.get(node.getImage());
List<ASTLiteral> occurrences = literals.get(node.getImage());
occurrences.add(node);
} else {
List occurrences = new ArrayList();
List<ASTLiteral> occurrences = new ArrayList<ASTLiteral>();
occurrences.add(node);
literals.put(node.getImage(), occurrences);
}

View File

@ -20,7 +20,7 @@ import java.util.List;
public class SimpleNodeTreeNodeAdapter implements TreeNode {
private SimpleNode node;
private List children;
private List<TreeNode> children;
private SimpleNodeTreeNodeAdapter parent;
/**
@ -48,7 +48,7 @@ public class SimpleNodeTreeNodeAdapter implements TreeNode {
*/
public TreeNode getChildAt(int childIndex) {
checkChildren();
return (TreeNode) children.get(childIndex);
return children.get(childIndex);
}
@ -99,7 +99,7 @@ public class SimpleNodeTreeNodeAdapter implements TreeNode {
* @see javax.swing.tree.TreeNode#children()
*/
public Enumeration children() {
public Enumeration<TreeNode> children() {
return Collections.enumeration(children);
}
@ -109,7 +109,7 @@ public class SimpleNodeTreeNodeAdapter implements TreeNode {
*/
private void checkChildren() {
if (children == null) {
children = new ArrayList(node.jjtGetNumChildren());
children = new ArrayList<TreeNode>(node.jjtGetNumChildren());
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
children.add(new SimpleNodeTreeNodeAdapter(this, (SimpleNode) node.jjtGetChild(i)));
}