1 /*
   2  * Copyright (c) 2017, 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 /*
  25  * @test
  26  * @modules jdk.incubator.vector
  27  * @run testng CovarOverrideTest
  28  *
  29  */
  30 
  31 import jdk.incubator.vector.ByteVector;
  32 import jdk.incubator.vector.DoubleVector;
  33 import jdk.incubator.vector.FloatVector;
  34 import jdk.incubator.vector.IntVector;
  35 import jdk.incubator.vector.ShortVector;
  36 import jdk.incubator.vector.Vector;
  37 import org.testng.annotations.DataProvider;
  38 import org.testng.annotations.Test;
  39 
  40 import java.lang.reflect.Method;
  41 import java.lang.reflect.Modifier;
  42 import java.util.ArrayList;
  43 import java.util.List;
  44 import java.util.Set;
  45 import java.util.stream.Stream;
  46 
  47 import static java.util.stream.Collectors.toList;
  48 import static org.testng.Assert.assertTrue;
  49 
  50 public class CovarOverrideTest {
  51 
  52     static final Set<String> NON_COVARIENT_RETURNING_METHOD_NAMES_ON_VECTOR =
  53             Set.of("cast", "reinterpret", "reshape");
  54 
  55     @DataProvider
  56     public static Object[][] classesProvider() {
  57         return List.of(
  58                 ByteVector.class,
  59                 ShortVector.class,
  60                 IntVector.class,
  61                 FloatVector.class,
  62                 DoubleVector.class).
  63                 stream().
  64                 map(c -> new Object[]{c}).
  65                 toArray(Object[][]::new);
  66     }
  67 
  68     @Test(dataProvider = "classesProvider")
  69     public void testCovarientOverridesExist(Class<?> c) {
  70         Class<?> superClass = c.getSuperclass();
  71 
  72         Class<?> vectorClass = c;
  73         if (superClass == Vector.Species.class) {
  74             vectorClass = c.getDeclaringClass();
  75         }
  76 
  77         List<Method> notFound = new ArrayList<>();
  78         List<Method> notCovarientlyOverridden = new ArrayList<>();
  79         for (Method superMethod : getVectorReturningMethods(superClass)) {
  80             try {
  81                 Method overrideMethod = c.getDeclaredMethod(superMethod.getName(), superMethod.getParameterTypes());
  82                 if (vectorClass != overrideMethod.getReturnType()) {
  83                     notCovarientlyOverridden.add(overrideMethod);
  84                 }
  85             }
  86             catch (NoSuchMethodException e) {
  87                 notFound.add(superMethod);
  88             }
  89         }
  90 
  91         if (!notFound.isEmpty()) {
  92             System.out.println("  Methods not found on sub-type " + c.getName());
  93             notFound.forEach(m -> System.out.println("    " + m));
  94         }
  95 
  96         if (!notCovarientlyOverridden.isEmpty()) {
  97             System.out.println("  Methods not covariently overridden on sub-type " + c.getName());
  98             notCovarientlyOverridden.forEach(m -> System.out.println("    " + m));
  99         }
 100 
 101         assertTrue(notFound.isEmpty() && notCovarientlyOverridden.isEmpty());
 102     }
 103 
 104     static List<Method> getVectorReturningMethods(Class<?> c) {
 105         var filteredMethods = Stream.of(c.getDeclaredMethods()).
 106                 filter(m -> Modifier.isPublic(m.getModifiers())).
 107                 filter(m -> Vector.class == m.getReturnType());
 108         if (c == Vector.class) {
 109             filteredMethods = filteredMethods.
 110                     filter(m -> !NON_COVARIENT_RETURNING_METHOD_NAMES_ON_VECTOR.contains(m.getName()));
 111         }
 112         return filteredMethods.collect(toList());
 113     }
 114 }