starting to work on type resolution

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@345 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-07-13 01:58:45 +00:00
parent 82e88de5ac
commit 31d4351c22
7 changed files with 118 additions and 56 deletions

View File

@ -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() :
{}
{

View File

@ -5,7 +5,17 @@
These are new ones for 0.5
</description>
<rule name="UnnecessaryCast"
message="Avoid unnecessary casts"
class="net.sourceforge.pmd.rules.UnnecessaryCastRule">
<description>
</description>
<example>
<![CDATA[
]]>
</example>
</rule>
</ruleset>

View File

@ -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);

View File

@ -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)) {

View File

@ -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;

View File

@ -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);
}
}

View File

@ -0,0 +1,5 @@
public class UnnecessaryCast1 {
void foo () {
int x = (int)2;
}
}