Identification of accessories and mutators (getters / setters) of all data types of IType type

I can access all methods ITypeusing the method getMethods(). Is there an effective way to determine if such an IMethodaccessor or mutator (getter / setter)?

Checking that the name IMethodmatches the schema prefix + NameOfAttributewith the help prefix ∈ {"get", "set", "is"}will help me find the obvious ones, but if the accessor or mutator (getter / setter) is not named that way, this will not work.

Is there a better way?

EDIT . I only want to define getter / setter methods that directly get / set the attribute ITypeand do nothing.

EDIT2 : Used technical terms: accessor and mutator

EDIT3 : here is my solution after reading all the answers:

    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
    }

IMPORTANT: This solution meets my requirements, but ignores some important cases (see accepted answer ) . This still does not test the functionality of the method, but it works very well. It checks the method name for the scheme I proposed. But it also checks the number of parameters and return type voidor not. If someone wanted to improve this, he could also check to see if the return / parameter type of the getter matched the type of the field that matches the method name.

+4
source share
2 answers

, JDT.

JDT API, / . . NamingConvention suggestGetterName().

, GetterSetterUtil, .

, . , , Collection s.

, IMethod + NameOfAttribute ∈ { "get", "set" }

is . Getters , . , JavaBeans . .

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

.

. , . . , visible 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;
}

.

+2

:

for (IMethod m : iType.getMethods()) {
    if (m.getElementName().substring(0,3).equals("get")) {
        //do something
    } else if (m.getElementName().substring(0,3).equals("set")) {
        //do something else
    }
}

getFields() IType, getElementName() .

+1

Source: https://habr.com/ru/post/1667244/


All Articles