public static InputStream openStream(final URL url) throws IOException { try { return (InputStream) AccessController.doPrivileged( new PrivilegedExceptionAction() { public Object run() throws IOException { return url.openStream(); } }); } catch (PrivilegedActionException e) { throw (IOException) e.getException(); } }
public static InputStream getResourceAsStream(final Class c, final String name) throws IOException { try { return (InputStream) AccessController.doPrivileged( new PrivilegedExceptionAction() { public Object run() throws IOException { return c.getResourceAsStream(name); } }); } catch (PrivilegedActionException e) { throw (IOException) e.getException(); } }
/** Invoke a method on an object using reflection. */ public static Object invoke(final Object target, final Method method, final Object... args) { try { return AccessController.doPrivileged( new PrivilegedExceptionAction<Object>() { public Object run() throws IllegalAccessException, InvocationTargetException { if (!method.isAccessible()) method.setAccessible( true); // Say please - else invoke() will fail if method is protected return method.invoke(target, args); } }); } catch (PrivilegedActionException e) { e.printStackTrace(); } return null; }