예제 #1
0
 /** 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;
 }
  /*
   * Does the real work of creating an AppletAudioClip from an InputStream.
   * This function is used by both constructors.
   */
  void createAppletAudioClip(InputStream in) throws IOException {

    // If we haven't initialized yet, we need to find the AudioClip constructor using reflection.
    // We'll use com.sun.media.sound.JavaSoundAudioClip to implement AudioClip if the Java Sound
    // extension is installed.  Otherwise, we use sun.audio.SunAudioClip.

    if (acConstructor == null) {

      if (DEBUG) System.out.println("Initializing AudioClip constructor.");

      try {

        acConstructor =
            (Constructor)
                AccessController.doPrivileged(
                    new PrivilegedExceptionAction() {

                      public Object run()
                          throws NoSuchMethodException, SecurityException, ClassNotFoundException {

                        Class acClass = null;

                        try {

                          // attempt to load the Java Sound extension class JavaSoundAudioClip

                          acClass =
                              Class.forName(
                                  "com.sun.media.sound.JavaSoundAudioClip",
                                  true,
                                  ClassLoader
                                      .getSystemClassLoader()); // may throw ClassNotFoundException

                          if (DEBUG) System.out.println("Loaded JavaSoundAudioClip");

                        } catch (ClassNotFoundException e) {

                          acClass =
                              Class.forName(
                                  "sun.audio.SunAudioClip",
                                  true,
                                  null); // may throw ClassNotFoundException

                          if (DEBUG) System.out.println("Loaded SunAudioClip");
                        }

                        Class[] parms = new Class[1];
                        parms[0] =
                            Class.forName(
                                "java.io.InputStream"); // may throw ClassNotFoundException
                        return acClass.getConstructor(
                            parms); // may throw NoSuchMethodException or SecurityException
                      }
                    });

      } catch (PrivilegedActionException e) {

        if (DEBUG) System.out.println("Got a PrivilegedActionException: " + e.getException());

        // e.getException() may be a NoSuchMethodException, SecurityException, or
        // ClassNotFoundException.
        // however, we throw an IOException to avoid changing the interfaces....

        throw new IOException("Failed to get AudioClip constructor: " + e.getException());
      }
    } // if not initialized

    // Now instantiate the AudioClip object using the constructor we discovered above.

    try {

      Object[] args = new Object[] {in};
      audioClip = (AudioClip) acConstructor.newInstance(args); // may throw InstantiationException,
      // IllegalAccessException,
      // IllegalArgumentException,
      // InvocationTargetException

    } catch (Exception e3) {

      // no matter what happened, we throw an IOException to avoid changing the interfaces....
      throw new IOException("Failed to construct the AudioClip: " + e3);
    }
  }