KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > coffi > TypeArray


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997 Clark Verbrugge
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30
31
32 package soot.coffi;
33
34 import java.io.*;
35 import soot.*;
36
37 class TypeArray
38 {
39     private Type[] types;
40
41     private TypeArray()
42     {
43     }
44
45     /**
46      * Returns an empty array of types.
47      *
48      */

49
50     public static TypeArray v(int size)
51     {
52         TypeArray newArray = new TypeArray();
53
54         newArray.types = new Type[size];
55
56         for(int i = 0; i < size; i++)
57             newArray.types[i] = UnusuableType.v();
58
59         return newArray;
60     }
61
62     public Type get(int index)
63     {
64         return types[index];
65     }
66
67     public TypeArray set(int index, Type type)
68     {
69         TypeArray newArray = new TypeArray();
70
71         newArray.types = (Type[]) types.clone();
72         newArray.types[index] = type;
73
74         return newArray;
75     }
76
77     public boolean equals(Object JavaDoc obj)
78     {
79         if(obj instanceof TypeArray)
80         {
81             TypeArray other = (TypeArray) obj;
82
83             if(types.length != other.types.length)
84                 return false;
85
86             for(int i = 0; i < types.length; i++)
87                 if(!types[i].equals(other.types[i]))
88                     return false;
89
90             return true;
91         }
92         else
93             return false;
94     }
95
96     public TypeArray merge(TypeArray otherArray)
97     {
98         TypeArray newArray = new TypeArray();
99
100         if(types.length != otherArray.types.length)
101             throw new RuntimeException JavaDoc("Merging of type arrays failed; unequal array length");
102
103         newArray.types = new Type[types.length];
104
105         for(int i = 0; i < types.length; i++)
106         {
107             if(types[i].equals(otherArray.types[i]))
108                 newArray.types[i] = types[i];
109             else if((types[i] instanceof ArrayType ||
110                 types[i] instanceof RefType) &&
111                 (otherArray.types[i] instanceof ArrayType
112                     || otherArray.types[i] instanceof RefType))
113             {
114                 // This type merge does not need to be accurate, because it is not really used
115

116                 newArray.types[i] = RefType.v("java.lang.Object");
117             }
118             else {
119                 newArray.types[i] = UnusuableType.v();
120             }
121         }
122         return newArray;
123     }
124
125     public void print(PrintStream out)
126     {
127         for(int i = 0; i < types.length; i++)
128             out.println(i + ": " + types[i].toString());
129     }
130 }
131
132
133
Popular Tags