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 void setAccessible(Constructor c) { 46 if (c.getDeclaringClass().getModule() != NASHORN_MOD) { 47 throw new RuntimeException(c + " is not from Nashorn module"); 48 } 49 50 c.setAccessible(true); 51 } 52 53 public static void setAccessible(Field f) { 54 if (f.getDeclaringClass().getModule() != NASHORN_MOD) { 55 throw new RuntimeException(f + " is not from Nashorn module"); 56 } 57 58 f.setAccessible(true); 59 } 60 61 public static void setAccessible(Method m) { 62 if (m.getDeclaringClass().getModule() != NASHORN_MOD) { 63 throw new RuntimeException(m + " is not from Nashorn module"); 64 } 65 66 m.setAccessible(true); 67 } 68 69 public static Object invoke(final Method m, final Object self, final Object...args) { 70 if (m.getDeclaringClass().getModule() != NASHORN_MOD) { 71 throw new RuntimeException(m + " is not from Nashorn module"); 72 } 73 74 try { 75 return m.invoke(self, args); 76 } catch (final Exception e) { 77 if (e instanceof RuntimeException) { 78 throw (RuntimeException)e; 79 } else { 80 throw new RuntimeException(e); 81 } 82 } 83 } 84 85 public static Object newInstance(final Constructor c, final Object...args) { 86 if (c.getDeclaringClass().getModule() != NASHORN_MOD) { 87 throw new RuntimeException(c + " is not from Nashorn module"); 88 } 89 90 try { 91 return c.newInstance(args); 92 } catch (final Exception e) { 93 if (e instanceof RuntimeException) { 94 throw (RuntimeException)e; 95 } else { 96 throw new RuntimeException(e); 97 } 98 } 99 } 100 101 public static Object get(final Field f, final Object self) { 102 if (f.getDeclaringClass().getModule() != NASHORN_MOD) { 103 throw new RuntimeException(f + " is not from Nashorn module"); 104 } 105 106 try { 107 return f.get(self); 108 } catch (final Exception e) { 109 if (e instanceof RuntimeException) { 110 throw (RuntimeException)e; 111 } else { 112 throw new RuntimeException(e); 113 } 114 } 115 } 116 }