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