dogfood, whitespaces

This commit is contained in:
Andreas Dangel
2015-03-23 20:16:03 +01:00
parent b7bf3fbb8c
commit 2fdb51fcb6
17 changed files with 800 additions and 833 deletions

View File

@ -56,11 +56,11 @@ public class CsTokenizer implements Tokenizer {
} else if (ic == c) { } else if (ic == c) {
ic = reader.read(); ic = reader.read();
if (ic == '=') { if (ic == '=') {
tokenEntries.add(new TokenEntry(String.valueOf(c) + String.valueOf(c) + "=", sourceCode tokenEntries.add(new TokenEntry(c + c + "=", sourceCode
.getFileName(), line)); .getFileName(), line));
ic = reader.read(); ic = reader.read();
} else { } else {
tokenEntries.add(new TokenEntry(String.valueOf(c) + String.valueOf(c), sourceCode tokenEntries.add(new TokenEntry(String.valueOf(c) + c, sourceCode
.getFileName(), line)); .getFileName(), line));
} }
} else { } else {
@ -76,7 +76,7 @@ public class CsTokenizer implements Tokenizer {
case '-': case '-':
ic = reader.read(); ic = reader.read();
if (ic == '=' || ic == c) { if (ic == '=' || ic == c) {
tokenEntries.add(new TokenEntry(String.valueOf(c) + String.valueOf((char) ic), sourceCode tokenEntries.add(new TokenEntry(c + String.valueOf((char) ic), sourceCode
.getFileName(), line)); .getFileName(), line));
ic = reader.read(); ic = reader.read();
} else { } else {

View File

@ -6,8 +6,8 @@ package net.sourceforge.pmd.cpd;
import java.io.IOException; import java.io.IOException;
import net.sourceforge.pmd.testframework.AbstractTokenizerTest; import net.sourceforge.pmd.testframework.AbstractTokenizerTest;
import net.sourceforge.pmd.testframework.StreamUtil;
import org.apache.commons.io.IOUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -18,14 +18,14 @@ public class MatlabTokenizerTest extends AbstractTokenizerTest {
@Before @Before
@Override @Override
public void buildTokenizer() { public void buildTokenizer() throws IOException {
this.tokenizer = new MatlabTokenizer(); this.tokenizer = new MatlabTokenizer();
this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME)); this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME));
} }
@Override @Override
public String getSampleCode() { public String getSampleCode() throws IOException {
return StreamUtil.toString(MatlabTokenizer.class.getResourceAsStream(FILENAME)); return IOUtils.toString(MatlabTokenizer.class.getResourceAsStream(FILENAME));
} }
@Test @Test
@ -33,8 +33,4 @@ public class MatlabTokenizerTest extends AbstractTokenizerTest {
this.expectedTokenCount = 3925; this.expectedTokenCount = 3925;
super.tokenizeTest(); super.tokenizeTest();
} }
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(MatlabTokenizerTest.class);
}
} }

View File

@ -6,8 +6,8 @@ package net.sourceforge.pmd.cpd;
import java.io.IOException; import java.io.IOException;
import net.sourceforge.pmd.testframework.AbstractTokenizerTest; import net.sourceforge.pmd.testframework.AbstractTokenizerTest;
import net.sourceforge.pmd.testframework.StreamUtil;
import org.apache.commons.io.IOUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -18,14 +18,14 @@ public class ObjectiveCTokenizerTest extends AbstractTokenizerTest {
@Before @Before
@Override @Override
public void buildTokenizer() { public void buildTokenizer() throws IOException {
this.tokenizer = new ObjectiveCTokenizer(); this.tokenizer = new ObjectiveCTokenizer();
this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME)); this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME));
} }
@Override @Override
public String getSampleCode() { public String getSampleCode() throws IOException {
return StreamUtil.toString(ObjectiveCTokenizer.class.getResourceAsStream(FILENAME)); return IOUtils.toString(ObjectiveCTokenizer.class.getResourceAsStream(FILENAME));
} }
@Test @Test
@ -33,8 +33,4 @@ public class ObjectiveCTokenizerTest extends AbstractTokenizerTest {
this.expectedTokenCount = 884; this.expectedTokenCount = 884;
super.tokenizeTest(); super.tokenizeTest();
} }
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(ObjectiveCTokenizerTest.class);
}
} }

View File

