[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

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.
0
_lychee_
Asked:
_lychee_
  • 5
  • 5
  • 3
  • +3
1 Solution
 
imladrisCommented:
Would it help to get the classes name?

You can do that with:

Class c=getClass();
String n=c.getName();
0
 
_lychee_Author Commented:
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
0
 
JodCommented:
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
              . . .
         }
     }
 
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
JodCommented:
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...)
0
 
JodCommented:
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?
0
 
Ravindra76Commented:


If you have not seen,

visit

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

He has done connection poolong. I thimnk same is applicable to object pooling.
0
 
_lychee_Author Commented:
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?
0
 
heyhey_Commented:
>> 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 ...)
0
 
_lychee_Author Commented:
pt well taken :)
0
 
heyhey_Commented:
so -
1. do you want my sample (ugly :) code ?
2. who will take the points ? :)
0
 
_lychee_Author Commented:
urm...
1. 'tis not ethical to 'cheat' don't u think :)
2. jod i guess :>
0
 
heyhey_Commented:
1. not ethical, but you have to sometimes ... cheat or die :)
2. it's ok :) you can accept his comment as answer
0
 
JodCommented:
1. we prefer to say over here "It's OK to cheat as long as you don't get caught"

2. you have been :)

lychee feel free to reduce the points down to a more reasonable level for these two quesstions as you suggest.

tis not ethical to be greedy...
0
 
_lychee_Author Commented:
alright then...
0
 
ianBCommented:
Community Support has reduced points to 100
0
 
JodCommented:
Cheers...
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 5
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now