Link to home
Start Free TrialLog in
Avatar of _lychee_
_lychee_

asked on

Object pooling

I have an abstract class, and I want to put object pooling code into this class. something like:

public abstract class A {
   static public A getInstance() {...}
}

how can getInstance 'know' the class it is running in? as in can getInstance get at the Class object it is running in? like...

public class B extends A {
   ...
}

so how can B.getInstance know that it is running 'within' B?

If there is any other way of providing object pooling code from a superclass aside from using instance methods that's welcome too.
Avatar of imladris
imladris
Flag of Canada image

Would it help to get the classes name?

You can do that with:

Class c=getClass();
String n=c.getName();
Avatar of _lychee_
_lychee_

ASKER

doesn't getClass only work from instance methods?

i know how to do it from instance methods... but i'm wondering if it's possible for static methods to do this
Never the easy questions, lychee....

http://forum.java.sun.com/forum?14@45.lK2jaUFjdik^102243@.eec5ebc

http://forum.java.sun.com/forum?14@45.lK2jaUFjdik^108328@.eef80a0

As you also cannot do static inheritance I think the answer is no.

This sort of object management needs an instance. You could do it from a factory but that would

Write your own class loader for this? EG:

For example, an application could create a network class loader to download class files from a server. Sample code might look like:

   ClassLoader loader = new NetworkClassLoader(host, port);
   Object main = loader.loadClass("Main", true).newInstance();
        . . .
 
The network class loader subclass must define the methods findClass and loadClassData to load a class from the network. Once it has downloaded the bytes that make up the class, it should use the method defineClass to create a class instance. A sample implementation is:


     class NetworkClassLoader extends ClassLoader {
         String host;
         int port;

         public Class findClass(String name) {
             byte[] b = loadClassData(name);
             return defineClass(name, b, 0, b.length);
         }

         private byte[] loadClassData(String name) {
             // load the class data from the connection
              . . .
         }
     }
 
Just thought of a possible solution but haven't tried it out.

If you have a static code block in the abstract base class that has a try/catch block and in that try catch block deliberately throws an exception does the stack trace tell you the correct class name?

If so, you could capture the stack trace printed to system.err and parse it for the class name of this class in the catch block and then have code in the catch or finally blocks to use this class name as you wish.

// static code block
{

  try {
    //forcibly throw an exception
  } catch (Exception e) {
    // parse exception to get class name
  }

}

Slightly messy, but this may well work as the JVM obviously knows the class name - you just need to force it to tell you somehow.

(heyhey has posted some code previously for parsing a stack trace I believe...)
Ahh nope - hung by my own petard there as the Static block will only run once and there is no way you can force subclasses to initialise it again.

What you need is a tool to get inside the JVM and retrieve the class name. Possibly a llittle excessive for your purposes.

What about using an interface and having different static initialisation in your subclasses that way?


If you have not seen,

visit

http://www.servlets.com/jsp/

He has done connection poolong. I thimnk same is applicable to object pooling.
jod... tried ur exception method but unfortunately it reports it as from the originating class... tis a pity that there's no static inheritance i guess :(

i guess i'll have to put the object pooling code in the factory class (in line with my other qn as well i guess)... but then returning the object to the pool gets troublesome (cos factory has to identify the correct pool/alternatively, factory has to search the pool when giving out the object)......

*sigh* i guess i'll just have to use the instance thingy...

ravindra, i can't connect to the url u gave... maybe my school's network slightly bozo now...

jod, i think i'll reduce the points for this and the other qn by 1/2 (so total = 200), and then accept ur comment; is that ok with u?
>> so how can B.getInstance know that it is running 'within' B?

in fact B.getInstance is not running 'within' B. there is only one definition of getInstance method, which can be called some other method (static or instance) 'within' class B.

so
1. you can't override static methods.
2. you can't get the Class object you are 'within' (from a static method).

but ... cheating :)
1. you can get (almost always)
- the class name
- the method name
- the java file name
- line number
for any piece of code. (static / instance method)

I have used successfully this technique for logging purposes.
I can post some (ugly :) example code, but generally speaking you
throw and Exception and parse its stacktrace (Jod mentioned it ?).

2.
you can implement your A.registerInstance and A.getInstance

A.registerInstance(A object)
{
  // find the caller class name (class B)
  // instances.put(callerName, object)
}

A.getInstance()
{
  // find the caller class name (class B)
  // return (A)instances.get(callerName)
}

3. you'd better change your design :)
REALLY !
since Java classes are not 'real objects' (hmmm ... I have to check
what's the result from obj.getClass().getClass())  you'd better leave
all the static stuff and make your own Factory hierarchy - you can
have Factories that makes Factory objects and you'll be able to override
Factory methods ...)
pt well taken :)
so -
1. do you want my sample (ugly :) code ?
2. who will take the points ? :)
urm...
1. 'tis not ethical to 'cheat' don't u think :)
2. jod i guess :>
1. not ethical, but you have to sometimes ... cheat or die :)
2. it's ok :) you can accept his comment as answer
ASKER CERTIFIED SOLUTION
Avatar of Jod
Jod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
alright then...
Community Support has reduced points to 100
Cheers...