2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
18 package gov.nasa.jpf.vm;
20 import java.util.Iterator;
21 import java.util.NoSuchElementException;
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
28 public class ClassLoaderList implements Cloneable, Iterable<ClassLoaderInfo>, Restorable<ClassLoaderList> {
30 /** the ordered list of the class loaders */
31 ClassLoaderInfo[] classLoaders;
33 static class CllMemento implements Memento<ClassLoaderList> {
34 Memento<ClassLoaderInfo>[] clMementos;
36 CllMemento (ClassLoaderList cll) {
37 ClassLoaderInfo[] classLoaders = cll.classLoaders;
39 int len = classLoaders.length;
40 clMementos = new Memento[len];
42 for (int i=0; i<len; i++){
43 ClassLoaderInfo cl = classLoaders[i];
44 Memento<ClassLoaderInfo> m = cl.getMemento();
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);
58 cll.classLoaders = classLoaders;
64 class CllIterator implements Iterator<ClassLoaderInfo>{
68 public boolean hasNext() {
69 if (classLoaders != null) {
70 return next < classLoaders.length;
77 public ClassLoaderInfo next() {
78 if (classLoaders != null) {
79 if (next < classLoaders.length) {
80 return classLoaders[next++];
84 throw new NoSuchElementException();
88 public void remove() {
89 throw new UnsupportedOperationException();
93 public ClassLoaderList() {
97 public Memento<ClassLoaderList> getMemento (MementoFactory factory) {
98 return factory.getMemento(this);
101 public Memento<ClassLoaderList> getMemento(){
102 return new CllMemento(this);
106 public Iterator<ClassLoaderInfo> iterator () {
107 return new CllIterator();
110 public void add (ClassLoaderInfo cli) {
111 int id = cli.getId();
113 if (classLoaders == null) {
114 classLoaders = new ClassLoaderInfo[1];
115 classLoaders[0] = cli;
117 } else { // sort it in
118 int len = classLoaders.length;
119 ClassLoaderInfo[] a = new ClassLoaderInfo[len+1];
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));
138 public ClassLoaderInfo get(int i) {
139 return classLoaders[i];
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) {
155 return classLoaders.length;
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);