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 java.util.stream.Collectors;
  30 import java.util.stream.Stream;
  31 
  32 import jdk.test.lib.jittester.ProductionParams;
  33 import jdk.test.lib.jittester.SymbolTable;
  34 import jdk.test.lib.jittester.Type;
  35 import jdk.test.lib.jittester.TypeList;
  36 import jdk.test.lib.jittester.VariableInfo;
  37 import jdk.test.lib.jittester.visitors.Visitor;
  38 import jdk.test.lib.jittester.utils.PseudoRandom;
  39 
  40 public class TypeArray extends TypeKlass {
  41 
  42     public List<Byte> getDims() {
  43         return dims;
  44     }
  45 
  46     public void setDimentions(List<Byte> dims) {
  47         this.dims = dims;
  48     }
  49     public final Type type;
  50     public final int dimensions;
  51     private List<Byte> dims = new ArrayList<>();
  52 
  53     public TypeArray(Type type, int dimensions) {
  54         super("Array", TypeKlass.FINAL);
  55         addParent(TypeList.OBJECT.getName());
  56         setParent(TypeList.OBJECT);
  57         this.type = type;
  58         this.dimensions = dimensions;
  59     }
  60 
  61     public String getName() {
  62         String dimString = Stream.generate(() -> "[]")
  63                 .limit(dimensions)
  64                 .collect(Collectors.joining());
  65         return type.getName() + dimString;
  66     }
  67 
  68     @Override
  69     protected void exportSymbols() {
  70         SymbolTable.add(new VariableInfo("length", this, TypeList.INT, VariableInfo.PUBLIC | VariableInfo.FINAL));
  71     }
  72 
  73     @Override
  74     public boolean equals(Object t) {
  75         if (this == t) {
  76             return true;
  77         }
  78         if (t == null || !(t instanceof TypeArray)) {
  79             return false;
  80         }
  81 
  82         if (super.equals(t)) { // make sure we're compating to an array
  83             try {
  84                 TypeArray a = (TypeArray) t;
  85                 return a.type.equals(type) && (a.dimensions == dimensions
  86                         || a.dimensions == -1
  87                         || dimensions == -1);
  88             } catch (Exception e) {
  89             }
  90         }
  91         return false;
  92     }
  93 
  94     @Override
  95     public int hashCode() {
  96         int hash = 3;
  97         hash = 53 * hash + Objects.hashCode(this.type);
  98         hash = 313 * hash + this.dimensions;
  99         return hash;
 100     }
 101 
 102     @Override
 103     public int compareTo(Type t) {
 104         int r = super.compareTo(t);
 105         if (r == 0) {
 106             try {
 107                 TypeArray a = (TypeArray) t;
 108                 r = type.compareTo(t);
 109                 if (r == 0) {
 110                     r = dimensions - a.dimensions;
 111                 }
 112             } catch (Exception e) {
 113             }
 114         }
 115 
 116         return r;
 117     }
 118 
 119     public TypeArray produce() {
 120         ArrayList<Type> all = new ArrayList<>(TypeList.getAll());
 121         PseudoRandom.shuffle(all);
 122         for (Type t : all) {
 123             if (t instanceof TypeArray) {
 124                 continue;
 125             }
 126             int dims = PseudoRandom.randomNotZero(ProductionParams.dimensionsLimit.value());
 127             return new TypeArray(t, dims);
 128         }
 129         throw new Error("Shouldn't happen");
 130     }
 131 
 132     @Override
 133     public<T> T accept(Visitor<T> v) {
 134         return v.visit(this);
 135     }
 136 
 137     public Type getType() {
 138         return type;
 139     }
 140 
 141     public int getDimensions() {
 142         return dimensions;
 143     }
 144 }