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 MethodOverideTest
  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.VectorShape;
  36 import jdk.incubator.vector.VectorSpecies;
  37 import jdk.incubator.vector.ShortVector;
  38 import jdk.incubator.vector.Vector;
  39 import org.testng.Assert;
  40 import org.testng.annotations.DataProvider;
  41 import org.testng.annotations.Test;
  42 
  43 import java.lang.reflect.Field;
  44 import java.lang.reflect.Method;
  45 import java.lang.reflect.Modifier;
  46 import java.util.ArrayList;
  47 import java.util.List;
  48 import java.util.stream.Collectors;
  49 import java.util.stream.Stream;
  50 
  51 public class MethodOverideTest {
  52 
  53     @DataProvider
  54     public static Object[][] vectorClassesProvider() {
  55         return Stream.of(
  56                 ByteVector.class,
  57                 ShortVector.class,
  58                 IntVector.class,
  59                 FloatVector.class,
  60                 DoubleVector.class).
  61                 map(c -> new Object[]{c}).
  62                 toArray(Object[][]::new);
  63     }
  64 
  65     static List<Object> getConcreteSpeciesInstances(Class<?> primitiveVectorClass) {
  66         try {
  67             List<Object> csis = new ArrayList<>();
  68             for (Field sf : primitiveVectorClass.getFields()) {
  69                 if (VectorSpecies.class.isAssignableFrom(sf.getType())) {
  70                     csis.add(sf.get(null));
  71                 }
  72             }
  73             return csis;
  74         }
  75         catch (ReflectiveOperationException e) {
  76             throw new InternalError(e);
  77         }
  78     }
  79 
  80     static List<Class<?>> getConcreteVectorClasses(Class<?> primitiveVectorClass) {
  81         try {
  82             List<Class<?>> cvcs = new ArrayList<>();
  83             for (Object speciesInstance : getConcreteSpeciesInstances(primitiveVectorClass)) {
  84                     Method zero = primitiveVectorClass.getMethod("zero", VectorSpecies.class);
  85                     Object vectorInstance = zero.invoke(null, speciesInstance);
  86 
  87                     cvcs.add(vectorInstance.getClass());
  88             }
  89             return cvcs;
  90         }
  91         catch (ReflectiveOperationException e) {
  92             throw new InternalError(e);
  93         }
  94     }
  95 
  96     static List<Method> getDeclaredPublicAndNonAbstractMethods(Class<?> c) {
  97         return Stream.of(c.getDeclaredMethods()).
  98                 filter(cc -> Modifier.isPublic(cc.getModifiers())).
  99                 filter(cc -> !Modifier.isAbstract(cc.getModifiers())).
 100                 filter(cc -> !Modifier.isFinal(cc.getModifiers())).
 101                 filter(cc -> !cc.isSynthetic()).
 102                 collect(Collectors.toList());
 103     }
 104 
 105     static int checkMethods(Class<?> primitiveClass, List<Class<?>> concreteClasses) {
 106         List<Method> publicNonAbstractMethods = getDeclaredPublicAndNonAbstractMethods(primitiveClass);
 107 
 108         int notOverriddenMethodsCount = 0;
 109         for (Class<?> cc : concreteClasses) {
 110             List<Method> overriddenMethods = new ArrayList<>();
 111             List<Method> notOverriddenMethods = new ArrayList<>();
 112 
 113             for (Method m : publicNonAbstractMethods) {
 114                 try {
 115                     Method ccm = cc.getDeclaredMethod(m.getName(), m.getParameterTypes());
 116                     // Method overridden by concrete vector
 117                     // This method can be made abstract
 118                     overriddenMethods.add(m);
 119                 }
 120                 catch (NoSuchMethodException e) {
 121                     // Method implemented on primitive vector but not concrete vector
 122                     // Method is not intrinsic
 123                     notOverriddenMethods.add(m);
 124                 }
 125             }
 126 
 127             System.out.println(cc.getName() + " <- " + primitiveClass.getName());
 128             System.out.println("--Methods overridden that can be abstract");
 129             overriddenMethods.stream().forEach(m -> System.out.println("    " + m));
 130 
 131             System.out.println("--Methods not overridden that may need to be so and use intrinsics");
 132             notOverriddenMethods.stream().forEach(m -> System.out.println("    " + m));
 133             notOverriddenMethodsCount += notOverriddenMethods.size();
 134         }
 135 
 136         return notOverriddenMethodsCount;
 137     }
 138 
 139     @Test(dataProvider = "vectorClassesProvider")
 140     public void checkMethodsOnPrimitiveVector(Class<?> primitiveVector) {
 141         int nonIntrinsicMethods = checkMethods(primitiveVector, getConcreteVectorClasses(primitiveVector));
 142 
 143 //        Assert.assertEquals(nonIntrinsicMethods, 0);
 144     }
 145 }