Extracting Class names in Abstract classes with Generics.

I

Ian Wilson

I have this working:

Abstract class Foo<E> {
Foo() {}

public fooFun(E enigma) {
// do important things. And then ...
String className = enigma.getClass().getName();
AuditLog.memo(className+" had things done to it");
}
}

I'd like to decide my class name when my subclass gets instantiated, but
the following doesn't work for any values of xxxxx I've been able to
dream up:

Abstract class Foo<E> {

private String className;

Foo() {
className = E.xxxxx.getClass().getName()
}

public fooFun(E enigma) {
// do important things. And then ...
AuditLog.memo(className+" had things done to it");
}
}

Any clues? Which TFM should I R?



P.S. In case the above is insufficiently clear, the following classes
may illuminate my meaning a little.

class Apple {
String variety;
Apple() {}
}

class Brick {
int weight;
Brick() {}
}

class Zap extends Foo<Apple> {
fooFun(new Apple());
// I want my Audit log to say "Apple had ..."
}

class Pow extends Foo<Brick> {
fooFun(new Brick());
// I want my Audit log to say "Brick had ..."
}
 
D

Daniel Pitts

Ian said:
I have this working:

Abstract class Foo<E> {
Foo() {}

public fooFun(E enigma) {
// do important things. And then ...
String className = enigma.getClass().getName();
AuditLog.memo(className+" had things done to it");
}
}

I'd like to decide my class name when my subclass gets instantiated, but
the following doesn't work for any values of xxxxx I've been able to
dream up:

Abstract class Foo<E> {

private String className;

Foo() {
className = E.xxxxx.getClass().getName()
}

public fooFun(E enigma) {
// do important things. And then ...
AuditLog.memo(className+" had things done to it");
}
}

Any clues? Which TFM should I R?



P.S. In case the above is insufficiently clear, the following classes
may illuminate my meaning a little.

How about this instead:

public fooFun(E enigma) {
// do important things. And then ...
AuditLog.memo(enigma.getClass().getName()+" had things done
to it");
}
 
H

Hemal Pandya

Ian said:
I have this working:
[....]
class Zap extends Foo<Apple> {
fooFun(new Apple());
// I want my Audit log to say "Apple had ..."

Far as I can tell it already says that. Can you post actual code sample
that compiles and runs? Please use System.out.println in place of
Audit.

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.

[....]
 
I

Ian Wilson

Hemal said:
Ian said:
I have this working:

[....]

class Zap extends Foo<Apple> {
fooFun(new Apple());
// I want my Audit log to say "Apple had ..."


Far as I can tell it already says that. Can you post actual code sample
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.
 
I

Ian Wilson

Ian said:
Hemal said:
Ian said:
I have this working:

[....]

class Zap extends Foo<Apple> {
fooFun(new Apple());
// I want my Audit log to say "Apple had ..."



Far as I can tell it already says that. Can you post actual code sample
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<----------------------------------
 
D

Daniel Pitts

Ian said:
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.
 
D

Daniel Pitts

Ian said:
I have this working:

Abstract class Foo<E> {
Foo() {}

public fooFun(E enigma) {
// do important things. And then ...
String className = enigma.getClass().getName();
AuditLog.memo(className+" had things done to it");
}
}

I might also suggest looking into log4j. It might do what you need,
with a lot less heartache and a lot more configurability. Oh, and its
already been written :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top