簡體   English   中英

java反射:是否可以通過更改方法ID覆蓋私有方法?

[英]java reflection: Is it possible to override a private method by change method Id?

有一個.jar文件,我需要在.class文件中更改一個私有方法實現。 下面的方法

private void vibrate() {
    Vibrator vibrator = (Vibrator)RongContext.getInstance().getSystemService("vibrator");
    vibrator.vibrate(new long[]{0L, 200L, 250L, 200L}, -1);
}

此私有方法由下面的另一個私有方法調用

private void notify(Context context, Message message, int left) {
    boolean isInBackground = SystemUtils.isInBackground(context);
    if(message.getConversationType() != ConversationType.CHATROOM) {
        if(isInBackground) {
            RongNotificationManager.getInstance().onReceiveMessageFromApp(message);
        } else if(!this.isInConversationPager(message.getTargetId(), message.getConversationType()) && left == 0 && System.currentTimeMillis() - this.lastSoundTime > 3000L) {
            this.lastSoundTime = System.currentTimeMillis();
            int ringerMode = NotificationUtil.getRingerMode(context);
            if(ringerMode != 0) {
                if(ringerMode != 1) {
                    this.sound();
                }

                this.vibrate();
            }
        }

    }
}

我需要用一個if塊包圍它

private void vibrate() {
    if(xxx){
        Vibrator vibrator = (Vibrator)RongContext.getInstance().getSystemService("vibrator");
        vibrator.vibrate(new long[]{0L, 200L, 250L, 200L}, -1);
    }
}

所以我只是想知道是否有必要更改私有方法的實現,例如使用ClassLoader更改方法ID或其他。

用Java反射無法做到這一點。

您可以使用AOP或CGlib或Javassist之類的字節碼操作庫來完成此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM