1 package jp.sourceforge.stigmata.birthmarks.wsp;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.Iterator;
10 import java.util.List;
12 import jp.sourceforge.stigmata.BirthmarkElement;
16 * @author Haruaki TAMADA
19 public class StackPatternBasedBirthmarkElement extends BirthmarkElement implements Iterable<CurrentDepth>{
20 private static final long serialVersionUID = 7965456413167854L;
22 private List<CurrentDepth> list = new ArrayList<CurrentDepth>();
23 private int weight = -1;
25 public StackPatternBasedBirthmarkElement(CurrentDepth[] depthArray){
26 super(getStringRepresentation(depthArray));
27 for(CurrentDepth depth: depthArray){
31 public StackPatternBasedBirthmarkElement(String value){
33 String[] depthList = value.split(", ");
34 for(int i = 0; i < depthList.length; i++){
35 String[] depthStringArray = depthList[i].split(":");
36 if(depthStringArray.length == 4){
37 int opcode = Integer.parseInt(depthStringArray[0]);
38 int depth = Integer.parseInt(depthStringArray[1]);
39 int weight = Integer.parseInt(depthStringArray[2]);
40 int act = Integer.parseInt(depthStringArray[3]);
42 Opcode o = new Opcode(OpcodeManager.getInstance().getOpcode(opcode));
44 if(o.getCategory() == Opcode.Category.OBJECT || o.getCategory() == Opcode.Category.INVOKE){
47 list.add(new CurrentDepth(depth, o));
52 public int getLength(){
56 public CurrentDepth getDepth(int index){
57 return list.get(index);
60 public Iterator<CurrentDepth> iterator(){
61 return Collections.unmodifiableList(list).iterator();
64 public int getWeight(StackPatternBasedBirthmarkElement element){
65 int[][] matrix = new int[element.getLength() + 1][getLength() + 1];
67 for(int i = 0; i <= element.getLength(); i++){
68 for(int j = 0; j <= getLength(); j++){
72 else if(element.getDepth(i - 1).getOpcode().getOpcode() == getDepth(j - 1).getOpcode().getOpcode()){
73 matrix[i][j] = (int)(matrix[i - 1][j - 1] + getDepth(j - 1).getOpcode().getWeight());
76 matrix[i][j] = Math.max(matrix[i - 1][j], matrix[i][j - 1]);
82 int last = element.getLength();
83 for(int i = 0; i < matrix[last].length; i++){
84 if(matrix[last][i] > max){
85 max = matrix[last][i];
91 public int getWeight(){
94 for(CurrentDepth depth: this){
95 w += depth.getOpcode().getWeight();
102 private static String getStringRepresentation(CurrentDepth[] depth){
103 StringBuilder builder = new StringBuilder();
104 for(int i = 0; i < depth.length; i++){
106 builder.append(", ");
108 builder.append(depth[i]);
110 return new String(builder);