From 31d4351c2215133f33a37208d1c47ac1ac03b9da Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Sat, 13 Jul 2002 01:58:45 +0000 Subject: [PATCH] starting to work on type resolution git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@345 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/Java1.4-c.jjt | 17 ++-- pmd/rulesets/new_for_0_5.xml | 10 ++ pmd/src/net/sourceforge/pmd/PMD.java | 2 +- pmd/src/net/sourceforge/pmd/TypeSet.java | 2 + .../net/sourceforge/pmd/ast/JavaParser.java | 98 ++++++++++++------- .../pmd/rules/UnnecessaryCastRule.java | 40 ++++++-- pmd/test-data/UnnecessaryCast1.java | 5 + 7 files changed, 118 insertions(+), 56 deletions(-) create mode 100644 pmd/test-data/UnnecessaryCast1.java diff --git a/pmd/etc/Java1.4-c.jjt b/pmd/etc/Java1.4-c.jjt index f44474b4e9..ad99b388e7 100644 --- a/pmd/etc/Java1.4-c.jjt +++ b/pmd/etc/Java1.4-c.jjt @@ -594,23 +594,24 @@ void Type() : void PrimitiveType() : {} { - "boolean" + "boolean" {jjtThis.setImage("boolean");} | - "char" + "char" {jjtThis.setImage("char");} | - "byte" + "byte" {jjtThis.setImage("byte");} | - "short" + "short" {jjtThis.setImage("short");} | - "int" + "int" {jjtThis.setImage("int");} | - "long" + "long" {jjtThis.setImage("long");} | - "float" + "float" {jjtThis.setImage("float");} | - "double" + "double" {jjtThis.setImage("double");} } + void ResultType() : {} { diff --git a/pmd/rulesets/new_for_0_5.xml b/pmd/rulesets/new_for_0_5.xml index 7e5bdccb04..56b64b01d5 100644 --- a/pmd/rulesets/new_for_0_5.xml +++ b/pmd/rulesets/new_for_0_5.xml @@ -5,7 +5,17 @@ These are new ones for 0.5 + + + + + + + diff --git a/pmd/src/net/sourceforge/pmd/PMD.java b/pmd/src/net/sourceforge/pmd/PMD.java index ac74b5ef06..d8abedfb7b 100644 --- a/pmd/src/net/sourceforge/pmd/PMD.java +++ b/pmd/src/net/sourceforge/pmd/PMD.java @@ -30,7 +30,7 @@ public class PMD { try { JavaParser parser = new JavaParser(reader); ASTCompilationUnit c = parser.CompilationUnit(); - //c.dump(""); + c.dump(""); List acus = new ArrayList(); acus.add(c); ruleSet.apply(acus, ctx); diff --git a/pmd/src/net/sourceforge/pmd/TypeSet.java b/pmd/src/net/sourceforge/pmd/TypeSet.java index 7bc0e485bd..1d4412662b 100644 --- a/pmd/src/net/sourceforge/pmd/TypeSet.java +++ b/pmd/src/net/sourceforge/pmd/TypeSet.java @@ -84,6 +84,8 @@ public class TypeSet { primitiveTypes.put("long", long.class); primitiveTypes.put("boolean", boolean.class); primitiveTypes.put("byte", byte.class); + primitiveTypes.put("short", short.class); + primitiveTypes.put("char", char.class); } public Class resolve(String name) throws ClassNotFoundException { if (!primitiveTypes.containsKey(name)) { diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaParser.java b/pmd/src/net/sourceforge/pmd/ast/JavaParser.java index c0ae2efd96..82b22e7a95 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaParser.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaParser.java @@ -1714,27 +1714,51 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case BOOLEAN: jj_consume_token(BOOLEAN); + jjtree.closeNodeScope(jjtn000, true); + jjtc000 = false; + jjtn000.setImage("boolean"); break; case CHAR: jj_consume_token(CHAR); + jjtree.closeNodeScope(jjtn000, true); + jjtc000 = false; + jjtn000.setImage("char"); break; case BYTE: jj_consume_token(BYTE); + jjtree.closeNodeScope(jjtn000, true); + jjtc000 = false; + jjtn000.setImage("byte"); break; case SHORT: jj_consume_token(SHORT); + jjtree.closeNodeScope(jjtn000, true); + jjtc000 = false; + jjtn000.setImage("short"); break; case INT: jj_consume_token(INT); + jjtree.closeNodeScope(jjtn000, true); + jjtc000 = false; + jjtn000.setImage("int"); break; case LONG: jj_consume_token(LONG); + jjtree.closeNodeScope(jjtn000, true); + jjtc000 = false; + jjtn000.setImage("long"); break; case FLOAT: jj_consume_token(FLOAT); + jjtree.closeNodeScope(jjtn000, true); + jjtc000 = false; + jjtn000.setImage("float"); break; case DOUBLE: jj_consume_token(DOUBLE); + jjtree.closeNodeScope(jjtn000, true); + jjtc000 = false; + jjtn000.setImage("double"); break; default: jj_la1[46] = jj_gen; @@ -4907,12 +4931,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return retval; } - final private boolean jj_3R_300() { - if (jj_scan_token(MINUS)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_296() { if (jj_scan_token(LSHIFT)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -6661,6 +6679,14 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } + final private boolean jj_3R_381() { + if (jj_scan_token(COLON)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + if (jj_3R_60()) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + final private boolean jj_3R_85() { if (jj_scan_token(PUBLIC)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -6681,14 +6707,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_381() { - if (jj_scan_token(COLON)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - if (jj_3R_60()) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3_5() { Token xsp; xsp = jj_scanpos; @@ -6995,23 +7013,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_175() { - if (jj_scan_token(CLASS)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - if (jj_scan_token(IDENTIFIER)) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - Token xsp; - xsp = jj_scanpos; - if (jj_3R_309()) jj_scanpos = xsp; - else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - xsp = jj_scanpos; - if (jj_3R_310()) jj_scanpos = xsp; - else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - if (jj_3R_245()) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_226() { if (jj_scan_token(TRY)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -7029,6 +7030,23 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } + final private boolean jj_3R_175() { + if (jj_scan_token(CLASS)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + if (jj_scan_token(IDENTIFIER)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + Token xsp; + xsp = jj_scanpos; + if (jj_3R_309()) jj_scanpos = xsp; + else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + xsp = jj_scanpos; + if (jj_3R_310()) jj_scanpos = xsp; + else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + if (jj_3R_245()) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + final private boolean jj_3R_42() { Token xsp; xsp = jj_scanpos; @@ -7052,6 +7070,12 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } + final private boolean jj_3R_375() { + if (jj_3R_387()) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + final private boolean jj_3_1() { Token xsp; while (true) { @@ -7064,12 +7088,6 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } - final private boolean jj_3R_375() { - if (jj_3R_387()) return true; - if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; - return false; - } - final private boolean jj_3R_225() { if (jj_scan_token(SYNCHRONIZED)) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; @@ -8519,6 +8537,12 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java return false; } + final private boolean jj_3R_300() { + if (jj_scan_token(MINUS)) return true; + if (jj_la == 0 && jj_scanpos == jj_lastpos) return false; + return false; + } + public JavaParserTokenManager token_source; JavaCharStream jj_input_stream; public Token token, jj_nt; diff --git a/pmd/src/net/sourceforge/pmd/rules/UnnecessaryCastRule.java b/pmd/src/net/sourceforge/pmd/rules/UnnecessaryCastRule.java index 78a25d410d..b2c6739181 100644 --- a/pmd/src/net/sourceforge/pmd/rules/UnnecessaryCastRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/UnnecessaryCastRule.java @@ -6,25 +6,45 @@ package net.sourceforge.pmd.rules; import net.sourceforge.pmd.AbstractRule; +import net.sourceforge.pmd.TypeSet; import net.sourceforge.pmd.ast.ASTName; import net.sourceforge.pmd.ast.ASTCastExpression; +import net.sourceforge.pmd.ast.ASTPrimitiveType; +import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration; public class UnnecessaryCastRule extends AbstractRule { -/* - public Object visit(ASTCastExpression node, Object data) { - System.out.println("CAST"); - return super.visit(node, data); - } - public Object visit(ASTName node, Object data) { + private boolean inCastCtx; + + // TODO look for things that involve casts: + // AssignmentExpression: int x = (int)2; + // ArgumentList: System.out.println((int)2); + + public Object visit(ASTCastExpression node, Object data) { + inCastCtx = true; + super.visit(node,data); + inCastCtx = false; + return data; + } + + public Object visit(ASTName node, Object data) { try { - System.out.println("name = " + node.getImage()); - //Class.forName() - System.out.println(System.getProperty("java.class.path")); + if (inCastCtx) { + TypeSet t = new TypeSet(); + System.out.println(t.findClass(node.getImage())); + } } catch (Exception e) {} return super.visit(node, data); } -*/ + public Object visit(ASTPrimitiveType node, Object data) { + try { + if (inCastCtx) { + TypeSet t = new TypeSet(); + System.out.println(t.findClass(node.getImage())); + } + } catch (Exception e) {} + return super.visit(node, data); + } } diff --git a/pmd/test-data/UnnecessaryCast1.java b/pmd/test-data/UnnecessaryCast1.java new file mode 100644 index 0000000000..4623251143 --- /dev/null +++ b/pmd/test-data/UnnecessaryCast1.java @@ -0,0 +1,5 @@ + public class UnnecessaryCast1 { + void foo () { + int x = (int)2; + } + } \ No newline at end of file