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.jtt.bytecode;
  24 
  25 import org.junit.Test;
  26 
  27 import org.graalvm.compiler.core.phases.HighTier;
  28 import org.graalvm.compiler.jtt.JTTTest;
  29 import org.graalvm.compiler.options.OptionValue;
  30 import org.graalvm.compiler.options.OptionValue.OverrideScope;
  31 import org.graalvm.compiler.phases.tiers.Suites;
  32 
  33 /**
  34  * Tests the instanceof works, when casting an array of interface.
  35  */
  36 public class BC_instanceof01 extends JTTTest {
  37 
  38     public interface IObject {
  39 
  40     }
  41 
  42     public interface IDerivedObject extends IObject {
  43 
  44     }
  45 
  46     private static class BaseClass {
  47 
  48     }
  49 
  50     private static class TestClass extends BaseClass implements IObject {
  51     }
  52 
  53     private static class DerivedTestClass extends BaseClass implements IDerivedObject {
  54 
  55     }
  56 
  57     static TestClass[] a1 = {new TestClass()};
  58     static DerivedTestClass[] a2 = {new DerivedTestClass()};
  59 
  60     public static BaseClass[] getBaseClassArray() {
  61         return a1;
  62     }
  63 
  64     public static BaseClass[] getDerivedBaseClassArray() {
  65         return a2;
  66     }
  67 
  68     public static boolean test() {
  69         return getBaseClassArray() instanceof IObject[];
  70     }
  71 
  72     public static int testConditionalElimination() {
  73         BaseClass[] result = getDerivedBaseClassArray();
  74         if (result instanceof IDerivedObject[]) {
  75             if (result instanceof IObject[]) {
  76                 return 1;
  77             } else {
  78                 return 2;
  79             }
  80         } else {
  81             return 3;
  82         }
  83     }
  84 
  85     @Test
  86     public void run0() throws Throwable {
  87         runTest("test");
  88     }
  89 
  90     @Override
  91     @SuppressWarnings("try")
  92     protected Suites getSuites() {
  93         try (OverrideScope scope = OptionValue.override(HighTier.Options.Inline, false)) {
  94             return super.getSuites();
  95         }
  96     }
  97 
  98     @Test
  99     public void run1() throws Throwable {
 100         runTest("testConditionalElimination");
 101     }
 102 }