Fixing a subtle bug: the method isParameterWithType() did not consider arbitrary...
[jpf-core.git] / src / main / gov / nasa / jpf / jvm / bytecode / NEWARRAY.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.jvm.bytecode;
19
20 import gov.nasa.jpf.vm.ClassInfo;
21 import gov.nasa.jpf.vm.ClassLoaderInfo;
22 import gov.nasa.jpf.vm.ElementInfo;
23 import gov.nasa.jpf.vm.Heap;
24 import gov.nasa.jpf.vm.Instruction;
25 import gov.nasa.jpf.vm.StackFrame;
26 import gov.nasa.jpf.vm.ThreadInfo;
27 import gov.nasa.jpf.vm.Types;
28
29
30 /**
31  * Create new array
32  * ..., count => ..., arrayref
33  */
34 public class NEWARRAY extends NewArrayInstruction {
35
36   public NEWARRAY(int typeCode) {
37     type = Types.getElementDescriptorOfType(typeCode);
38   }
39
40   @Override
41   public Instruction execute (ThreadInfo ti) {
42     StackFrame frame = ti.getModifiableTopFrame();
43
44     arrayLength = frame.pop();
45     Heap heap = ti.getHeap();
46
47     if (arrayLength < 0){
48       return ti.createAndThrowException("java.lang.NegativeArraySizeException");
49     }
50
51     // there is no clinit for array classes, but we still have  to create a class object
52     // since its a builtin class, we also don't have to bother with NoClassDefFoundErrors
53     String clsName = "[" + type;
54     ClassInfo ci = ClassLoaderInfo.getCurrentResolvedClassInfo(clsName);
55
56     if (!ci.isRegistered()) {
57       ci.registerClass(ti);
58       ci.setInitialized();
59     }
60    
61     if (heap.isOutOfMemory()) { // simulate OutOfMemoryError
62       return ti.createAndThrowException("java.lang.OutOfMemoryError",
63                                         "trying to allocate new " +
64                                           getTypeName() +
65                                         "[" + arrayLength + "]");
66     }
67     
68     ElementInfo eiArray = heap.newArray(type, arrayLength, ti);
69     int arrayRef = eiArray.getObjectRef();
70     
71     frame.pushRef(arrayRef);
72
73     return getNext(ti);
74   }
75
76   @Override
77   public int getLength() {
78     return 2; // opcode, atype
79   }
80   
81   @Override
82   public int getByteCode () {
83     return 0xBC;
84   }
85   
86   @Override
87   public void accept(JVMInstructionVisitor insVisitor) {
88           insVisitor.visit(this);
89   }
90
91   @Override
92   public String toString() {
93     StringBuilder sb = new StringBuilder();
94     sb.append("newarray ");
95     sb.append(getTypeName());
96     sb.append('[');
97     if (arrayLength >=0){
98       sb.append(arrayLength);
99     }
100     sb.append(']');
101
102     return sb.toString();
103   }
104 }