
jp.go.nict.langrid.commons.lang.ObjectUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jp.go.nict.langrid.commons Show documentation
Show all versions of jp.go.nict.langrid.commons Show documentation
Common and utility library for the Service Grid Server Software and java web services.
The newest version!
/*
* This is a program for Language Grid Core Node. This combines multiple language resources and provides composite language services.
* Copyright (C) 2005-2012 NICT Language Grid Project.
* Copyright (C) 2013 Language Grid Project.
*
* 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 2.1 of the License, or (at
* your option) any later version.
*
* This program 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. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package jp.go.nict.langrid.commons.lang;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import jp.go.nict.langrid.commons.lang.reflect.MethodUtil;
import jp.go.nict.langrid.commons.transformer.TransformationException;
import jp.go.nict.langrid.commons.transformer.Transformer;
import jp.go.nict.langrid.commons.util.ArrayUtil;
/**
*
* @author Takao Nakaguchi
*/
public class ObjectUtil {
@SuppressWarnings("unchecked")
public static T cast(Object object){
return (T)object;
}
@SuppressWarnings("unchecked")
public static T getProperty(Object instance, String name)
throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
return (T)ClassUtil.findGetter(instance.getClass(), name).invoke(instance);
}
public static void setProperty(Object instance, String name, Object value)
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
ClassUtil.findSetter(instance.getClass(), name).invoke(instance, value);
}
public static void padProperties(Object object){
padProperties(object, new HashSet>(), 0);
}
private static void padProperties(Object object, Set> classes, int nest){
Class> c = object.getClass();
if(classes.contains(c) && nest > 16) return;
classes.add(c);
for(Method m : c.getMethods()){
if(!MethodUtil.isGetter(m)) continue;
Method s = ClassUtil.findSetter(m);
if(s == null) return;
try {
s.invoke(object, ClassUtil.newDummyInstance(m.getReturnType()));
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException e) {
} catch (InvocationTargetException e) {
}
}
}
/**
*
*
*/
public static int getSize(Object... objects){
int size = 0;
for(Object o : objects){
size += o.toString().length();
}
return size;
}
/**
*
*
*/
public static Object invoke(Object instance, String methodName
, Class>[] parameterTypes, Object[] parameters)
throws IllegalAccessException, InvocationTargetException
, NoSuchMethodException
{
Method m = instance.getClass().getMethod(methodName, parameterTypes);
return m.invoke(instance, parameters);
}
/**
*
*
*/
public static Object invoke(Class> clazz, String methodName
, Object... parameters)
throws IllegalAccessException, InvocationTargetException
, NoSuchMethodException
{
return doInvoke(clazz, null, methodName, parameters);
}
/**
*
*
*/
public static Object invoke(Object instance, String methodName
, Object... parameters)
throws IllegalAccessException, InvocationTargetException
, NoSuchMethodException
{
return doInvoke(instance.getClass(), instance, methodName, parameters);
}
public static InvocationStream invocationStream(Object value){
return new InvocationStream(value);
}
@SuppressWarnings("rawtypes")
private static Object doInvoke(Class extends T> clazz, T instance, String methodName
, Object... parameters)
throws IllegalAccessException, InvocationTargetException
, NoSuchMethodException
{
Class>[] parameterTypes = ArrayUtil.collect(
parameters, Class.class, new Transformer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy