View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software 
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License.
16   */
17  
18  
19  package org.apache.jdo.impl.enhancer.classfile;
20  
21  import java.io.PrintStream;
22  import java.util.Stack;
23  
24  /***
25   * Special instruction form for the opc_multianewarray instruction
26   */
27  public class InsnMultiDimArrayNew extends Insn {
28      /* The array class for creation */
29      private ConstClass classOp;
30  
31      /* The number of dimensions present on the stack */
32      private int nDimsOp;
33  
34      /* public accessors */
35  
36      public boolean isSimpleLoad() {
37          return false;
38      }
39  
40      public int nStackArgs() {
41          return nDimsOp;
42      }
43  
44      public int nStackResults() {
45          return 1;
46      }
47  
48      /***
49       * What are the types of the stack operands ?
50       */
51      public String argTypes() {
52          StringBuffer buf = new StringBuffer();
53          for (int i=0; i<nDimsOp; i++) {
54              buf.append("I");
55          }
56          return buf.toString();
57      }
58  
59      /***
60       * What are the types of the stack results?
61       */
62      public String resultTypes() {
63          return "A";
64      }
65  
66      public boolean branches() {
67          return false;
68      }
69  
70      /***
71       * Return the array class being created
72       */
73      public ConstClass arrayClass() {
74          return classOp;
75      }
76  
77      /***
78       * Sets the array class being created
79       */
80      public void setArrayClass(ConstClass classOp) {
81          this.classOp = classOp;
82      }
83  
84      /***
85       * Return the number of dimensions of the array class being created
86       */
87      public int nDims() {
88          return nDimsOp;
89      }
90  
91      /***
92       * Constructor for opc_multianewarray.
93       * classOp must be an array class
94       * nDimsOp must be > 0 and <= number of array dimensions for classOp
95       */
96      public InsnMultiDimArrayNew (ConstClass classOp, int nDimsOp) {
97          this(classOp, nDimsOp, NO_OFFSET);
98      }
99  
100     /***
101      * Compares this instance with another for structural equality.
102      */
103     //@olsen: added method
104     public boolean isEqual(Stack msg, Object obj) {
105         if (!(obj instanceof InsnMultiDimArrayNew)) {
106             msg.push("obj/obj.getClass() = "
107                      + (obj == null ? null : obj.getClass()));
108             msg.push("this.getClass() = "
109                      + this.getClass());
110             return false;
111         }
112         InsnMultiDimArrayNew other = (InsnMultiDimArrayNew)obj;
113 
114         if (!super.isEqual(msg, other)) {
115             return false;
116         }
117 
118         if (!this.classOp.isEqual(msg, other.classOp)) {
119             msg.push(String.valueOf("classOp = "
120                                     + other.classOp));
121             msg.push(String.valueOf("classOp = "
122                                     + this.classOp));
123             return false;
124         }
125         if (this.nDimsOp != other.nDimsOp) {
126             msg.push(String.valueOf("nDimsOp = "
127                                     + other.nDimsOp));
128             msg.push(String.valueOf("nDimsOp = "
129                                     + this.nDimsOp));
130             return false;
131         }
132         return true;
133     }
134 
135     /* package local methods *//package-summary/html">class="comment"> package local methods *//package-summary.html">/* package local methods *//package-summary.html">class="comment"> package local methods */
136 
137     InsnMultiDimArrayNew (ConstClass classOp, int nDimsOp, int offset) {
138         super(opc_multianewarray, offset);
139 
140         this.classOp = classOp;
141         this.nDimsOp = nDimsOp; 
142 
143         if (classOp == null || nDimsOp < 1)
144             throw new InsnError ("attempt to create an opc_multianewarray" +
145                                  " with invalid operands");
146     }  
147 
148     void print (PrintStream out, int indent) {
149         ClassPrint.spaces(out, indent);
150         out.println(offset() + "  opc_multianewarray  pool(" +
151                     classOp.getIndex() + ")," + nDimsOp);
152     }
153 
154     int store(byte[] buf, int index) {
155         buf[index++] = (byte) opcode();
156         index = storeShort(buf, index, (short) classOp.getIndex());
157         buf[index++] = (byte) nDimsOp;
158         return index;
159     }
160 
161     int size() {
162         return 4;
163     }
164 
165     static InsnMultiDimArrayNew read (InsnReadEnv insnEnv, int myPC) {
166         ConstClass classOp = (ConstClass)
167             insnEnv.pool().constantAt(insnEnv.getUShort());
168         int nDims = insnEnv.getUByte();
169         return new InsnMultiDimArrayNew(classOp, nDims, myPC);
170     }
171 }