Here you can find the source of getSettersForType(Class clazz, String typeName)
Parameter | Description |
---|---|
clazz | The class to find setter methods on |
typeName | The name of the type to find setters for |
public static Method[] getSettersForType(Class clazz, String typeName)
//package com.java2s; /******************************************************************************* * Copyright SemanticBits, Northwestern University and Akaza Research * /*from w w w . j a va 2 s .c om*/ * Distributed under the OSI-approved BSD 3-Clause License. * See http://ncip.github.com/caaers/LICENSE.txt for details. ******************************************************************************/ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.HashSet; import java.util.Set; public class Main { /** * Gets the setter methods of a class that take a parameter of a certain type * * @param clazz * The class to find setter methods on * @param typeName * The name of the type to find setters for * @return * All setter methods which take a parameter of the named type */ public static Method[] getSettersForType(Class clazz, String typeName) { Set allMethods = new HashSet(); Class checkClass = clazz; while (checkClass != null) { Method[] classMethods = checkClass.getDeclaredMethods(); for (int i = 0; i < classMethods.length; i++) { Method current = classMethods[i]; if (current.getName().startsWith("set")) { if (Modifier.isPublic(current.getModifiers())) { Class[] paramTypes = current.getParameterTypes(); if (paramTypes.length == 1) { if (paramTypes[0].getName().equals(typeName)) { allMethods.add(current); } } } } } checkClass = checkClass.getSuperclass(); } Method[] methodArray = new Method[allMethods.size()]; allMethods.toArray(methodArray); return methodArray; } }