gpt4 book ai didi

java - 识别 IType 的所有 IMethods 的访问器和修改器(getters/setters)

转载 作者:搜寻专家 更新时间:2023-11-01 02:59:02 26 4
gpt4 key购买 nike

我可以使用 getMethods() 方法访问 IType 的所有方法。是否有一种有效的方法来确定这样的 IMethod 是访问器还是修改器(getter/setter)?

检查 IMethod 的名称是否匹配方案 prefix + NameOfAttribute with prefix ∈ {"get", "set", "is"} 会帮助我检测明显的那些,但如果访问器或修改器(getter/setter)不是这样命名的,它就不会工作。

有没有更好的办法?

编辑:我只想识别直接获取/设置 IType 属性的 getter/setter 方法,不做任何其他事情。

EDIT2:使用的技术术语:accessor & mutator

EDIT3:这是我阅读所有答案后的解决方案:

    private boolean isAccessor(IMethod method) throws JavaModelException {
if (isAccessMethod("get", method) || isAccessMethod("is", method)) { // if name fits
return method.getNumberOfParameters() == 0 && !Signature.SIG_VOID.equals(method.getReturnType());
}
return false;
}

private boolean isMutator(IMethod method) throws JavaModelException {
if (isAccessMethod("set", method)) { // if name fits
return method.getNumberOfParameters() == 1 && Signature.SIG_VOID.equals(method.getReturnType());
}
return false;
}

private boolean isAccessMethod(String prefix, IMethod method) throws JavaModelException {
IType type = method.getDeclaringType();
for (IField field : type.getFields()) { // for ever field of IType:
if (method.getElementName().equalsIgnoreCase(prefix + field.getElementName())) {
return true; // is access method if name scheme fits for one field
}
}
return false; // is not an access method if no field fits
}

重要提示:此解决方案符合我的要求,但忽略了一些重要情况(请参阅 accepted answer)。这仍然不检查该方法的功能,但它工作得很好。它检查我提出的方案的方法名称。但它还会检查参数计数以及返回类型是否为 void。如果有人想对此进行改进,他还可以检查 getter 的返回/参数类型是否与与方法名称匹配的字段类型相匹配。

最佳答案

我猜 JDT 核心不支持识别 getter 和 setter。

JDT 中只有一个公共(public) API 提供基于字段名称的 getter/setter 名称生成。例如。 NamingConvention 看看 suggestGetterName() 方法。

其他内部类,如 GetterSetterUtil也不要提供你想要的方法。

最后我会自己创建一个属性访问器检测器。也许作为一个过滤器,我可以在 Collection 上应用

Checking if the name of an IMethod matches the scheme prefix + NameOfAttribute with prefix ∈ {"get", "set"}

同时检查 is如果是 boolean 属性和方法的参数列表,则为前缀。 Getters 通常不带参数,而 setter 通常带一个参数。我说通常,因为 JavaBeans specification还允许索引属性。例如

 void setter(int index, PropertyType value); // indexed setter
PropertyType getter(int index); // indexed getter
void setter(PropertyType values[]); // array setter
PropertyType[] getter(); // array getter

编辑

I added my own solution based on your proposals to my post.

您的解决方案可能符合您的要求。请记住,属性可能没有直接的支持字段。属性的概念有点抽象。我的意思是,如果您有一个名为 visible 的属性和 setter/getter isVisible()该类可能没有字段 private boolean visible .

为了清楚起见,您应该查看属性 rootPane来自 JComponent .这绝对是一个属性,因为 Introspector 返回它。

BeanInfo jcomponentBeanInfo = Introspector.getBeanInfo(JComponent.class);

PropertyDescriptor[] propertyDescriptors = jcomponentBeanInfo.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
if("rootPane".equals(propertyDescriptor.getName())){
System.out.println("Found property rootPane");
break;
}
}

会打印出来

Found property rootPane

但如果您看一下实现,它不会使用直接的支持字段。

public JRootPane getRootPane() {
return SwingUtilities.getRootPane(this);
}

SwingUtilities.getRootPane

public static JRootPane getRootPane(Component c) {
if (c instanceof RootPaneContainer) {
return ((RootPaneContainer)c).getRootPane();
}
for( ; c != null; c = c.getParent()) {
if (c instanceof JRootPane) {
return (JRootPane)c;
}
}
return null;
}

我只是想更清楚地说明这一点。

关于java - 识别 IType 的所有 IMethods 的访问器和修改器(getters/setters),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41738462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com