1 /* 2 * Copyright (c) 2016 Vivid Solutions. 3 * 4 * All rights reserved. This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * and Eclipse Distribution License v. 1.0 which accompanies this distribution. 7 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html 8 * and the Eclipse Distribution License is available at 9 * 10 * http://www.eclipse.org/org/documents/edl-v10.php. 11 */ 12 13 package org.locationtech.jtstest.util; 14 15 import java.lang.reflect.Constructor; 16 import java.lang.reflect.Field; 17 import java.lang.reflect.InvocationTargetException; 18 import java.lang.reflect.Method; 19 20 import org.locationtech.jts.geom.Geometry; 21 22 public class ClassUtil 23 { getClassname(Class clz)24 public static String getClassname(Class clz) 25 { 26 String jClassName = clz.getName(); 27 int lastDotPos = jClassName.lastIndexOf("."); 28 return jClassName.substring(lastDotPos + 1, jClassName.length()); 29 } 30 getStringArrayClassField(Class clz, String name)31 public static String[] getStringArrayClassField(Class clz, String name) 32 { 33 try { 34 Field field = clz.getField(name); 35 String[] str = (String[]) field.get(null); 36 return str; 37 } 38 catch (NoSuchFieldException ex) { } 39 catch (IllegalAccessException ex) { } 40 return null; 41 } 42 getStringClassField(Class clz, String name)43 public static String getStringClassField(Class clz, String name) 44 { 45 try { 46 Field[] f = clz.getDeclaredFields(); 47 Field field = clz.getField(name); 48 String str = (String) field.get(null); 49 return str; 50 } 51 catch (NoSuchFieldException ex) { } 52 catch (IllegalAccessException ex) { } 53 return null; 54 } 55 dynamicCall(String clzName, String methodName, Class[] methodParamTypes, Object[] methodArgs)56 public static Object dynamicCall(String clzName, String methodName, Class[] methodParamTypes, Object[] methodArgs) 57 throws ClassNotFoundException, SecurityException, NoSuchMethodException, 58 IllegalArgumentException, InstantiationException, IllegalAccessException, 59 InvocationTargetException 60 { 61 Class clz = Class.forName(clzName); 62 63 Class[] constParTypes = new Class[] { String.class, String.class }; 64 Constructor constr = clz.getConstructor(new Class[0]); 65 Object dummyto = constr.newInstance(new Object[0]); 66 67 Method meth = clz.getMethod(methodName, methodParamTypes); 68 Object result = meth.invoke(dummyto, methodArgs); 69 return result; 70 } 71 isNumber(Class clz)72 public static boolean isNumber(Class clz) { 73 if (clz == Double.class) return true; 74 if (clz == double.class) return true; 75 if (clz == Integer.class) return true; 76 if (clz == int.class) return true; 77 return false; 78 } 79 80 /** 81 * Converts a number-like object to a Double. 82 * If the object cannot be converted null is returned. 83 * 84 * @param o a number-like object 85 * @return the value of the number, or null 86 */ toDouble(Object o)87 public static Double toDouble(Object o) { 88 double d = 0; 89 try { 90 // get Java to figure out the conversion to double 91 d = (Double) o; 92 // d = ((Number) o).doubleValue(); 93 } catch (ClassCastException e) { 94 return null; 95 } 96 return d; 97 } 98 isDouble(Class clz)99 public static boolean isDouble(Class clz) { 100 if (clz == Double.class) return true; 101 if (clz == double.class) return true; 102 return false; 103 } 104 isGeometry(Class<?> clz)105 public static boolean isGeometry(Class<?> clz) { 106 return Geometry.class.isAssignableFrom(clz); 107 } 108 109 } 110