Hemal Pandya wrote:
>>I have this working:
>
[quoted text clipped - 7 lines]
> that compiles and runs? Please use System.out.println in place of
> Audit.
See below for SSCCE (60 lines, short enough I hope)
> Perhaps you don't want to have to pass the Apple to fooFun and still
> have it print Apple? That would not work, I think. It has to do with
> Type Erasure, about which you can R in T M, Java Language Spec
> actually. The best you can do is to accept a Class object in your
> constructor, but that is error prone.
Now that I've pondered what you and Daniel have said, I suspect that
Erasure really prevents me doing what I want in Foo's constructor.
On reflection, I seem to be spending too much effort worrying about
writing lots of "o.getClass().getSimpleName()" instead of "eName" :-)
I also try to keep statements to one line and dislike it when I have to
split them across multiple lines. However there's a limit to the amount
of effort I'll put into avoiding this.
I doubt there's much of an efficiency gain either but I first learned
programming with Fortran and it was drilled into me that I should avoid
calling functions more often than strictly necessary. Old habits are
hard to break.
I think the best I can do is declare String className in abstract class
Foo and then assign it in my concrete subclasses Zap and Zog. In reality
I have dozens of such subclasses.
-------------------------------------8<------------------------
public class GenericErasureProblem {
public static void main(String[] args) {
Zap zap = new Zap();
Zog zog = new Zog();
}
}
abstract class Foo<E> {
String className;
Foo() {
// below is WRONG, says "Zap" or "Zog", want "Apple" or "Brick".
eName = this.getClass().getName();
}
void fooFun(E o) {
System.out.println("FooFunned a "+eName);
// below works but I wanted to avoid having this in every method
// System.out.println(
// "FooFunned a "+o.getClass().getSimpleName());
}
void fooFiz(E o) {
System.out.println("FooFizzed a "+eName);
}
void fooFar(E o) {
System.out.println("FooFarred a "+eName);
}
void fooFie(E o) {
System.out.println("FooFied a "+eName);
}
}
class Apple {
String colour;
Apple(String colour) {
this.colour = colour;
}
}
class Brick {
int weight;
Brick(int weight) {
this.weight = weight;
}
}
class Zap extends Foo<Apple> {
Zap() {
Apple apple = new Apple("Red");
fooFun(apple);
fooFar(apple);
}
}
class Zog extends Foo<Brick> {
Zog() {
Brick brick = new Brick(12);
fooFun(brick);
fooFiz(brick);
fooFie(brick);
}
}
-------------------------------------8<------------------------
P.P.S. My AuditLog.memo() call is actually used like this ...
void fooFar(E o) {
AuditLog.memo("objectname", "action", o.toString());
}
Maybe I can change this to pass o and then maybe I can hide
o.getClass().getSimpleName in AuditLog.memo() somehow.
Ian Wilson - 22 Dec 2006 11:13 GMT
> Hemal Pandya wrote:
>
[quoted text clipped - 9 lines]
>> that compiles and runs? Please use System.out.println in place of
>> Audit.
<SSCCE snipped>
The following does what I want but it is unlikely to be of interest to
anyone else as it doesn't involve generics or cleverness. I include it
for completeness :-)
------------------------------------8<----------------------------------
public class ProblemSolved {
public static void main(String[] args) {
Zap zap = new Zap();
Zog zog = new Zog();
}
}
abstract class Foo<E> {
Foo() { }
void fooFun(E o) {
AuditLog.memo("fooFunned a ", o);
}
void fooFiz(E o) {
AuditLog.memo("fooFizzed a ", o);
}
void fooFar(E o) {
AuditLog.memo("fooFarred a ", o);
}
void fooFie(E o) {
AuditLog.memo("fooFied a ", o);
}
}
class Apple {
String colour;
Apple(String colour) {
this.colour = colour;
}
public String toString() {
return "A " + colour + " apple";
}
}
class Brick {
int weight;
Brick(int weight) {
this.weight = weight;
}
public String toString() {
return "A brick weighing " + weight;
}
}
class Zap extends Foo<Apple> {
Zap() {
Apple apple = new Apple("Red");
fooFun(apple);
fooFar(apple);
}
}
class Zog extends Foo<Brick> {
Zog() {
Brick brick = new Brick(12);
fooFun(brick);
fooFiz(brick);
fooFie(brick);
}
}
class AuditLog {
static void memo(String action, Object o) {
System.out.println(action + o.getClass().getSimpleName()
+ " (" + o.toString() + ")");
}
}
------------------------------------8<----------------------------------
Daniel Pitts - 23 Dec 2006 19:25 GMT
> Now that I've pondered what you and Daniel have said, I suspect that
> Erasure really prevents me doing what I want in Foo's constructor.
Kind of. Unless you have
public class Foo<E> {
private final string name;
public Foo(Class<? extends E> fooClass) {
name = fooClass.getSimpleName();
}
}
There basically isn't any way to get access to information about
Generics by using simple reflection, unless you have an object to
reflect upon.
> I doubt there's much of an efficiency gain either but I first learned
> programming with Fortran and it was drilled into me that I should avoid
> calling functions more often than strictly necessary. Old habits are
> hard to break.
That is a very bad habit, and will lead you down the road of bad design
time and time again. Function calls are considered neglagible on
todays modern systems. And in any case, you should always strive for a
easy-to-understand design first, and then optimize where a profiler
tells you to.
> I think the best I can do is declare String className in abstract class
> Foo and then assign it in my concrete subclasses Zap and Zog. In reality
> I have dozens of such subclasses.
Why subclass? It seems like instead you could just have a String
parameter, instead of automating the name...
public class Foo<E> {
private final String name;
public Foo(String name) {
this.name = name;
}
}
That seems like the easiest way to me.
> P.P.S. My AuditLog.memo() call is actually used like this ...
> void fooFar(E o) {
> AuditLog.memo("objectname", "action", o.toString());
> }
> Maybe I can change this to pass o and then maybe I can hide
> o.getClass().getSimpleName in AuditLog.memo() somehow.
That seems like another good approach. If audit log has the same basic
format all over, why not let AuditLog handle that format?
The best advice I can give you right now is to not over-engineer this.
Do something that works and simple. Its the K.I.S.S. principal - Keep
It Simple Stupid. Once you've gotten something that works, refactor it
so that it works the same way, but has the balance you need between
good design, ease of understanding, and speed.
HTH
- Daniel.