-
-
Save testanull/6d52769c6704770c76359f3d56946ba2 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.logging.ILogger; | |
import com.q1labs.frameworks.logging.LoggerFactory; | |
import com.q1labs.frameworks.util.Base64; | |
import java.io.ByteArrayInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InvalidClassException; | |
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.ArrayList; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Base64.Decoder; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import org.apache.commons.io.serialization.ValidatingObjectInputStream; | |
import org.apache.commons.json.JSONObject; | |
import org.apache.commons.lang3.SerializationUtils; | |
public class ReflectionUtils { | |
private static ILogger logger = LoggerFactory.getLogger(ReflectionUtils.class); | |
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 var21) { | |
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 var22) { | |
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 var23) { | |
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 var30) { | |
try { | |
Constructor<T> stringConstructor = type.getConstructor(String.class); | |
return stringConstructor.newInstance(param); | |
} catch (NoSuchMethodException var29) { | |
try { | |
InputStream paramStream = new ByteArrayInputStream(Base64.decode(param)); | |
ValidatingObjectInputStream vOIS = new ValidatingObjectInputStream(paramStream); | |
Object var40; | |
try { | |
vOIS.accept(new String[]{type.getCanonicalName()}); | |
vOIS.accept(Pattern.compile("org\\.postgresql\\.util.*")); | |
vOIS.accept(Pattern.compile("com\\.q1labs.*")); | |
vOIS.accept(Pattern.compile("java\\.lang.*")); | |
vOIS.accept(Pattern.compile("java\\.util.*")); | |
vOIS.accept(Pattern.compile("gnu\\.trove.*")); | |
vOIS.accept(new String[]{"[B"}); | |
Object obj = vOIS.readObject(); | |
if (logger.isDebugEnabled()) { | |
logger.debug("obj.getClass: " + obj.getClass().getCanonicalName() + " type.getCanonicalName: " + type.getCanonicalName() + " type.getClass " + type.getClass()); | |
} | |
if (!obj.getClass().getCanonicalName().equals(type.getCanonicalName())) { | |
if (obj instanceof ArrayList) { | |
if (!type.isAssignableFrom(ArrayList.class)) { | |
logger.error("Expected: " + type.getCanonicalName() + ", found :" + obj.getClass().getName() + ". Rejecting parameter for serialization"); | |
var40 = null; | |
return var40; | |
} | |
if (logger.isDebugEnabled()) { | |
logger.debug("PASS Expected: " + type.getCanonicalName() + ", found :" + obj.getClass().getName() + ". It is assignable, allowing parameter for serialization"); | |
} | |
} else { | |
if (!type.isAssignableFrom(obj.getClass())) { | |
logger.error("Expected: " + type.getCanonicalName() + ", found :" + obj.getClass().getName() + ". Rejecting parameter for serialization"); | |
var40 = null; | |
return var40; | |
} | |
if (logger.isDebugEnabled()) { | |
logger.debug("PASS Expected: " + type.getCanonicalName() + ", found :" + obj.getClass().getName() + ". It is assignable, allowing parameter for serialization"); | |
} | |
} | |
} | |
var40 = SerializationUtils.deserialize(Base64.decode(param)); | |
return var40; | |
} catch (InvalidClassException var25) { | |
logger.error("Failed to validate " + type.getCanonicalName() + ". Rejecting parameter for serialization"); | |
var40 = null; | |
return var40; | |
} catch (Exception var26) { | |
logger.error("Failed to validate " + type.getCanonicalName() + ". Rejecting parameter for serialization"); | |
var40 = null; | |
} finally { | |
paramStream.close(); | |
vOIS.close(); | |
} | |
return var40; | |
} catch (IOException var28) { | |
logger.error("Failed to validate " + type.getCanonicalName() + ". Rejecting parameter for serialization"); | |
return null; | |
} | |
} | |
} | |
} 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 { | |
Decoder decoder = java.util.Base64.getDecoder(); | |
try { | |
decoder.decode(param); | |
} catch (IllegalArgumentException var24) { | |
String msg = String.format("Map [%s] is not formatted properly", param); | |
throw new RuntimeException(msg); | |
} | |
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) { | |
logger.error("Runtime 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