1 /*
   2  * Copyright (c) 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 package org.graalvm.compiler.core.test;
  24 
  25 import jdk.vm.ci.meta.Assumptions.Assumption;
  26 import jdk.vm.ci.meta.Assumptions.ConcreteSubtype;
  27 
  28 import org.junit.Test;
  29 
  30 import org.graalvm.compiler.nodes.StructuredGraph;
  31 
  32 /**
  33  * Ensure that abstract classes with a single implementor are properly optimized and that loading a
  34  * subclass below the leaf type triggers invalidation.
  35  */
  36 public class ConcreteSubtypeTest extends GraalCompilerAssumptionsTest {
  37     abstract static class AbstractBase {
  38         abstract void check();
  39     }
  40 
  41     static class Subclass extends AbstractBase {
  42         @Override
  43         public void check() {
  44             throw new InternalError();
  45         }
  46     }
  47 
  48     static class SubSubclass extends Subclass {
  49         @Override
  50         public void check() {
  51         }
  52     }
  53 
  54     public void callAbstractType(AbstractBase object) {
  55         object.check();
  56     }
  57 
  58     @Override
  59     protected void checkGraph(Assumption expectedAssumption, StructuredGraph graph) {
  60         super.checkGraph(expectedAssumption, graph);
  61         assertTrue(graph.isTrivial());
  62     }
  63 
  64     /**
  65      * Test that {@link #callAbstractType} gets compiled into an empty method with a
  66      * {@link ConcreteSubtype} assumption on {@link AbstractBase} and {@link Subclass}. Then ensures
  67      * that loading and initialization of {@link SubSubclass} causes the compiled method to be
  68      * invalidated.
  69      */
  70     @Test
  71     public void testLeafAbstractType() {
  72         testAssumptionInvalidate("callAbstractType", new ConcreteSubtype(resolveAndInitialize(AbstractBase.class), resolveAndInitialize(Subclass.class)), "SubSubclass");
  73     }
  74 }