View Javadoc
1   /*
2    * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package jdk.nashorn.internal.codegen.types;
27  
28  import static jdk.internal.org.objectweb.asm.Opcodes.AALOAD;
29  import static jdk.internal.org.objectweb.asm.Opcodes.AASTORE;
30  import static jdk.internal.org.objectweb.asm.Opcodes.ALOAD;
31  import static jdk.internal.org.objectweb.asm.Opcodes.ANEWARRAY;
32  import static jdk.internal.org.objectweb.asm.Opcodes.ARRAYLENGTH;
33  
34  import jdk.internal.org.objectweb.asm.MethodVisitor;
35  
36  /**
37   * This is an array type, i.e. OBJECT_ARRAY, NUMBER_ARRAY.
38   */
39  public class ArrayType extends ObjectType implements BytecodeArrayOps {
40  
41      /**
42       * Constructor
43       *
44       * @param clazz the Java class representation of the array
45       */
46      protected ArrayType(final Class<?> clazz) {
47          super(clazz);
48      }
49  
50      /**
51       * Get the element type of the array elements e.g. for OBJECT_ARRAY, this is OBJECT
52       *
53       * @return the element type
54       */
55      public Type getElementType() {
56          return Type.typeFor(getTypeClass().getComponentType());
57      }
58  
59      @Override
60      public void astore(final MethodVisitor method) {
61          method.visitInsn(AASTORE);
62      }
63  
64      @Override
65      public Type aload(final MethodVisitor method) {
66          method.visitInsn(AALOAD);
67          return getElementType();
68      }
69  
70      @Override
71      public Type arraylength(final MethodVisitor method) {
72          method.visitInsn(ARRAYLENGTH);
73          return INT;
74      }
75  
76      @Override
77      public Type newarray(final MethodVisitor method) {
78          method.visitTypeInsn(ANEWARRAY, getElementType().getInternalName());
79          return this;
80      }
81  
82      @Override
83      public Type newarray(final MethodVisitor method, final int dims) {
84          method.visitMultiANewArrayInsn(getInternalName(), dims);
85          return this;
86      }
87  
88      @Override
89      public Type load(final MethodVisitor method, final int slot) {
90          assert slot != -1;
91          method.visitVarInsn(ALOAD, slot);
92          return this;
93      }
94  
95      @Override
96      public String toString() {
97          return "array<elementType=" + getElementType().getTypeClass().getSimpleName() + '>';
98      }
99  
100     @Override
101     public Type convert(final MethodVisitor method, final Type to) {
102         assert to.isObject();
103         assert !to.isArray() || ((ArrayType)to).getElementType() == getElementType();
104         return to;
105     }
106 
107 }