View Javadoc

1   package org.kit.furia.fragment.asm;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Iterator;
6   import java.util.LinkedList;
7   import java.util.List;
8   import java.util.Set;
9   
10  import org.kit.furia.fragment.soot.HugeFragmentException;
11  import org.kit.furia.misc.IntegerHolder;
12  import org.objectweb.asm.tree.analysis.BasicValue;
13  import org.objectweb.asm.tree.analysis.Value;
14  
15  /*
16   Furia-chan: An Open Source software license violation detector.    
17   Copyright (C) 2008 Kyushu Institute of Technology
18  
19   This program is free software: you can redistribute it and/or modify
20   it under the terms of the GNU General Public License as published by
21   the Free Software Foundation, either version 3 of the License, or
22   (at your option) any later version.
23  
24   This program is distributed in the hope that it will be useful,
25   but WITHOUT ANY WARRANTY; without even the implied warranty of
26   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27   GNU General Public License for more details.
28  
29   You should have received a copy of the GNU General Public License
30   along with this program.  If not, see <http://www.gnu.org/licenses/>.
31   */
32  
33  /**
34   * AbstractFunction holds common logic for representing functions.
35   * @author Arnoldo Jose Muller Molina
36   */
37  
38  public abstract class AbstractFunction implements FValue{
39      protected List < Value > params;
40      protected Set < Value > paramSet;
41      private int size;
42      protected AbstractFunction(){
43          super();
44          params = new LinkedList<Value>();
45          paramSet = new HashSet<Value>();
46          size = 0;
47      }
48    
49      /**
50       * Adds a new parameter to this function.
51       * @param param the new parameter.
52       */
53      public void addParam(Value param){
54          if(!paramSet.contains(param) && this != param){
55              params.add(param);
56              paramSet.add(param);
57              size+= param.getSize();
58          }
59      }
60      
61      public int getSize(){
62          return size;
63      }
64      
65      /**
66       * Returns the string representation of the function name.
67       * @return string representation of the function name.
68       */
69      protected abstract String printFunctionName();
70      
71      /**
72       * Returns true if the function identifiers are equal.
73       * @param x
74       * @return
75       */
76      protected abstract boolean equalFunctions(Value x);
77      
78      /*public boolean equals(Object other){
79          if(other == this){
80              return true;
81          }else if(other instanceof AbstractFunction){
82              AbstractFunction other2 = (AbstractFunction)other;
83              if(equalFunctions(other2)){
84                  // check that each element is equal.
85                  return this.params.equals(other2);
86              }else{
87                  return false;
88              }
89          }else{
90              return false;
91          }
92      }*/
93      
94      /**
95       * Generate the hash code for the function name.
96       */
97      protected abstract int  hashCodeFunctionName();
98      
99      /*public int hashCode(){
100         return params.hashCode() + hashCodeFunctionName();
101     }*/
102     
103     /**
104      * Prints the fragment into the given result.
105      */
106     public void  toFragment(StringBuilder result, Set visited, IntegerHolder count, int max) throws HugeFragmentException{
107         if(count.getValue()> max){
108             throw new HugeFragmentException();
109         }
110             
111         // check for repeated elements.
112         if(visited.contains(this)){
113             result.append("s()");//self
114             count.inc();
115             return;
116         }else{
117             visited.add(this);
118         }
119         result.append(printFunctionName());
120         count.inc();
121         result.append("(");
122         Iterator<Value> it = params.iterator();
123         String comma = "";
124         while(it.hasNext()){
125             Value n = it.next();
126             result.append(comma);
127             if(n instanceof BasicValue){
128                 BasicValue n2 = (BasicValue)n;
129                 String j = n2.toString();
130                 if(j.equals(".")){
131                     j = "z";
132                 }
133                 result.append(j);
134                 count.inc();
135             }else if (n instanceof FValue){
136                 FValue n2 = (FValue)n;
137                 n2.toFragment(result, visited, count, max);
138             }else{
139                 throw new IllegalArgumentException("Not supported value");
140             }
141             comma = ",";
142         }
143       
144         result.append(")");
145     }
146 }