Java Reflection Method Setter Get getSetterMethod(final Class clazz, final Class annotation, final String name, final String value)

Here you can find the source of getSetterMethod(final Class clazz, final Class annotation, final String name, final String value)

Description

Return the setter method using the specified annotation attribute and value.

License

Mozilla Public License

Parameter

Parameter Description
clazz the class to investigate
annotation the annotation class
name the annotation method name to check when annotation exists
value the query value too match against annotation method value or class name

Exception

Parameter Description
IllegalAccessExceptioncan throw illegal access exception
IllegalArgumentException can throw illegal argument exception
InvocationTargetException can throw invocation target exception

Return

return the found method, or null when not found

Declaration

public static Method getSetterMethod(final Class<?> clazz, final Class<? extends Annotation> annotation,
        final String name, final String value)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException 

Method Source Code

//package com.java2s;
/*//  ww  w . ja  v a2 s .  com
 * Copyright (c) 2015 Jeremy Miller
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * Return the setter method using the specified annotation attribute and value. Duplicates
     * will cause an IllegalstateException. If non can be found the method name will be check as
     * possible match.
     * @param clazz                        the class to investigate
     * @param annotation                   the annotation class
     * @param name                         the annotation method name to check when annotation exists
     * @param value                        the query value too match against annotation method value or class name
     * @return                             return the found method, or null when not found
     * @throws IllegalAccessException      can throw illegal access exception
     * @throws IllegalArgumentException    can throw illegal argument exception
     * @throws InvocationTargetException   can throw invocation target exception
     */
    public static Method getSetterMethod(final Class<?> clazz, final Class<? extends Annotation> annotation,
            final String name, final String value)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {

        Method setterMethod = null;

        for (Method possibleMethod : clazz.getMethods()) {
            if (possibleMethod.isAnnotationPresent(annotation)) {
                Annotation[] methodAnnotations = possibleMethod.getDeclaredAnnotations();

                for (Annotation methodAnnotation : methodAnnotations) {
                    if (annotation.isInstance(methodAnnotation)) {
                        final Class<? extends Annotation> type = methodAnnotation.annotationType();

                        for (Method annotationMethod : type.getDeclaredMethods()) {
                            if (annotationMethod.getName().equals(name)
                                    && annotationMethod.getParameterTypes().length == 0) {
                                Object attributeValue = annotationMethod.invoke(methodAnnotation);

                                if (attributeValue != null && attributeValue.equals(value)) {
                                    if (setterMethod != null) {
                                        throw new IllegalStateException("Conflict with value (" + name + "=" + value
                                                + " ) between methods: " + setterMethod.getName() + ", "
                                                + possibleMethod.getName());
                                    }

                                    setterMethod = possibleMethod;
                                }
                            }
                        }
                    }

                }
            }

            // check name is not the same
            if (possibleMethod.getParameterTypes().length == 1) {
                final String methodName = possibleMethod.getName();

                if (methodName.equalsIgnoreCase(value) || methodName.equalsIgnoreCase("set" + value)) {
                    if (setterMethod != null) {
                        throw new IllegalStateException(
                                "Conflict with value (" + name + "=" + value + " ) between methods: "
                                        + setterMethod.getName() + ", " + possibleMethod.getName());
                    }

                    setterMethod = possibleMethod;
                }
            }
        }

        return setterMethod;
    }

    /**
     * Return all method(s) of a specified class that contain the specified annotation.
     * @param clazz                    the class to interrogate
     * @param annotation               the annotation to find
     * @return                         return the list of methods found (an empty list is possible)
     */
    public static List<Method> getMethods(final Class<?> clazz, final Class<? extends Annotation> annotation) {
        final List<Method> annotatedMethods = new LinkedList<Method>();

        for (Method method : clazz.getMethods()) {
            if (method.isAnnotationPresent(annotation)) {
                annotatedMethods.add(method);
            }
        }

        return annotatedMethods;
    }
}

Related

  1. getSetterMethod(Class beanClass, String property)
  2. getSetterMethod(Class classType, String fieldName, Class paramType)
  3. getSetterMethod(Class clazz, Field field)
  4. getSetterMethod(Class clazz, String name)
  5. getSetterMethod(Class cls, String property, Class valueCls)
  6. getSetterMethod(final Class clazz, final String propertyName, final Class type)
  7. getSetterMethod(final String methodName, final Class pojoClass, final Class attributeClass)
  8. getSetterMethod(Object obj, String name)
  9. getSetterMethod(String getterName, Class returnType, Class containingClass)