第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用受保護(hù)的類進(jìn)行類型轉(zhuǎn)換

使用受保護(hù)的類進(jìn)行類型轉(zhuǎn)換

幕布斯7119047 2021-10-28 15:45:14
我正在嘗試覆蓋某類 vertx web 項(xiàng)目,因?yàn)槲冶仨毟哪承┕δ?。所以棘手的部分來了?nbsp; @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();  }這段代碼給我一個(gè)編譯錯(cuò)誤說:'HttpServerRequestWrapper cannot be accessed from outside package'我知道我們可以使用反射來創(chuàng)建無法訪問的類的對象。在這種情況下可以使用反射嗎?我該如何解決這樣的問題。任何幫助都感激不盡。
查看完整描述

3 回答

?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊

在 java 8 和/或沒有模塊的情況下,可以將類似的類放在與原始包相同的包中以訪問所有包默認(rèn)類。

否則,你需要在其他響應(yīng)使用反射像,但我想補(bǔ)充一點(diǎn),這是好主意,緩存ClassMethod實(shí)例,如使用Class.forNameclazz.getDeclaredMethod每次都會(huì)放緩代碼。


查看完整回答
反對 回復(fù) 2021-10-28
?
撒科打諢

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊

獲取Class對象然后調(diào)用特定(未轉(zhuǎn)換)對象上的方法怎么樣?


我假設(shè)request是 type 的類屬性HttpServerRequestWrapper。然后,這就是我的建議:


import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;


...


private final Method setMethod;

private final Method setPath;


public MyConstructor() {

    Method tmp1 = null, tmp2 = null;

    try {

        final Class<?> clazz = Class.forName("io.vertx.ext.web.impl.HttpServerRequestWrapper");

        tmp1 = clazz.getMethod("setMethod", HttpMethod.class);

        tmp1.setAccessible(true);

        tmp2 = clazz.getMethod("setPath", String.class);

        tmp2.setAccessible(true);

    } catch (ClassNotFoundException e) {

        // do something

    } catch (NoSuchMethodException e) {

        // do something

    } catch (SecurityException e) {

        // do something

    }

    this.setMethod = tmp1;

    this.setPath = tmp2;

}

...


@Override

public void reroute(HttpMethod method, String path) {

    ...

    try {

        this.setMethod.invoke(request, method);

        this.setPath.invoke(request, path);

    } catch (IllegalAccessException e) {

        // do something

    } catch (IllegalArgumentException e) {

        // do something

    } catch (InvocationTargetException e) {

        // do something

    }

    ...

}

編輯:我根據(jù)@GotoFinal 的建議更新了這個(gè)答案。


查看完整回答
反對 回復(fù) 2021-10-28
  • 3 回答
  • 0 關(guān)注
  • 241 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)