Warning: I've just started creating this file 2005.May.15, so it's still
very incomplete, so after you've seen what's here already you might wish to
bookmark it and come back in a couple days to see it in a more complete state.
This file consists of a listing of problems I've encountered while working
on Lab assignment #3 for the Distributed Java class at De Anza College,
and solutions to those problems which I've found to make progress possible.
In most cases I haven't found a proper decent solution because the instructor
for the class knows next to nothing about Linux, and none of the other
students in the class are using Linux either, so I mostly have to find
solutions myself, even if they are hacks to bypass the roadblock. A hack
to get around a problem, no matter how ugly, is better than total defeat.
But still I'd like to eventually find out how to do things correctly,
and then clean up my code accordingly.
Table of contents, for my Java programming problems with solutions:
- Runtime validation of class against interface:
- Printing data objects in s-expression format for debugging.
- Not yet finished: Parser for such s-expressions to allow easy input of
test data into test rigs.
If you see a typo here, even a trivial spelling error, please let me
know. Unlike newsgroups, and traditional print publishing, where you
post or publish something and then never have a chance to fix anything
you typed incorrectly, with Web pages there's never a deadline for
corrections, and my goal is the same as Knuth's policy with his books,
eventual 100% accuracy if I get the cooperation of readers to spot
typos I missed. One difference from Knuth, however: Nobody ever pays me
money for my work, so I have no money even for food or paying off
credit-card bills, so there's absolutely no money to pay proofreaders.
Runtime validation of class against interface (one-of-a-kind)
(Need to include explanation of JSP.page:out and System.out incompatibility
which required this interface to cover the two of them.)
Source code for MyPrintableI.java
Sample source code for
the interface against which MyPrintableI effectively verifies a Java class
(not yet tested, since I haven't yet converted MyPrintableI to
use MyPrintable.class, instead it still has ad hoc hand-coded tests).
......
Runtime validation of class against interface (flexible)
I wanted to generate (from scratch) objects of type
HttpServletRequest and HttpServletResponse,
but I haven't been able to find any public constructors for them that
don't presume I already have a simlar object. I need to create such objects
from scratch to emulate a HTTP Servlet session when in fact none is
in progress, in order to debug the HttpServlet classes I'll be writing.
If anybody knows a proper way to make an actual object of those classes
from scratch, please let me know. Until and unless I get such info,
I've decided on an alternate plan:
My initial alternate plan was to write my own class that implements
the javax.servlet.http.HttpServletRequest interface.
But then I looked at it, and there are 49 (fourty-nine) different methods
that must be implemented!! There's no way I can figure out what all
that code is supposed to do, much less actually implement it in time to
finish my lab assignment by the due date. So I thought of this
minimal alternate plan:
I'll define a much smaller interface that contains just the methods
my HttpServlets will actually use. Unfortunately, with compile-time
checking of interfaces for both my own code and code I must use from
other sources, it'll be impossible to force that "other" code, whose
source I don't have, to implement my minimal interface. This was a
roadblock until early this morning (2005.May.15) when I thought of
this idea while lying in bed:
I'll invent run-time class/interface validation, just like I did
a few days ago for output streams capable of doing print and println
even though they have no superclass (except Object) or common interface
in the Java API, except this time I'll do it the right way, by
actually creating an in interface and compiling it and then using
the Reflection API to collect the methods it specifies and compare
them against the methods actually implemented in a regular class file.
(The print/println runtime interface was one-of-a-kind, hand-coded
to know specifically that
void print(String) and void println(String)
are the only methods required. But it was a good warm-up exercise
before this done-right version today.) So I did it today
(2995.May.15), and already have it finished and working.
Here's the code!
And here's a sample interface
and a
sample class
that doesn't quite implement it (download all three files and
try it yourself).
By comparison, here's the older MyPrintableI class (not yet uploaded).
At some point I should refactor the MyPrintableI to just call the new
CompareIntCls. Maybe tomorrow... or maybe after this lab assignment is done.
How to invoke HttpServlet via JSP or URL?
What I posted here
details here
......