Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More example for proxy support for constructor class with Enhancer ? #40

Closed
bwzhang2011 opened this issue Jul 15, 2015 · 3 comments
Closed

Comments

@bwzhang2011
Copy link

Hi, cglib team. I encountered some problem when make proxy support towards the bean with constructor params. e.g when I want to some proxy usecases towards jedis (framework integration with redis server). here comes my code like this with enhancer:

public T getInstance(T target, Class[] args, Object[] argsValue) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(target.getClass());
enhancer.setCallback(this);
return (T) enhancer.create(args, argsValue);
}

the target is jedis like this:
public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands, AdvancedJedisCommands, ScriptingCommands {
public Jedis(final String host) {
super(host);
}

public Jedis(final String host, final int port) {
super(host, port);
}

public Jedis(final String host, final int port, final int timeout) {
super(host, port, timeout);
}

public Jedis(JedisShardInfo shardInfo) {
super(shardInfo);
}

public Jedis(URI uri) {
super(uri);
}
//omitted

}

when I want to proxy jedis, the constructor for such is for such
public Jedis(final String host, final int port, final int timeout) {
super(host, port, timeout);
}

so here comes the class info and arguments comes like this
Class[] args = new Class[] {String.class, Integer.class, Integer.class};

Object[] arguments = new Object[] {jedis.getClient().getHost(), jedis.getClient().getPort(),
jedis.getClient().getTimeout()};

if such above is not wrong, the cglib will help to create the proxy class with enhancer. but here comes my exception stracktrace:
Caused by: java.lang.NoSuchMethodException: redis.clients.jedis.Jedis$$EnhancerByCGLIB$$e2ffdb0f.(java.lang.String, java.lang.Integer, java.lang.Integer)
at java.lang.Class.getConstructor0(Class.java:2902)
at java.lang.Class.getDeclaredConstructor(Class.java:2066)
at net.sf.cglib.core.ReflectUtils.getConstructor(ReflectUtils.java:244)
... 14 more

I don't know the reason and whether I was wrong. hope someone could point out my mistake with cglib.

@bwzhang2011
Copy link
Author

Could anybody who encounter the similar use case leave me some advice on how to resolve such problem ?

@raphw
Copy link
Member

raphw commented Jul 15, 2015

You are using Integer wrapper objects instead of primitive ints when trying to locate the constructor. Therefore, the lookup fails.

@bwzhang2011
Copy link
Author

@raphw, thanks for review. and it should work with the type corrections. I will continue to get to know about cglib with its use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants