Fixes null captured parameters
[jpf-core.git] / src / main / gov / nasa / jpf / vm / CharArrayFields.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
19
20 package gov.nasa.jpf.vm;
21
22 import gov.nasa.jpf.util.HashData;
23 import gov.nasa.jpf.util.IntVector;
24 import gov.nasa.jpf.util.PrintUtils;
25
26 import java.io.PrintStream;
27
28 /**
29  * element values for char[] objects
30  */
31 public class CharArrayFields extends ArrayFields {
32
33   char[] values;
34
35   public CharArrayFields (int length) {
36     values = new char[length];
37   }
38
39   @Override
40   public char[] asCharArray(){
41     return values;
42   }
43
44   @Override
45   public void copyElements (ArrayFields src, int srcPos, int dstPos, int len){
46     CharArrayFields a = (CharArrayFields) src;
47     System.arraycopy(a.values, srcPos, values, dstPos, len);
48   }
49   
50   @Override
51   protected void printValue(PrintStream ps, int idx){
52     PrintUtils.printCharLiteral(ps, values[idx]);
53   }
54   
55   @Override
56   public void printElements( PrintStream ps, int max){
57     PrintUtils.printStringLiteral(ps, values, max);
58   }  
59   
60   @Override
61   public char[] asCharArray (int offset, int length) {
62     char[] result = new char[length];
63     System.arraycopy(values, offset, result, 0, length);
64
65     return result;
66   }
67
68   @Override
69   public Object getValues(){
70     return values;
71   }
72
73   @Override
74   public int arrayLength() {
75     return values.length;
76   }
77
78   @Override
79   public int getHeapSize() {  // in bytes
80     return values.length * 2;
81   }
82
83   @Override
84   public void appendTo (IntVector v) {
85     v.appendPacked(values);
86   }
87
88   @Override
89   public CharArrayFields clone(){
90     CharArrayFields f = (CharArrayFields)cloneFields();
91     f.values = values.clone();
92     return f;
93   }
94
95
96   @Override
97   public boolean equals (Object o) {
98     if (o instanceof CharArrayFields) {
99       CharArrayFields other = (CharArrayFields)o;
100
101       char[] v = values;
102       char[] vOther = other.values;
103       if (v.length != vOther.length) {
104         return false;
105       }
106
107       for (int i=0; i<v.length; i++) {
108         if (v[i] != vOther[i]) {
109           return false;
110         }
111       }
112
113       return compareAttrs(other);
114
115     } else {
116       return false;
117     }
118   }
119
120   @Override
121   public char getCharValue(int pos) {
122     return values[pos];
123   }
124
125   @Override
126   public void setCharValue(int pos, char newValue) {
127     values[pos] = newValue;
128   }
129
130   public void setCharValues(char[] v){
131     System.arraycopy(v,0,values,0,v.length);
132   }
133
134   //--- some methods to ease native String operations
135
136   public String asString(int offset, int length) {
137     return new String(values, offset, length);
138   }
139
140   // a special string compare utility
141   public boolean equals (int offset, int length, String s) {
142     char[] v = values;
143
144     if (offset+length > v.length) {
145       return false;
146     }
147
148     for (int i=offset, j=0; j<length; i++, j++) {
149       if (v[i] != s.charAt(j)) {
150         return false;
151       }
152     }
153
154     return true;
155   }
156
157   @Override
158   public void hash(HashData hd) {
159     char[] v = values;
160     for (int i=0; i < v.length; i++) {
161       hd.add(v[i]);
162     }
163   }
164
165 }