
pl.bristleback.server.bristle.utils.Getter Maven / Gradle / Ivy
/*
* Bristleback Websocket Framework - Copyright (c) 2010-2013 http://bristleback.pl
* ---------------------------------------------------------------------------
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
* This library is distributed in the hope that it will be useful,
* but without any warranty; without even the implied warranty of merchantability
* or fitness for a particular purpose.
* You should have received a copy of the GNU Lesser General Public License along
* with this program; if not, see .
* ---------------------------------------------------------------------------
*/
package pl.bristleback.server.bristle.utils;
import pl.bristleback.server.bristle.BristleRuntimeException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
/**
* //@todo class description
*
* Created on: 2011-09-02 12:30:11
*
* @author Wojciech Niemiec
*/
public class Getter {
private String fieldName;
private Method getterMethod;
/**
* Creates a new Getter object with complete information.
*
* @param fieldName name of the field.
* @param getterMethod getter method.
*/
public Getter(String fieldName, Method getterMethod) {
this.fieldName = fieldName;
this.getterMethod = getterMethod;
}
/**
* Invokes getter method on the object given as parameter.
* Any exception while invoking method will cause {@link BristleRuntimeException} to throw.
*
* @param bean the object the underlying method is invoked from.
* @return the result of invoking getter method.
*/
public Object invoke(Object bean) {
try {
return getterMethod.invoke(bean);
} catch (IllegalAccessException e) {
throw new BristleRuntimeException(e.getMessage(), e);
} catch (InvocationTargetException e) {
throw new BristleRuntimeException(e.getMessage(), e);
}
}
/**
* Invokes getter method on the object given as parameter.
* Any exception while invoking method will cause {@link BristleRuntimeException} to throw.
*
* @param bean the object the underlying method is invoked from.
* @return the result of invoking getter method.
* @throws Exception exceptions related with invoking method using reflection.
*/
public Object invokeWithoutCheck(Object bean) throws Exception {
return getterMethod.invoke(bean);
}
/**
* Type of object returned by getter method.
*
* @return type of object returned by getter method.
*/
public Class getReturnType() {
return getterMethod.getReturnType();
}
/**
* Name of the returned field name.
*
* @return name of the returned field name.
*/
public String getFieldName() {
return fieldName;
}
/**
* Getter method.
*
* @return getter method.
*/
public Method getGetterMethod() {
return getterMethod;
}
public Type getGenericReturnType() {
return getterMethod.getGenericReturnType();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy