Purpose of using java.lang.reflect.Proxy ?

  • Thread starter Sébastien de Mapias
  • Start date
S

Sébastien de Mapias

Hi,

This is a newbie question... I've just read that using a proxy inside
application code is quite unusual, and that it's mostly used in
frameworks... In an app I've inherited, I find the following lines:

DFLT_SESS_FACT = HibernateSupport.getSessionFactory
("hibernate.cfg.xml",
JdbcStuffDAO.class.getClassLoader
(),
new StuffBaseEntityInterceptor());
[...]
public IStuffService2 getStuffService(String p_user,
String p_password,
ApplicationCode
p_applicationCode,
SessionFactory
p_sessionFactory)
throws StuffException
{
JdbcStuffServiceImpl l_service =
new JdbcStuffServiceImpl(p_user,p_applicationCode,new
JdbcStuffDAO(p_sessionFactory));
IStuffService2 l_proxy =
(IStuffService2)Proxy.newProxyInstance
(JdbcStuffFactory.class.getClassLoader(),
new Class[] {IStuffService2.class},
new HibernateSessionInvocationHandler
(DFLT_SESS_FACT,l_service,l_service.getLocale()));
return l_proxy;
}

I'm not familiar with this Proxy thing... What purpose does it serve ?
I've noticed this method could return 'l_service' as well (cause
JdbcStuffServiceImpl implements IStuffService2), and thus have:
{
JdbcStuffServiceImpl l_service =
new JdbcStuffServiceImpl(p_user,p_applicationCode,new
JdbcStuffDAO(p_sessionFactory));
return l_service;
}
but I haven't tested it.
What did the guy who wrote these lines want to achieve ? (he's left,
so
I can't ask him...)

Thanks for your help...
Regards,
Sébastien
 
J

Joshua Cranmer

Sébastien de Mapias said:
This is a newbie question...

Proxies are not newbie questions (nor anything to do with reflection,
for that matter).
I'm not familiar with this Proxy thing... What purpose does it serve ?

Proxies are one of the more obscure features of reflection. The basic
idea is that the end result is an object which dynamically implements
certain interfaces.
What did the guy who wrote these lines want to achieve ? (he's left,
so
I can't ask him...)

In lieu of context, I can only make assumptions. One possibility is that
the person is wrapping a generic extender around the original object.
Since you mention that the class also appears to extend the interface in
question as well, I can presume that either this was a short-term hack
intended to support something while he was working on making
JdbcStuffServiceImpl implement IService2.

The other possibility that strikes me is that the wrapper is doing some
external operations, such as (judging from Google results) wrapping the
method in a transaction device:

<wrapped method impl>:
try {
beginTransaction();
<call original method>
commitTransaction();
} catch (Throwable t) {
rollbackTransaction();
throw t;
}
 

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,999
Messages
2,570,247
Members
46,844
Latest member
JudyGvh32

Latest Threads

Top