Here you can find the source of getSetter(Class clazz, String name)
Parameter | Description |
---|---|
clazz | the class enclosing the setter. |
name | the name of the setter to look for. |
Method
object, or null if the class has no setter with the specfied name.
public static Method getSetter(Class clazz, String name)
//package com.java2s; /*/*w w w . ja v a 2 s . c om*/ * JPPF. * Copyright (C) 2005-2010 JPPF Team. * http://www.jppf.org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.lang.reflect.*; public class Main { /** * Get a setter with a given name from a class. * @param clazz the class enclosing the setter. * @param name the name of the setter to look for. * @return a <code>Method</code> object, or null if the class has no setter with the specfied name. */ public static Method getSetter(Class clazz, String name) { Method[] methods = clazz.getMethods(); Method setter = null; for (int i = 0; i < methods.length; i++) { if (isSetter(methods[i]) && name.equals(methods[i].getName())) { setter = methods[i]; break; } } return setter; } /** * Determines whether a method is a setter (mutator), according to Sun's naming conventions. * @param meth the method to analyse. * @return true if the method is a setter, false otherwise. */ public static boolean isSetter(Method meth) { Class type = meth.getReturnType(); if (!Void.TYPE.equals(type)) return false; if (!meth.getName().startsWith("set")) return false; Class[] paramTypes = meth.getParameterTypes(); if ((paramTypes == null) || (paramTypes.length != 1)) return false; return true; } }