
jp.go.nict.langrid.commons.lang.ClassUtil 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!
/*
* Copyright (C) 2002, 2004 Takao Nakaguchi.
*
* This is a program for Language Grid Core Node. This combines multiple language resources and provides composite language services.
* Copyright (C) 2005-2008 NICT Language Grid Project.
* Copyright (C) 2014 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.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import jp.go.nict.langrid.commons.util.Pair;
import jp.go.nict.langrid.commons.util.function.Optional;
/**
*
*
* @author Takao Nakaguchi
*/
public class ClassUtil {
public static Object newInstance(Class> clazz)
throws InstantiationException, IllegalAccessException{
if(clazz.isPrimitive()){
return primitiveToDefault.get(clazz);
} else if(Calendar.class.isAssignableFrom(clazz)){
return Calendar.getInstance();
} else if(clazz.isArray()){
Class> ct = clazz.getComponentType();
Object a = Array.newInstance(ct, 1);
Array.set(a, 0, newInstance(ct));
return a;
}
return clazz.newInstance();
}
@SuppressWarnings("rawtypes")
public static Object newDummyInstance(Class> clazz)
throws InstantiationException, IllegalAccessException{
if(clazz.isPrimitive()){
return primitiveToDefault.get(clazz);
} else if(Calendar.class.isAssignableFrom(clazz)){
return Calendar.getInstance();
} else if(clazz.isArray()){
Class> ct = clazz.getComponentType();
Object a = Array.newInstance(ct, 1);
Array.set(a, 0, newDummyInstance(ct));
return a;
} else if(clazz.isAssignableFrom(ArrayList.class)){
return new ArrayList();
} else if(clazz.isAssignableFrom(HashSet.class)){
return new HashSet();
} else if(clazz.isAssignableFrom(HashMap.class)){
return new HashMap();
}
Object ret = clazz.newInstance();
ObjectUtil.padProperties(ret);
return ret;
}
public static Collection> getReadableProperties(Class> clazz){
List> ret = new ArrayList>();
for(Method m : clazz.getMethods()){
if(m.getReturnType().equals(void.class)) continue;
if(m.getParameterTypes().length != 0) continue;
String name = m.getName();
if(name.startsWith("get")){
if(name.length() == 3) continue;
if(m.getDeclaringClass().equals(Object.class)) continue;
ret.add(Pair.create(name.substring(3, 4).toLowerCase() + (name.length() > 4 ? name.substring(4) : ""), m));
} else if(name.startsWith("is")){
if(name.length() == 2) continue;
if(m.getDeclaringClass().equals(Object.class)) continue;
if(!m.getReturnType().equals(boolean.class)) continue;
ret.add(Pair.create(name.substring(2, 3).toLowerCase() + (name.length() > 3 ? name.substring(3) : ""), m));
}
}
return ret;
}
public static Method findGetter(Class> clazz, String name)
throws SecurityException, NoSuchMethodException{
{
String getterName = "get" + name.substring(0).toUpperCase() +
((name.length() > 1) ? name.substring(1) : "");
Method m = clazz.getMethod(getterName);
if(m != null){
if(!m.getReturnType().equals(void.class)) return m;
return null;
}
}
{
String getterName = "is" + name.substring(0).toUpperCase() +
((name.length() > 1) ? name.substring(1) : "");
Method m = clazz.getMethod(getterName);
if(m != null){
if(m.getReturnType().equals(boolean.class)) return m;
return null;
}
}
return null;
}
public static Method findSetter(Method getter){
String n = getter.getName();
if(!n.startsWith("get")) return null;
if(n.length() <= 3) return null;
try {
return getter.getDeclaringClass().getMethod("set" + n.substring(3), getter.getReturnType());
} catch (NoSuchMethodException e) {
return null;
} catch (SecurityException e) {
return null;
}
}
public static Method findSetter(Class> clazz, String property){
String setterName = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
for(Method m : clazz.getMethods()){
if(!m.getReturnType().equals(void.class)) continue;
if(m.getParameterTypes().length != 1) continue;
if(m.getName().equals(setterName)) return m;
}
return null;
}
public static Iterable findSetters(Class> clazz, String property){
List ret = new ArrayList();
String setterName = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
for(Method m : clazz.getMethods()){
if(!m.getName().equals(setterName)) continue;
if(!m.getReturnType().equals(void.class)) continue;
if(m.getParameterTypes().length != 1) continue;
ret.add(m);
}
return ret;
}
public static Method findMethod(Class> clazz, String methodName){
for(Method m : clazz.getMethods()){
if(!m.getName().equals(methodName)) continue;
return m;
}
return null;
}
public static Method findMethod(Class> clazz, String methodName, int paramCount){
for(Method m : clazz.getMethods()){
if(!m.getName().equals(methodName)) continue;
Class>[] t = m.getParameterTypes();
if(t.length != paramCount) continue;
return m;
}
return null;
}
public static Object getDefaultValueForPrimitive(Class> type){
return primitiveToDefault.get(type);
}
public static Object getRandomValueForPrimitive(Class> type){
Callable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy