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

Cannot proxy java.net.SocketImpl #337

Open
bmarwell opened this issue Sep 24, 2020 · 1 comment
Open

Cannot proxy java.net.SocketImpl #337

bmarwell opened this issue Sep 24, 2020 · 1 comment

Comments

@bmarwell
Copy link

bmarwell commented Sep 24, 2020

Hi,

I would like to proxy the class java.net.SocketImpl.

I created an implementation of SocketImplFactory like so:

import javassist.ClassPool;
import javassist.util.proxy.ProxyFactory;

import java.net.SocketImpl;
import java.net.SocketImplFactory;

class SocketProxyFactory implements SocketImplFactory {
  private final SocketImpl defaultSocketImpl;

  public SocketFactoryProxy(final SocketImpl defaultSocketImpl) {
    this.defaultSocketImpl = defaultSocketImpl;
  }

  @Override
  public SocketImpl createSocketImpl() {
    final ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setSuperclass(SocketImpl.class);

    try {
      return (SocketImpl) proxyFactory.create(
          new Class<?>[0], new Object[0], new SocketProxy(this.defaultSocketImpl)
      );
    } catch (final Exception ex) {
      throw new IllegalStateException("Cannot construct proxy.", ex);
    }
  }
}

When I run the code, I get:

javassist.CannotCompileException: The package of the requested class: javassist.util.proxy is different from the package of the lookup class: java.net: java.net.SocketImpl has no permission to define the class

Is there anything I could do here? I want to delegate all methods but one.

env

Compiling with JDK >= 11 (any) to maven.compiler.release = 8.
I tried Java 11, 14 and 15 from zulu, adopt-openjdk-hotspot and adopt-openjdk-openj9.

@NingZhang-e
Copy link
Contributor

Which Javassist version are you using?
Seems same as #228
It has been fixed in latest version.

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