1 /*
   2  * Copyright (c) 2013, 2014, 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 package org.graalvm.compiler.replacements;
  24 
  25 import java.util.Arrays;
  26 
  27 import org.graalvm.compiler.api.replacements.ClassSubstitution;
  28 import org.graalvm.compiler.api.replacements.MethodSubstitution;
  29 import org.graalvm.compiler.replacements.nodes.ArrayEqualsNode;
  30 
  31 // JaCoCo Exclude
  32 
  33 /**
  34  * Substitutions for {@link java.util.Arrays} methods.
  35  */
  36 @ClassSubstitution(Arrays.class)
  37 public class ArraysSubstitutions {
  38 
  39     @MethodSubstitution
  40     public static boolean equals(boolean[] a, boolean[] a2) {
  41         if (a == a2) {
  42             return true;
  43         }
  44         if (a == null || a2 == null || a.length != a2.length) {
  45             return false;
  46         }
  47         return ArrayEqualsNode.equals(a, a2, a.length);
  48     }
  49 
  50     @MethodSubstitution
  51     public static boolean equals(byte[] a, byte[] a2) {
  52         if (a == a2) {
  53             return true;
  54         }
  55         if (a == null || a2 == null || a.length != a2.length) {
  56             return false;
  57         }
  58         return ArrayEqualsNode.equals(a, a2, a.length);
  59     }
  60 
  61     @MethodSubstitution
  62     public static boolean equals(char[] a, char[] a2) {
  63         if (a == a2) {
  64             return true;
  65         }
  66         if (a == null || a2 == null || a.length != a2.length) {
  67             return false;
  68         }
  69         return ArrayEqualsNode.equals(a, a2, a.length);
  70     }
  71 
  72     @MethodSubstitution
  73     public static boolean equals(short[] a, short[] a2) {
  74         if (a == a2) {
  75             return true;
  76         }
  77         if (a == null || a2 == null || a.length != a2.length) {
  78             return false;
  79         }
  80         return ArrayEqualsNode.equals(a, a2, a.length);
  81     }
  82 
  83     @MethodSubstitution
  84     public static boolean equals(int[] a, int[] a2) {
  85         if (a == a2) {
  86             return true;
  87         }
  88         if (a == null || a2 == null || a.length != a2.length) {
  89             return false;
  90         }
  91         return ArrayEqualsNode.equals(a, a2, a.length);
  92     }
  93 
  94     @MethodSubstitution
  95     public static boolean equals(long[] a, long[] a2) {
  96         if (a == a2) {
  97             return true;
  98         }
  99         if (a == null || a2 == null || a.length != a2.length) {
 100             return false;
 101         }
 102         return ArrayEqualsNode.equals(a, a2, a.length);
 103     }
 104 
 105     @MethodSubstitution
 106     public static boolean equals(float[] a, float[] a2) {
 107         if (a == a2) {
 108             return true;
 109         }
 110         if (a == null || a2 == null || a.length != a2.length) {
 111             return false;
 112         }
 113         return ArrayEqualsNode.equals(a, a2, a.length);
 114     }
 115 
 116     @MethodSubstitution
 117     public static boolean equals(double[] a, double[] a2) {
 118         if (a == a2) {
 119             return true;
 120         }
 121         if (a == null || a2 == null || a.length != a2.length) {
 122             return false;
 123         }
 124         return ArrayEqualsNode.equals(a, a2, a.length);
 125     }
 126 }