Description
Gets the setter.
License
Open Source License
Parameter
Parameter | Description |
---|
A | the generic type |
instance | the instance |
strAttributeName | the str attribute name |
clazz | the clazz |
Exception
Parameter | Description |
---|
SecurityException | the security exception |
NoSuchMethodException | the no such method exception |
Return
the setter
Declaration
public static <A> Method getSetter(A instance, String strAttributeName, Class<?> clazz)
throws SecurityException, NoSuchMethodException
Method Source Code
//package com.java2s;
/*/* w ww .java2 s.c o m*/
* Copyright (c) 2002-2013, Mairie de Paris
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice
* and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice
* and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* License 1.0
*/
import java.lang.reflect.Method;
public class Main {
private static final String PREFIX_SET = "set";
/**
* Gets the setter.
*
* @param <A> the generic type
* @param instance the instance
* @param strAttributeName the str attribute name
* @param clazz the clazz
* @return the setter
* @throws SecurityException the security exception
* @throws NoSuchMethodException the no such method exception
*/
public static <A> Method getSetter(A instance, String strAttributeName, Class<?> clazz)
throws SecurityException, NoSuchMethodException {
return getMethod(PREFIX_SET, instance, strAttributeName, clazz);
}
/**
* Gets the method.
*
* @param <A> the generic type of the instance
* @param strMethodPrefix the str method prefix
* @param instance the instance
* @param strAttributeName the str attribute name
* @param clazz the clazz
* @return the method
* @throws SecurityException the security exception
* @throws NoSuchMethodException the no such method exception
*/
public static <A> Method getMethod(String strMethodPrefix, A instance, String strAttributeName, Class<?> clazz)
throws SecurityException, NoSuchMethodException {
String strFirstLetter = strAttributeName.substring(0, 1).toUpperCase();
String strMethodName = strMethodPrefix + strFirstLetter
+ strAttributeName.substring(1, strAttributeName.length());
try {
return instance.getClass().getMethod(strMethodName, new Class[] { clazz });
} catch (NoSuchMethodException e) {
return getPrimitiveMethod(strMethodName, instance, clazz);
}
}
/**
* Gets the primitive method.
*
* @param <A> the generic type of the instance
* @param strMethodName the str method name
* @param instance the instance
* @param clazz the clazz
* @return the primitive method
* @throws SecurityException the security exception
* @throws NoSuchMethodException the no such method exception
*/
public static <A> Method getPrimitiveMethod(String strMethodName, A instance, Class<?> clazz)
throws SecurityException, NoSuchMethodException {
if (clazz.equals(Integer.class)) {
return instance.getClass().getMethod(strMethodName, new Class[] { int.class });
} else if (clazz.equals(Long.class)) {
return instance.getClass().getMethod(strMethodName, new Class[] { long.class });
} else if (clazz.equals(Double.class)) {
return instance.getClass().getMethod(strMethodName, new Class[] { double.class });
} else if (clazz.equals(Short.class)) {
return instance.getClass().getMethod(strMethodName, new Class[] { short.class });
} else if (clazz.equals(Byte.class)) {
return instance.getClass().getMethod(strMethodName, new Class[] { byte.class });
} else if (clazz.equals(Float.class)) {
return instance.getClass().getMethod(strMethodName, new Class[] { float.class });
} else if (clazz.equals(Character.class)) {
return instance.getClass().getMethod(strMethodName, new Class[] { char.class });
} else if (clazz.equals(Boolean.class)) {
return instance.getClass().getMethod(strMethodName, new Class[] { boolean.class });
}
throw new NoSuchMethodException();
}
}
Related
- getSetter(Class _class, String fieldName)
- getSetter(Class clazz, String name)
- getSetter(Class> clazz, Field field)
- getSetter(Class> clazz, Field field)