From c162ee1c0d70658d0ac03727050120088072b490 Mon Sep 17 00:00:00 2001 From: "daan.vandenheuvel" Date: Thu, 10 Mar 2022 13:32:31 +0100 Subject: [PATCH 1/3] add first model classes for ControlFlow --- src/main/java/model/CodeBlockCondition.java | 63 +++++++++++++++++++++ src/main/java/model/DataFlowCodeBlock.java | 61 ++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/main/java/model/CodeBlockCondition.java create mode 100644 src/main/java/model/DataFlowCodeBlock.java diff --git a/src/main/java/model/CodeBlockCondition.java b/src/main/java/model/CodeBlockCondition.java new file mode 100644 index 0000000..4297ffd --- /dev/null +++ b/src/main/java/model/CodeBlockCondition.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022 by Eyefreight BV (www.eyefreight.com). All rights reserved. + * + * This software is provided by the copyright holder and contributors "as is" and any express or implied warranties, including, but + * not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall + * Eyefreight BV or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages + * (including, but not limited to, procurement of substitute goods or services; * loss of use, data, or profits; or business + * interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including + * negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. + */ +package model; + +/** + * The condition that needs to be true before a {@link DataFlowCodeBlock} is executed. + * + * @author daan.vandenheuvel + */ +public class CodeBlockCondition { + + // TODO maybe this class should also be a DataFlowNode because I assume that all if statements are also nodes themselves. However, I'm not sure about else-if + // statements in regards to the internal preCondition. The general rule that there should be a one-on-one mapping between JavaParser and JavaDataFlow. We + // could also wrap them in an "inverted condition", but keep a reference to the original CodeBlockCondition. + + /** The DataFlowNode should evaluate to this same value before the CodeBlock will be executed. */ + private final boolean nodeEvaluatesTo; + /** + * If this node evaluates to the same value as {@link CodeBlockCondition#nodeEvaluatesTo} the codeBlock should be executed. This DataFlowNode should have an + * outgoing edge to this CodeBlockCondition. + */ + private final DataFlowNode node; + /** + * The codeBlock should only be executed if this precondition also evaluates to true or if this is null. This is typically used in case of an else-if + * statement where the first if needs to be false and the second if needs to be true. + */ + private final CodeBlockCondition preCondition; + + public CodeBlockCondition(DataFlowNode node, boolean nodeEvaluatesTo, CodeBlockCondition preCondition) { + this.node = node; + this.nodeEvaluatesTo = nodeEvaluatesTo; + this.preCondition = preCondition; + } + + public CodeBlockCondition(DataFlowNode node, boolean nodeEvaluatesTo) { + this(node, nodeEvaluatesTo, null); + } + + public CodeBlockCondition(DataFlowNode node) { + this(node, true, null); + } + + public CodeBlockCondition getPreCondition() { + return preCondition; + } + + public DataFlowNode getNode() { + return node; + } + + public boolean isNodeEvaluatesTo() { + return nodeEvaluatesTo; + } + +} diff --git a/src/main/java/model/DataFlowCodeBlock.java b/src/main/java/model/DataFlowCodeBlock.java new file mode 100644 index 0000000..32eb213 --- /dev/null +++ b/src/main/java/model/DataFlowCodeBlock.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2022 by Eyefreight BV (www.eyefreight.com). All rights reserved. + * + * This software is provided by the copyright holder and contributors "as is" and any express or implied warranties, including, but + * not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall + * Eyefreight BV or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages + * (including, but not limited to, procurement of substitute goods or services; * loss of use, data, or profits; or business + * interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including + * negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. + */ +package model; + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Optional; + +/** + * A block of code in a java class. This contains a list of nodes that will be executed in sequence. A code block has a (possibly empty) list of codeBlock's + * that succeed this codeBlock as well as a list preceeding it. + * + * @author daan.vandenheuvel + */ +public class DataFlowCodeBlock extends OwnerNode { // TODO find the JavaParser Node that represents a code block and add it as type argument. + + // TODO methods and classes should extend this. + + /** The list of all possible CodeBlocks that might lead to this CodeBlock. */ + private List calledBy; + + /** + * A map of all CodeBlocks that might be executed inside this CodeBlock in order of occurrence. The CodeBlock is only executed if the {@link DataFlowNode} key + * evaluates to true. + */ + // TODO What do we do with CodeBlocks that are always executed? + // TODO What do we do with CodeBlocks that are executed if the DataFlowNode evaaluates to false (in case of else statements) + // TODO What do we do with if-else statements, where one node should be false (the if-statement) and the next should be true (the else-if-statement). + private LinkedHashMap called; + + /** List of {@link DataFlowNode}s that are executed inside this CodeBlock. */ + private List dataFlowNodes; + + @Override + Collection getOwnedOwners() { + // TODO Auto-generated method stub + return null; + } + + @Override + Collection getDirectOwnedNodes() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Optional getOwner() { + // TODO Auto-generated method stub + return null; + } + +} From 1696783d15d896dfe9b41f4b6dc3c21d827dcfe9 Mon Sep 17 00:00:00 2001 From: "daan.vandenheuvel" Date: Thu, 10 Mar 2022 17:18:05 +0100 Subject: [PATCH 2/3] Change type of called --- src/main/java/model/DataFlowCodeBlock.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/model/DataFlowCodeBlock.java b/src/main/java/model/DataFlowCodeBlock.java index 32eb213..d3b0034 100644 --- a/src/main/java/model/DataFlowCodeBlock.java +++ b/src/main/java/model/DataFlowCodeBlock.java @@ -11,10 +11,11 @@ package model; import java.util.Collection; -import java.util.LinkedHashMap; import java.util.List; import java.util.Optional; +import org.apache.commons.lang3.tuple.Pair; + /** * A block of code in a java class. This contains a list of nodes that will be executed in sequence. A code block has a (possibly empty) list of codeBlock's * that succeed this codeBlock as well as a list preceeding it. @@ -30,12 +31,10 @@ public class DataFlowCodeBlock extends OwnerNode { // TODO find the JavaParser N /** * A map of all CodeBlocks that might be executed inside this CodeBlock in order of occurrence. The CodeBlock is only executed if the {@link DataFlowNode} key - * evaluates to true. + * evaluates to true. The CodeBlockCondition can be null, if the DataFlowCodeBlock is always executed. */ - // TODO What do we do with CodeBlocks that are always executed? - // TODO What do we do with CodeBlocks that are executed if the DataFlowNode evaaluates to false (in case of else statements) // TODO What do we do with if-else statements, where one node should be false (the if-statement) and the next should be true (the else-if-statement). - private LinkedHashMap called; + private List> called; /** List of {@link DataFlowNode}s that are executed inside this CodeBlock. */ private List dataFlowNodes; From a1279d751d641781d6f4ef0cd91b6621c2d57d42 Mon Sep 17 00:00:00 2001 From: "daan.vandenheuvel" Date: Tue, 22 Mar 2022 09:18:22 +0100 Subject: [PATCH 3/3] add extra model stuff, not final --- src/main/java/model/DataFlowGraph.java | 4 +- src/main/java/model/OwnerNode.java | 4 +- .../{ => controlflow}/CodeBlockCondition.java | 4 +- .../model/controlflow/ControlFlowGraph.java | 20 ++++++++++ .../{ => controlflow}/DataFlowCodeBlock.java | 40 ++++++++++++++++--- 5 files changed, 62 insertions(+), 10 deletions(-) rename src/main/java/model/{ => controlflow}/CodeBlockCondition.java (98%) create mode 100644 src/main/java/model/controlflow/ControlFlowGraph.java rename src/main/java/model/{ => controlflow}/DataFlowCodeBlock.java (68%) diff --git a/src/main/java/model/DataFlowGraph.java b/src/main/java/model/DataFlowGraph.java index 948777c..45b65cf 100644 --- a/src/main/java/model/DataFlowGraph.java +++ b/src/main/java/model/DataFlowGraph.java @@ -178,13 +178,13 @@ public Optional> getOwner() { } @Override - Collection> getOwnedOwners() { + public Collection> getOwnedOwners() { // streaming and collecting needed for casting. return this.methods.values().stream().collect(Collectors.toList()); } @Override - Collection getDirectOwnedNodes() { + public Collection getDirectOwnedNodes() { return this.fields; } diff --git a/src/main/java/model/OwnerNode.java b/src/main/java/model/OwnerNode.java index 7bc3ed1..e9e9a3b 100644 --- a/src/main/java/model/OwnerNode.java +++ b/src/main/java/model/OwnerNode.java @@ -66,11 +66,11 @@ public final Set getOwnedNodes() { /** * @return all nodes directly owned by this {@link OwnerNode} for which it holds that */ - abstract Collection> getOwnedOwners(); + abstract public Collection> getOwnedOwners(); /** * @return all {@link DataFlowNode}s directly owned by this {@link OwnerNode}. */ - abstract Collection getDirectOwnedNodes(); + abstract public Collection getDirectOwnedNodes(); } diff --git a/src/main/java/model/CodeBlockCondition.java b/src/main/java/model/controlflow/CodeBlockCondition.java similarity index 98% rename from src/main/java/model/CodeBlockCondition.java rename to src/main/java/model/controlflow/CodeBlockCondition.java index 4297ffd..89ec483 100644 --- a/src/main/java/model/CodeBlockCondition.java +++ b/src/main/java/model/controlflow/CodeBlockCondition.java @@ -8,7 +8,9 @@ * interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including * negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. */ -package model; +package model.controlflow; + +import model.DataFlowNode; /** * The condition that needs to be true before a {@link DataFlowCodeBlock} is executed. diff --git a/src/main/java/model/controlflow/ControlFlowGraph.java b/src/main/java/model/controlflow/ControlFlowGraph.java new file mode 100644 index 0000000..90d249d --- /dev/null +++ b/src/main/java/model/controlflow/ControlFlowGraph.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022 by Eyefreight BV (www.eyefreight.com). All rights reserved. + * + * This software is provided by the copyright holder and contributors "as is" and any express or implied warranties, including, but + * not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall + * Eyefreight BV or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages + * (including, but not limited to, procurement of substitute goods or services; * loss of use, data, or profits; or business + * interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including + * negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. + */ +package model.controlflow; + +/** + * TODO javadoc + * + * @author daan.vandenheuvel + */ +public class ControlFlowGraph { + +} diff --git a/src/main/java/model/DataFlowCodeBlock.java b/src/main/java/model/controlflow/DataFlowCodeBlock.java similarity index 68% rename from src/main/java/model/DataFlowCodeBlock.java rename to src/main/java/model/controlflow/DataFlowCodeBlock.java index d3b0034..db8f5dd 100644 --- a/src/main/java/model/DataFlowCodeBlock.java +++ b/src/main/java/model/controlflow/DataFlowCodeBlock.java @@ -8,7 +8,7 @@ * interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including * negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. */ -package model; +package model.controlflow; import java.util.Collection; import java.util.List; @@ -16,13 +16,19 @@ import org.apache.commons.lang3.tuple.Pair; +import com.github.javaparser.ast.stmt.BlockStmt; + +import model.DataFlowNode; +import model.OwnedNode; +import model.OwnerNode; + /** * A block of code in a java class. This contains a list of nodes that will be executed in sequence. A code block has a (possibly empty) list of codeBlock's * that succeed this codeBlock as well as a list preceeding it. * * @author daan.vandenheuvel */ -public class DataFlowCodeBlock extends OwnerNode { // TODO find the JavaParser Node that represents a code block and add it as type argument. +public class DataFlowCodeBlock extends OwnerNode { // TODO find the JavaParser Node that represents a code block and add it as type argument. // TODO methods and classes should extend this. @@ -40,21 +46,45 @@ public class DataFlowCodeBlock extends OwnerNode { // TODO find the JavaParser N private List dataFlowNodes; @Override - Collection getOwnedOwners() { + public Collection> getOwnedOwners() { // TODO Auto-generated method stub return null; } @Override - Collection getDirectOwnedNodes() { + public Collection getDirectOwnedNodes() { // TODO Auto-generated method stub return null; } @Override - public Optional getOwner() { + public Optional> getOwner() { // TODO Auto-generated method stub return null; } + public List getCalledBy() { + return calledBy; + } + + public void setCalledBy(List calledBy) { + this.calledBy = calledBy; + } + + public List> getCalled() { + return called; + } + + public void setCalled(List> called) { + this.called = called; + } + + public List getDataFlowNodes() { + return dataFlowNodes; + } + + public void setDataFlowNodes(List dataFlowNodes) { + this.dataFlowNodes = dataFlowNodes; + } + }