diff --git a/pmd-cs/src/main/java/net/sourceforge/pmd/cpd/CsTokenizer.java b/pmd-cs/src/main/java/net/sourceforge/pmd/cpd/CsTokenizer.java index 673ca2e2e0..2ccdccc3c5 100644 --- a/pmd-cs/src/main/java/net/sourceforge/pmd/cpd/CsTokenizer.java +++ b/pmd-cs/src/main/java/net/sourceforge/pmd/cpd/CsTokenizer.java @@ -56,11 +56,11 @@ public class CsTokenizer implements Tokenizer { } else if (ic == c) { ic = reader.read(); if (ic == '=') { - tokenEntries.add(new TokenEntry(String.valueOf(c) + String.valueOf(c) + "=", sourceCode + tokenEntries.add(new TokenEntry(c + c + "=", sourceCode .getFileName(), line)); ic = reader.read(); } else { - tokenEntries.add(new TokenEntry(String.valueOf(c) + String.valueOf(c), sourceCode + tokenEntries.add(new TokenEntry(String.valueOf(c) + c, sourceCode .getFileName(), line)); } } else { @@ -76,7 +76,7 @@ public class CsTokenizer implements Tokenizer { case '-': ic = reader.read(); 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)); ic = reader.read(); } else { diff --git a/pmd-matlab/src/test/java/net/sourceforge/pmd/cpd/MatlabTokenizerTest.java b/pmd-matlab/src/test/java/net/sourceforge/pmd/cpd/MatlabTokenizerTest.java index f90e7340d6..8d97ecfa49 100644 --- a/pmd-matlab/src/test/java/net/sourceforge/pmd/cpd/MatlabTokenizerTest.java +++ b/pmd-matlab/src/test/java/net/sourceforge/pmd/cpd/MatlabTokenizerTest.java @@ -6,8 +6,8 @@ package net.sourceforge.pmd.cpd; import java.io.IOException; 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.Test; @@ -18,14 +18,14 @@ public class MatlabTokenizerTest extends AbstractTokenizerTest { @Before @Override - public void buildTokenizer() { + public void buildTokenizer() throws IOException { this.tokenizer = new MatlabTokenizer(); this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME)); } @Override - public String getSampleCode() { - return StreamUtil.toString(MatlabTokenizer.class.getResourceAsStream(FILENAME)); + public String getSampleCode() throws IOException { + return IOUtils.toString(MatlabTokenizer.class.getResourceAsStream(FILENAME)); } @Test @@ -33,8 +33,4 @@ public class MatlabTokenizerTest extends AbstractTokenizerTest { this.expectedTokenCount = 3925; super.tokenizeTest(); } - - public static junit.framework.Test suite() { - return new junit.framework.JUnit4TestAdapter(MatlabTokenizerTest.class); - } } diff --git a/pmd-objectivec/src/test/java/net/sourceforge/pmd/cpd/ObjectiveCTokenizerTest.java b/pmd-objectivec/src/test/java/net/sourceforge/pmd/cpd/ObjectiveCTokenizerTest.java index c8b2e72743..097f28a221 100644 --- a/pmd-objectivec/src/test/java/net/sourceforge/pmd/cpd/ObjectiveCTokenizerTest.java +++ b/pmd-objectivec/src/test/java/net/sourceforge/pmd/cpd/ObjectiveCTokenizerTest.java @@ -6,8 +6,8 @@ package net.sourceforge.pmd.cpd; import java.io.IOException; 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.Test; @@ -18,14 +18,14 @@ public class ObjectiveCTokenizerTest extends AbstractTokenizerTest { @Before @Override - public void buildTokenizer() { + public void buildTokenizer() throws IOException { this.tokenizer = new ObjectiveCTokenizer(); this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME)); } @Override - public String getSampleCode() { - return StreamUtil.toString(ObjectiveCTokenizer.class.getResourceAsStream(FILENAME)); + public String getSampleCode() throws IOException { + return IOUtils.toString(ObjectiveCTokenizer.class.getResourceAsStream(FILENAME)); } @Test @@ -33,8 +33,4 @@ public class ObjectiveCTokenizerTest extends AbstractTokenizerTest { this.expectedTokenCount = 884; super.tokenizeTest(); } - - public static junit.framework.Test suite() { - return new junit.framework.JUnit4TestAdapter(ObjectiveCTokenizerTest.class); - } } diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/dfa/StatementAndBraceFinder.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/dfa/StatementAndBraceFinder.java index 1f2b243ca3..e3e88e98cd 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/dfa/StatementAndBraceFinder.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/dfa/StatementAndBraceFinder.java @@ -50,35 +50,29 @@ import net.sourceforge.pmd.lang.plsql.ast.PLSQLParserVisitorAdapter; * @author raik *

* Sublayer of DataFlowFacade. Finds all data flow nodes and stores the - * type information (@see StackObject). At last it uses this information to - * link the nodes. + * type information (@see StackObject). At last it uses this information + * to link the nodes. */ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { - private final static Logger LOGGER = Logger.getLogger(StatementAndBraceFinder.class.getName()); + private final static Logger LOGGER = Logger.getLogger(StatementAndBraceFinder.class.getName()); private final DataFlowHandler dataFlowHandler; private Structure dataFlow; - + public StatementAndBraceFinder(DataFlowHandler dataFlowHandler) { - this.dataFlowHandler = dataFlowHandler; + this.dataFlowHandler = dataFlowHandler; } public void buildDataFlowFor(PLSQLNode node) { - LOGGER.entering(this.getClass().getCanonicalName(),"buildDataFlowFor"); + LOGGER.entering(this.getClass().getCanonicalName(), "buildDataFlowFor"); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("buildDataFlowFor: node class " - + node.getClass().getCanonicalName() + " @ line " - + node.getBeginLine() - +", column " + node.getBeginColumn() - + " --- " + new Throwable().getStackTrace() - ); + LOGGER.finest("buildDataFlowFor: node class " + node.getClass().getCanonicalName() + " @ line " + + node.getBeginLine() + ", column " + node.getBeginColumn() + " --- " + + new Throwable().getStackTrace()); } - if (!(node instanceof ASTMethodDeclaration) - && !(node instanceof ASTProgramUnit) - && !(node instanceof ASTTypeMethod) - && !(node instanceof ASTTriggerUnit) - && !(node instanceof ASTTriggerTimingPointSection) - ) { + if (!(node instanceof ASTMethodDeclaration) && !(node instanceof ASTProgramUnit) + && !(node instanceof ASTTypeMethod) && !(node instanceof ASTTriggerUnit) + && !(node instanceof ASTTriggerTimingPointSection)) { throw new RuntimeException("Can't build a data flow for anything other than a Method or a Trigger"); } @@ -90,9 +84,8 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { this.dataFlow.createEndNode(node.getEndLine()); - if (LOGGER.isLoggable(Level.FINE)) - { - LOGGER.fine("DataFlow is " + this.dataFlow.dump() ); + if (LOGGER.isLoggable(Level.FINE)) { + LOGGER.fine("DataFlow is " + this.dataFlow.dump()); } Linker linker = new Linker(dataFlowHandler, dataFlow.getBraceStack(), dataFlow.getContinueBreakReturnStack()); try { @@ -104,88 +97,95 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { LOGGER.severe("SequenceException"); e.printStackTrace(); } - LOGGER.exiting(this.getClass().getCanonicalName(),"buildDataFlowFor"); + LOGGER.exiting(this.getClass().getCanonicalName(), "buildDataFlowFor"); } - - public Object visit(ASTSqlStatement node, Object data) { + public Object visit(ASTSqlStatement node, Object data) { if (!(data instanceof Structure)) { if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("immediate return ASTSqlStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("immediate return ASTSqlStatement: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return data; } Structure dataFlow = (Structure) data; dataFlow.createNewNode(node); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("createNewNode ASTSqlStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("createNewNode ASTSqlStatement: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return super.visit(node, data); - } - - public Object visit(ASTEmbeddedSqlStatement node, Object data) { + } + + public Object visit(ASTEmbeddedSqlStatement node, Object data) { if (!(data instanceof Structure)) { if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("immediate return ASTEmbeddedSqlStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("immediate return ASTEmbeddedSqlStatement: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return data; } Structure dataFlow = (Structure) data; dataFlow.createNewNode(node); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("createNewNode ASTEmbeddedSqlStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("createNewNode ASTEmbeddedSqlStatement: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return super.visit(node, data); - } - - public Object visit(ASTCloseStatement node, Object data) { + } + + public Object visit(ASTCloseStatement node, Object data) { if (!(data instanceof Structure)) { return data; } Structure dataFlow = (Structure) data; dataFlow.createNewNode(node); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("createNewNode ASTCloseStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("createNewNode ASTCloseStatement: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return super.visit(node, data); - } - - public Object visit(ASTOpenStatement node, Object data) { + } + + public Object visit(ASTOpenStatement node, Object data) { if (!(data instanceof Structure)) { return data; } Structure dataFlow = (Structure) data; dataFlow.createNewNode(node); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("createNewNode ASTOpenStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("createNewNode ASTOpenStatement: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return super.visit(node, data); - } - - public Object visit(ASTFetchStatement node, Object data) { + } + + public Object visit(ASTFetchStatement node, Object data) { if (!(data instanceof Structure)) { return data; } Structure dataFlow = (Structure) data; dataFlow.createNewNode(node); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("createNewNode ASTFetchStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("createNewNode ASTFetchStatement: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return super.visit(node, data); - } - - public Object visit(ASTPipelineStatement node, Object data) { + } + + public Object visit(ASTPipelineStatement node, Object data) { if (!(data instanceof Structure)) { return data; } Structure dataFlow = (Structure) data; dataFlow.createNewNode(node); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("createNewNode ASTPipelineStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("createNewNode ASTPipelineStatement: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return super.visit(node, data); - } - + } + /* */ public Object visit(ASTVariableOrConstantDeclarator node, Object data) { @@ -195,79 +195,88 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { Structure dataFlow = (Structure) data; dataFlow.createNewNode(node); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("createNewNode ASTVariableOrConstantDeclarator: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("createNewNode ASTVariableOrConstantDeclarator: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return super.visit(node, data); } public Object visit(ASTExpression node, Object data) { if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("Entry ASTExpression: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("Entry ASTExpression: line " + node.getBeginLine() + ", column " + node.getBeginColumn()); } if (!(data instanceof Structure)) { if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("immediate return ASTExpression: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("immediate return ASTExpression: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return data; } Structure dataFlow = (Structure) data; - //The equivalent of an ASTExpression is an Expression whose parent is an UnLabelledStatement + // The equivalent of an ASTExpression is an Expression whose parent is + // an UnLabelledStatement if (node.jjtGetParent() instanceof ASTUnlabelledStatement) { if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("createNewNode ASTSUnlabelledStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("createNewNode ASTSUnlabelledStatement: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } dataFlow.createNewNode(node); - } else + } else // TODO what about throw stmts? if (node.jjtGetParent() instanceof ASTIfStatement) { dataFlow.createNewNode(node); // START IF dataFlow.pushOnStack(NodeType.IF_EXPR, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack parent IF_EXPR: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack parent IF_EXPR: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } } else if (node.jjtGetParent() instanceof ASTElsifClause) { if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("parent (Elsif) IF_EXPR at " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("parent (Elsif) IF_EXPR at " + node.getBeginLine() + ", column " + node.getBeginColumn()); } dataFlow.createNewNode(node); // START IF dataFlow.pushOnStack(NodeType.IF_EXPR, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack parent (Elsif) IF_EXPR: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack parent (Elsif) IF_EXPR: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } } else if (node.jjtGetParent() instanceof ASTWhileStatement) { dataFlow.createNewNode(node); // START WHILE dataFlow.pushOnStack(NodeType.WHILE_EXPR, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack parent WHILE_EXPR: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack parent WHILE_EXPR: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } } else if (node.jjtGetParent() instanceof ASTCaseStatement) { dataFlow.createNewNode(node); // START SWITCH dataFlow.pushOnStack(NodeType.SWITCH_START, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack parent SWITCH_START: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack parent SWITCH_START: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } } else if (node.jjtGetParent() instanceof ASTForStatement) { - /* A PL/SQL loop control: - * [] Expression()[".."Expression()] - * + /* + * A PL/SQL loop control: [] Expression()[".."Expression()] */ - if (node.equals( node.jjtGetParent().getFirstChildOfType(ASTExpression.class) ) ) - { - dataFlow.createNewNode(node); // FOR EXPR - dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast()); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack parent FOR_EXPR: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } + if (node.equals(node.jjtGetParent().getFirstChildOfType(ASTExpression.class))) { + dataFlow.createNewNode(node); // FOR EXPR + dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast()); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("pushOnStack parent FOR_EXPR: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); + } } if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("parent (ASTForStatement): line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("parent (ASTForStatement): line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } } else if (node.jjtGetParent() instanceof ASTLoopStatement) { dataFlow.createNewNode(node); // DO EXPR dataFlow.pushOnStack(NodeType.DO_EXPR, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack parent DO_EXPR: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack parent DO_EXPR: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } } @@ -278,166 +287,177 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { dataFlow.createNewNode(node); dataFlow.pushOnStack(NodeType.LABEL_STATEMENT, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack LABEL_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack LABEL_STATEMENT: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return super.visit(node, data); - } + } /** - * PL/SQL does not have a do/while statement or repeat/until statement: the equivalent is a LOOP statement. - * A PL/SQL LOOP statement is exited using an explicit EXIT ( == break;) statement - * It does not have a test expression, so the Java control processing (on the expression) does not fire. - * The best way to cope it to push a DO_EXPR after the loop. + * PL/SQL does not have a do/while statement or repeat/until statement: the + * equivalent is a LOOP statement. A PL/SQL LOOP statement is exited using + * an explicit EXIT ( == break;) statement It does not have a test + * expression, so the Java control processing (on the expression) does not + * fire. The best way to cope it to push a DO_EXPR after the loop. */ public Object visit(ASTLoopStatement node, Object data) { if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("entry ASTLoopStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("entry ASTLoopStatement: line " + node.getBeginLine() + ", column " + node.getBeginColumn()); } if (!(data instanceof Structure)) { if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("immediate return ASTLoopStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("immediate return ASTLoopStatement: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return data; } Structure dataFlow = (Structure) data; - //process the contents on the LOOP statement + // process the contents on the LOOP statement super.visit(node, data); dataFlow.createNewNode(node); dataFlow.pushOnStack(NodeType.DO_EXPR, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (ASTLoopStatement) DO_EXPR: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack (ASTLoopStatement) DO_EXPR: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return data; - } + } /** - * A PL/SQL WHILE statement includes the LOOP statement and all Expressions within it: - * it does not have a single test expression, so the Java control processing (on the Expression) fires for each - * Expression in the LOOP. - * The best way to cope it to push a WHILE_LAST_STATEMENT after the WhileStatement has been processed. + * A PL/SQL WHILE statement includes the LOOP statement and all Expressions + * within it: it does not have a single test expression, so the Java control + * processing (on the Expression) fires for each Expression in the LOOP. The + * best way to cope it to push a WHILE_LAST_STATEMENT after the + * WhileStatement has been processed. */ public Object visit(ASTWhileStatement node, Object data) { if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("entry ASTWhileStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("entry ASTWhileStatement: line " + node.getBeginLine() + ", column " + node.getBeginColumn()); } if (!(data instanceof Structure)) { if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("immediate return ASTWhileStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("immediate return ASTWhileStatement: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return data; } - //process the contents on the WHILE statement + // process the contents on the WHILE statement super.visit(node, data); return data; - } + } - -// ---------------------------------------------------------------------------- -// BRANCH OUT + // ---------------------------------------------------------------------------- + // BRANCH OUT public Object visit(ASTStatement node, Object data) { if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("entry ASTStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn() + " -> " + node.getClass().getCanonicalName()); + LOGGER.finest("entry ASTStatement: line " + node.getBeginLine() + ", column " + node.getBeginColumn() + + " -> " + node.getClass().getCanonicalName()); } if (!(data instanceof Structure)) { if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("immediate return ASTStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("immediate return ASTStatement: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return data; } Structure dataFlow = (Structure) data; if (node.jjtGetParent() instanceof ASTForStatement) { - ASTForStatement st = (ASTForStatement) node.jjtGetParent(); - if (node.equals(st.getFirstChildOfType(ASTStatement.class))) - { - this.addForExpressionNode(node, dataFlow); - dataFlow.pushOnStack(NodeType.FOR_BEFORE_FIRST_STATEMENT, dataFlow.getLast()); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack FOR_BEFORE_FIRST_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } + ASTForStatement st = (ASTForStatement) node.jjtGetParent(); + if (node.equals(st.getFirstChildOfType(ASTStatement.class))) { + addForExpressionNode(node, dataFlow); + dataFlow.pushOnStack(NodeType.FOR_BEFORE_FIRST_STATEMENT, dataFlow.getLast()); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("pushOnStack FOR_BEFORE_FIRST_STATEMENT: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); + } } } else if (node.jjtGetParent() instanceof ASTLoopStatement) { - ASTLoopStatement st = (ASTLoopStatement) node.jjtGetParent(); - if (node.equals(st.getFirstChildOfType(ASTStatement.class))) - { - dataFlow.pushOnStack(NodeType.DO_BEFORE_FIRST_STATEMENT, dataFlow.getLast()); - dataFlow.createNewNode(node.jjtGetParent()); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack DO_BEFORE_FIRST_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } + ASTLoopStatement st = (ASTLoopStatement) node.jjtGetParent(); + if (node.equals(st.getFirstChildOfType(ASTStatement.class))) { + dataFlow.pushOnStack(NodeType.DO_BEFORE_FIRST_STATEMENT, dataFlow.getLast()); + dataFlow.createNewNode(node.jjtGetParent()); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("pushOnStack DO_BEFORE_FIRST_STATEMENT: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); + } } - } - + } super.visit(node, data); if (node.jjtGetParent() instanceof ASTElseClause) { - List allStatements = node.jjtGetParent().findChildrenOfType(ASTStatement.class) ; + List allStatements = node.jjtGetParent().findChildrenOfType(ASTStatement.class); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("ElseClause has " + allStatements.size() + " Statements " ); + LOGGER.finest("ElseClause has " + allStatements.size() + " Statements "); } - /* - //Restrict to the last Statement of the Else Clause - if (node == allStatements.get(allStatements.size()-1) ) - { - if (node.jjtGetParent().jjtGetParent() instanceof ASTCaseStatement) { - dataFlow.pushOnStack(NodeType.SWITCH_LAST_DEFAULT_STATEMENT, dataFlow.getLast()); - LOGGER.finest("pushOnStack (Else-Below Case) SWITCH_LAST_DEFAULT_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } /*SRT else // if (node == node.jjtGetParent() instanceof ASTElseClause) { - { - dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT, dataFlow.getLast()); - LOGGER.finest("pushOnStack (Else-Below If) ELSE_LAST_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } */ - //} + /* + * //Restrict to the last Statement of the Else Clause if (node == + * allStatements.get(allStatements.size()-1) ) { if + * (node.jjtGetParent().jjtGetParent() instanceof ASTCaseStatement) + * { dataFlow.pushOnStack(NodeType.SWITCH_LAST_DEFAULT_STATEMENT, + * dataFlow.getLast()); LOGGER.finest( + * "pushOnStack (Else-Below Case) SWITCH_LAST_DEFAULT_STATEMENT: line " + * + node.getBeginLine() +", column " + node.getBeginColumn()); } + * /*SRT else // if (node == node.jjtGetParent() instanceof + * ASTElseClause) { { + * dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT, + * dataFlow.getLast()); LOGGER.finest( + * "pushOnStack (Else-Below If) ELSE_LAST_STATEMENT: line " + + * node.getBeginLine() +", column " + node.getBeginColumn()); } + */ + // } } else if (node.jjtGetParent() instanceof ASTWhileStatement) { ASTWhileStatement statement = (ASTWhileStatement) node.jjtGetParent(); List children = statement.findChildrenOfType(ASTStatement.class); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("(LastChildren): size " + children.size() ); + LOGGER.finest("(LastChildren): size " + children.size()); } - ASTStatement lastChild = children.get(children.size()-1); + ASTStatement lastChild = children.get(children.size() - 1); - // Push on stack if this Node is the LAST Statement associated with the FOR Statment - if ( node.equals(lastChild) ) - { - dataFlow.pushOnStack(NodeType.WHILE_LAST_STATEMENT, dataFlow.getLast()); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack WHILE_LAST_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } + // Push on stack if this Node is the LAST Statement associated with + // the FOR Statment + if (node.equals(lastChild)) { + dataFlow.pushOnStack(NodeType.WHILE_LAST_STATEMENT, dataFlow.getLast()); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("pushOnStack WHILE_LAST_STATEMENT: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); + } } - } else if (node.jjtGetParent() instanceof ASTForStatement ) { + } else if (node.jjtGetParent() instanceof ASTForStatement) { ASTForStatement statement = (ASTForStatement) node.jjtGetParent(); List children = statement.findChildrenOfType(ASTStatement.class); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("(LastChildren): size " + children.size() ); + LOGGER.finest("(LastChildren): size " + children.size()); } - ASTStatement lastChild = children.get(children.size()-1); + ASTStatement lastChild = children.get(children.size() - 1); - // Push on stack if this Node is the LAST Statement associated with the FOR Statment - if ( node.equals(lastChild) ) - { - dataFlow.pushOnStack(NodeType.FOR_END, dataFlow.getLast()); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (LastChildStatemnt) FOR_END: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } + // Push on stack if this Node is the LAST Statement associated with + // the FOR Statment + if (node.equals(lastChild)) { + dataFlow.pushOnStack(NodeType.FOR_END, dataFlow.getLast()); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("pushOnStack (LastChildStatemnt) FOR_END: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); + } } } else if (node.jjtGetParent() instanceof ASTLabelledStatement) { dataFlow.pushOnStack(NodeType.LABEL_LAST_STATEMENT, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack LABEL_LAST_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack LABEL_LAST_STATEMENT: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } - } + } if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("exit ASTStatement: line " + node.getBeginLine() +", column " + node.getBeginColumn() - + " -> " + node.getClass().getCanonicalName() - + " ->-> " + node.jjtGetParent().getClass().getCanonicalName() - ); + LOGGER.finest("exit ASTStatement: line " + node.getBeginLine() + ", column " + node.getBeginColumn() + + " -> " + node.getClass().getCanonicalName() + " ->-> " + + node.jjtGetParent().getClass().getCanonicalName()); } return data; } @@ -451,9 +471,10 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { if (node.jjtGetParent() instanceof ASTLabelledStatement) { dataFlow.pushOnStack(NodeType.LABEL_LAST_STATEMENT, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (ASTUnlabelledStatement) LABEL_LAST_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack (ASTUnlabelledStatement) LABEL_LAST_STATEMENT: line " + node.getBeginLine() + + ", column " + node.getBeginColumn()); } - } + } return data; } @@ -464,146 +485,149 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { Structure dataFlow = (Structure) data; /* - * A PL/SQL CASE statement may be either:- - * SIMPLE, e.g. CASE case_operand WHEN when_operand THEN statement ; ELSE statement ; END CASE - * SEARCHED, e.g. CASE WHEN boolean_expression THEN statement ; ELSE statement ; END CASE + * A PL/SQL CASE statement may be either:- SIMPLE, e.g. CASE + * case_operand WHEN when_operand THEN statement ; ELSE statement ; END + * CASE SEARCHED, e.g. CASE WHEN boolean_expression THEN statement ; + * ELSE statement ; END CASE * - * A SIMPLE CASE statement may be treated as a normal Java SWITCH statement ( see visit(ASTExpression)), - * but a SEARCHED CASE statement must have an atificial start node + * A SIMPLE CASE statement may be treated as a normal Java SWITCH + * statement ( see visit(ASTExpression)), but a SEARCHED CASE statement + * must have an atificial start node */ - if (null == node.getFirstChildOfType(ASTExpression.class) // CASE is "searched case statement" - ) - { - dataFlow.createNewNode(node); - dataFlow.pushOnStack(NodeType.SWITCH_START, dataFlow.getLast()); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack SWITCH_START: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } + if (null == node.getFirstChildOfType(ASTExpression.class) // CASE is + // "searched case statement" + ) { + dataFlow.createNewNode(node); + dataFlow.pushOnStack(NodeType.SWITCH_START, dataFlow.getLast()); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("pushOnStack SWITCH_START: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); + } } super.visit(node, data); dataFlow.pushOnStack(NodeType.SWITCH_END, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack SWITCH_END: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack SWITCH_END: line " + node.getBeginLine() + ", column " + node.getBeginColumn()); } return data; } - public Object visit(ASTCaseWhenClause node, Object data) { + public Object visit(ASTCaseWhenClause node, Object data) { if (!(data instanceof Structure)) { return data; } Structure dataFlow = (Structure) data; - - //Java ASTSwitchLabel - //SRT dataFlow.createNewNode(node); + // Java ASTSwitchLabel + // SRT dataFlow.createNewNode(node); dataFlow.pushOnStack(NodeType.CASE_LAST_STATEMENT, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack CASE_LAST_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack CASE_LAST_STATEMENT: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } super.visit(node, data); - //dataFlow.createNewNode(node); + // dataFlow.createNewNode(node); dataFlow.pushOnStack(NodeType.BREAK_STATEMENT, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (ASTCaseWhenClause) BREAK_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack (ASTCaseWhenClause) BREAK_STATEMENT: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return data; } - - public Object visit(ASTIfStatement node, Object data) { + + public Object visit(ASTIfStatement node, Object data) { if (!(data instanceof Structure)) { return data; } Structure dataFlow = (Structure) data; - LOGGER.finest("ElsifClause) super.visit line" ); + LOGGER.finest("ElsifClause) super.visit line"); super.visit(node, data); - /* - * PLSQL AST now has explicit ELSIF and ELSE clauses - * All of the ELSE_END_STATEMENTS in an IF clause - * should point to the outer last clause because - * we have to convert a single PL/SQL IF/ELSIF/ELSE satement into the equivalent - * set of nested Java if/else {if/else {if/else}} statements + * PLSQL AST now has explicit ELSIF and ELSE clauses All of the + * ELSE_END_STATEMENTS in an IF clause should point to the outer last + * clause because we have to convert a single PL/SQL IF/ELSIF/ELSE + * satement into the equivalent set of nested Java if/else {if/else + * {if/else}} statements */ - List elsifs = node.findChildrenOfType(ASTElsifClause.class); - ASTElseClause elseClause = node.getFirstChildOfType(ASTElseClause.class); - if (null == elseClause - && - elsifs.isEmpty() - ) // The IF statements has no ELSE or ELSIF statements and this is the last Statement - { - dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, dataFlow.getLast()); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (ASTIfClause - no ELSIFs) IF_LAST_STATEMENT_WITHOUT_ELSE: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } - } - else - { - if (!elsifs.isEmpty() ) - { + List elsifs = node.findChildrenOfType(ASTElsifClause.class); + ASTElseClause elseClause = node.getFirstChildOfType(ASTElseClause.class); + if (null == elseClause && elsifs.isEmpty()) // The IF statements has no + // ELSE or ELSIF statements + // and this is the last + // Statement + { + dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, dataFlow.getLast()); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("pushOnStack (ASTIfClause - no ELSIFs) IF_LAST_STATEMENT_WITHOUT_ELSE: line " + + node.getBeginLine() + ", column " + node.getBeginColumn()); + } + } else { + if (!elsifs.isEmpty()) { - ASTElsifClause lastElsifClause = elsifs.get(elsifs.size()-1); - for (ASTElsifClause elsifClause : elsifs ) - { + ASTElsifClause lastElsifClause = elsifs.get(elsifs.size() - 1); + for (ASTElsifClause elsifClause : elsifs) { - /* If last ELSIF clause is not followed by a ELSE clause - * then the last ELSIF is equivalent to an if statement without an else - * Otherwise, it is equivalent to an if/else statement - */ - if (lastElsifClause.equals(elsifClause) && elseClause == null) - { - dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, dataFlow.getLast()); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (ASTIfClause - with ELSIFs) IF_LAST_STATEMENT_WITHOUT_ELSE: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } + /* + * If last ELSIF clause is not followed by a ELSE clause + * then the last ELSIF is equivalent to an if statement + * without an else Otherwise, it is equivalent to an if/else + * statement + */ + if (lastElsifClause.equals(elsifClause) && elseClause == null) { + dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, dataFlow.getLast()); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("pushOnStack (ASTIfClause - with ELSIFs) IF_LAST_STATEMENT_WITHOUT_ELSE: line " + + node.getBeginLine() + ", column " + node.getBeginColumn()); + } + } + + { + dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT, dataFlow.getLast()); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("pushOnStack (ASTIfClause - with ELSIFs) ELSE_LAST_STATEMENT : line " + + node.getBeginLine() + ", column " + node.getBeginColumn()); + } + } } - - { - dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT, dataFlow.getLast()); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (ASTIfClause - with ELSIFs) ELSE_LAST_STATEMENT : line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } - } - } } - if (null != elseClause) - { - //Output one terminating else - dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT, dataFlow.getLast()); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (ASTIfClause - with ELSE) ELSE_LAST_STATEMENT : line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } + if (null != elseClause) { + // Output one terminating else + dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT, dataFlow.getLast()); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("pushOnStack (ASTIfClause - with ELSE) ELSE_LAST_STATEMENT : line " + + node.getBeginLine() + ", column " + node.getBeginColumn()); + } } - } + } return data; } - public Object visit(ASTElseClause node, Object data) { + public Object visit(ASTElseClause node, Object data) { if (!(data instanceof Structure)) { return data; } Structure dataFlow = (Structure) data; if (node.jjtGetParent() instanceof ASTIfStatement) { - dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT, dataFlow.getLast()); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (Visit ASTElseClause) IF_LAST_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - LOGGER.finest("ElseClause) super.visit line" ); - } - } - else - { - //SRT dataFlow.createNewNode(node); - dataFlow.pushOnStack(NodeType.SWITCH_LAST_DEFAULT_STATEMENT, dataFlow.getLast()); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack SWITCH_LAST_DEFAULT_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } + dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT, dataFlow.getLast()); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("pushOnStack (Visit ASTElseClause) IF_LAST_STATEMENT: line " + node.getBeginLine() + + ", column " + node.getBeginColumn()); + LOGGER.finest("ElseClause) super.visit line"); + } + } else { + // SRT dataFlow.createNewNode(node); + dataFlow.pushOnStack(NodeType.SWITCH_LAST_DEFAULT_STATEMENT, dataFlow.getLast()); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("pushOnStack SWITCH_LAST_DEFAULT_STATEMENT: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); + } } super.visit(node, data); @@ -611,15 +635,16 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { return data; } - public Object visit(ASTElsifClause node, Object data) { + public Object visit(ASTElsifClause node, Object data) { if (!(data instanceof Structure)) { return data; } Structure dataFlow = (Structure) data; dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (Visit ASTElsifClause) IF_LAST_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); - LOGGER.finest("ElsifClause) super.visit line" ); + LOGGER.finest("pushOnStack (Visit ASTElsifClause) IF_LAST_STATEMENT: line " + node.getBeginLine() + + ", column " + node.getBeginColumn()); + LOGGER.finest("ElsifClause) super.visit line"); } super.visit(node, data); @@ -631,7 +656,7 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { * * @param node * @param data - * @return + * @return */ public Object visit(ASTContinueStatement node, Object data) { if (!(data instanceof Structure)) { @@ -641,7 +666,8 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { dataFlow.createNewNode(node); dataFlow.pushOnStack(NodeType.CONTINUE_STATEMENT, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (ASTContinueStatement) CONTINUE_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack (ASTContinueStatement) CONTINUE_STATEMENT: line " + node.getBeginLine() + + ", column " + node.getBeginColumn()); } return super.visit(node, data); } @@ -651,7 +677,7 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { * * @param node * @param data - * @return + * @return */ public Object visit(ASTExitStatement node, Object data) { if (!(data instanceof Structure)) { @@ -661,17 +687,18 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { dataFlow.createNewNode(node); dataFlow.pushOnStack(NodeType.BREAK_STATEMENT, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (ASTExitStatement) BREAK_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack (ASTExitStatement) BREAK_STATEMENT: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return super.visit(node, data); } - + /** * Treat a PLSQL GOTO like a Java "continue" * * @param node * @param data - * @return + * @return */ public Object visit(ASTGotoStatement node, Object data) { if (!(data instanceof Structure)) { @@ -681,7 +708,8 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { dataFlow.createNewNode(node); dataFlow.pushOnStack(NodeType.CONTINUE_STATEMENT, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack (ASTGotoStatement) CONTINUE_STATEMENT (GOTO): line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack (ASTGotoStatement) CONTINUE_STATEMENT (GOTO): line " + node.getBeginLine() + + ", column " + node.getBeginColumn()); } return super.visit(node, data); } @@ -694,7 +722,8 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { dataFlow.createNewNode(node); dataFlow.pushOnStack(NodeType.RETURN_STATEMENT, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack RETURN_STATEMENT: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack RETURN_STATEMENT: line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); } return super.visit(node, data); } @@ -707,7 +736,7 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { dataFlow.createNewNode(node); dataFlow.pushOnStack(NodeType.THROW_STATEMENT, dataFlow.getLast()); if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("pushOnStack THROW: line " + node.getBeginLine() +", column " + node.getBeginColumn()); + LOGGER.finest("pushOnStack THROW: line " + node.getBeginLine() + ", column " + node.getBeginColumn()); } return super.visit(node, data); } @@ -715,7 +744,7 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { /* * The method handles the special "for" loop. It creates always an * expression node even if the loop looks like for(;;). - * */ + */ private void addForExpressionNode(Node node, Structure dataFlow) { ASTForStatement parent = (ASTForStatement) node.jjtGetParent(); boolean hasExpressionChild = false; @@ -723,15 +752,17 @@ public class StatementAndBraceFinder extends PLSQLParserVisitorAdapter { for (int i = 0; i < parent.jjtGetNumChildren(); i++) { if (parent.jjtGetChild(i) instanceof ASTExpression) { hasExpressionChild = true; - } - + } + } if (!hasExpressionChild) { if (node instanceof ASTStatement) { - /* if (!hasForInitNode && !hasForUpdateNode) { - dataFlow.createNewNode(node); - dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast()); - } */ + /* + * if (!hasForInitNode && !hasForUpdateNode) { + * dataFlow.createNewNode(node); + * dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast()); + * } + */ } } } diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/codesize/NPathComplexityRule.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/codesize/NPathComplexityRule.java index 5ba023ddfb..f86eb1ae55 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/codesize/NPathComplexityRule.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/codesize/NPathComplexityRule.java @@ -39,414 +39,402 @@ import net.sourceforge.pmd.util.NumericConstants; * @author Jason Bennett */ public class NPathComplexityRule extends AbstractStatisticalPLSQLRule { - private final static String CLASS_NAME = NPathComplexityRule.class.getCanonicalName() ; - private final static Logger LOGGER = Logger.getLogger(NPathComplexityRule.class.getName()); - + private final static String CLASS_NAME = NPathComplexityRule.class.getCanonicalName(); + private final static Logger LOGGER = Logger.getLogger(NPathComplexityRule.class.getName()); + public NPathComplexityRule() { - super(); - setProperty(MINIMUM_DESCRIPTOR, 200d); + super(); + setProperty(MINIMUM_DESCRIPTOR, 200d); } private int complexityMultipleOf(PLSQLNode node, int npathStart, Object data) { - LOGGER.entering(CLASS_NAME,"complexityMultipleOf(SimpleNode)"); + LOGGER.entering(CLASS_NAME, "complexityMultipleOf(SimpleNode)"); - int npath = npathStart; - PLSQLNode n; + int npath = npathStart; + PLSQLNode n; - for (int i = 0; i < node.jjtGetNumChildren(); i++) { - n = (PLSQLNode) node.jjtGetChild(i); - npath *= (Integer) n.jjtAccept(this, data); - } + for (int i = 0; i < node.jjtGetNumChildren(); i++) { + n = (PLSQLNode) node.jjtGetChild(i); + npath *= (Integer) n.jjtAccept(this, data); + } - LOGGER.exiting(CLASS_NAME,"complexityMultipleOf(SimpleNode)", npath); - return npath; - } - - private int complexitySumOf(PLSQLNode node, int npathStart, Object data) { - LOGGER.entering(CLASS_NAME,"complexitySumOf(SimpleNode)", npathStart ); - - int npath = npathStart; - PLSQLNode n; - - for (int i = 0; i < node.jjtGetNumChildren(); i++) { - n = (PLSQLNode) node.jjtGetChild(i); - npath += (Integer) n.jjtAccept(this, data); - } - - LOGGER.exiting(CLASS_NAME,"complexitySumOf(SimpleNode)", npath); - return npath; + LOGGER.exiting(CLASS_NAME, "complexityMultipleOf(SimpleNode)", npath); + return npath; } @Override public Object visit(ASTMethodDeclaration node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTMethodDeclaration)"); - int npath = complexityMultipleOf(node, 1, data); + LOGGER.entering(CLASS_NAME, "visit(ASTMethodDeclaration)"); + int npath = complexityMultipleOf(node, 1, data); - DataPoint point = new DataPoint(); - point.setNode(node); - point.setScore(1.0 * npath); - point.setMessage(getMessage()); - addDataPoint(point); + DataPoint point = new DataPoint(); + point.setNode(node); + point.setScore(1.0 * npath); + point.setMessage(getMessage()); + addDataPoint(point); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("NPath complexity: " + npath + " for line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } - LOGGER.exiting(CLASS_NAME,"visit(ASTMethodDeclaration)", npath); - return Integer.valueOf(npath); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("NPath complexity: " + npath + " for line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); + } + LOGGER.exiting(CLASS_NAME, "visit(ASTMethodDeclaration)", npath); + return Integer.valueOf(npath); } @Override public Object visit(ASTProgramUnit node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTProgramUnit)"); - int npath = complexityMultipleOf(node, 1, data); + LOGGER.entering(CLASS_NAME, "visit(ASTProgramUnit)"); + int npath = complexityMultipleOf(node, 1, data); - DataPoint point = new DataPoint(); - point.setNode(node); - point.setScore(1.0 * npath); - point.setMessage(getMessage()); - addDataPoint(point); + DataPoint point = new DataPoint(); + point.setNode(node); + point.setScore(1.0 * npath); + point.setMessage(getMessage()); + addDataPoint(point); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("NPath complexity: " + npath + " for line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } - LOGGER.exiting(CLASS_NAME,"visit(ASTProgramUnit)", npath); - return Integer.valueOf(npath); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("NPath complexity: " + npath + " for line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); + } + LOGGER.exiting(CLASS_NAME, "visit(ASTProgramUnit)", npath); + return Integer.valueOf(npath); } @Override public Object visit(ASTTypeMethod node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTTypeMethod)"); - int npath = complexityMultipleOf(node, 1, data); + LOGGER.entering(CLASS_NAME, "visit(ASTTypeMethod)"); + int npath = complexityMultipleOf(node, 1, data); - DataPoint point = new DataPoint(); - point.setNode(node); - point.setScore(1.0 * npath); - point.setMessage(getMessage()); - addDataPoint(point); + DataPoint point = new DataPoint(); + point.setNode(node); + point.setScore(1.0 * npath); + point.setMessage(getMessage()); + addDataPoint(point); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("NPath complexity: " + npath + " for line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } - LOGGER.exiting(CLASS_NAME,"visit(ASTTypeMethod)", npath); - return Integer.valueOf(npath); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("NPath complexity: " + npath + " for line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); + } + LOGGER.exiting(CLASS_NAME, "visit(ASTTypeMethod)", npath); + return Integer.valueOf(npath); } @Override public Object visit(ASTTriggerUnit node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTTriggerUnit)"); - int npath = complexityMultipleOf(node, 1, data); + LOGGER.entering(CLASS_NAME, "visit(ASTTriggerUnit)"); + int npath = complexityMultipleOf(node, 1, data); - DataPoint point = new DataPoint(); - point.setNode(node); - point.setScore(1.0 * npath); - point.setMessage(getMessage()); - addDataPoint(point); + DataPoint point = new DataPoint(); + point.setNode(node); + point.setScore(1.0 * npath); + point.setMessage(getMessage()); + addDataPoint(point); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("NPath complexity: " + npath + " for line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } - LOGGER.exiting(CLASS_NAME,"visit(ASTTriggerUnit)", npath); - return Integer.valueOf(npath); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("NPath complexity: " + npath + " for line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); + } + LOGGER.exiting(CLASS_NAME, "visit(ASTTriggerUnit)", npath); + return Integer.valueOf(npath); } @Override public Object visit(ASTTriggerTimingPointSection node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTTriggerTimingPointSection)"); - int npath = complexityMultipleOf(node, 1, data); + LOGGER.entering(CLASS_NAME, "visit(ASTTriggerTimingPointSection)"); + int npath = complexityMultipleOf(node, 1, data); - DataPoint point = new DataPoint(); - point.setNode(node); - point.setScore(1.0 * npath); - point.setMessage(getMessage()); - addDataPoint(point); + DataPoint point = new DataPoint(); + point.setNode(node); + point.setScore(1.0 * npath); + point.setMessage(getMessage()); + addDataPoint(point); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("NPath complexity: " + npath + " for line " + node.getBeginLine() +", column " + node.getBeginColumn()); - } - LOGGER.exiting(CLASS_NAME,"visit(ASTTriggerTimingPointSection)", npath); - return Integer.valueOf(npath); + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("NPath complexity: " + npath + " for line " + node.getBeginLine() + ", column " + + node.getBeginColumn()); + } + LOGGER.exiting(CLASS_NAME, "visit(ASTTriggerTimingPointSection)", npath); + return Integer.valueOf(npath); } @Override public Object visit(PLSQLNode node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(SimpleNode)"); - int npath = complexityMultipleOf(node, 1, data); - LOGGER.exiting(CLASS_NAME,"visit(SimpleNode)" ,npath ); - return Integer.valueOf(npath); + LOGGER.entering(CLASS_NAME, "visit(SimpleNode)"); + int npath = complexityMultipleOf(node, 1, data); + LOGGER.exiting(CLASS_NAME, "visit(SimpleNode)", npath); + return Integer.valueOf(npath); } @Override public Object visit(ASTIfStatement node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTIfStatement)"); - // (npath of if + npath of else (or 1) + bool_comp of if) * npath of next + LOGGER.entering(CLASS_NAME, "visit(ASTIfStatement)"); + // (npath of if + npath of else (or 1) + bool_comp of if) * npath of + // next - int boolCompIf = sumExpressionComplexity(node.getFirstChildOfType(ASTExpression.class)); + int boolCompIf = sumExpressionComplexity(node.getFirstChildOfType(ASTExpression.class)); - int complexity = 0; + int complexity = 0; - List statementChildren = new ArrayList(); - for (int i = 0; i < node.jjtGetNumChildren(); i++) { - if ( - node.jjtGetChild(i).getClass() == ASTStatement.class - ||node.jjtGetChild(i).getClass() == ASTElsifClause.class - ||node.jjtGetChild(i).getClass() == ASTElseClause.class - ) { - statementChildren.add((PLSQLNode) node.jjtGetChild(i)); - } - } - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest(statementChildren.size() + " statementChildren found for IF statement " + node.getBeginLine() +", column " + node.getBeginColumn()); - } + List statementChildren = new ArrayList(); + for (int i = 0; i < node.jjtGetNumChildren(); i++) { + if (node.jjtGetChild(i).getClass() == ASTStatement.class + || node.jjtGetChild(i).getClass() == ASTElsifClause.class + || node.jjtGetChild(i).getClass() == ASTElseClause.class) { + statementChildren.add((PLSQLNode) node.jjtGetChild(i)); + } + } + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest(statementChildren.size() + " statementChildren found for IF statement " + node.getBeginLine() + + ", column " + node.getBeginColumn()); + } - /* SRT - if (statementChildren.isEmpty() - || statementChildren.size() == 1 && ( null != node.getFirstChildOfType(ASTElseClause.class) ) //.hasElse() - || statementChildren.size() != 1 && ( null == node.getFirstChildOfType(ASTElseClause.class) ) // !node.hasElse() - ) { - throw new IllegalStateException("If node has wrong number of children"); - } - */ + /* + * SRT if (statementChildren.isEmpty() || statementChildren.size() == 1 + * && ( null != node.getFirstChildOfType(ASTElseClause.class) ) + * //.hasElse() || statementChildren.size() != 1 && ( null == + * node.getFirstChildOfType(ASTElseClause.class) ) // !node.hasElse() ) + * { throw new + * IllegalStateException("If node has wrong number of children"); } + */ - /* @TODO Any explicit Elsif clause(s) and Else clause are included in the list of statements - // add path for not taking if - if (null == node.getFirstChildOfType(ASTElsifClause.class) ) // !node.hasElse()!node.hasElse()) - { - complexity++; - } + /* + * @TODO Any explicit Elsif clause(s) and Else clause are included in + * the list of statements // add path for not taking if if (null == + * node.getFirstChildOfType(ASTElsifClause.class) ) // + * !node.hasElse()!node.hasElse()) { complexity++; } + * + * if (null == node.getFirstChildOfType(ASTElseClause.class) ) // + * !node.hasElse()!node.hasElse()) { complexity++; } + */ - if (null == node.getFirstChildOfType(ASTElseClause.class) ) // !node.hasElse()!node.hasElse()) - { - complexity++; - } - * */ + for (PLSQLNode element : statementChildren) { + complexity += (Integer) element.jjtAccept(this, data); + } - for (PLSQLNode element : statementChildren) { - complexity += (Integer) element.jjtAccept(this, data); - } - - LOGGER.exiting(CLASS_NAME,"visit(ASTIfStatement)", boolCompIf + complexity); - return Integer.valueOf(boolCompIf + complexity); + LOGGER.exiting(CLASS_NAME, "visit(ASTIfStatement)", boolCompIf + complexity); + return Integer.valueOf(boolCompIf + complexity); } @Override public Object visit(ASTElsifClause node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTElsifClause)"); - // (npath of if + npath of else (or 1) + bool_comp of if) * npath of next + LOGGER.entering(CLASS_NAME, "visit(ASTElsifClause)"); + // (npath of if + npath of else (or 1) + bool_comp of if) * npath of + // next - int boolCompIf = sumExpressionComplexity(node.getFirstChildOfType(ASTExpression.class)); + int boolCompIf = sumExpressionComplexity(node.getFirstChildOfType(ASTExpression.class)); - int complexity = 0; + int complexity = 0; - List statementChildren = new ArrayList(); - for (int i = 0; i < node.jjtGetNumChildren(); i++) { - if ( - node.jjtGetChild(i).getClass() == ASTStatement.class - ) { - statementChildren.add((PLSQLNode) node.jjtGetChild(i)); - } - } - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest(statementChildren.size() + " statementChildren found for ELSIF statement " + node.getBeginLine() +", column " + node.getBeginColumn()); - } + List statementChildren = new ArrayList(); + for (int i = 0; i < node.jjtGetNumChildren(); i++) { + if (node.jjtGetChild(i).getClass() == ASTStatement.class) { + statementChildren.add((PLSQLNode) node.jjtGetChild(i)); + } + } + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest(statementChildren.size() + " statementChildren found for ELSIF statement " + + node.getBeginLine() + ", column " + node.getBeginColumn()); + } - /* SRT - if (statementChildren.isEmpty() - || statementChildren.size() == 1 && ( null != node.getFirstChildOfType(ASTElseClause.class) ) //.hasElse() - || statementChildren.size() != 1 && ( null == node.getFirstChildOfType(ASTElseClause.class) ) // !node.hasElse() - ) { - throw new IllegalStateException("If node has wrong number of children"); - } - */ + /* + * SRT if (statementChildren.isEmpty() || statementChildren.size() == 1 + * && ( null != node.getFirstChildOfType(ASTElseClause.class) ) + * //.hasElse() || statementChildren.size() != 1 && ( null == + * node.getFirstChildOfType(ASTElseClause.class) ) // !node.hasElse() ) + * { throw new + * IllegalStateException("If node has wrong number of children"); } + */ - for (PLSQLNode element : statementChildren) { - complexity += (Integer) element.jjtAccept(this, data); - } + for (PLSQLNode element : statementChildren) { + complexity += (Integer) element.jjtAccept(this, data); + } - LOGGER.exiting(CLASS_NAME,"visit(ASTElsifClause)", boolCompIf + complexity); - return Integer.valueOf(boolCompIf + complexity); + LOGGER.exiting(CLASS_NAME, "visit(ASTElsifClause)", boolCompIf + complexity); + return Integer.valueOf(boolCompIf + complexity); } @Override public Object visit(ASTElseClause node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTElseClause)"); - // (npath of if + npath of else (or 1) + bool_comp of if) * npath of next + LOGGER.entering(CLASS_NAME, "visit(ASTElseClause)"); + // (npath of if + npath of else (or 1) + bool_comp of if) * npath of + // next - int complexity = 0; + int complexity = 0; - List statementChildren = new ArrayList(); - for (int i = 0; i < node.jjtGetNumChildren(); i++) { - if ( - node.jjtGetChild(i).getClass() == ASTStatement.class - ) { - statementChildren.add((PLSQLNode) node.jjtGetChild(i)); - } - } - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest(statementChildren.size() + " statementChildren found for ELSE clause statement " + node.getBeginLine() +", column " + node.getBeginColumn()); - } + List statementChildren = new ArrayList(); + for (int i = 0; i < node.jjtGetNumChildren(); i++) { + if (node.jjtGetChild(i).getClass() == ASTStatement.class) { + statementChildren.add((PLSQLNode) node.jjtGetChild(i)); + } + } + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest(statementChildren.size() + " statementChildren found for ELSE clause statement " + + node.getBeginLine() + ", column " + node.getBeginColumn()); + } - for (PLSQLNode element : statementChildren) { - complexity += (Integer) element.jjtAccept(this, data); - } + for (PLSQLNode element : statementChildren) { + complexity += (Integer) element.jjtAccept(this, data); + } - LOGGER.exiting(CLASS_NAME,"visit(ASTElseClause)", complexity); - return Integer.valueOf(complexity); + LOGGER.exiting(CLASS_NAME, "visit(ASTElseClause)", complexity); + return Integer.valueOf(complexity); } @Override public Object visit(ASTWhileStatement node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTWhileStatement)"); - // (npath of while + bool_comp of while + 1) * npath of next + LOGGER.entering(CLASS_NAME, "visit(ASTWhileStatement)"); + // (npath of while + bool_comp of while + 1) * npath of next - int boolCompWhile = sumExpressionComplexity(node.getFirstChildOfType(ASTExpression.class)); + int boolCompWhile = sumExpressionComplexity(node.getFirstChildOfType(ASTExpression.class)); - Integer nPathWhile = (Integer) ((PLSQLNode) node.getFirstChildOfType(ASTStatement.class)).jjtAccept(this, data); + Integer nPathWhile = (Integer) ((PLSQLNode) node.getFirstChildOfType(ASTStatement.class)).jjtAccept(this, data); - LOGGER.exiting(CLASS_NAME,"visit(ASTWhileStatement)", boolCompWhile + nPathWhile + 1); - return Integer.valueOf(boolCompWhile + nPathWhile + 1); + LOGGER.exiting(CLASS_NAME, "visit(ASTWhileStatement)", boolCompWhile + nPathWhile + 1); + return Integer.valueOf(boolCompWhile + nPathWhile + 1); } @Override public Object visit(ASTLoopStatement node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTLoopStatement)"); - // (npath of do + bool_comp of do + 1) * npath of next + LOGGER.entering(CLASS_NAME, "visit(ASTLoopStatement)"); + // (npath of do + bool_comp of do + 1) * npath of next - int boolCompDo = sumExpressionComplexity(node.getFirstChildOfType(ASTExpression.class)); + int boolCompDo = sumExpressionComplexity(node.getFirstChildOfType(ASTExpression.class)); - Integer nPathDo = (Integer) ((PLSQLNode) node.getFirstChildOfType(ASTStatement.class)).jjtAccept(this, data); + Integer nPathDo = (Integer) ((PLSQLNode) node.getFirstChildOfType(ASTStatement.class)).jjtAccept(this, data); - LOGGER.exiting(CLASS_NAME,"visit(ASTLoopStatement)" ,boolCompDo + nPathDo + 1); - return Integer.valueOf(boolCompDo + nPathDo + 1); + LOGGER.exiting(CLASS_NAME, "visit(ASTLoopStatement)", boolCompDo + nPathDo + 1); + return Integer.valueOf(boolCompDo + nPathDo + 1); } @Override public Object visit(ASTForStatement node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTForStatement)"); - // (npath of for + bool_comp of for + 1) * npath of next + LOGGER.entering(CLASS_NAME, "visit(ASTForStatement)"); + // (npath of for + bool_comp of for + 1) * npath of next - int boolCompFor = sumExpressionComplexity(node.getFirstDescendantOfType(ASTExpression.class)); + int boolCompFor = sumExpressionComplexity(node.getFirstDescendantOfType(ASTExpression.class)); - Integer nPathFor = (Integer) ((PLSQLNode) node.getFirstChildOfType(ASTStatement.class)).jjtAccept(this, data); + Integer nPathFor = (Integer) ((PLSQLNode) node.getFirstChildOfType(ASTStatement.class)).jjtAccept(this, data); - LOGGER.exiting(CLASS_NAME,"visit(ASTForStatement)",boolCompFor + nPathFor + 1); - return Integer.valueOf(boolCompFor + nPathFor + 1); + LOGGER.exiting(CLASS_NAME, "visit(ASTForStatement)", boolCompFor + nPathFor + 1); + return Integer.valueOf(boolCompFor + nPathFor + 1); } @Override public Object visit(ASTReturnStatement node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTReturnStatement)"); - // return statements are valued at 1, or the value of the boolean expression + LOGGER.entering(CLASS_NAME, "visit(ASTReturnStatement)"); + // return statements are valued at 1, or the value of the boolean + // expression - ASTExpression expr = node.getFirstChildOfType(ASTExpression.class); + ASTExpression expr = node.getFirstChildOfType(ASTExpression.class); - if (expr == null) { - return NumericConstants.ONE; - } + if (expr == null) { + return NumericConstants.ONE; + } - int boolCompReturn = sumExpressionComplexity(expr); - int conditionalExpressionComplexity = complexityMultipleOf(expr, 1, data); + int boolCompReturn = sumExpressionComplexity(expr); + int conditionalExpressionComplexity = complexityMultipleOf(expr, 1, data); - if (conditionalExpressionComplexity > 1) { - boolCompReturn += conditionalExpressionComplexity; - } + if (conditionalExpressionComplexity > 1) { + boolCompReturn += conditionalExpressionComplexity; + } - if (boolCompReturn > 0) { - return Integer.valueOf(boolCompReturn); - } - LOGGER.entering(CLASS_NAME,"visit(ASTReturnStatement)", NumericConstants.ONE); - return NumericConstants.ONE; + if (boolCompReturn > 0) { + return Integer.valueOf(boolCompReturn); + } + LOGGER.entering(CLASS_NAME, "visit(ASTReturnStatement)", NumericConstants.ONE); + return NumericConstants.ONE; } @Override public Object visit(ASTCaseWhenClause node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTCaseWhenClause)"); - // bool_comp of switch + sum(npath(case_range)) + LOGGER.entering(CLASS_NAME, "visit(ASTCaseWhenClause)"); + // bool_comp of switch + sum(npath(case_range)) - int boolCompSwitch = sumExpressionComplexity(node.getFirstChildOfType(ASTExpression.class)); + int boolCompSwitch = sumExpressionComplexity(node.getFirstChildOfType(ASTExpression.class)); - int npath = 1; - int caseRange = 0; - for (int i = 0; i < node.jjtGetNumChildren(); i++) { - PLSQLNode n = (PLSQLNode) node.jjtGetChild(i); + int npath = 1; + int caseRange = 0; + for (int i = 0; i < node.jjtGetNumChildren(); i++) { + PLSQLNode n = (PLSQLNode) node.jjtGetChild(i); - // Fall-through labels count as 1 for complexity + // Fall-through labels count as 1 for complexity Integer complexity = (Integer) n.jjtAccept(this, data); caseRange *= complexity; - } - // add in npath of last label - npath += caseRange; - LOGGER.exiting(CLASS_NAME,"visit(ASTCaseWhenClause)", (boolCompSwitch + npath) ); - return Integer.valueOf(boolCompSwitch + npath); + } + // add in npath of last label + npath += caseRange; + LOGGER.exiting(CLASS_NAME, "visit(ASTCaseWhenClause)", (boolCompSwitch + npath)); + return Integer.valueOf(boolCompSwitch + npath); } @Override public Object visit(ASTCaseStatement node, Object data) { - LOGGER.entering(CLASS_NAME,"visit(ASTCaseStatement)"); - // bool_comp of switch + sum(npath(case_range)) + LOGGER.entering(CLASS_NAME, "visit(ASTCaseStatement)"); + // bool_comp of switch + sum(npath(case_range)) - int boolCompSwitch = sumExpressionComplexity(node.getFirstChildOfType(ASTExpression.class)); + int boolCompSwitch = sumExpressionComplexity(node.getFirstChildOfType(ASTExpression.class)); - int npath = 0; - int caseRange = 0; - for (int i = 0; i < node.jjtGetNumChildren(); i++) { - PLSQLNode n = (PLSQLNode) node.jjtGetChild(i); + int npath = 0; + int caseRange = 0; + for (int i = 0; i < node.jjtGetNumChildren(); i++) { + PLSQLNode n = (PLSQLNode) node.jjtGetChild(i); - // Fall-through labels count as 1 for complexity + // Fall-through labels count as 1 for complexity Integer complexity = (Integer) n.jjtAccept(this, data); caseRange *= complexity; - } - // add in npath of last label - npath += caseRange; - LOGGER.exiting(CLASS_NAME,"visit(ASTCaseStatement)", (boolCompSwitch + npath)); - return Integer.valueOf(boolCompSwitch + npath); + } + // add in npath of last label + npath += caseRange; + LOGGER.exiting(CLASS_NAME, "visit(ASTCaseStatement)", (boolCompSwitch + npath)); + return Integer.valueOf(boolCompSwitch + npath); } @Override public Object visit(ASTConditionalOrExpression node, Object data) { - return NumericConstants.ONE; + return NumericConstants.ONE; } /** * Calculate the boolean complexity of the given expression. NPath boolean * complexity is the sum of && and || tokens. This is calculated by summing - * the number of children of the &&'s (minus one) and the children of the ||'s - * (minus one). + * the number of children of the &&'s (minus one) and the children of the + * ||'s (minus one). *

* Note that this calculation applies to Cyclomatic Complexity as well. * * @param expr - * control structure expression + * control structure expression * @return complexity of the boolean expression */ public static int sumExpressionComplexity(ASTExpression expr) { - LOGGER.entering(CLASS_NAME,"visit(ASTExpression)"); - if (expr == null) { - LOGGER.exiting(CLASS_NAME,"visit(ASTExpression)", 0); - return 0; - } + LOGGER.entering(CLASS_NAME, "visit(ASTExpression)"); + if (expr == null) { + LOGGER.exiting(CLASS_NAME, "visit(ASTExpression)", 0); + return 0; + } - List andNodes = expr.findDescendantsOfType(ASTConditionalAndExpression.class); - List orNodes = expr.findDescendantsOfType(ASTConditionalOrExpression.class); + List andNodes = expr.findDescendantsOfType(ASTConditionalAndExpression.class); + List orNodes = expr.findDescendantsOfType(ASTConditionalOrExpression.class); - int children = 0; + int children = 0; - for (ASTConditionalOrExpression element : orNodes) { - children += element.jjtGetNumChildren(); - children--; - } + for (ASTConditionalOrExpression element : orNodes) { + children += element.jjtGetNumChildren(); + children--; + } - for (ASTConditionalAndExpression element : andNodes) { - children += element.jjtGetNumChildren(); - children--; - } + for (ASTConditionalAndExpression element : andNodes) { + children += element.jjtGetNumChildren(); + children--; + } - LOGGER.exiting(CLASS_NAME,"visit(ASTExpression)", children ); - return children; + LOGGER.exiting(CLASS_NAME, "visit(ASTExpression)", children); + return children; } @Override public Object[] getViolationParameters(DataPoint point) { - return new String[] { ((ExecutableCode) point.getNode()).getMethodName(), - String.valueOf((int) point.getScore()) }; + return new String[] { ((ExecutableCode) point.getNode()).getMethodName(), + String.valueOf((int) point.getScore()) }; } } diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/codesize/TooManyFieldsRule.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/codesize/TooManyFieldsRule.java index 4b249a0b0d..e6e4aa4100 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/codesize/TooManyFieldsRule.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/codesize/TooManyFieldsRule.java @@ -17,7 +17,6 @@ import net.sourceforge.pmd.lang.plsql.rule.AbstractPLSQLRule; import net.sourceforge.pmd.lang.rule.properties.IntegerProperty; import net.sourceforge.pmd.util.NumericConstants; - public class TooManyFieldsRule extends AbstractPLSQLRule { private static final int DEFAULT_MAXFIELDS = 15; @@ -25,19 +24,16 @@ public class TooManyFieldsRule extends AbstractPLSQLRule { private Map stats; private Map nodes; - private static final IntegerProperty MAX_FIELDS_DESCRIPTOR = new IntegerProperty( - "maxfields", "Max allowable fields", - 1, 300, DEFAULT_MAXFIELDS, 1.0f - ); - + private static final IntegerProperty MAX_FIELDS_DESCRIPTOR = new IntegerProperty("maxfields", + "Max allowable fields", 1, 300, DEFAULT_MAXFIELDS, 1.0f); + public TooManyFieldsRule() { - definePropertyDescriptor(MAX_FIELDS_DESCRIPTOR); + definePropertyDescriptor(MAX_FIELDS_DESCRIPTOR); } @Override public Object visit(ASTInput node, Object data) { - stats = new HashMap(5); nodes = new HashMap(5); @@ -48,11 +44,11 @@ public class TooManyFieldsRule extends AbstractPLSQLRule { public Object visit(ASTPackageSpecification node, Object data) { int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR); - + List l = node.findDescendantsOfType(ASTVariableOrConstantDeclaration.class); - for (ASTVariableOrConstantDeclaration fd: l) { - bumpCounterFor(node); + for (ASTVariableOrConstantDeclaration fd : l) { + bumpCounterFor(fd); } for (String k : stats.keySet()) { int val = stats.get(k); @@ -68,11 +64,11 @@ public class TooManyFieldsRule extends AbstractPLSQLRule { public Object visit(ASTTypeSpecification node, Object data) { int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR); - + List l = node.findDescendantsOfType(ASTVariableOrConstantDeclaration.class); - for (ASTVariableOrConstantDeclaration fd: l) { - bumpCounterFor(node); + for (ASTVariableOrConstantDeclaration fd : l) { + bumpCounterFor(fd); } for (String k : stats.keySet()) { int val = stats.get(k); diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/ClassScope.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/ClassScope.java index b3dbb177b4..3285044bd2 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/ClassScope.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/ClassScope.java @@ -17,11 +17,13 @@ import net.sourceforge.pmd.lang.symboltable.NameDeclaration; import net.sourceforge.pmd.lang.symboltable.NameOccurrence; 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 private static ThreadLocal anonymousInnerClassCounter = new ThreadLocal() { - protected Integer initialValue() { return Integer.valueOf(1); } + protected Integer initialValue() { + return Integer.valueOf(1); + } }; private String className; @@ -34,12 +36,13 @@ public class ClassScope extends AbstractScope { /** * This is only for anonymous inner classes *

- * FIXME - should have name like Foo$1, not Anonymous$1 - * to get this working right, the parent scope needs - * to be passed in when instantiating a ClassScope + * FIXME - should have name like Foo$1, not Anonymous$1 to get this working + * right, the parent scope needs to be passed in when instantiating a + * ClassScope */ public ClassScope() { - //this.className = getParent().getEnclosingClassScope().getClassName() + "$" + String.valueOf(anonymousInnerClassCounter); + // this.className = getParent().getEnclosingClassScope().getClassName() + // + "$" + String.valueOf(anonymousInnerClassCounter); int v = anonymousInnerClassCounter.get().intValue(); this.className = "Anonymous$" + v; anonymousInnerClassCounter.set(v + 1); @@ -55,7 +58,7 @@ public class ClassScope extends AbstractScope { @Override public NameDeclaration addNameOccurrence(NameOccurrence occ) { - PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence)occ; + PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence) occ; NameDeclaration decl = findVariableHere(occurrence); Map> methodNames = getMethodDeclarations(); if (decl != null && occurrence.isMethodOrConstructorInvocation()) { @@ -113,15 +116,16 @@ public class ClassScope extends AbstractScope { if (variableDeclarations.isEmpty() && methodDeclarations.isEmpty()) { // this could happen if you do this: // public class Foo { - // private String x = super.toString(); + // private String x = super.toString(); // } 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 // public class Foo { - // private static final int X = 2; - // private int y = Foo.X; + // private static final int X = 2; + // private int y = Foo.X; // } // we'll look up Foo just to get a handle to the class scope // and then we'll look up X. @@ -132,14 +136,16 @@ public class ClassScope extends AbstractScope { } if (occurrence.isMethodOrConstructorInvocation()) { - for (MethodNameDeclaration mnd: methodDeclarations.keySet()) { + for (MethodNameDeclaration mnd : methodDeclarations.keySet()) { if (mnd.getImage().equals(occurrence.getImage())) { 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 // and parameter count, only one will get caught here - // we need to make some attempt at type lookup and discrimination - // or, failing that, mark this as a usage of all those methods + // we need to make some attempt at type lookup and + // discrimination + // or, failing that, mark this as a usage of all those + // methods return mnd; } } @@ -150,16 +156,13 @@ public class ClassScope extends AbstractScope { List images = new ArrayList(); images.add(occurrence.getImage()); - if (null==occurrence.getImage()) { - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("occurrence==" +occurrence.toString() - + "with Argumanet Count == "+occurrence.getArgumentCount() - + " for className="+className - ) ; - } - } + if (null == occurrence.getImage()) { + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("occurrence==" + occurrence.toString() + "with Argumanet Count == " + + occurrence.getArgumentCount() + " for className=" + className); + } + } - if (occurrence.getImage().startsWith(className)) { images.add(clipClassName(occurrence.getImage())); } @@ -177,7 +180,7 @@ public class ClassScope extends AbstractScope { res += "(" + classNames.keySet() + ")"; } if (!methodNames.isEmpty()) { - for (MethodNameDeclaration mnd: methodNames.keySet()) { + for (MethodNameDeclaration mnd : methodNames.keySet()) { res += mnd.toString(); int usages = methodNames.get(mnd).size(); res += "(begins at line " + mnd.getNode().getBeginLine() + ", " + usages + " usages)"; diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/MethodNameDeclaration.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/MethodNameDeclaration.java index d5e05f05f0..16bc6478f0 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/MethodNameDeclaration.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/MethodNameDeclaration.java @@ -15,17 +15,17 @@ import net.sourceforge.pmd.lang.plsql.ast.AbstractPLSQLNode; import net.sourceforge.pmd.lang.symboltable.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) { super(node); } - /** Treat a TimingPointSection within a Compound Trigger like a - * packaged FUNCTION or PROCEDURE. - * SRT + /** + * Treat a TimingPointSection within a Compound Trigger like a packaged + * FUNCTION or PROCEDURE. SRT * - * @param node + * @param node */ public MethodNameDeclaration(ASTTriggerTimingPointSection node) { super(node); @@ -37,16 +37,10 @@ public class MethodNameDeclaration extends AbstractNameDeclaration { /** * PL/SQL does not currently allow varargs outside the STANDARD package. - * @return false + * + * @return false */ 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; } @@ -55,16 +49,16 @@ public class MethodNameDeclaration extends AbstractNameDeclaration { } public String getParameterDisplaySignature() { - StringBuilder sb = new StringBuilder("("); + StringBuilder sb = new StringBuilder("("); ASTFormalParameters params = (ASTFormalParameters) node.jjtGetChild(0); // 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++) { ASTFormalParameter p = (ASTFormalParameter) params.jjtGetChild(i); sb.append(p.getTypeNode().getTypeImage()); - //if (p.isVarargs()) { - // sb.append("..."); - //} + // if (p.isVarargs()) { + // sb.append("..."); + // } sb.append(','); } if (sb.charAt(sb.length() - 1) == ',') { @@ -87,24 +81,27 @@ public class MethodNameDeclaration extends AbstractNameDeclaration { 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()) { return false; } // compare parameter types - //SRT ASTFormalParameters myParams = (ASTFormalParameters) node.jjtGetChild(0); - //SRT ASTFormalParameters otherParams = (ASTFormalParameters) other.node.jjtGetChild(0); - ASTFormalParameters myParams = node.getFirstDescendantOfType(ASTFormalParameters.class) ; - ASTFormalParameters otherParams = other.node.getFirstDescendantOfType(ASTFormalParameters.class) ; + // SRT ASTFormalParameters myParams = (ASTFormalParameters) + // node.jjtGetChild(0); + // SRT ASTFormalParameters otherParams = (ASTFormalParameters) + // 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++) { ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i); ASTFormalParameter otherParam = (ASTFormalParameter) otherParams.jjtGetChild(i); // Compare vararg - //if (myParam.isVarargs() != otherParam.isVarargs()) { - // return false; - //} + // if (myParam.isVarargs() != otherParam.isVarargs()) { + // return false; + // } Node myTypeNode = myParam.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 String myTypeImg; String otherTypeImg; - //if (myTypeNode instanceof ASTPrimitiveType) { - // myTypeImg = myTypeNode.getImage(); - // otherTypeImg = otherTypeNode.getImage(); - //} else { - myTypeImg = ( (AbstractPLSQLNode) myTypeNode .jjtGetChild(0) ) .getImage(); - otherTypeImg = ( (AbstractPLSQLNode) otherTypeNode.jjtGetChild(0) ).getImage(); - //} + // if (myTypeNode instanceof ASTPrimitiveType) { + // myTypeImg = myTypeNode.getImage(); + // otherTypeImg = otherTypeNode.getImage(); + // } else { + myTypeImg = ((AbstractPLSQLNode) myTypeNode.jjtGetChild(0)).getImage(); + otherTypeImg = ((AbstractPLSQLNode) otherTypeNode.jjtGetChild(0)).getImage(); + // } if (!myTypeImg.equals(otherTypeImg)) { 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; } @Override public int hashCode() { - try - { - return node.hashCode(); //SRT node.getImage().hashCode() + ((ASTMethodDeclarator) node).getParameterCount(); - } - catch (Exception e) - { - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("MethodNameDeclaration problem for " + node - +" of class " + node.getClass().getCanonicalName() - +" => "+ node.getBeginLine()+"/"+node.getBeginColumn() - ); - } - //@TODO SRT restore the thrown exception - throw e; - return 0; - } + try { + return node.hashCode(); // SRT node.getImage().hashCode() + + // ((ASTMethodDeclarator) + // node).getParameterCount(); + } catch (Exception e) { + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("MethodNameDeclaration problem for " + node + " of class " + + node.getClass().getCanonicalName() + " => " + node.getBeginLine() + "/" + + node.getBeginColumn()); + } + // @TODO SRT restore the thrown exception - throw e; + return 0; + } } @Override 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(); } } diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/PLSQLNameOccurrence.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/PLSQLNameOccurrence.java index 74579ff927..6916d21f4c 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/PLSQLNameOccurrence.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/PLSQLNameOccurrence.java @@ -5,7 +5,6 @@ package net.sourceforge.pmd.lang.plsql.symboltable; import net.sourceforge.pmd.lang.ast.Node; 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.PLSQLNode; import net.sourceforge.pmd.lang.symboltable.NameOccurrence; @@ -60,133 +59,110 @@ public class PLSQLNameOccurrence implements NameOccurrence { } public boolean isOnRightHandSide() { - Node node = location.jjtGetParent().jjtGetParent().jjtGetParent(); + Node node = location.jjtGetParent().jjtGetParent().jjtGetParent(); return node instanceof ASTExpression && node.jjtGetNumChildren() == 3; } - public boolean isOnLeftHandSide() { // I detest this method with every atom of my being - Node primaryExpression; + Node primaryExpression; if (location.jjtGetParent() instanceof ASTPrimaryExpression) { primaryExpression = location.jjtGetParent().jjtGetParent(); } else if (location.jjtGetParent().jjtGetParent() instanceof ASTPrimaryExpression) { primaryExpression = location.jjtGetParent().jjtGetParent().jjtGetParent(); } else { - throw new RuntimeException("Found a NameOccurrence that didn't have an ASTPrimaryExpression as parent or grandparent. " - + " Node = " + location.getClass().getCanonicalName() - + ", Parent = " + location.jjtGetParent().getClass().getCanonicalName() - + " and grandparent = " + location.jjtGetParent().jjtGetParent().getClass().getCanonicalName() - + " @ line = " + location.getBeginLine() + ", column = " + location.getBeginColumn() - ); + throw new RuntimeException( + "Found a NameOccurrence that didn't have an ASTPrimaryExpression as parent or grandparent. " + + " Node = " + location.getClass().getCanonicalName() + ", Parent = " + + location.jjtGetParent().getClass().getCanonicalName() + " and grandparent = " + + location.jjtGetParent().jjtGetParent().getClass().getCanonicalName() + " @ line = " + + location.getBeginLine() + ", column = " + location.getBeginColumn()); } /* - if (isStandAlonePostfix(primaryExpression)) { - return true; - } - */ + * if (isStandAlonePostfix(primaryExpression)) { return true; } + */ if (primaryExpression.jjtGetNumChildren() <= 1) { return false; } /* - if (!(primaryExpression.jjtGetChild(1) instanceof ASTAssignmentOperator)) { - return false; - } - */ + * if (!(primaryExpression.jjtGetChild(1) instanceof + * ASTAssignmentOperator)) { return false; } + */ if (isPartOfQualifiedName() /* or is an array type */) { return false; } /* - if (isCompoundAssignment(primaryExpression)) { - return false; - } - */ + * if (isCompoundAssignment(primaryExpression)) { return false; } + */ return true; } /* - private boolean isCompoundAssignment(Node primaryExpression) { - return ((ASTAssignmentOperator) primaryExpression.jjtGetChild(1)).isCompound(); - } - - private boolean isStandAlonePostfix(Node primaryExpression) { - if (!(primaryExpression instanceof ASTPostfixExpression) || !(primaryExpression.jjtGetParent() instanceof ASTStatementExpression)) { - return false; - } - - ASTPrimaryPrefix pf = (ASTPrimaryPrefix) ((ASTPrimaryExpression) primaryExpression.jjtGetChild(0)).jjtGetChild(0); - if (pf.usesThisModifier()) { - return true; - } - - return thirdChildHasDottedName(primaryExpression); - } - */ - - private boolean thirdChildHasDottedName(Node primaryExpression) { - Node thirdChild = primaryExpression.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0); - return thirdChild instanceof ASTName && ((ASTName) thirdChild).getImage().indexOf('.') == -1; - } + * private boolean isCompoundAssignment(Node primaryExpression) { return + * ((ASTAssignmentOperator) primaryExpression.jjtGetChild(1)).isCompound(); + * } + * + * private boolean isStandAlonePostfix(Node primaryExpression) { if + * (!(primaryExpression instanceof ASTPostfixExpression) || + * !(primaryExpression.jjtGetParent() instanceof ASTStatementExpression)) { + * return false; } + * + * ASTPrimaryPrefix pf = (ASTPrimaryPrefix) ((ASTPrimaryExpression) + * primaryExpression.jjtGetChild(0)).jjtGetChild(0); if + * (pf.usesThisModifier()) { return true; } + * + * return thirdChildHasDottedName(primaryExpression); } + * + * private boolean thirdChildHasDottedName(Node primaryExpression) { Node + * thirdChild = + * primaryExpression.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0); 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: * i += 3; * * * @return true, if the occurrence is self-assignment, false, otherwise. */ /* - @SuppressWarnings("PMD.AvoidBranchingStatementAsLastInLoop") - public boolean isSelfAssignment() { - Node l = location; - while (true) { - Node p = l.jjtGetParent(); - Node gp = p.jjtGetParent(); - Node node = gp.jjtGetParent(); - if (node instanceof ASTPreDecrementExpression || node instanceof ASTPreIncrementExpression || node instanceof ASTPostfixExpression) { - return true; - } - - if (hasAssignmentOperator(gp)) { - return isCompoundAssignment(gp); - } - - if (hasAssignmentOperator(node)) { - return isCompoundAssignment(node); - } - - // deal with extra parenthesis: "(i)++" - if (p instanceof ASTPrimaryPrefix && p.jjtGetNumChildren() == 1 && - 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; - } - } - */ + * @SuppressWarnings("PMD.AvoidBranchingStatementAsLastInLoop") public + * boolean isSelfAssignment() { Node l = location; while (true) { Node p = + * l.jjtGetParent(); Node gp = p.jjtGetParent(); Node node = + * gp.jjtGetParent(); if (node instanceof ASTPreDecrementExpression || node + * instanceof ASTPreIncrementExpression || node instanceof + * ASTPostfixExpression) { return true; } + * + * if (hasAssignmentOperator(gp)) { return isCompoundAssignment(gp); } + * + * if (hasAssignmentOperator(node)) { return isCompoundAssignment(node); } + * + * // deal with extra parenthesis: "(i)++" if (p instanceof ASTPrimaryPrefix + * && p.jjtGetNumChildren() == 1 && 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) { - if (node instanceof ASTStatementExpression || node instanceof ASTExpression) { - if (node.jjtGetNumChildren() >= 2 && node.jjtGetChild(1) instanceof ASTAssignmentOperator) { - return true; - } - } - return false; - } - */ + * private boolean hasAssignmentOperator(Node node) { if (node instanceof + * ASTStatementExpression || node instanceof ASTExpression) { if + * (node.jjtGetNumChildren() >= 2 && node.jjtGetChild(1) instanceof + * ASTAssignmentOperator) { return true; } } return false; } + */ /** * 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. */ /* - public boolean useThisOrSuper() { - Node node = location.jjtGetParent(); - if ( node instanceof ASTPrimaryExpression ) { - ASTPrimaryExpression primaryExpression = (ASTPrimaryExpression)node; - ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) primaryExpression.jjtGetChild(0); - if ( prefix != null ) { - return prefix.usesSuperModifier() || prefix.usesThisModifier(); - } - } - return image.startsWith(THIS_DOT) || image.startsWith(SUPER_DOT); - } - */ + * public boolean useThisOrSuper() { Node node = location.jjtGetParent(); if + * ( node instanceof ASTPrimaryExpression ) { ASTPrimaryExpression + * primaryExpression = (ASTPrimaryExpression)node; ASTPrimaryPrefix prefix = + * (ASTPrimaryPrefix) primaryExpression.jjtGetChild(0); if ( prefix != null + * ) { return prefix.usesSuperModifier() || prefix.usesThisModifier(); } } + * return image.startsWith(THIS_DOT) || image.startsWith(SUPER_DOT); } + */ @Override public boolean equals(Object o) { - if (o instanceof PLSQLNameOccurrence) { - PLSQLNameOccurrence n = (PLSQLNameOccurrence) o; - return n.getImage().equals(getImage()); - } - return false; + if (o instanceof PLSQLNameOccurrence) { + PLSQLNameOccurrence n = (PLSQLNameOccurrence) o; + return n.getImage().equals(getImage()); + } + return false; } @Override @@ -236,6 +207,7 @@ public class PLSQLNameOccurrence implements NameOccurrence { @Override public String toString() { - return getImage() + ":" + location.getBeginLine() + ":" + location.getClass() + (this.isMethodOrConstructorInvocation() ? "(method call)" : ""); + return getImage() + ":" + location.getBeginLine() + ":" + location.getClass() + + (this.isMethodOrConstructorInvocation() ? "(method call)" : ""); } } diff --git a/pmd-plsql/src/test/java/net/sourceforge/pmd/cpd/PLSQLTokenizerTest.java b/pmd-plsql/src/test/java/net/sourceforge/pmd/cpd/PLSQLTokenizerTest.java index cec2a7ecd6..e006446ea2 100644 --- a/pmd-plsql/src/test/java/net/sourceforge/pmd/cpd/PLSQLTokenizerTest.java +++ b/pmd-plsql/src/test/java/net/sourceforge/pmd/cpd/PLSQLTokenizerTest.java @@ -6,8 +6,8 @@ package net.sourceforge.pmd.cpd; import java.io.IOException; 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.Test; @@ -18,14 +18,14 @@ public class PLSQLTokenizerTest extends AbstractTokenizerTest { @Before @Override - public void buildTokenizer() { + public void buildTokenizer() throws IOException { this.tokenizer = new PLSQLTokenizer(); this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME)); } @Override - public String getSampleCode() { - return StreamUtil.toString(PLSQLTokenizer.class.getResourceAsStream(FILENAME)); + public String getSampleCode() throws IOException { + return IOUtils.toString(PLSQLTokenizer.class.getResourceAsStream(FILENAME)); } @Test diff --git a/pmd-python/src/test/java/net/sourceforge/pmd/cpd/PythonTokenizerTest.java b/pmd-python/src/test/java/net/sourceforge/pmd/cpd/PythonTokenizerTest.java index 315d1d8e94..141446520f 100644 --- a/pmd-python/src/test/java/net/sourceforge/pmd/cpd/PythonTokenizerTest.java +++ b/pmd-python/src/test/java/net/sourceforge/pmd/cpd/PythonTokenizerTest.java @@ -6,8 +6,8 @@ package net.sourceforge.pmd.cpd; import java.io.IOException; 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.Test; @@ -18,14 +18,14 @@ public class PythonTokenizerTest extends AbstractTokenizerTest { @Before @Override - public void buildTokenizer() { + public void buildTokenizer() throws IOException { this.tokenizer = new PythonTokenizer(); this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME)); } @Override - public String getSampleCode() { - return StreamUtil.toString(PythonTokenizer.class.getResourceAsStream(FILENAME)); + public String getSampleCode() throws IOException { + return IOUtils.toString(PythonTokenizer.class.getResourceAsStream(FILENAME)); } @Test diff --git a/pmd-scala/src/main/java/org/sonar/plugins/scala/util/StringUtils.java b/pmd-scala/src/main/java/org/sonar/plugins/scala/util/StringUtils.java index 4ea6ec8830..1db4d308a2 100644 --- a/pmd-scala/src/main/java/org/sonar/plugins/scala/util/StringUtils.java +++ b/pmd-scala/src/main/java/org/sonar/plugins/scala/util/StringUtils.java @@ -41,8 +41,6 @@ public final class StringUtils { while ((line = reader.readLine()) != null) { lines.add(line); } - } catch (IOException ioe) { - throw ioe; } finally { IOUtils.closeQuietly(reader); } diff --git a/pmd-scala/src/test/java/net/sourceforge/pmd/cpd/ScalaTokenizerTest.java b/pmd-scala/src/test/java/net/sourceforge/pmd/cpd/ScalaTokenizerTest.java index 44c00d48dd..d3fc905927 100644 --- a/pmd-scala/src/test/java/net/sourceforge/pmd/cpd/ScalaTokenizerTest.java +++ b/pmd-scala/src/test/java/net/sourceforge/pmd/cpd/ScalaTokenizerTest.java @@ -7,9 +7,9 @@ import java.io.File; import java.io.IOException; import net.sourceforge.pmd.testframework.AbstractTokenizerTest; -import net.sourceforge.pmd.testframework.StreamUtil; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -18,32 +18,29 @@ import org.sonar.plugins.scala.cpd.ScalaTokenizer; public class ScalaTokenizerTest extends AbstractTokenizerTest { + private static final String ENCODING = "UTF-8"; + private static final String FILENAME = "sample-LiftActor.scala"; private File tempFile; @Before @Override - public void buildTokenizer() { + public void buildTokenizer() throws IOException { createTempFileOnDisk(); this.tokenizer = new ScalaTokenizer(); this.sourceCode = new SourceCode(new SourceCode.FileCodeLoader(tempFile, "UTF-8")); } - private void createTempFileOnDisk() { - try { - this.tempFile = File.createTempFile("scala-tokenizer-test-", ".scala"); - - FileUtils.writeStringToFile(tempFile, getSampleCode(), "UTF-8"); - } catch (IOException e) { - throw new RuntimeException("Unable to create temporary file on disk for Scala tokenizer test", e); - } + private void createTempFileOnDisk() throws IOException { + this.tempFile = File.createTempFile("scala-tokenizer-test-", ".scala"); + FileUtils.writeStringToFile(tempFile, getSampleCode(), ENCODING); } @Override - public String getSampleCode() { - return StreamUtil.toString(ScalaTokenizer.class.getResourceAsStream(FILENAME)); + public String getSampleCode() throws IOException { + return IOUtils.toString(ScalaTokenizer.class.getResourceAsStream(FILENAME), ENCODING); } @Test @@ -57,8 +54,4 @@ public class ScalaTokenizerTest extends AbstractTokenizerTest { FileUtils.deleteQuietly(this.tempFile); this.tempFile = null; } - - public static junit.framework.Test suite() { - return new junit.framework.JUnit4TestAdapter(ScalaTokenizerTest.class); - } } diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/lang/ParserOptionsTest.java b/pmd-test/src/main/java/net/sourceforge/pmd/lang/ParserOptionsTest.java index 211e7889f5..deff1a614f 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/lang/ParserOptionsTest.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/lang/ParserOptionsTest.java @@ -3,12 +3,7 @@ */ package net.sourceforge.pmd.lang; -import static org.junit.Assert.assertEquals; -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.Assert; import org.junit.Test; /** @@ -22,9 +17,9 @@ public class ParserOptionsTest { @Test public void testSuppressMarker() { ParserOptions parserOptions = new ParserOptions(); - assertNull(parserOptions.getSuppressMarker()); + Assert.assertNull(parserOptions.getSuppressMarker()); 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, ParserOptions options3, ParserOptions options4) { // Objects should be different - assertNotSame(options1, options2); - assertNotSame(options1, options2); - assertNotSame(options1, options3); - assertNotSame(options2, options3); - assertNotSame(options2, options4); - assertNotSame(options3, options4); + Assert.assertNotSame(options1, options2); + Assert.assertNotSame(options1, options2); + Assert.assertNotSame(options1, options3); + Assert.assertNotSame(options2, options3); + Assert.assertNotSame(options2, options4); + Assert.assertNotSame(options3, options4); // Check all 16 equality combinations - assertEquals(options1, options1); - assertFalse(options1.equals(options2)); - assertEquals(options1, options3); - assertFalse(options1.equals(options4)); + Assert.assertEquals(options1, options1); + Assert.assertFalse(options1.equals(options2)); + Assert.assertEquals(options1, options3); + Assert.assertFalse(options1.equals(options4)); - assertFalse(options2.equals(options1)); - assertEquals(options2, options2); - assertFalse(options2.equals(options3)); - assertEquals(options2, options4); + Assert.assertFalse(options2.equals(options1)); + Assert.assertEquals(options2, options2); + Assert.assertFalse(options2.equals(options3)); + Assert.assertEquals(options2, options4); - assertEquals(options3, options1); - assertFalse(options3.equals(options2)); - assertEquals(options3, options3); - assertFalse(options3.equals(options4)); + Assert.assertEquals(options3, options1); + Assert.assertFalse(options3.equals(options2)); + Assert.assertEquals(options3, options3); + Assert.assertFalse(options3.equals(options4)); - assertFalse(options4.equals(options1)); - assertEquals(options4, options2); - assertFalse(options4.equals(options3)); - assertEquals(options4, options4); + Assert.assertFalse(options4.equals(options1)); + Assert.assertEquals(options4, options2); + Assert.assertFalse(options4.equals(options3)); + Assert.assertEquals(options4, options4); // Hashcodes should match up - assertNotEquals(options1.hashCode(), options2.hashCode()); - assertEquals(options1.hashCode(), options3.hashCode()); - assertNotEquals(options1.hashCode(), options4.hashCode()); - assertNotEquals(options2.hashCode(), options3.hashCode()); - assertEquals(options2.hashCode(), options4.hashCode()); - assertNotEquals(options3.hashCode(), options4.hashCode()); + Assert.assertNotEquals(options1.hashCode(), options2.hashCode()); + Assert.assertEquals(options1.hashCode(), options3.hashCode()); + Assert.assertNotEquals(options1.hashCode(), options4.hashCode()); + Assert.assertNotEquals(options2.hashCode(), options3.hashCode()); + Assert.assertEquals(options2.hashCode(), options4.hashCode()); + Assert.assertNotEquals(options3.hashCode(), options4.hashCode()); } } diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/AbstractTokenizerTest.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/AbstractTokenizerTest.java index 7a54498788..5675184e07 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/AbstractTokenizerTest.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/AbstractTokenizerTest.java @@ -19,19 +19,19 @@ import net.sourceforge.pmd.cpd.Tokens; */ public abstract class AbstractTokenizerTest { - protected int expectedTokenCount; - protected Tokenizer tokenizer; - protected SourceCode sourceCode; + protected int expectedTokenCount; + protected Tokenizer tokenizer; + 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 { - Tokens tokens = new Tokens(); - tokenizer.tokenize(sourceCode, tokens); - List entries = tokens.getTokens(); - assertEquals(expectedTokenCount,entries.size()); - } + protected void tokenizeTest() throws IOException { + Tokens tokens = new Tokens(); + tokenizer.tokenize(sourceCode, tokens); + List entries = tokens.getTokens(); + assertEquals(expectedTokenCount, entries.size()); + } } diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/StreamUtil.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/StreamUtil.java index bd05960dcd..3c8cd5ed40 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/StreamUtil.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/StreamUtil.java @@ -8,26 +8,25 @@ import java.io.InputStream; import org.apache.commons.io.IOUtils; - +/** + * @deprecated Just use apache {@link IOUtils} + */ +@Deprecated public final class StreamUtil { private StreamUtil() { // utility class } - public static String toString(InputStream in) { - StringBuilder sb = new StringBuilder(); - int c; - try { - while ((c = in.read()) != -1) { - sb.append((char) c); - } - } catch (IOException e) { - // ignored - } finally { - IOUtils.closeQuietly(in); - } - return sb.toString(); - } - + /** + * @deprecated use {@link IOUtils#toString(InputStream)} instead + */ + @Deprecated + public static String toString(InputStream stream) { + try { + return IOUtils.toString(stream); + } catch (IOException e) { + throw new RuntimeException(e); + } + } } diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 722d5fac24..478b2c7a6e 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -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. 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. + +* pmd-test: The utility class `StreamUtil` is deprecated. Just use Apache Commons IO Utils instead.