猿问

使用受保护的类进行类型转换

我正在尝试覆盖某类 vertx web 项目,因为我必须更改某些功能。所以棘手的部分来了。


  @Override

  public void reroute(HttpMethod method, String path) {

    int split = path.indexOf('?');


    if (split == -1) {

      split = path.indexOf('#');

    }


    if (split != -1) {

      log.warn("Non path segment is not considered: " + path.substring(split));

      // reroute is path based so we trim out the non url path parts

      path = path.substring(0, split);

    }


    /*((HttpServerRequestWrapper) request).setMethod(method);

    ((HttpServerRequestWrapper) request).setPath(path);*/

    ((HttpServerRequestWrapper) request).setMethod(method);

    ((HttpServerRequestWrapper) request).setPath(path);

    request.params().clear();

    // we need to reset the normalized path

    normalisedPath = null;

    // we also need to reset any previous status

    statusCode = -1;

    // we need to reset any response headers

    response().headers().clear();

    // special header case cookies are parsed and cached

    if (cookies != null) {

      cookies.clear();

    }

    // reset the end handlers

    if (headersEndHandlers != null) {

      headersEndHandlers.clear();

    }

    if (bodyEndHandlers != null) {

      bodyEndHandlers.clear();

    }


    failure = null;

    restart();

  }

这段代码给我一个编译错误说:


'HttpServerRequestWrapper cannot be accessed from outside package'

我知道我们可以使用反射来创建无法访问的类的对象。在这种情况下可以使用反射吗?我该如何解决这样的问题。


任何帮助都感激不尽。


幕布斯7119047
浏览 202回答 3
3回答

大话西游666

在 java 8 和/或没有模块的情况下,可以将类似的类放在与原始包相同的包中以访问所有包默认类。否则,你需要在其他响应使用反射像,但我想补充一点,这是好主意,缓存Class和Method实例,如使用Class.forName和clazz.getDeclaredMethod每次都会放缓代码。

撒科打诨

获取Class对象然后调用特定(未转换)对象上的方法怎么样?我假设request是 type 的类属性HttpServerRequestWrapper。然后,这就是我的建议:import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;...private final Method setMethod;private final Method setPath;public MyConstructor() {&nbsp; &nbsp; Method tmp1 = null, tmp2 = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; final Class<?> clazz = Class.forName("io.vertx.ext.web.impl.HttpServerRequestWrapper");&nbsp; &nbsp; &nbsp; &nbsp; tmp1 = clazz.getMethod("setMethod", HttpMethod.class);&nbsp; &nbsp; &nbsp; &nbsp; tmp1.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; tmp2 = clazz.getMethod("setPath", String.class);&nbsp; &nbsp; &nbsp; &nbsp; tmp2.setAccessible(true);&nbsp; &nbsp; } catch (ClassNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; // do something&nbsp; &nbsp; } catch (NoSuchMethodException e) {&nbsp; &nbsp; &nbsp; &nbsp; // do something&nbsp; &nbsp; } catch (SecurityException e) {&nbsp; &nbsp; &nbsp; &nbsp; // do something&nbsp; &nbsp; }&nbsp; &nbsp; this.setMethod = tmp1;&nbsp; &nbsp; this.setPath = tmp2;}...@Overridepublic void reroute(HttpMethod method, String path) {&nbsp; &nbsp; ...&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; this.setMethod.invoke(request, method);&nbsp; &nbsp; &nbsp; &nbsp; this.setPath.invoke(request, path);&nbsp; &nbsp; } catch (IllegalAccessException e) {&nbsp; &nbsp; &nbsp; &nbsp; // do something&nbsp; &nbsp; } catch (IllegalArgumentException e) {&nbsp; &nbsp; &nbsp; &nbsp; // do something&nbsp; &nbsp; } catch (InvocationTargetException e) {&nbsp; &nbsp; &nbsp; &nbsp; // do something&nbsp; &nbsp; }&nbsp; &nbsp; ...}编辑:我根据@GotoFinal 的建议更新了这个答案。
随时随地看视频慕课网APP

相关分类

Java
我要回答