确定方法是否在运行时覆盖另一个方法

编程入门 行业动态 更新时间:2024-10-27 18:29:19
本文介绍了确定方法是否在运行时覆盖另一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道是否有办法确定给定 java.lang.Method 对象所代表的方法是否会覆盖另一个表示的另一个方法java.lang.Method object?

I was wondering if there was any way to determine if a method represented by given java.lang.Method object overrides another methods represented by another java.lang.Method object?

我正在研究 Stronlgy类型的javascript ,我需要能够知道一个方法是否覆盖了另一个方法,以便能够将它们重命名为更短的名称。

I'm working on Stronlgy typed javascript, and I need to be able to be able to know if a method overrides another one in order to be able to rename both of them to a shorter name.

在这种情况下,我说的是覆盖的扩展定义,由 @Override 注释支持,其中包括接口和抽象的实现类方法。

In this case, I am talking about the extended definition of overriding, as supported by the @Override annotation, which includes implementation of interface and abstract class methods.

我对任何涉及直接反射或使用任何已经执行此操作的库的解决方案感到满意。

I'd be happy with any solution involving either reflection directly, or using any library that already does this.

推荐答案

您可以简单地交叉检查方法名称和签名。

You can simply cross-check method names and signatures.

public static boolean isOverriden(Method parent, Method toCheck) { if (parent.getDeclaringClass().isAssignableFrom(toCheck.getDeclaringClass()) && parent.getName().equals(toCheck.getName())) { Class<?>[] params1 = parent.getParameterTypes(); Class<?>[] params2 = toCheck.getParameterTypes(); if (params1.length == params2.length) { for (int i = 0; i < params1.length; i++) { if (!params1[i].equals(params2[i])) { return false; } } return true; } } return false; }

但是,由于您的目标是重命名方法,您可能希望使用字节码分析/操作库,例如 ASM ,您可以在其中执行相同的测试以及轻松修改方法'如果方法返回true,则命名。

However, since your goal is to rename methods, you might instead wish to use a bytecode analysis/manipulation library such as ASM, where you can perform the same tests as well as easily modify the methods' names if the method returns true.

更多推荐

确定方法是否在运行时覆盖另一个方法

本文发布于:2023-11-29 00:07:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644503.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!