@ -17,7 +17,6 @@ import net.sourceforge.pmd.lang.plsql.rule.AbstractPLSQLRule;
import net.sourceforge.pmd.lang.rule.properties.IntegerProperty; import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;
import net.sourceforge.pmd.util.NumericConstants; import net.sourceforge.pmd.util.NumericConstants;
public class TooManyFieldsRule extends AbstractPLSQLRule { public class TooManyFieldsRule extends AbstractPLSQLRule {
private static final int DEFAULT_MAXFIELDS = 15; private static final int DEFAULT_MAXFIELDS = 15;
@ -25,19 +24,16 @@ public class TooManyFieldsRule extends AbstractPLSQLRule {
private Map<String, Integer> stats; private Map<String, Integer> stats;
private Map<String, PLSQLNode> nodes; private Map<String, PLSQLNode> nodes;
private static final IntegerProperty MAX_FIELDS_DESCRIPTOR = new IntegerProperty( private static final IntegerProperty MAX_FIELDS_DESCRIPTOR = new IntegerProperty("maxfields",
"maxfields", "Max allowable fields", "Max allowable fields", 1, 300, DEFAULT_MAXFIELDS, 1.0f);
1, 300, DEFAULT_MAXFIELDS, 1.0f
);
public TooManyFieldsRule() { public TooManyFieldsRule() {
definePropertyDescriptor(MAX_FIELDS_DESCRIPTOR); definePropertyDescriptor(MAX_FIELDS_DESCRIPTOR);
} }
@Override @Override
public Object visit(ASTInput node, Object data) { public Object visit(ASTInput node, Object data) {
stats = new HashMap<String, Integer>(5); stats = new HashMap<String, Integer>(5);
nodes = new HashMap<String, PLSQLNode>(5); nodes = new HashMap<String, PLSQLNode>(5);
@ -48,11 +44,11 @@ public class TooManyFieldsRule extends AbstractPLSQLRule {
public Object visit(ASTPackageSpecification node, Object data) { public Object visit(ASTPackageSpecification node, Object data) {
int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR); int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR);
List<ASTVariableOrConstantDeclaration> l = node.findDescendantsOfType(ASTVariableOrConstantDeclaration.class); List<ASTVariableOrConstantDeclaration> l = node.findDescendantsOfType(ASTVariableOrConstantDeclaration.class);
for (ASTVariableOrConstantDeclaration fd: l) { for (ASTVariableOrConstantDeclaration fd : l) {
bumpCounterFor(node); bumpCounterFor(fd);
} }
for (String k : stats.keySet()) { for (String k : stats.keySet()) {
int val = stats.get(k); int val = stats.get(k);
@ -68,11 +64,11 @@ public class TooManyFieldsRule extends AbstractPLSQLRule {
public Object visit(ASTTypeSpecification node, Object data) { public Object visit(ASTTypeSpecification node, Object data) {
int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR); int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR);
List<ASTVariableOrConstantDeclaration> l = node.findDescendantsOfType(ASTVariableOrConstantDeclaration.class); List<ASTVariableOrConstantDeclaration> l = node.findDescendantsOfType(ASTVariableOrConstantDeclaration.class);
for (ASTVariableOrConstantDeclaration fd: l) { for (ASTVariableOrConstantDeclaration fd : l) {
bumpCounterFor(node); bumpCounterFor(fd);
} }
for (String k : stats.keySet()) { for (String k : stats.keySet()) {
int val = stats.get(k); int val = stats.get(k);

View File

@ -17,11 +17,13 @@ import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence; import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
public class ClassScope extends AbstractScope { public class ClassScope extends AbstractScope {
private final static Logger LOGGER = Logger.getLogger(ClassScope.class.getName()); private final static Logger LOGGER = Logger.getLogger(ClassScope.class.getName());
// FIXME - this breaks given sufficiently nested code // FIXME - this breaks given sufficiently nested code
private static ThreadLocal<Integer> anonymousInnerClassCounter = new ThreadLocal<Integer>() { private static ThreadLocal<Integer> anonymousInnerClassCounter = new ThreadLocal<Integer>() {
protected Integer initialValue() { return Integer.valueOf(1); } protected Integer initialValue() {
return Integer.valueOf(1);
}
}; };
private String className; private String className;
@ -34,12 +36,13 @@ public class ClassScope extends AbstractScope {
/** /**
* This is only for anonymous inner classes * This is only for anonymous inner classes
* <p/> * <p/>
* FIXME - should have name like Foo$1, not Anonymous$1 * FIXME - should have name like Foo$1, not Anonymous$1 to get this working
* to get this working right, the parent scope needs * right, the parent scope needs to be passed in when instantiating a
* to be passed in when instantiating a ClassScope * ClassScope
*/ */
public ClassScope() { public ClassScope() {
//this.className = getParent().getEnclosingClassScope().getClassName() + "$" + String.valueOf(anonymousInnerClassCounter); // this.className = getParent().getEnclosingClassScope().getClassName()
// + "$" + String.valueOf(anonymousInnerClassCounter);
int v = anonymousInnerClassCounter.get().intValue(); int v = anonymousInnerClassCounter.get().intValue();
this.className = "Anonymous$" + v; this.className = "Anonymous$" + v;
anonymousInnerClassCounter.set(v + 1); anonymousInnerClassCounter.set(v + 1);
@ -55,7 +58,7 @@ public class ClassScope extends AbstractScope {
@Override @Override
public NameDeclaration addNameOccurrence(NameOccurrence occ) { public NameDeclaration addNameOccurrence(NameOccurrence occ) {
PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence)occ; PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence) occ;
NameDeclaration decl = findVariableHere(occurrence); NameDeclaration decl = findVariableHere(occurrence);
Map<MethodNameDeclaration, List<NameOccurrence>> methodNames = getMethodDeclarations(); Map<MethodNameDeclaration, List<NameOccurrence>> methodNames = getMethodDeclarations();
if (decl != null && occurrence.isMethodOrConstructorInvocation()) { if (decl != null && occurrence.isMethodOrConstructorInvocation()) {
@ -113,15 +116,16 @@ public class ClassScope extends AbstractScope {
if (variableDeclarations.isEmpty() && methodDeclarations.isEmpty()) { if (variableDeclarations.isEmpty() && methodDeclarations.isEmpty()) {
// this could happen if you do this: // this could happen if you do this:
// public class Foo { // public class Foo {
// private String x = super.toString(); // private String x = super.toString();
// } // }
return null; return null;
} }
// return any name declaration, since all we really want is to get the scope // return any name declaration, since all we really want is to get
// the scope
// for example, if there's a // for example, if there's a
// public class Foo { // public class Foo {
// private static final int X = 2; // private static final int X = 2;
// private int y = Foo.X; // private int y = Foo.X;
// } // }
// we'll look up Foo just to get a handle to the class scope // we'll look up Foo just to get a handle to the class scope
// and then we'll look up X. // and then we'll look up X.
@ -132,14 +136,16 @@ public class ClassScope extends AbstractScope {
} }
if (occurrence.isMethodOrConstructorInvocation()) { if (occurrence.isMethodOrConstructorInvocation()) {
for (MethodNameDeclaration mnd: methodDeclarations.keySet()) { for (MethodNameDeclaration mnd : methodDeclarations.keySet()) {
if (mnd.getImage().equals(occurrence.getImage())) { if (mnd.getImage().equals(occurrence.getImage())) {
int args = occurrence.getArgumentCount(); int args = occurrence.getArgumentCount();
if (args == mnd.getParameterCount() || (mnd.isVarargs() && args >= mnd.getParameterCount() - 1)) { if (args == mnd.getParameterCount() || mnd.isVarargs() && args >= mnd.getParameterCount() - 1) {
// FIXME if several methods have the same name // FIXME if several methods have the same name
// and parameter count, only one will get caught here // and parameter count, only one will get caught here
// we need to make some attempt at type lookup and discrimination // we need to make some attempt at type lookup and
// or, failing that, mark this as a usage of all those methods // discrimination
// or, failing that, mark this as a usage of all those
// methods
return mnd; return mnd;
} }
} }
@ -150,16 +156,13 @@ public class ClassScope extends AbstractScope {
List<String> images = new ArrayList<String>(); List<String> images = new ArrayList<String>();
images.add(occurrence.getImage()); images.add(occurrence.getImage());
if (null==occurrence.getImage()) { if (null == occurrence.getImage()) {
if (LOGGER.isLoggable(Level.FINEST)) { if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("occurrence==" +occurrence.toString() LOGGER.finest("occurrence==" + occurrence.toString() + "with Argumanet Count == "
+ "with Argumanet Count == "+occurrence.getArgumentCount() + occurrence.getArgumentCount() + " for className=" + className);
+ " for className="+className }
) ; }
}
}
if (occurrence.getImage().startsWith(className)) { if (occurrence.getImage().startsWith(className)) {
images.add(clipClassName(occurrence.getImage())); images.add(clipClassName(occurrence.getImage()));
} }
@ -177,7 +180,7 @@ public class ClassScope extends AbstractScope {
res += "(" + classNames.keySet() + ")"; res += "(" + classNames.keySet() + ")";
} }
if (!methodNames.isEmpty()) { if (!methodNames.isEmpty()) {
for (MethodNameDeclaration mnd: methodNames.keySet()) { for (MethodNameDeclaration mnd : methodNames.keySet()) {
res += mnd.toString(); res += mnd.toString();
int usages = methodNames.get(mnd).size(); int usages = methodNames.get(mnd).size();
res += "(begins at line " + mnd.getNode().getBeginLine() + ", " + usages + " usages)"; res += "(begins at line " + mnd.getNode().getBeginLine() + ", " + usages + " usages)";

View File

@ -15,17 +15,17 @@ import net.sourceforge.pmd.lang.plsql.ast.AbstractPLSQLNode;
import net.sourceforge.pmd.lang.symboltable.AbstractNameDeclaration; import net.sourceforge.pmd.lang.symboltable.AbstractNameDeclaration;
public class MethodNameDeclaration extends AbstractNameDeclaration { public class MethodNameDeclaration extends AbstractNameDeclaration {
private final static Logger LOGGER = Logger.getLogger(MethodNameDeclaration.class.getName()); private final static Logger LOGGER = Logger.getLogger(MethodNameDeclaration.class.getName());
public MethodNameDeclaration(ASTMethodDeclarator node) { public MethodNameDeclaration(ASTMethodDeclarator node) {
super(node); super(node);
} }
/** Treat a TimingPointSection within a Compound Trigger like a /**
* packaged FUNCTION or PROCEDURE. * Treat a TimingPointSection within a Compound Trigger like a packaged
* SRT * FUNCTION or PROCEDURE. SRT
* *
* @param node * @param node
*/ */
public MethodNameDeclaration(ASTTriggerTimingPointSection node) { public MethodNameDeclaration(ASTTriggerTimingPointSection node) {
super(node); super(node);
@ -37,16 +37,10 @@ public class MethodNameDeclaration extends AbstractNameDeclaration {
/** /**
* PL/SQL does not currently allow varargs outside the STANDARD package. * PL/SQL does not currently allow varargs outside the STANDARD package.
* @return false *
* @return false
*/ */
public boolean isVarargs() { public boolean isVarargs() {
ASTFormalParameters params = (ASTFormalParameters) node.jjtGetChild(0);
for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
ASTFormalParameter p = (ASTFormalParameter) params.jjtGetChild(i);
//if (p.isVarargs()) {
// return true;
//}
}
return false; return false;
} }
@ -55,16 +49,16 @@ public class MethodNameDeclaration extends AbstractNameDeclaration {
} }
public String getParameterDisplaySignature() { public String getParameterDisplaySignature() {
StringBuilder sb = new StringBuilder("("); StringBuilder sb = new StringBuilder("(");
ASTFormalParameters params = (ASTFormalParameters) node.jjtGetChild(0); ASTFormalParameters params = (ASTFormalParameters) node.jjtGetChild(0);
// TODO - this can be optimized - add [0] then ,[n] in a loop. // TODO - this can be optimized - add [0] then ,[n] in a loop.
// no need to trim at the end // no need to trim at the end
for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) { for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
ASTFormalParameter p = (ASTFormalParameter) params.jjtGetChild(i); ASTFormalParameter p = (ASTFormalParameter) params.jjtGetChild(i);
sb.append(p.getTypeNode().getTypeImage()); sb.append(p.getTypeNode().getTypeImage());
//if (p.isVarargs()) { // if (p.isVarargs()) {
// sb.append("..."); // sb.append("...");
//} // }
sb.append(','); sb.append(',');
} }
if (sb.charAt(sb.length() - 1) == ',') { if (sb.charAt(sb.length() - 1) == ',') {
@ -87,24 +81,27 @@ public class MethodNameDeclaration extends AbstractNameDeclaration {
return false; return false;
} }
// compare parameter count - this catches the case where there are no params, too // compare parameter count - this catches the case where there are no
// params, too
if (((ASTMethodDeclarator) other.node).getParameterCount() != ((ASTMethodDeclarator) node).getParameterCount()) { if (((ASTMethodDeclarator) other.node).getParameterCount() != ((ASTMethodDeclarator) node).getParameterCount()) {
return false; return false;
} }
// compare parameter types // compare parameter types
//SRT ASTFormalParameters myParams = (ASTFormalParameters) node.jjtGetChild(0); // SRT ASTFormalParameters myParams = (ASTFormalParameters)
//SRT ASTFormalParameters otherParams = (ASTFormalParameters) other.node.jjtGetChild(0); // node.jjtGetChild(0);
ASTFormalParameters myParams = node.getFirstDescendantOfType(ASTFormalParameters.class) ; // SRT ASTFormalParameters otherParams = (ASTFormalParameters)
ASTFormalParameters otherParams = other.node.getFirstDescendantOfType(ASTFormalParameters.class) ; // other.node.jjtGetChild(0);
ASTFormalParameters myParams = node.getFirstDescendantOfType(ASTFormalParameters.class);
ASTFormalParameters otherParams = other.node.getFirstDescendantOfType(ASTFormalParameters.class);
for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) { for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i); ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i);
ASTFormalParameter otherParam = (ASTFormalParameter) otherParams.jjtGetChild(i); ASTFormalParameter otherParam = (ASTFormalParameter) otherParams.jjtGetChild(i);
// Compare vararg // Compare vararg
//if (myParam.isVarargs() != otherParam.isVarargs()) { // if (myParam.isVarargs() != otherParam.isVarargs()) {
// return false; // return false;
//} // }
Node myTypeNode = myParam.getTypeNode().jjtGetChild(0); Node myTypeNode = myParam.getTypeNode().jjtGetChild(0);
Node otherTypeNode = otherParam.getTypeNode().jjtGetChild(0); Node otherTypeNode = otherParam.getTypeNode().jjtGetChild(0);
@ -120,45 +117,46 @@ public class MethodNameDeclaration extends AbstractNameDeclaration {
// once we get real types in here that should get fixed // once we get real types in here that should get fixed
String myTypeImg; String myTypeImg;
String otherTypeImg; String otherTypeImg;
//if (myTypeNode instanceof ASTPrimitiveType) { // if (myTypeNode instanceof ASTPrimitiveType) {
// myTypeImg = myTypeNode.getImage(); // myTypeImg = myTypeNode.getImage();
// otherTypeImg = otherTypeNode.getImage(); // otherTypeImg = otherTypeNode.getImage();
//} else { // } else {
myTypeImg = ( (AbstractPLSQLNode) myTypeNode .jjtGetChild(0) ) .getImage(); myTypeImg = ((AbstractPLSQLNode) myTypeNode.jjtGetChild(0)).getImage();
otherTypeImg = ( (AbstractPLSQLNode) otherTypeNode.jjtGetChild(0) ).getImage(); otherTypeImg = ((AbstractPLSQLNode) otherTypeNode.jjtGetChild(0)).getImage();
//} // }
if (!myTypeImg.equals(otherTypeImg)) { if (!myTypeImg.equals(otherTypeImg)) {
return false; return false;
} }
// if type is ASTPrimitiveType and is an array, make sure the other one is also // if type is ASTPrimitiveType and is an array, make sure the other
// one is also
} }
return true; return true;
} }
@Override @Override
public int hashCode() { public int hashCode() {
try try {
{ return node.hashCode(); // SRT node.getImage().hashCode() +
return node.hashCode(); //SRT node.getImage().hashCode() + ((ASTMethodDeclarator) node).getParameterCount(); // ((ASTMethodDeclarator)
} // node).getParameterCount();
catch (Exception e) } catch (Exception e) {
{ if (LOGGER.isLoggable(Level.FINEST)) {
if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest("MethodNameDeclaration problem for " + node + " of class "
LOGGER.finest("MethodNameDeclaration problem for " + node + node.getClass().getCanonicalName() + " => " + node.getBeginLine() + "/"
+" of class " + node.getClass().getCanonicalName() + node.getBeginColumn());
+" => "+ node.getBeginLine()+"/"+node.getBeginColumn() }
); // @TODO SRT restore the thrown exception - throw e;
} return 0;
//@TODO SRT restore the thrown exception - throw e; }
return 0;
}
} }
@Override @Override
public String toString() { public String toString() {
//SRT return "Method " + node.getImage() + ", line " + node.getBeginLine() + ", params = " + ((ASTMethodDeclarator) node).getParameterCount(); // SRT return "Method " + node.getImage() + ", line " +
// node.getBeginLine() + ", params = " + ((ASTMethodDeclarator)
// node).getParameterCount();
return node.toString(); return node.toString();
} }
} }

View File

@ -5,7 +5,6 @@ package net.sourceforge.pmd.lang.plsql.symboltable;
import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.plsql.ast.ASTExpression; import net.sourceforge.pmd.lang.plsql.ast.ASTExpression;
import net.sourceforge.pmd.lang.plsql.ast.ASTName;
import net.sourceforge.pmd.lang.plsql.ast.ASTPrimaryExpression; import net.sourceforge.pmd.lang.plsql.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.plsql.ast.PLSQLNode; import net.sourceforge.pmd.lang.plsql.ast.PLSQLNode;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence; import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
@ -60,133 +59,110 @@ public class PLSQLNameOccurrence implements NameOccurrence {
} }
public boolean isOnRightHandSide() { public boolean isOnRightHandSide() {
Node node = location.jjtGetParent().jjtGetParent().jjtGetParent(); Node node = location.jjtGetParent().jjtGetParent().jjtGetParent();
return node instanceof ASTExpression && node.jjtGetNumChildren() == 3; return node instanceof ASTExpression && node.jjtGetNumChildren() == 3;
} }
public boolean isOnLeftHandSide() { public boolean isOnLeftHandSide() {
// I detest this method with every atom of my being // I detest this method with every atom of my being
Node primaryExpression; Node primaryExpression;
if (location.jjtGetParent() instanceof ASTPrimaryExpression) { if (location.jjtGetParent() instanceof ASTPrimaryExpression) {
primaryExpression = location.jjtGetParent().jjtGetParent(); primaryExpression = location.jjtGetParent().jjtGetParent();
} else if (location.jjtGetParent().jjtGetParent() instanceof ASTPrimaryExpression) { } else if (location.jjtGetParent().jjtGetParent() instanceof ASTPrimaryExpression) {
primaryExpression = location.jjtGetParent().jjtGetParent().jjtGetParent(); primaryExpression = location.jjtGetParent().jjtGetParent().jjtGetParent();
} else { } else {
throw new RuntimeException("Found a NameOccurrence that didn't have an ASTPrimaryExpression as parent or grandparent. " throw new RuntimeException(
+ " Node = " + location.getClass().getCanonicalName() "Found a NameOccurrence that didn't have an ASTPrimaryExpression as parent or grandparent. "
+ ", Parent = " + location.jjtGetParent().getClass().getCanonicalName() + " Node = " + location.getClass().getCanonicalName() + ", Parent = "
+ " and grandparent = " + location.jjtGetParent().jjtGetParent().getClass().getCanonicalName() + location.jjtGetParent().getClass().getCanonicalName() + " and grandparent = "
+ " @ line = " + location.getBeginLine() + ", column = " + location.getBeginColumn() + location.jjtGetParent().jjtGetParent().getClass().getCanonicalName() + " @ line = "
); + location.getBeginLine() + ", column = " + location.getBeginColumn());
} }
/* /*
if (isStandAlonePostfix(primaryExpression)) { * if (isStandAlonePostfix(primaryExpression)) { return true; }
return true; */
}
*/
if (primaryExpression.jjtGetNumChildren() <= 1) { if (primaryExpression.jjtGetNumChildren() <= 1) {
return false; return false;
} }
/* /*
if (!(primaryExpression.jjtGetChild(1) instanceof ASTAssignmentOperator)) { * if (!(primaryExpression.jjtGetChild(1) instanceof
return false; * ASTAssignmentOperator)) { return false; }
} */
*/
if (isPartOfQualifiedName() /* or is an array type */) { if (isPartOfQualifiedName() /* or is an array type */) {
return false; return false;
} }
/* /*
if (isCompoundAssignment(primaryExpression)) { * if (isCompoundAssignment(primaryExpression)) { return false; }
return false; */
}
*/
return true; return true;
} }
/* /*
private boolean isCompoundAssignment(Node primaryExpression) { * private boolean isCompoundAssignment(Node primaryExpression) { return
return ((ASTAssignmentOperator) primaryExpression.jjtGetChild(1)).isCompound(); * ((ASTAssignmentOperator) primaryExpression.jjtGetChild(1)).isCompound();
} * }
*
private boolean isStandAlonePostfix(Node primaryExpression) { * private boolean isStandAlonePostfix(Node primaryExpression) { if
if (!(primaryExpression instanceof ASTPostfixExpression) || !(primaryExpression.jjtGetParent() instanceof ASTStatementExpression)) { * (!(primaryExpression instanceof ASTPostfixExpression) ||
return false; * !(primaryExpression.jjtGetParent() instanceof ASTStatementExpression)) {
} * return false; }
*
ASTPrimaryPrefix pf = (ASTPrimaryPrefix) ((ASTPrimaryExpression) primaryExpression.jjtGetChild(0)).jjtGetChild(0); * ASTPrimaryPrefix pf = (ASTPrimaryPrefix) ((ASTPrimaryExpression)
if (pf.usesThisModifier()) { * primaryExpression.jjtGetChild(0)).jjtGetChild(0); if
return true; * (pf.usesThisModifier()) { return true; }
} *
* return thirdChildHasDottedName(primaryExpression); }
return thirdChildHasDottedName(primaryExpression); *
} * private boolean thirdChildHasDottedName(Node primaryExpression) { Node
*/ * thirdChild =
* primaryExpression.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0); return
private boolean thirdChildHasDottedName(Node primaryExpression) { * thirdChild instanceof ASTName && ((ASTName)
Node thirdChild = primaryExpression.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0); * thirdChild).getImage().indexOf('.') == -1; }
return thirdChild instanceof ASTName && ((ASTName) thirdChild).getImage().indexOf('.') == -1; */
}
/** /**
* Assert it the occurrence is a self assignment such as: * Assert it the occurrence is a self assignment such as: <code>
* <code>
* i += 3; * i += 3;
* </code> * </code>
* *
* @return true, if the occurrence is self-assignment, false, otherwise. * @return true, if the occurrence is self-assignment, false, otherwise.
*/ */
/* /*
@SuppressWarnings("PMD.AvoidBranchingStatementAsLastInLoop") * @SuppressWarnings("PMD.AvoidBranchingStatementAsLastInLoop") public
public boolean isSelfAssignment() { * boolean isSelfAssignment() { Node l = location; while (true) { Node p =
Node l = location; * l.jjtGetParent(); Node gp = p.jjtGetParent(); Node node =
while (true) { * gp.jjtGetParent(); if (node instanceof ASTPreDecrementExpression || node
Node p = l.jjtGetParent(); * instanceof ASTPreIncrementExpression || node instanceof
Node gp = p.jjtGetParent(); * ASTPostfixExpression) { return true; }
Node node = gp.jjtGetParent(); *
if (node instanceof ASTPreDecrementExpression || node instanceof ASTPreIncrementExpression || node instanceof ASTPostfixExpression) { * if (hasAssignmentOperator(gp)) { return isCompoundAssignment(gp); }
return true; *
} * if (hasAssignmentOperator(node)) { return isCompoundAssignment(node); }
*
if (hasAssignmentOperator(gp)) { * // deal with extra parenthesis: "(i)++" if (p instanceof ASTPrimaryPrefix
return isCompoundAssignment(gp); * && p.jjtGetNumChildren() == 1 && gp instanceof ASTPrimaryExpression &&
} * gp.jjtGetNumChildren() == 1&& node instanceof ASTExpression &&
* node.jjtGetNumChildren() == 1 && node.jjtGetParent() instanceof
if (hasAssignmentOperator(node)) { * ASTPrimaryPrefix && node.jjtGetParent().jjtGetNumChildren() == 1) { l =
return isCompoundAssignment(node); * node; continue; }
} *
* // catch this.i++ or ++this.i return gp instanceof
// deal with extra parenthesis: "(i)++" * ASTPreDecrementExpression || gp instanceof ASTPreIncrementExpression ||
if (p instanceof ASTPrimaryPrefix && p.jjtGetNumChildren() == 1 && * gp instanceof ASTPostfixExpression; } }
gp instanceof ASTPrimaryExpression && gp.jjtGetNumChildren() == 1&& */
node instanceof ASTExpression && node.jjtGetNumChildren() == 1 &&
node.jjtGetParent() instanceof ASTPrimaryPrefix && node.jjtGetParent().jjtGetNumChildren() == 1) {
l = node;
continue;
}
// catch this.i++ or ++this.i
return gp instanceof ASTPreDecrementExpression || gp instanceof ASTPreIncrementExpression || gp instanceof ASTPostfixExpression;
}
}
*/
/* /*
private boolean hasAssignmentOperator(Node node) { * private boolean hasAssignmentOperator(Node node) { if (node instanceof
if (node instanceof ASTStatementExpression || node instanceof ASTExpression) { * ASTStatementExpression || node instanceof ASTExpression) { if
if (node.jjtGetNumChildren() >= 2 && node.jjtGetChild(1) instanceof ASTAssignmentOperator) { * (node.jjtGetNumChildren() >= 2 && node.jjtGetChild(1) instanceof
return true; * ASTAssignmentOperator) { return true; } } return false; }
} */
}
return false;
}
*/
/** /**
* Simply return true is the image is equal to keyword 'this' or 'super'. * Simply return true is the image is equal to keyword 'this' or 'super'.
@ -203,26 +179,21 @@ public class PLSQLNameOccurrence implements NameOccurrence {
* @return true, if keyword is used, false otherwise. * @return true, if keyword is used, false otherwise.
*/ */
/* /*
public boolean useThisOrSuper() { * public boolean useThisOrSuper() { Node node = location.jjtGetParent(); if
Node node = location.jjtGetParent(); * ( node instanceof ASTPrimaryExpression ) { ASTPrimaryExpression
if ( node instanceof ASTPrimaryExpression ) { * primaryExpression = (ASTPrimaryExpression)node; ASTPrimaryPrefix prefix =
ASTPrimaryExpression primaryExpression = (ASTPrimaryExpression)node; * (ASTPrimaryPrefix) primaryExpression.jjtGetChild(0); if ( prefix != null
ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) primaryExpression.jjtGetChild(0); * ) { return prefix.usesSuperModifier() || prefix.usesThisModifier(); } }
if ( prefix != null ) { * return image.startsWith(THIS_DOT) || image.startsWith(SUPER_DOT); }
return prefix.usesSuperModifier() || prefix.usesThisModifier(); */
}
}
return image.startsWith(THIS_DOT) || image.startsWith(SUPER_DOT);
}
*/
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (o instanceof PLSQLNameOccurrence) { if (o instanceof PLSQLNameOccurrence) {
PLSQLNameOccurrence n = (PLSQLNameOccurrence) o; PLSQLNameOccurrence n = (PLSQLNameOccurrence) o;
return n.getImage().equals(getImage()); return n.getImage().equals(getImage());
} }
return false; return false;
} }
@Override @Override
@ -236,6 +207,7 @@ public class PLSQLNameOccurrence implements NameOccurrence {
@Override @Override
public String toString() { public String toString() {
return getImage() + ":" + location.getBeginLine() + ":" + location.getClass() + (this.isMethodOrConstructorInvocation() ? "(method call)" : ""); return getImage() + ":" + location.getBeginLine() + ":" + location.getClass()
+ (this.isMethodOrConstructorInvocation() ? "(method call)" : "");
} }
} }

View File

@ -6,8 +6,8 @@ package net.sourceforge.pmd.cpd;
import java.io.IOException; import java.io.IOException;
import net.sourceforge.pmd.testframework.AbstractTokenizerTest; import net.sourceforge.pmd.testframework.AbstractTokenizerTest;
import net.sourceforge.pmd.testframework.StreamUtil;
import org.apache.commons.io.IOUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -18,14 +18,14 @@ public class PLSQLTokenizerTest extends AbstractTokenizerTest {
@Before @Before
@Override @Override
public void buildTokenizer() { public void buildTokenizer() throws IOException {
this.tokenizer = new PLSQLTokenizer(); this.tokenizer = new PLSQLTokenizer();
this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME)); this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME));
} }
@Override @Override
public String getSampleCode() { public String getSampleCode() throws IOException {
return StreamUtil.toString(PLSQLTokenizer.class.getResourceAsStream(FILENAME)); return IOUtils.toString(PLSQLTokenizer.class.getResourceAsStream(FILENAME));
} }
@Test @Test

View File

@ -6,8 +6,8 @@ package net.sourceforge.pmd.cpd;
import java.io.IOException; import java.io.IOException;
import net.sourceforge.pmd.testframework.AbstractTokenizerTest; import net.sourceforge.pmd.testframework.AbstractTokenizerTest;
import net.sourceforge.pmd.testframework.StreamUtil;
import org.apache.commons.io.IOUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -18,14 +18,14 @@ public class PythonTokenizerTest extends AbstractTokenizerTest {
@Before @Before
@Override @Override
public void buildTokenizer() { public void buildTokenizer() throws IOException {
this.tokenizer = new PythonTokenizer(); this.tokenizer = new PythonTokenizer();
this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME)); this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME));
} }
@Override @Override
public String getSampleCode() { public String getSampleCode() throws IOException {
return StreamUtil.toString(PythonTokenizer.class.getResourceAsStream(FILENAME)); return IOUtils.toString(PythonTokenizer.class.getResourceAsStream(FILENAME));
} }
@Test @Test

