-
-
Save testanull/86a214d3db3ea6be33796b8e74fb8da4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Source code recreated from a .class file by IntelliJ IDEA | |
// (powered by FernFlower decompiler) | |
// | |
package com.q1labs.core.shared.util; | |
import com.q1labs.frameworks.util.Base64; | |
import java.lang.reflect.Array; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import org.apache.commons.json.JSONObject; | |
import org.apache.commons.lang3.SerializationUtils; | |
public class ReflectionUtils { | |
public ReflectionUtils() { | |
} | |
public static <T> Object stringToObject(Class<T> type, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { | |
long i; | |
int i; | |
if (type.isPrimitive()) { | |
if (type.equals(Integer.TYPE)) { | |
if (param == null) { | |
param = "0"; | |
} | |
i = (new Double(param)).longValue(); | |
if (i > 2147483647L) { | |
i = 0L - (2147483647L - i); | |
} | |
return (int)i; | |
} else if (type.equals(Character.TYPE)) { | |
return param.charAt(0); | |
} else if (type.equals(Double.TYPE)) { | |
return new Double(param); | |
} else if (type.equals(Long.TYPE)) { | |
try { | |
return new Long(param); | |
} catch (NumberFormatException var11) { | |
return new Double(param); | |
} | |
} else if (type.equals(Float.TYPE)) { | |
return new Float(param); | |
} else if (type.equals(Byte.TYPE)) { | |
return new Byte(param); | |
} else if (type.equals(Short.TYPE)) { | |
i = (new Double(param)).intValue(); | |
if (i > 32767) { | |
i = 0 - (32767 - i); | |
} | |
return (short)i; | |
} else if (type.equals(Boolean.TYPE)) { | |
return Boolean.valueOf(param); | |
} else { | |
throw new RuntimeException("Unknown primitive type: " + type.getName()); | |
} | |
} else if (param != null && !param.equalsIgnoreCase("$_NULL_$")) { | |
if (type.equals(Short.class)) { | |
i = (new Double(param)).intValue(); | |
if (i > 32767) { | |
i = 0 - (32767 - i); | |
} | |
return (short)i; | |
} else if (type.equals(Integer.class)) { | |
i = (new Double(param)).longValue(); | |
if (i > 2147483647L) { | |
i = 0L - (2147483647L - i); | |
} | |
return (int)i; | |
} else if (type.equals(Long.class)) { | |
return (new Double(param)).longValue(); | |
} else if (type.equals(Date.class)) { | |
try { | |
return (new SimpleDateFormat("yyyy-MM-dd 00:00:00")).parse(param); | |
} catch (ParseException var12) { | |
throw new RuntimeException("Cannot parse Date type: " + param); | |
} | |
} else if (!type.equals(String.class) && !type.equals(Object.class)) { | |
Method fromString; | |
if (Enum.class.isAssignableFrom(type)) { | |
fromString = type.getMethod("valueOf", String.class); | |
return fromString.invoke((Object)null, param); | |
} else if (type.equals(byte[].class)) { | |
return param.getBytes(); | |
} else if (JSONObject.class.isAssignableFrom(type)) { | |
try { | |
return new JSONObject(param); | |
} catch (Exception var13) { | |
throw new InstantiationException("Error creating JSONObject from " + param); | |
} | |
} else if (!Map.class.isAssignableFrom(type)) { | |
try { | |
fromString = type.getMethod("fromString", String.class); | |
return fromString.invoke((Object)null, param); | |
} catch (NoSuchMethodException var14) { | |
try { | |
Constructor<T> stringConstructor = type.getConstructor(String.class); | |
return stringConstructor.newInstance(param); | |
} catch (NoSuchMethodException var10) { | |
return SerializationUtils.deserialize(Base64.decode(param)); | |
} | |
} | |
} else if (param.startsWith("{") && param.endsWith("}")) { | |
HashMap<String, Object> result = new HashMap(); | |
String internString = param.substring(1, param.length() - 1); | |
String[] keyVals = internString.split(Pattern.quote(",")); | |
String[] var5 = keyVals; | |
int var6 = keyVals.length; | |
for(int var7 = 0; var7 < var6; ++var7) { | |
String keyVal = var5[var7]; | |
String[] keyPair = keyVal.split(Pattern.quote(":")); | |
result.put(keyPair[0].replace("\"", ""), keyPair[1].replace("\"", "")); | |
} | |
return result; | |
} else { | |
return SerializationUtils.deserialize(Base64.decode(param)); | |
} | |
} else { | |
return param; | |
} | |
} else { | |
return null; | |
} | |
} | |
public static Object[] stringsToObjects(Class<?>[] types, String... parameters) throws RuntimeException { | |
Pattern arrayMatcher = Pattern.compile("^\\[(.+)\\]$"); | |
if (parameters != null && parameters.length > 0) { | |
Object[] newArgs = new Object[types.length]; | |
for(int i = 0; i < types.length; ++i) { | |
try { | |
Class<?> type = types[i]; | |
if (!type.isArray()) { | |
newArgs[i] = stringToObject(type, parameters[i]); | |
} else { | |
String className = type.getCanonicalName().replace('/', '.').replace("[]", ""); | |
if (className.equals("long")) { | |
type = Long.TYPE; | |
} else if (className.equals("byte")) { | |
type = Byte.TYPE; | |
} else if (className.equals("short")) { | |
type = Short.TYPE; | |
} else if (className.equals("int")) { | |
type = Integer.TYPE; | |
} else if (className.equals("double")) { | |
type = Double.TYPE; | |
} else if (className.equals("float")) { | |
type = Float.TYPE; | |
} else if (className.equals("boolean")) { | |
type = Boolean.TYPE; | |
} else if (className.equals("char")) { | |
type = Character.TYPE; | |
} else { | |
type = Class.forName(className); | |
} | |
if (parameters[i] != null && parameters[i].equals("[]")) { | |
newArgs[i] = Array.newInstance(type, 0); | |
} else if (i == types.length - 1) { | |
Object objArr = Array.newInstance(type, parameters.length - i); | |
int arrLen = Array.getLength(objArr); | |
for(int j = 0; j < arrLen; ++j) { | |
Array.set(objArr, j, stringToObject(type, parameters[i + j])); | |
} | |
newArgs[i] = objArr; | |
} else if (parameters[i] != null) { | |
Matcher m = arrayMatcher.matcher(parameters[i]); | |
if (!m.matches()) { | |
Object objArr = Array.newInstance(type, 1); | |
Array.set(objArr, 0, stringToObject(type, parameters[i])); | |
newArgs[i] = objArr; | |
} else { | |
String[] args = m.group(1).split(Pattern.quote(",")); | |
Object objArr = Array.newInstance(type, args.length); | |
for(int j = 0; j < args.length; ++j) { | |
Array.set(objArr, j, stringToObject(type, args[j])); | |
} | |
newArgs[i] = objArr; | |
} | |
} | |
} | |
} catch (Exception var11) { | |
throw new RuntimeException(var11); | |
} | |
} | |
return newArgs; | |
} else { | |
return null; | |
} | |
} | |
public static void invokeMethod(Object instance, String methodName, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { | |
Method method = getMethod(instance, methodName); | |
if (method == null) { | |
throw new IllegalArgumentException(String.format("Could not find a method with specified parameters. Object: [%s] Method: [%s] Args: [%s]", instance, methodName, args)); | |
} else { | |
method.invoke(instance, args); | |
} | |
} | |
public static boolean hasMethod(Object instance, String methodName) { | |
boolean result = false; | |
Method method = getMethod(instance, methodName); | |
if (method != null) { | |
result = true; | |
} | |
return result; | |
} | |
public static boolean hasSetterMethod(Object instance, String propertyName) { | |
boolean result = false; | |
String property = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); | |
String methodName = "set" + property; | |
Method setterMethod = getMethod(instance, methodName); | |
if (setterMethod != null) { | |
result = true; | |
} | |
return result; | |
} | |
public static Method getMethod(Object instance, String methodName) { | |
Method result = null; | |
Method[] methods = instance.getClass().getMethods(); | |
Method[] var4 = methods; | |
int var5 = methods.length; | |
for(int var6 = 0; var6 < var5; ++var6) { | |
Method method = var4[var6]; | |
String foundMethodName = method.getName(); | |
if (foundMethodName.equals(methodName)) { | |
result = method; | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment