1 /*
   2  * Copyright (c) 2016, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.test.models;
  27 
  28 import java.lang.reflect.Constructor;
  29 import java.lang.reflect.Field;
  30 import java.lang.reflect.Method;
  31 import java.lang.reflect.Module;
  32 import jdk.nashorn.internal.runtime.Context;
  33 
  34 /**
  35  * Few tests reflectively invoke or read fields of Nashorn classes
  36  * and objects - but of packages that are not exported to any module!
  37  * But, those packages are qualified exported to test [java] code
  38  * such as this class. So, test scripts can invoke the methods of this
  39  * class instead.
  40  */
  41 public final class Reflector {
  42     private Reflector() {}
  43     private static final Module NASHORN_MOD = Context.class.getModule();
  44 
  45     public static Object invoke(final Method m, final Object self, final Object...args) {
  46         if (m.getDeclaringClass().getModule() != NASHORN_MOD) {
  47             throw new RuntimeException(m + " is not from Nashorn module");
  48         }
  49 
  50         try {
  51             return m.invoke(self, args);
  52         } catch (final Exception e) {
  53             if (e instanceof RuntimeException) {
  54                 throw (RuntimeException)e;
  55             } else {
  56                 throw new RuntimeException(e);
  57             }
  58         }
  59     }
  60 
  61     public static Object newInstance(final Constructor c, final Object...args) {
  62         if (c.getDeclaringClass().getModule() != NASHORN_MOD) {
  63             throw new RuntimeException(c + " is not from Nashorn module");
  64         }
  65 
  66         try {
  67             return c.newInstance(args);
  68         } catch (final Exception e) {
  69             if (e instanceof RuntimeException) {
  70                 throw (RuntimeException)e;
  71             } else {
  72                 throw new RuntimeException(e);
  73             }
  74         }
  75     }
  76 
  77     public static Object get(final Field f, final Object self) {
  78         if (f.getDeclaringClass().getModule() != NASHORN_MOD) {
  79             throw new RuntimeException(f + " is not from Nashorn module");
  80         }
  81 
  82         try {
  83             return f.get(self);
  84         } catch (final Exception e) {
  85             if (e instanceof RuntimeException) {
  86                 throw (RuntimeException)e;
  87             } else {
  88                 throw new RuntimeException(e);
  89             }
  90         }
  91     }
  92 }