Here you can find the source of getSetterMethod(final Class> clazz, final Class extends Annotation> annotation, final String name, final String value)
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 |
Parameter | Description |
---|---|
IllegalAccessException | can throw illegal access exception |
IllegalArgumentException | can throw illegal argument exception |
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
//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; } }