public void dispose() { getRGBPixelsMethodParam = null; Method method = getRGBPixelsMethod; try { if (method != null) { getRGBPixelsMethod = null; method.setAccessible(false); } } finally { try { Class<?>[] tailSig = new Class<?>[0]; peer.getClass().getDeclaredMethod("dispose", tailSig).invoke(peer, (Object[]) tailSig); } catch (IllegalArgumentException e) { LLog.e(e); } catch (SecurityException e) { LLog.e(e); } catch (IllegalAccessException e) { LLog.e(e); } catch (InvocationTargetException e) { LLog.e(e); } catch (NoSuchMethodException e) { LLog.e(e); } } }
public DirectRobot(GraphicsDevice device) throws AWTException { if (device == null) { device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); } robot = new Robot(device); ArrayList<Exception> exceptions = new ArrayList<Exception>(); this.device = device; Toolkit toolkit = Toolkit.getDefaultToolkit(); peer = ((ComponentFactory) toolkit).createRobot(null, device); Class<?> peerClass = peer.getClass(); Method method = null; int methodType = -1; Object methodParam = null; try { method = peerClass.getDeclaredMethod( "getRGBPixels", new Class<?>[] {Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, int[].class}); methodType = 0; } catch (SecurityException e) { exceptions.add(e); } catch (NoSuchMethodException e) { exceptions.add(e); } if (methodType < 0) { try { method = peerClass.getDeclaredMethod( "getScreenPixels", new Class<?>[] {Rectangle.class, int[].class}); methodType = 1; } catch (SecurityException e) { exceptions.add(e); } catch (NoSuchMethodException e) { exceptions.add(e); } } if (methodType < 0) { try { method = peerClass.getDeclaredMethod( "getScreenPixels", new Class<?>[] {Integer.TYPE, Rectangle.class, int[].class}); methodType = 2; GraphicsDevice[] devices = getScreenDevices(); int count = devices.length; for (int i = 0; i < count; i++) { if (device.equals(devices[i])) { methodParam = Integer.valueOf(i); break; } } } catch (SecurityException e) { exceptions.add(e); } catch (NoSuchMethodException e) { exceptions.add(e); } } if (methodType < 0) { try { boolean accessibleChanged = false; method = peerClass.getDeclaredMethod( "getRGBPixelsImpl", new Class<?>[] { Class.forName("sun.awt.X11GraphicsConfig"), Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, int[].class }); Field field = peerClass.getDeclaredField("xgc"); try { if (!field.isAccessible()) { field.setAccessible(true); accessibleChanged = true; } methodParam = field.get(peer); methodType = 3; } catch (IllegalArgumentException e) { exceptions.add(e); } catch (IllegalAccessException e) { exceptions.add(e); } finally { if (accessibleChanged) { field.setAccessible(false); } } } catch (SecurityException e) { exceptions.add(e); } catch (NoSuchFieldException e) { exceptions.add(e); } catch (NoSuchMethodException e) { exceptions.add(e); } catch (ClassNotFoundException e) { exceptions.add(e); } } if (methodType >= 0 && method != null && (methodType <= 1 || methodParam != null)) { if (DEBUG) LLog.i(String.format("Method type = %d", methodType)); getRGBPixelsMethod = method; getRGBPixelsMethodType = methodType; getRGBPixelsMethodParam = methodParam; } else { if (DEBUG) { for (Exception e : exceptions) { e.printStackTrace(); } } exceptions.clear(); exceptions = null; if (DEBUG) { LLog.w( String.format( "WARNING: Failed to acquire direct method for grabbing pixels, please post this on the main thread!\n\n%s\n\n", peer.getClass().getName())); try { Method[] methods = peer.getClass().getDeclaredMethods(); for (Method method1 : methods) { LLog.w(method1 + "\n"); } LLog.w("\n"); } catch (Exception e) { LLog.e(e); } } throw new RuntimeException("No supported method for getting pixels"); } }