1 /*
   2  * Copyright (c) 2005, 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.jittester.types;
  25 
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 import java.util.Objects;
  29 import jdk.test.lib.jittester.ProductionParams;
  30 import jdk.test.lib.jittester.SymbolTable;
  31 import jdk.test.lib.jittester.Type;
  32 import jdk.test.lib.jittester.TypeList;
  33 import jdk.test.lib.jittester.VariableInfo;
  34 import jdk.test.lib.jittester.visitors.Visitor;
  35 import jdk.test.lib.jittester.utils.PseudoRandom;
  36 
  37 public class TypeArray extends TypeKlass {
  38 
  39     public List<Byte> getDims() {
  40         return dims;
  41     }
  42 
  43     public void setDimentions(List<Byte> dims) {
  44         this.dims = dims;
  45     }
  46     public final Type type;
  47     public final int dimensions;
  48     private List<Byte> dims = new ArrayList<>();
  49 
  50     public TypeArray() {
  51         this(new TypeVoid(), 0);
  52     }
  53 
  54     public TypeArray(Type type, int dimensions) {
  55         super("Array", TypeKlass.FINAL);
  56         addParent("java.lang.Object");
  57         setParent((TypeKlass) TypeList.find("java.lang.Object"));
  58         this.type = type;
  59         this.dimensions = dimensions;
  60     }
  61 
  62     @Override
  63     protected void exportSymbols() {
  64         SymbolTable.add(new VariableInfo("length", this, new TypeInt(), VariableInfo.PUBLIC | VariableInfo.FINAL));
  65     }
  66 
  67     @Override
  68     public boolean equals(Object t) {
  69                 if (this == t) {
  70             return true;
  71         }
  72         if (t == null || !(t instanceof TypeArray)) {
  73             return false;
  74         }
  75 
  76         if (super.equals(t)) { // make sure we're compating to an array
  77             try {
  78                 TypeArray a = (TypeArray) t;
  79                 return a.type.equals(type) && (a.dimensions == dimensions
  80                         || a.dimensions == -1
  81                         || dimensions == -1);
  82             } catch (Exception e) {
  83             }
  84         }
  85         return false;
  86     }
  87 
  88     @Override
  89     public int hashCode() {
  90         int hash = 3;
  91         hash = 53 * hash + Objects.hashCode(this.type);
  92         hash = 313 * hash + this.dimensions;
  93         return hash;
  94     }
  95 
  96     @Override
  97     public int compareTo(Type t) {
  98         int r = 0;
  99         r = super.compareTo(t);
 100         if (r == 0) {
 101             try {
 102                 TypeArray a = (TypeArray) t;
 103                 r = type.compareTo(t);
 104                 if (r == 0) {
 105                     r = dimensions - a.dimensions;
 106                 }
 107             } catch (Exception e) {
 108             }
 109         }
 110 
 111         return r;
 112     }
 113 
 114     public TypeArray produce() {
 115         ArrayList<Type> all = new ArrayList<>(TypeList.getAll());
 116         PseudoRandom.shuffle(all);
 117         for (Type t : all) {
 118             if (t instanceof TypeArray) {
 119                 continue;
 120             }
 121             int dims = PseudoRandom.randomNotZero(ProductionParams.dimensionsLimit.value());
 122             return new TypeArray(t, dims);
 123         }
 124         throw new Error("Shouldn't happen");
 125     }
 126 
 127     @Override
 128     public<T> T accept(Visitor<T> v) {
 129         return v.visit(this);
 130     }
 131 
 132     public Type getType() {
 133         return type;
 134     }
 135 
 136     public int getDimensions() {
 137         return dimensions;
 138     }
 139 }