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.jvm.bytecode;
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;
32 * ..., count => ..., arrayref
34 public class NEWARRAY extends NewArrayInstruction {
36 public NEWARRAY(int typeCode) {
37 type = Types.getElementDescriptorOfType(typeCode);
41 public Instruction execute (ThreadInfo ti) {
42 StackFrame frame = ti.getModifiableTopFrame();
44 arrayLength = frame.pop();
45 Heap heap = ti.getHeap();
48 return ti.createAndThrowException("java.lang.NegativeArraySizeException");
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);
56 if (!ci.isRegistered()) {
61 if (heap.isOutOfMemory()) { // simulate OutOfMemoryError
62 return ti.createAndThrowException("java.lang.OutOfMemoryError",
63 "trying to allocate new " +
65 "[" + arrayLength + "]");
68 ElementInfo eiArray = heap.newArray(type, arrayLength, ti);
69 int arrayRef = eiArray.getObjectRef();
71 frame.pushRef(arrayRef);
77 public int getLength() {
78 return 2; // opcode, atype
82 public int getByteCode () {
87 public void accept(JVMInstructionVisitor insVisitor) {
88 insVisitor.visit(this);
92 public String toString() {
93 StringBuilder sb = new StringBuilder();
94 sb.append("newarray ");
95 sb.append(getTypeName());
98 sb.append(arrayLength);
102 return sb.toString();