src/java.base/share/classes/sun/reflect/misc/MethodUtil.java

Print this page




   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 sun.reflect.misc;
  27 

  28 import java.security.AllPermission;
  29 import java.security.AccessController;
  30 import java.security.PermissionCollection;
  31 import java.security.SecureClassLoader;
  32 import java.security.PrivilegedExceptionAction;
  33 import java.security.CodeSource;
  34 import java.io.InputStream;
  35 import java.io.BufferedInputStream;
  36 import java.io.IOException;
  37 import java.net.URL;
  38 import java.net.URLConnection;
  39 import java.lang.reflect.Method;
  40 import java.lang.reflect.InvocationTargetException;
  41 import java.lang.reflect.AccessibleObject;
  42 import java.lang.reflect.Modifier;
  43 import java.util.Arrays;
  44 import java.util.HashMap;
  45 import java.util.Map;
  46 import sun.misc.IOUtils;
  47 
  48 
  49 class Trampoline {
  50     static {
  51         if (Trampoline.class.getClassLoader() == null) {
  52             throw new Error(
  53                 "Trampoline must not be defined by the bootstrap classloader");
  54         }
  55     }
  56 
  57     private static void ensureInvocableMethod(Method m)
  58         throws InvocationTargetException
  59     {
  60         Class<?> clazz = m.getDeclaringClass();
  61         if (clazz.equals(AccessController.class) ||
  62             clazz.equals(Method.class) ||
  63             clazz.getName().startsWith("java.lang.invoke."))
  64             throw new InvocationTargetException(
  65                 new UnsupportedOperationException("invocation not supported"));
  66     }


 351         if (!name.equals(TRAMPOLINE)) {
 352             throw new IOException("MethodUtil: bad name " + name);
 353         }
 354         return defineClass(name, b, 0, b.length, cs);
 355     }
 356 
 357 
 358     /*
 359      * Returns the contents of the specified URL as an array of bytes.
 360      */
 361     private static byte[] getBytes(URL url) throws IOException {
 362         URLConnection uc = url.openConnection();
 363         if (uc instanceof java.net.HttpURLConnection) {
 364             java.net.HttpURLConnection huc = (java.net.HttpURLConnection) uc;
 365             int code = huc.getResponseCode();
 366             if (code >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) {
 367                 throw new IOException("open HTTP connection failed.");
 368             }
 369         }
 370         int len = uc.getContentLength();
 371         InputStream in = new BufferedInputStream(uc.getInputStream());
 372 
 373         byte[] b;
 374         try {
 375             b = IOUtils.readFully(in, len, true);
 376         } finally {
 377             in.close();
 378         }
 379         return b;
 380     }

 381 
 382 
 383     protected PermissionCollection getPermissions(CodeSource codesource)
 384     {
 385         PermissionCollection perms = super.getPermissions(codesource);
 386         perms.add(new AllPermission());
 387         return perms;
 388     }
 389 
 390     private static Class<?> getTrampolineClass() {
 391         try {
 392             return Class.forName(TRAMPOLINE, true, new MethodUtil());
 393         } catch (ClassNotFoundException e) {
 394         }
 395         return null;
 396     }
 397 
 398 }


   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 sun.reflect.misc;
  27 
  28 import java.io.EOFException;
  29 import java.security.AllPermission;
  30 import java.security.AccessController;
  31 import java.security.PermissionCollection;
  32 import java.security.SecureClassLoader;
  33 import java.security.PrivilegedExceptionAction;
  34 import java.security.CodeSource;
  35 import java.io.InputStream;
  36 import java.io.BufferedInputStream;
  37 import java.io.IOException;
  38 import java.net.URL;
  39 import java.net.URLConnection;
  40 import java.lang.reflect.Method;
  41 import java.lang.reflect.InvocationTargetException;
  42 import java.lang.reflect.AccessibleObject;
  43 import java.lang.reflect.Modifier;
  44 import java.util.Arrays;
  45 import java.util.HashMap;
  46 import java.util.Map;

  47 
  48 
  49 class Trampoline {
  50     static {
  51         if (Trampoline.class.getClassLoader() == null) {
  52             throw new Error(
  53                 "Trampoline must not be defined by the bootstrap classloader");
  54         }
  55     }
  56 
  57     private static void ensureInvocableMethod(Method m)
  58         throws InvocationTargetException
  59     {
  60         Class<?> clazz = m.getDeclaringClass();
  61         if (clazz.equals(AccessController.class) ||
  62             clazz.equals(Method.class) ||
  63             clazz.getName().startsWith("java.lang.invoke."))
  64             throw new InvocationTargetException(
  65                 new UnsupportedOperationException("invocation not supported"));
  66     }


 351         if (!name.equals(TRAMPOLINE)) {
 352             throw new IOException("MethodUtil: bad name " + name);
 353         }
 354         return defineClass(name, b, 0, b.length, cs);
 355     }
 356 
 357 
 358     /*
 359      * Returns the contents of the specified URL as an array of bytes.
 360      */
 361     private static byte[] getBytes(URL url) throws IOException {
 362         URLConnection uc = url.openConnection();
 363         if (uc instanceof java.net.HttpURLConnection) {
 364             java.net.HttpURLConnection huc = (java.net.HttpURLConnection) uc;
 365             int code = huc.getResponseCode();
 366             if (code >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) {
 367                 throw new IOException("open HTTP connection failed.");
 368             }
 369         }
 370         int len = uc.getContentLength();
 371         try (InputStream in = new BufferedInputStream(uc.getInputStream())) {
 372             byte[] b = in.readAllBytes();
 373             if (len != -1 && b.length != len)
 374                 throw new EOFException("Expected:" + len + ", read:" + b.length);




 375             return b;
 376         }
 377     }
 378 
 379 
 380     protected PermissionCollection getPermissions(CodeSource codesource)
 381     {
 382         PermissionCollection perms = super.getPermissions(codesource);
 383         perms.add(new AllPermission());
 384         return perms;
 385     }
 386 
 387     private static Class<?> getTrampolineClass() {
 388         try {
 389             return Class.forName(TRAMPOLINE, true, new MethodUtil());
 390         } catch (ClassNotFoundException e) {
 391         }
 392         return null;
 393     }
 394 
 395 }