[m-rev.] For review: Java implementation of IO library

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Jan 15 18:27:16 AEDT 2004


On 15-Jan-2004, James Goddard <goddardjames at yahoo.com> wrote:
> > I think it would be possible to use reflection to do it correctly for
> > Java 1.4, by extracting the FileChannel for System.out and System.in,
> > and invoking seek on that.  If you use Reflection for everything, the
> > code for it should compile fine in Java < 1.4.  This code is a bit
> > implementation-dependent, but if it doesn't work it will just throw
> > an exception.
> 
> Ok, here's my next attempt.  I've copied your get_output_channel()
> function in and altered it to make get_input_channel() as well.
> seek(), size() and getOffset() all use Reflection to access those
> FileChannels now.
> 
> It's all getting so ugly!! =(

I had a bit more of a think about get_input_channel().
Rather than hard-coding the knowledge of how to get to the FileChannel
field, it might be better to just dynamically search all the fields for it,
using code roughly along the lines of what is below.

// This program uses reflection to dump out the fields of
// System.out and System.in
import java.lang.reflect.*;
import java.util.*;
class Traverse {
	static class Pair {
		Class c;
		Object o;
		public Pair(Class cc, Object oo) { c = cc; o = oo; }
	}
	static void indent(int depth) {
		for (int d = 0; d < depth; d++) {
			System.out.print(" ");
		}
	}
	static void traverse(Object o) {
		traverse(o, new LinkedList(), 0);
	}
	static void traverse(Object o, Collection visited, int depth) {
		if (!visited.contains(o)) {
			visited.add(o);
			traverse(o.getClass(), o, visited, depth);
		}
	}
	static void traverse(Class c, Object o, Collection visited, int depth) {
		indent(depth);
		System.out.print("Class  = ");
		System.out.println(c);
		if (c.isPrimitive()) { return; }
		Field[] flds = null;
		try {
			flds = c.getDeclaredFields();
		} catch (java.lang.Exception e) {
			System.out.println("got exception: ");
			System.out.println(e);
		}
		for (int i = 0; i < flds.length; i++) {
			indent(depth + 1);
			System.out.print("Field " + i + " = ");
			System.out.println(flds[i]);
			try {
				flds[i].setAccessible(true);
				if ((flds[i].getModifiers()
				    & Modifier.STATIC) == 0
				    && !flds[i].getType().isPrimitive()) {
					traverse(flds[i].get(o), visited,
						depth + 2);
				}
			} catch (Exception e) {
				System.out.println("nope");
				System.out.println(e);
			}
		}
		if (c.getSuperclass() != null) {
			indent(depth + 1);
			System.out.println("superclass:");
			traverse(c.getSuperclass(), o, visited, depth + 1);
		}
	}
	public static void main(String[] args) {
		traverse(System.out);
		traverse(System.in);
	}
}

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list