1 /*
   2  * Copyright (c) 2012, 2013, 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 package vm;
  25 
  26 import java.io.*;
  27 
  28 import org.testng.annotations.Test;
  29 import separate.*;
  30 import separate.Compiler;
  31 
  32 import static org.testng.Assert.*;
  33 import static separate.SourceModel.*;
  34 import static separate.SourceModel.Class;
  35 
  36 public class InterfaceAccessFlagsTest extends TestHarness {
  37     public InterfaceAccessFlagsTest() {
  38         super(false, false);
  39     }
  40 
  41     public void testMethodCallWithFlag(AccessFlag ... flags) {
  42         Class I = new Class("I",
  43             new ConcreteMethod("int", "m", "return priv();", AccessFlag.PUBLIC),
  44             new ConcreteMethod("int", "priv", "return 99;", flags));
  45         Interface Istub = new Interface("I",
  46             new DefaultMethod("int", "m", "return 0;"));
  47         Class C = new Class("C", Istub,
  48             new ConcreteMethod("int", "foo", "return (new C()).m();",
  49                 AccessFlag.PUBLIC, AccessFlag.STATIC));
  50         C.addCompilationDependency(Istub.findMethod("m"));
  51 
  52         Compiler compiler = new Compiler(/*Compiler.Flags.VERBOSE*/);
  53         compiler.addPostprocessor(new ClassToInterfaceConverter("I"));
  54         // Turns I from a class into an interface when loading
  55 
  56         ClassLoader cl = compiler.compile(C, I);
  57         try {
  58             java.lang.Class<?> C_class =
  59                 java.lang.Class.forName("C", true, cl);
  60             assertNotNull(C_class);
  61             java.lang.reflect.Method m = C_class.getMethod("foo");
  62             assertNotNull(m);
  63             Integer res = (Integer)m.invoke(null);
  64             assertEquals(res.intValue(), 99);
  65         } catch (java.lang.reflect.InvocationTargetException e) {
  66             fail("Unexpected exception: " + e.getCause());
  67         } catch (Throwable e) {
  68             fail("Unexpected exception: " + e);
  69         } finally {
  70             compiler.cleanup();
  71         }
  72     }
  73 
  74     @Test(groups = "vm_prototype")
  75     public void testPrivateMethodCall() {
  76         testMethodCallWithFlag(AccessFlag.PRIVATE);
  77     }
  78 
  79     @Test(groups = "vm_prototype")
  80     public void testStaticMethodCall() {
  81         testMethodCallWithFlag(AccessFlag.PUBLIC, AccessFlag.STATIC);
  82     }
  83 
  84     @Test(groups = "vm_prototype")
  85     public void testPrivateStaticMethodCall() {
  86         testMethodCallWithFlag(AccessFlag.PRIVATE, AccessFlag.STATIC);
  87     }
  88 
  89     // Test other combos?  Protected?
  90 }