View File

@ -41,8 +41,6 @@ public final class StringUtils {
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
lines.add(line); lines.add(line);
} }
} catch (IOException ioe) {
throw ioe;
} finally { } finally {
IOUtils.closeQuietly(reader); IOUtils.closeQuietly(reader);
} }

View File

@ -7,9 +7,9 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import net.sourceforge.pmd.testframework.AbstractTokenizerTest; import net.sourceforge.pmd.testframework.AbstractTokenizerTest;
import net.sourceforge.pmd.testframework.StreamUtil;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -18,32 +18,29 @@ import org.sonar.plugins.scala.cpd.ScalaTokenizer;
public class ScalaTokenizerTest extends AbstractTokenizerTest { public class ScalaTokenizerTest extends AbstractTokenizerTest {
private static final String ENCODING = "UTF-8";
private static final String FILENAME = "sample-LiftActor.scala"; private static final String FILENAME = "sample-LiftActor.scala";
private File tempFile; private File tempFile;
@Before @Before
@Override @Override
public void buildTokenizer() { public void buildTokenizer() throws IOException {
createTempFileOnDisk(); createTempFileOnDisk();
this.tokenizer = new ScalaTokenizer(); this.tokenizer = new ScalaTokenizer();
this.sourceCode = new SourceCode(new SourceCode.FileCodeLoader(tempFile, "UTF-8")); this.sourceCode = new SourceCode(new SourceCode.FileCodeLoader(tempFile, "UTF-8"));
} }
private void createTempFileOnDisk() { private void createTempFileOnDisk() throws IOException {
try { this.tempFile = File.createTempFile("scala-tokenizer-test-", ".scala");
this.tempFile = File.createTempFile("scala-tokenizer-test-", ".scala"); FileUtils.writeStringToFile(tempFile, getSampleCode(), ENCODING);
FileUtils.writeStringToFile(tempFile, getSampleCode(), "UTF-8");
} catch (IOException e) {
throw new RuntimeException("Unable to create temporary file on disk for Scala tokenizer test", e);
}
} }
@Override @Override
public String getSampleCode() { public String getSampleCode() throws IOException {
return StreamUtil.toString(ScalaTokenizer.class.getResourceAsStream(FILENAME)); return IOUtils.toString(ScalaTokenizer.class.getResourceAsStream(FILENAME), ENCODING);
} }
@Test @Test
@ -57,8 +54,4 @@ public class ScalaTokenizerTest extends AbstractTokenizerTest {
FileUtils.deleteQuietly(this.tempFile); FileUtils.deleteQuietly(this.tempFile);
this.tempFile = null; this.tempFile = null;
} }
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(ScalaTokenizerTest.class);
}
} }

