Search in sources :

Example 1 with JsonConverter

use of com.builtbroken.mc.lib.json.conversion.JsonConverter in project Engine by VoltzEngine-Project.

the class JsonProcessorInjectionMap method handle.

public boolean handle(O objectToInjection, String keyValue, Object valueToInject, boolean override, String overrideType) {
    try {
        final String injectionKeyID = keyValue.toLowerCase();
        if (supports(injectionKeyID, override, overrideType)) {
            if (valueToInject instanceof JsonElement) {
                if (valueToInject instanceof JsonPrimitive) {
                    if (((JsonPrimitive) valueToInject).isBoolean()) {
                        Boolean bool = ((JsonPrimitive) valueToInject).getAsBoolean();
                        if (injectionFields.containsKey(injectionKeyID)) {
                            Field field = injectionFields.get(injectionKeyID);
                            try {
                                field.setAccessible(true);
                                field.setBoolean(objectToInjection, bool);
                                return true;
                            } catch (IllegalAccessException e) {
                                throw new RuntimeException("Failed to access field " + field, e);
                            } catch (Exception e) {
                                throw new RuntimeException("Unexpected error setting " + field + " with " + bool, e);
                            }
                        } else {
                            Method method = injectionMethods.get(injectionKeyID);
                            try {
                                method.setAccessible(true);
                                method.invoke(objectToInjection, bool);
                                return true;
                            } catch (InvocationTargetException e) {
                                throw new RuntimeException("Failed to invoke method " + method + " with data " + bool, e);
                            } catch (IllegalAccessException e) {
                                throw new RuntimeException("Failed to access method " + method, e);
                            } catch (Exception e) {
                                throw new RuntimeException("Unexpected error invoking " + method + " with " + bool, e);
                            }
                        }
                    } else if (((JsonPrimitive) valueToInject).isNumber()) {
                        if (injectionFields.containsKey(injectionKeyID)) {
                            Field field = injectionFields.get(injectionKeyID);
                            try {
                                field.setAccessible(true);
                                String type = injectionTypes.get(injectionKeyID);
                                if (type != null) {
                                    if (type.equals("int") || type.equals("integer")) {
                                        field.setInt(objectToInjection, ((JsonPrimitive) valueToInject).getAsInt());
                                    } else if (type.equals("byte")) {
                                        field.setByte(objectToInjection, ((JsonPrimitive) valueToInject).getAsByte());
                                    } else if (type.equals("short")) {
                                        field.setShort(objectToInjection, ((JsonPrimitive) valueToInject).getAsShort());
                                    } else if (type.equals("double")) {
                                        field.setDouble(objectToInjection, ((JsonPrimitive) valueToInject).getAsDouble());
                                    } else if (type.equals("float")) {
                                        field.setFloat(objectToInjection, ((JsonPrimitive) valueToInject).getAsFloat());
                                    } else if (type.equals("long")) {
                                        field.setLong(objectToInjection, ((JsonPrimitive) valueToInject).getAsLong());
                                    } else {
                                        throw new RuntimeException("Unknown number type for " + field);
                                    }
                                    return true;
                                } else {
                                    throw new RuntimeException("Failed to get number type for " + field);
                                }
                            } catch (IllegalAccessException e) {
                                throw new RuntimeException("Failed to access field " + field, e);
                            }
                        } else {
                            Method method = injectionMethods.get(injectionKeyID);
                            try {
                                method.setAccessible(true);
                                String type = injectionTypes.get(injectionKeyID);
                                if (type != null) {
                                    if (type.equals("int") || type.equals("integer")) {
                                        method.invoke(objectToInjection, (int) ((JsonPrimitive) valueToInject).getAsInt());
                                    } else if (type.equals("byte")) {
                                        method.invoke(objectToInjection, (byte) ((JsonPrimitive) valueToInject).getAsByte());
                                    } else if (type.equals("short")) {
                                        method.invoke(objectToInjection, (short) ((JsonPrimitive) valueToInject).getAsShort());
                                    } else if (type.equals("double")) {
                                        method.invoke(objectToInjection, (double) ((JsonPrimitive) valueToInject).getAsDouble());
                                    } else if (type.equals("float")) {
                                        method.invoke(objectToInjection, (float) ((JsonPrimitive) valueToInject).getAsFloat());
                                    } else if (type.equals("long")) {
                                        method.invoke(objectToInjection, (long) ((JsonPrimitive) valueToInject).getAsLong());
                                    } else {
                                        throw new RuntimeException("Unknown number type " + type);
                                    }
                                    return true;
                                } else {
                                    throw new RuntimeException("Failed to get type");
                                }
                            } catch (InvocationTargetException e) {
                                throw new RuntimeException("Failed to invoke method " + method + " with data " + valueToInject, e);
                            } catch (IllegalAccessException e) {
                                throw new RuntimeException("Failed to access method " + method, e);
                            } catch (Exception e) {
                                throw new RuntimeException("Error injecting " + valueToInject + " into method " + method, e);
                            }
                        }
                    } else if (((JsonPrimitive) valueToInject).isString()) {
                        String string = ((JsonPrimitive) valueToInject).getAsString();
                        return handle(objectToInjection, keyValue, string);
                    }
                } else {
                    if (injectionFields.containsKey(injectionKeyID)) {
                        Field field = injectionFields.get(injectionKeyID);
                        field.setAccessible(true);
                        try {
                            String type = injectionTypes.get(injectionKeyID);
                            if (type != null && JsonLoader.conversionHandlers.containsKey(type.toLowerCase())) {
                                JsonConverter converter = JsonLoader.conversionHandlers.get(type.toLowerCase());
                                if (converter != null) {
                                    Object conversion = converter.convert((JsonElement) valueToInject);
                                    if (conversion != null) {
                                        field.set(objectToInjection, conversion);
                                    } else {
                                        throw new IllegalArgumentException("Field was marked as type[" + type + "] but could not be converted to inject into " + field + ", data: " + objectToInjection);
                                    }
                                } else {
                                    throw new IllegalArgumentException("Field was marked as type[" + type + "] but a converter could not be found to use with " + field + ", data: " + objectToInjection);
                                }
                            } else {
                                field.set(objectToInjection, valueToInject);
                            }
                            return true;
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException("Failed to access field " + field, e);
                        } catch (Exception e) {
                            throw new RuntimeException("Unexpected error setting " + field + " with " + valueToInject, e);
                        }
                    } else {
                        Method method = injectionMethods.get(injectionKeyID);
                        try {
                            method.setAccessible(true);
                            String type = injectionTypes.get(injectionKeyID);
                            if (type != null && JsonLoader.conversionHandlers.containsKey(type.toLowerCase())) {
                                JsonConverter converter = JsonLoader.conversionHandlers.get(type.toLowerCase());
                                if (converter != null) {
                                    Object conversion = converter.convert((JsonElement) valueToInject);
                                    if (conversion != null) {
                                        method.invoke(objectToInjection, conversion);
                                    } else {
                                        throw new IllegalArgumentException("Method was marked as type[" + type + "] but could not be converted to inject into " + method + ", data: " + objectToInjection);
                                    }
                                } else {
                                    throw new IllegalArgumentException("Method was marked as type[" + type + "] but a converter could not be found to use with " + method + ", data: " + objectToInjection);
                                }
                            } else {
                                method.invoke(objectToInjection, valueToInject);
                            }
                            return true;
                        } catch (InvocationTargetException e) {
                            throw new RuntimeException("Failed to invoke method " + method + " with data " + valueToInject, e);
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException("Failed to access method " + method, e);
                        } catch (Exception e) {
                            throw new RuntimeException("Unexpected error invoking " + method + " with " + valueToInject, e);
                        }
                    }
                }
            } else if (valueToInject instanceof String) {
                if (injectionFields.containsKey(injectionKeyID)) {
                    Field field = injectionFields.get(injectionKeyID);
                    try {
                        field.setAccessible(true);
                        field.set(objectToInjection, valueToInject);
                        return true;
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException("Failed to access field " + field, e);
                    } catch (Exception e) {
                        throw new RuntimeException("Unexpected error setting " + field + " with " + valueToInject, e);
                    }
                } else {
                    Method method = injectionMethods.get(injectionKeyID);
                    try {
                        method.setAccessible(true);
                        method.invoke(objectToInjection, valueToInject);
                        return true;
                    } catch (InvocationTargetException e) {
                        throw new RuntimeException("Failed to invoke method " + method + " with data " + valueToInject, e);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException("Failed to access method " + method, e);
                    } catch (Exception e) {
                        throw new RuntimeException("Unexpected error invoking " + method + " with " + valueToInject, e);
                    }
                }
            } else if (injectionFields.containsKey(injectionKeyID)) {
                Field field = injectionFields.get(injectionKeyID);
                try {
                    field.setAccessible(true);
                    field.set(objectToInjection, valueToInject);
                    return true;
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Failed to access field " + field, e);
                } catch (Exception e) {
                    throw new RuntimeException("Unexpected error setting " + field + " with " + valueToInject, e);
                }
            } else {
                Method method = injectionMethods.get(injectionKeyID);
                try {
                    method.setAccessible(true);
                    method.invoke(objectToInjection, valueToInject);
                    return true;
                } catch (InvocationTargetException e) {
                    throw new RuntimeException("Failed to invoke method " + method + " with data " + valueToInject, e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Failed to access method " + method, e);
                } catch (Exception e) {
                    throw new RuntimeException("Unexpected error invoking " + method + " with " + valueToInject, e);
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to inject data " + valueToInject + " into " + objectToInjection, e);
    }
    return false;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonConverter(com.builtbroken.mc.lib.json.conversion.JsonConverter) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) JsonElement(com.google.gson.JsonElement)

Aggregations

JsonConverter (com.builtbroken.mc.lib.json.conversion.JsonConverter)1 JsonElement (com.google.gson.JsonElement)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1