/* * Copyright 2008 Wishnu Prasetya. * * This file is part of T2. * T2 is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License (GPL) as published by the * Free Software Foundation; either version 3 of the License, or any * later version. * * T2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * A copy of the GNU General Public License can be found in T2 distribution. * If it is missing, see http://www.gnu.org/licenses. */ package t2ext.bcel.utils; import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; import com.sun.org.apache.bcel.internal.generic.InstructionHandle; import com.sun.org.apache.bcel.internal.generic.InstructionList; /** * Represents a node in the CFG * * @author Maaike Gerritsen */ public class Node implements Serializable{ private static final long serialVersionUID = 1L; private int _id; private Node _childLeft; private InstructionHandle _nodeHandle; private InstructionList _listToInsert; private ArrayList _parents; private ArrayList _children; private Node _childRight; private boolean _isBranchNode; private boolean _isRightChild; private boolean _listSet; private boolean _isLast; private boolean _cycleBegin; private boolean _cycleEnd; private boolean _addToCFG; private boolean _targetOfGoto; private ArrayList _pathIds; private int _lineNr; public Node() { _parents = new ArrayList(); _children = new ArrayList(); _pathIds = new ArrayList(); _isBranchNode = false; _isRightChild = false; _listSet = false; _isLast = false; _cycleBegin = false; _cycleEnd = false; _addToCFG = false; _targetOfGoto = false; } public int getId() { return _id; } public void setLineNr(int lineNr){ _lineNr = lineNr; } public int getLineNr(){ return _lineNr; } public void setId(int id) { _id = id; } public Node getChildLeft() { return _childLeft; } public boolean isLast(){ return _isLast; } public boolean getCycleBegin(){ return _cycleBegin; } public void setCycleBegin(boolean begin){ _cycleBegin = begin; } public boolean getCycleEnd(){ return _cycleEnd; } public void setCycleEnd(boolean end){ _cycleEnd = end; } public void setIsLast(boolean last){ _isLast = last; } public void setNodeHandle(InstructionHandle handle) { _nodeHandle = handle; } public InstructionHandle getNodeHandle() { return _nodeHandle; } public void setChildLeft(Node childLeft) { _childLeft = childLeft; } public ArrayList getParents() { return _parents; } public void addToParents(Node parent) { _parents.add(parent); } public ArrayList getChildren() { return _children; } public void addToChildren(Node child) { _children.add(child); } public Node getChildRight() { return _childRight; } public void setChildRight(Node right) { _childRight = right; } public boolean isBranchNode() { return _isBranchNode; } public void setIsBranchNode(boolean branchNode) { _isBranchNode = branchNode; } public boolean isRightChild() { return _isRightChild; } public void setIsRightChild(boolean isRight) { _isRightChild = isRight; } public void setListToInsert(InstructionList list) { _listToInsert = list; } public void addToList(InstructionList list) { if (_listToInsert == null) setListToInsert(list); else if (!_listSet) _listToInsert.insert(list); _listSet = true; } public InstructionList getListToInsert() { return _listToInsert; } public boolean hasIfParent() { for (Iterator iterator = _parents.iterator(); iterator.hasNext();) { Node parent = iterator.next(); if (parent.isBranchNode()) return true; } return false; } public boolean hasSameLineNrAsParent(){ for (Iterator iterator = _parents.iterator(); iterator.hasNext();) { Node parent = iterator.next(); if (parent.isBranchNode() && parent.getLineNr()!=_lineNr){ return false; } } return true; } public boolean isUnnecessaryNode(){ if(isBranchNode()) return false; if(!hasIfParent()) return true; if(hasSameLineNrAsParent() && allowAddToCFG()) return false; if(hasSameLineNrAsParent()) return true; return false; } public boolean hasInstructionList() { return _listToInsert != null; } public void addToPathIds(int id) { if (!_pathIds.contains(id)) _pathIds.add(id); } public ArrayList getPathIds() { return _pathIds; } public boolean allowAddToCFG(){ return _addToCFG; } public void addToCFG() { _addToCFG = true; } public boolean isTargetOfGoto(){ return _targetOfGoto; } public void targetOfGoto() { _targetOfGoto = true; } }