View File

@ -3,12 +3,7 @@
*/ */
package net.sourceforge.pmd.lang; package net.sourceforge.pmd.lang;
import static org.junit.Assert.assertEquals; import org.junit.Assert;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotEquals;
import org.junit.Test; import org.junit.Test;
/** /**
@ -22,9 +17,9 @@ public class ParserOptionsTest {
@Test @Test
public void testSuppressMarker() { public void testSuppressMarker() {
ParserOptions parserOptions = new ParserOptions(); ParserOptions parserOptions = new ParserOptions();
assertNull(parserOptions.getSuppressMarker()); Assert.assertNull(parserOptions.getSuppressMarker());
parserOptions.setSuppressMarker("foo"); parserOptions.setSuppressMarker("foo");
assertEquals("foo", parserOptions.getSuppressMarker()); Assert.assertEquals("foo", parserOptions.getSuppressMarker());
} }
/** /**
@ -55,40 +50,40 @@ public class ParserOptionsTest {
public static void verifyOptionsEqualsHashcode(ParserOptions options1, ParserOptions options2, public static void verifyOptionsEqualsHashcode(ParserOptions options1, ParserOptions options2,
ParserOptions options3, ParserOptions options4) { ParserOptions options3, ParserOptions options4) {
// Objects should be different // Objects should be different
assertNotSame(options1, options2); Assert.assertNotSame(options1, options2);
assertNotSame(options1, options2); Assert.assertNotSame(options1, options2);
assertNotSame(options1, options3); Assert.assertNotSame(options1, options3);
assertNotSame(options2, options3); Assert.assertNotSame(options2, options3);
assertNotSame(options2, options4); Assert.assertNotSame(options2, options4);
assertNotSame(options3, options4); Assert.assertNotSame(options3, options4);
// Check all 16 equality combinations // Check all 16 equality combinations
assertEquals(options1, options1); Assert.assertEquals(options1, options1);
assertFalse(options1.equals(options2)); Assert.assertFalse(options1.equals(options2));
assertEquals(options1, options3); Assert.assertEquals(options1, options3);
assertFalse(options1.equals(options4)); Assert.assertFalse(options1.equals(options4));
assertFalse(options2.equals(options1)); Assert.assertFalse(options2.equals(options1));
assertEquals(options2, options2); Assert.assertEquals(options2, options2);
assertFalse(options2.equals(options3)); Assert.assertFalse(options2.equals(options3));
assertEquals(options2, options4); Assert.assertEquals(options2, options4);
assertEquals(options3, options1); Assert.assertEquals(options3, options1);
assertFalse(options3.equals(options2)); Assert.assertFalse(options3.equals(options2));
assertEquals(options3, options3); Assert.assertEquals(options3, options3);
assertFalse(options3.equals(options4)); Assert.assertFalse(options3.equals(options4));
assertFalse(options4.equals(options1)); Assert.assertFalse(options4.equals(options1));
assertEquals(options4, options2); Assert.assertEquals(options4, options2);
assertFalse(options4.equals(options3)); Assert.assertFalse(options4.equals(options3));
assertEquals(options4, options4); Assert.assertEquals(options4, options4);
// Hashcodes should match up // Hashcodes should match up
assertNotEquals(options1.hashCode(), options2.hashCode()); Assert.assertNotEquals(options1.hashCode(), options2.hashCode());
assertEquals(options1.hashCode(), options3.hashCode()); Assert.assertEquals(options1.hashCode(), options3.hashCode());
assertNotEquals(options1.hashCode(), options4.hashCode()); Assert.assertNotEquals(options1.hashCode(), options4.hashCode());
assertNotEquals(options2.hashCode(), options3.hashCode()); Assert.assertNotEquals(options2.hashCode(), options3.hashCode());
assertEquals(options2.hashCode(), options4.hashCode()); Assert.assertEquals(options2.hashCode(), options4.hashCode());
assertNotEquals(options3.hashCode(), options4.hashCode()); Assert.assertNotEquals(options3.hashCode(), options4.hashCode());
} }
} }

View File

@ -19,19 +19,19 @@ import net.sourceforge.pmd.cpd.Tokens;
*/ */
public abstract class AbstractTokenizerTest { public abstract class AbstractTokenizerTest {
protected int expectedTokenCount; protected int expectedTokenCount;
protected Tokenizer tokenizer; protected Tokenizer tokenizer;
protected SourceCode sourceCode; protected SourceCode sourceCode;
public abstract void buildTokenizer(); public abstract void buildTokenizer() throws IOException;
public abstract String getSampleCode(); public abstract String getSampleCode() throws IOException;
protected void tokenizeTest() throws IOException { protected void tokenizeTest() throws IOException {
Tokens tokens = new Tokens(); Tokens tokens = new Tokens();
tokenizer.tokenize(sourceCode, tokens); tokenizer.tokenize(sourceCode, tokens);
List<TokenEntry> entries = tokens.getTokens(); List<TokenEntry> entries = tokens.getTokens();
assertEquals(expectedTokenCount,entries.size()); assertEquals(expectedTokenCount, entries.size());
} }
} }

