1 /*
   2  * Copyright (c) 2007, 2012, 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.optimize;
  24 
  25 import org.junit.Test;
  26 
  27 import org.graalvm.compiler.jtt.JTTTest;
  28 
  29 /*
  30  */
  31 public class TypeCastElem extends JTTTest {
  32 
  33     interface Int1 {
  34 
  35         int do1();
  36     }
  37 
  38     interface Int2 {
  39 
  40         int do2();
  41     }
  42 
  43     interface Int3 extends Int1 {
  44 
  45         int do3();
  46     }
  47 
  48     public static class ClassA implements Int1 {
  49 
  50         private int a;
  51 
  52         public ClassA(int a) {
  53             this.a = a;
  54         }
  55 
  56         @Override
  57         public int do1() {
  58             return a;
  59         }
  60     }
  61 
  62     public static class ClassB extends ClassA implements Int2 {
  63 
  64         int b;
  65 
  66         public ClassB(int a, int b) {
  67             super(a);
  68             this.b = b;
  69         }
  70 
  71         @Override
  72         public int do2() {
  73             return b;
  74         }
  75     }
  76 
  77     public static class ClassC implements Int3 {
  78 
  79         private int a;
  80         private int b;
  81 
  82         public ClassC(int a, int b) {
  83             this.a = a;
  84             this.b = b;
  85         }
  86 
  87         @Override
  88         public int do3() {
  89             return b;
  90         }
  91 
  92         @Override
  93         public int do1() {
  94             return a;
  95         }
  96 
  97     }
  98 
  99     public static int test1(Object o) {
 100         if (o instanceof ClassB) {
 101             ClassB b = (ClassB) o;
 102             if (o instanceof Int1) {
 103                 return b.b - b.b + 1;
 104             }
 105             return 7;
 106         }
 107         return 3;
 108     }
 109 
 110     public static int test2(Object o) {
 111         Object b = o;
 112         if (o instanceof ClassB) {
 113             ClassA a = (ClassA) o;
 114             if (b instanceof Int1) {
 115                 return ((Int1) a).do1();
 116             }
 117             return 7;
 118         }
 119         return 3;
 120     }
 121 
 122     public static int test3(Object o) {
 123         Object b = o;
 124         boolean t = o instanceof Int3;
 125         if (t) {
 126             Int1 a = (Int1) b;
 127             return a.do1();
 128         }
 129         return 3;
 130     }
 131 
 132     public static int test(int a, int b, int c) {
 133         ClassA ca = new ClassA(a);
 134         ClassB cb = new ClassB(a, b);
 135         ClassC cc = new ClassC(c, c);
 136         int sum1 = test1(ca) + test1(cb) * 10 + test1(cc) * 100;
 137         int sum2 = test2(ca) + test2(cb) * 10 + test2(cc) * 100;
 138         int sum3 = test3(ca) + test3(cb) * 10 + test3(cc) * 100;
 139         int result = sum1 * 5 + sum2 * 7 + sum3 * 9;
 140         return result;
 141     }
 142 
 143     @Test
 144     public void run0() throws Throwable {
 145         runTest("test", 10, 13, 25);
 146     }
 147 
 148 }