01: /*
02: * $RCSfile: DisposableNullOpImage.java,v $
03: *
04: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
05: *
06: * Use is subject to license terms.
07: *
08: * $Revision: 1.1 $
09: * $Date: 2006/06/17 00:02:27 $
10: * $State: Exp $
11: */
12:
13: package com.sun.media.jai.util;
14:
15: import java.awt.image.RenderedImage;
16: import java.lang.reflect.AccessibleObject;
17: import java.lang.reflect.Method;
18: import java.util.Map;
19: import javax.media.jai.ImageLayout;
20: import javax.media.jai.NullOpImage;
21: import javax.media.jai.PlanarImage;
22: import javax.media.jai.RenderedImageAdapter;
23:
24: /**
25: * <code>NullOpImage</code> subclass which conditionally forwards
26: * {@link #dispose()} to its source. The call will be forwarded if the
27: * source is a <code>PlanarImage</code> or a <code>RenderedImage</code>
28: * wrapped by <code>RenderedImageAdapter</code> and which has a
29: * <code>dispose()</code> method with no parameters. In the former case
30: * the call is forwarded directly, and in the latter via reflection.
31: *
32: * @since JAI 1.1.3
33: */
34: public class DisposableNullOpImage extends NullOpImage {
35: public DisposableNullOpImage(RenderedImage source,
36: ImageLayout layout, Map configuration, int computeType) {
37: super (source, layout, configuration, computeType);
38: }
39:
40: public synchronized void dispose() {
41: PlanarImage src = getSource(0);
42: if (src instanceof RenderedImageAdapter) {
43: // Use relection to invoke dispose();
44: RenderedImage trueSrc = ((RenderedImageAdapter) src)
45: .getWrappedImage();
46: Method disposeMethod = null;
47: try {
48: Class cls = trueSrc.getClass();
49: disposeMethod = cls.getMethod("dispose", null);
50: if (!disposeMethod.isAccessible()) {
51: AccessibleObject.setAccessible(
52: new AccessibleObject[] { disposeMethod },
53: true);
54: }
55: disposeMethod.invoke(trueSrc, null);
56: } catch (Exception e) {
57: // Ignore it.
58: }
59: } else {
60: // Invoke dispose() directly.
61: src.dispose();
62: }
63: }
64: }
|