View File

@ -8,26 +8,25 @@ import java.io.InputStream;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
/**
* @deprecated Just use apache {@link IOUtils}
*/
@Deprecated
public final class StreamUtil { public final class StreamUtil {
private StreamUtil() { private StreamUtil() {
// utility class // utility class
} }
public static String toString(InputStream in) { /**
StringBuilder sb = new StringBuilder(); * @deprecated use {@link IOUtils#toString(InputStream)} instead
int c; */
try { @Deprecated
while ((c = in.read()) != -1) { public static String toString(InputStream stream) {
sb.append((char) c); try {
} return IOUtils.toString(stream);
} catch (IOException e) { } catch (IOException e) {
// ignored throw new RuntimeException(e);
} finally { }
IOUtils.closeQuietly(in); }
}
return sb.toString();
}
} }

View File

@ -93,3 +93,5 @@ and will be removed with the next release of PMD.
This Mark is useful for reporting the correct line count for each duplication. Previously only one line count was available. This Mark is useful for reporting the correct line count for each duplication. Previously only one line count was available.
As for some languages CPD can be instructed to ignore comments, the line count could be different in the different files As for some languages CPD can be instructed to ignore comments, the line count could be different in the different files
for the same duplication. for the same duplication.
* pmd-test: The utility class `StreamUtil` is deprecated. Just use Apache Commons IO Utils instead.