Adding ParameterizedTypeImpl to getGenericSuperclass method.
[jpf-core.git] / src / main / gov / nasa / jpf / vm / ClassLoaderList.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.vm;
19
20 import java.util.Iterator;
21 import java.util.NoSuchElementException;
22
23 /**
24  * container for all ClassLoaderInfos that are in the current state.
25  * It is important to keep this as canonically (search globally) sorted list so that
26  * we can use the iterator for state matching
27  */
28 public class ClassLoaderList implements Cloneable, Iterable<ClassLoaderInfo>, Restorable<ClassLoaderList> {
29
30   /** the ordered list of the class loaders */
31   ClassLoaderInfo[] classLoaders;
32
33   static class CllMemento implements Memento<ClassLoaderList> {
34     Memento<ClassLoaderInfo>[] clMementos;
35
36     CllMemento (ClassLoaderList cll) {
37       ClassLoaderInfo[] classLoaders = cll.classLoaders;
38       
39       int len = classLoaders.length;
40       clMementos =  new Memento[len];
41     
42       for (int i=0; i<len; i++){
43         ClassLoaderInfo cl = classLoaders[i];
44         Memento<ClassLoaderInfo> m = cl.getMemento();
45         clMementos[i] = m;
46       }
47     }
48
49     @Override
50         public ClassLoaderList restore (ClassLoaderList cll){
51       int len = clMementos.length;
52       ClassLoaderInfo[] classLoaders = new ClassLoaderInfo[len];
53       for (int i=0; i<len; i++){
54         Memento<ClassLoaderInfo> m = clMementos[i];
55         ClassLoaderInfo cl = m.restore(null);
56         classLoaders[i] = cl;
57       }
58       cll.classLoaders = classLoaders;
59
60       return cll;
61     }
62   }
63   
64   class CllIterator implements Iterator<ClassLoaderInfo>{
65     int next = 0;
66     
67     @Override
68     public boolean hasNext() {
69       if (classLoaders != null) {
70         return next < classLoaders.length;
71       } else {
72         return false;
73       }
74     }
75
76     @Override
77     public ClassLoaderInfo next() {
78       if (classLoaders != null) {
79         if (next < classLoaders.length) {
80           return classLoaders[next++];
81         }
82       }
83       
84       throw new NoSuchElementException();
85     }
86
87     @Override
88     public void remove() {
89       throw new UnsupportedOperationException();
90     }
91   }
92
93   public ClassLoaderList() {
94   }
95
96   @Override
97   public Memento<ClassLoaderList> getMemento (MementoFactory factory) {
98     return factory.getMemento(this);
99   }
100
101   public Memento<ClassLoaderList> getMemento(){
102     return new CllMemento(this);
103   }
104
105   @Override
106   public Iterator<ClassLoaderInfo> iterator () {
107     return new CllIterator();
108   }
109
110   public void add (ClassLoaderInfo cli) {
111     int id = cli.getId();
112     
113     if (classLoaders == null) {
114       classLoaders = new ClassLoaderInfo[1];
115       classLoaders[0] = cli;
116       
117     } else { // sort it in
118       int len = classLoaders.length;
119       ClassLoaderInfo[] a = new ClassLoaderInfo[len+1];
120       
121       for (int i=0; i<len; i++) {
122         ClassLoaderInfo c = classLoaders[i];
123         if (c.getId() > id) {
124           System.arraycopy(classLoaders, i, a, i+1, (len-i));
125           a[i] = cli;
126           classLoaders = a;
127           return;
128         } else {
129           a[i] = c;
130         }
131       }
132       
133       a[len] = cli;
134       classLoaders = a;
135     }
136   }
137
138   public ClassLoaderInfo get(int i) {
139     return classLoaders[i];
140   }
141   
142   public ClassLoaderInfo getClassLoaderInfoWithId (int id) {
143     int len = classLoaders.length;
144     for (int i=0; i<len; i++) {
145       ClassLoaderInfo c = classLoaders[i];
146       if (c.getId() == id) {
147         return c;
148       }
149     }
150     
151     return null;
152   }
153
154   public int size() {
155     return classLoaders.length;
156   }
157   
158   public void markRoots (Heap heap) {
159     int len = classLoaders.length;
160     for (int i=0; i<len; i++) {
161       ClassLoaderInfo cli = classLoaders[i];
162       cli.getStatics().markRoots(heap);
163     }
164   }
165 }