'Why this constructor is not intercepted by my code?
I want to write a javaagent using byte-buddy. I wan to intercept constructor of any class that is a sub class of java.sql.Connection.
my setup code:
log.info("{} profile activated", profile);
if (profile.equals("hikari")) {
startMatcher = nameStartsWith("com.zaxxer.hikari");
JDBCAPIInterceptor.profile = new HikariProfile();
} else if (profile.equals("dbcp1")) {
startMatcher = nameStartsWith("org.apache.commons.dbcp");
JDBCAPIInterceptor.profile = new DBCP1Profile();
} else if (profile.equals("dbcp2")) {
startMatcher = nameStartsWith("org.apache.commons.dbcp2");
JDBCAPIInterceptor.profile = new DBCP2Profile();
} else if (profile.equals("druid")) {
startMatcher = nameStartsWith("com.alibaba.druid.pool");
} else {
startMatcher = any();
}
new AgentBuilder.Default().type(startMatcher.and(isSubTypeOf(java.sql.Connection.class).or(isSubTypeOf(java.sql.Statement.class))))
.transform(constructorTransformer).transform(methodsTransformer).with(listener).installOn(inst);
but why constructor of org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper can't be intercepted while it's definitely a sub class of java.sql.Connection as it extends org.apache.commons.dbcp2.DelegatingConnection which implements java.sql.Connection?
also, my intercepting code is executed while constructor of org.apache.commons.dbcp2.DelegatingConnection being called.
public class PoolingDataSource<C extends Connection> implements DataSource, AutoCloseable {
/**
* PoolGuardConnectionWrapper is a Connection wrapper that makes sure a closed connection cannot be used anymore.
*
* @since 2.0
*/
private class PoolGuardConnectionWrapper<D extends Connection> extends DelegatingConnection<D> {
PoolGuardConnectionWrapper(final D delegate) {
super(delegate);
}
@Override
public void close() throws SQLException {
if (getDelegateInternal() != null) {
super.close();
super.setDelegate(null);
}
}
/**
* @see org.apache.commons.dbcp2.DelegatingConnection#getDelegate()
*/
@Override
public D getDelegate() {
return isAccessToUnderlyingConnectionAllowed() ? super.getDelegate() : null;
}
/**
* @see org.apache.commons.dbcp2.DelegatingConnection#getInnermostDelegate()
*/
@Override
public Connection getInnermostDelegate() {
return isAccessToUnderlyingConnectionAllowed() ? super.getInnermostDelegate() : null;
}
@Override
public boolean isClosed() throws SQLException {
return getDelegateInternal() == null || super.isClosed();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
