1 /*
   2  * Copyright (c) 2018, 2018, 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 package org.graalvm.compiler.core.test;
  26 
  27 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  28 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  29 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver;
  30 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  31 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.LateRegistration;
  32 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
  33 import org.junit.Test;
  34 
  35 import jdk.vm.ci.meta.ResolvedJavaMethod;
  36 
  37 public class InvocationPluginsTest extends GraalCompilerTest {
  38 
  39     private static void assertNotIsEmpty(InvocationPlugins invocationPlugins) {
  40         InvocationPlugins childInvocationPlugins = new InvocationPlugins(invocationPlugins);
  41         assertFalse(invocationPlugins.isEmpty());
  42         assertFalse(childInvocationPlugins.isEmpty());
  43 
  44         invocationPlugins.closeRegistration();
  45         assertFalse(invocationPlugins.isEmpty());
  46         assertFalse(childInvocationPlugins.isEmpty());
  47     }
  48 
  49     @Test
  50     public void testIsEmptyWithNormalRegistration() {
  51         InvocationPlugins invocationPlugins = new InvocationPlugins();
  52         assertTrue(invocationPlugins.isEmpty());
  53 
  54         Registration r = new Registration(invocationPlugins, Class.class);
  55         r.register1("isAnonymousClass", Receiver.class, new InvocationPlugin() {
  56             @Override
  57             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
  58                 return false;
  59             }
  60         });
  61 
  62         assertNotIsEmpty(invocationPlugins);
  63     }
  64 
  65     @Test
  66     public void testIsEmptyWithDeferredRegistration() {
  67         InvocationPlugins invocationPlugins = new InvocationPlugins();
  68         assertTrue(invocationPlugins.isEmpty());
  69         invocationPlugins.defer(new Runnable() {
  70 
  71             @Override
  72             public void run() {
  73                 Registration r = new Registration(invocationPlugins, Class.class);
  74                 r.register1("isAnonymousClass", Receiver.class, new InvocationPlugin() {
  75                     @Override
  76                     public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
  77                         return false;
  78                     }
  79                 });
  80             }
  81         });
  82 
  83         assertNotIsEmpty(invocationPlugins);
  84     }
  85 
  86     @Test
  87     public void testIsEmptyWithLateRegistration() {
  88         InvocationPlugins invocationPlugins = new InvocationPlugins();
  89         assertTrue(invocationPlugins.isEmpty());
  90 
  91         try (LateRegistration lr = new LateRegistration(invocationPlugins, Class.class)) {
  92             lr.register(new InvocationPlugin() {
  93                 @Override
  94                 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
  95                     return false;
  96                 }
  97             }, "isAnonymousClass", Receiver.class);
  98         }
  99         assertNotIsEmpty(invocationPlugins);
 100     }
 101 }