Reflector.java revision 1829:dbe6b239d681
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 26package jdk.nashorn.test.models; 27 28import java.lang.reflect.Constructor; 29import java.lang.reflect.Executable; 30import java.lang.reflect.Field; 31import java.lang.reflect.Method; 32import java.lang.reflect.Module; 33import jdk.nashorn.internal.runtime.Context; 34 35/** 36 * Few tests reflectively invoke or read fields of Nashorn classes 37 * and objects - but of packages that are not exported to any module! 38 * But, those packages are qualified exported to test [java] code 39 * such as this class. So, test scripts can invoke the methods of this 40 * class instead. 41 */ 42public final class Reflector { 43 private Reflector() {} 44 private static final Module NASHORN_MOD = Context.class.getModule(); 45 46 public static void setAccessible(Executable e) { 47 if (e.getDeclaringClass().getModule() != NASHORN_MOD) { 48 throw new RuntimeException(e + " is not from Nashorn module"); 49 } 50 51 e.setAccessible(true); 52 } 53 54 public static void setAccessible(Field f) { 55 if (f.getDeclaringClass().getModule() != NASHORN_MOD) { 56 throw new RuntimeException(f + " is not from Nashorn module"); 57 } 58 59 f.setAccessible(true); 60 } 61 62 public static Object invoke(final Method m, final Object self, final Object...args) { 63 if (m.getDeclaringClass().getModule() != NASHORN_MOD) { 64 throw new RuntimeException(m + " is not from Nashorn module"); 65 } 66 67 try { 68 return m.invoke(self, args); 69 } catch (final Exception e) { 70 if (e instanceof RuntimeException) { 71 throw (RuntimeException)e; 72 } else { 73 throw new RuntimeException(e); 74 } 75 } 76 } 77 78 public static Object newInstance(final Constructor c, final Object...args) { 79 if (c.getDeclaringClass().getModule() != NASHORN_MOD) { 80 throw new RuntimeException(c + " is not from Nashorn module"); 81 } 82 83 try { 84 return c.newInstance(args); 85 } catch (final Exception e) { 86 if (e instanceof RuntimeException) { 87 throw (RuntimeException)e; 88 } else { 89 throw new RuntimeException(e); 90 } 91 } 92 } 93 94 public static Object get(final Field f, final Object self) { 95 if (f.getDeclaringClass().getModule() != NASHORN_MOD) { 96 throw new RuntimeException(f + " is not from Nashorn module"); 97 } 98 99 try { 100 return f.get(self); 101 } catch (final Exception e) { 102 if (e instanceof RuntimeException) { 103 throw (RuntimeException)e; 104 } else { 105 throw new RuntimeException(e); 106 } 107 } 108 } 109} 110