|
import static art.library.gui.FlatGUI.getDeclaredField;
|
import static art.library.gui.FlatGUI.getDeclaredMethod;
|
import art.library.interop.serialization.Serialization;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import java.lang.reflect.Array;
|
import java.lang.reflect.Field;
|
import java.lang.reflect.Method;
|
import java.util.Date;
|
|
/*
|
* To change this license header, choose License Headers in Project Properties.
|
* To change this template file, choose Tools | Templates
|
* and open the template in the editor.
|
*/
|
|
/**
|
*
|
* @author Andres
|
*/
|
public class Test1
|
{
|
public static void main(String[] args)
|
{
|
try
|
{
|
// RTZ32_Configuration_Program_Trolley trolley = new RTZ32_Configuration_Program_Trolley();
|
// trolley.times1 = new RTZ32_Configuration_Program_Trolley_Times1[8];
|
// for (int i=0; i<8; i++)
|
// {
|
// trolley.times1[i] = new RTZ32_Configuration_Program_Trolley_Times1();
|
// }
|
// setValue("times1@0.type", 5, trolley);
|
|
// RTZ32_Controller rtz32 = (RTZ32_Controller)Serialization.deserialize(RTZ32_Controller.class, new File("controller-zaragoza-90020_v2.json"));
|
// Object object1 = getValue(rtz32.getDeviceConfiguration().rtz32.programs[0], "trolley.saturations@0.startTime");
|
// Object object2 = getValue(rtz32.getDeviceConfiguration().rtz32.programs[0], "trolley.saturations@0.timesClosing@0");
|
// Object object3 = getValue(rtz32.getDeviceConfiguration().rtz32.programs[0].groups[0], "impulses@0");
|
//
|
// int a = 0;
|
|
|
// FlatGUI.initialise();
|
// FlatRegexFormatter formatter = new FlatRegexFormatter("\\^[0-6]$|");
|
// JFormattedTextField textField = new JFormattedTextField(formatter);
|
// JFrame frame = new JFrame();
|
// frame.getContentPane().setLayout(new BorderLayout());
|
// frame.getContentPane().add(textField, BorderLayout.CENTER);
|
// frame.setSize(600,200);
|
// frame.setVisible(true);
|
|
|
}
|
catch (Exception exception)
|
{
|
exception.printStackTrace();
|
}
|
}
|
|
|
|
@JsonIgnore
|
public static void setValue(String name, Object value, Object object) throws Exception
|
{
|
if ((name == null) || (object == null)) return;
|
|
|
int indice1 = name.indexOf(".");
|
int indice2 = name.indexOf("@");
|
|
if ((indice2 > 0) && (indice2 < indice1)) // Array
|
{
|
String nombreClase = name.substring(0, indice2);
|
String nombreRestante = name.substring(indice1 + 1, name.length());
|
int index = Integer.parseInt(name.substring(indice2 + 1, indice1));
|
|
Field field = object.getClass().getDeclaredField(nombreClase);
|
Object child = Array.get(field.get(object), index);
|
setValue(nombreRestante, value, child);
|
}
|
else if (indice1 > 0)
|
{
|
String nombreClase = name.substring(0, indice1);
|
String nombreRestante = name.substring(indice1 + 1, name.length());
|
Field field = object.getClass().getDeclaredField(nombreClase);
|
field.setAccessible(true);
|
Object child = field.get(object);
|
setValue(nombreRestante, value, child);
|
}
|
else
|
{
|
try
|
{
|
Field field = null;
|
int fieldArrayIndex = 0;
|
|
// Array
|
|
if (name.indexOf("@") > 0)
|
{
|
String[] tokens = name.split("@");
|
field = getDeclaredField(object.getClass(), tokens[0]);
|
fieldArrayIndex = Integer.parseInt(tokens[1]);
|
}
|
else
|
{
|
field = getDeclaredField(object.getClass(), name);
|
}
|
|
field.setAccessible(true);
|
Class classType = field.getType();
|
|
if (value instanceof String)
|
{
|
if (classType.isAssignableFrom(Boolean.class) || classType.isAssignableFrom(boolean.class))field.set(object, ((String)value).equalsIgnoreCase("true"));
|
else if (classType.isAssignableFrom(Byte.class) || classType.isAssignableFrom(byte.class)) field.set(object, Byte.parseByte((String)value));
|
else if (classType.isAssignableFrom(Short.class) || classType.isAssignableFrom(short.class)) field.set(object, Short.parseShort((String)value));
|
else if (classType.isAssignableFrom(Integer.class) || classType.isAssignableFrom(int.class)) field.set(object, Integer.parseInt((String)value));
|
else if (classType.isAssignableFrom(Long.class) || classType.isAssignableFrom(long.class)) field.set(object, Long.parseLong((String)value));
|
else if (classType.isAssignableFrom(Float.class) || classType.isAssignableFrom(float.class)) field.set(object, Float.parseFloat(((String)value).replace(",", ".")));
|
else if (classType.isAssignableFrom(Double.class) || classType.isAssignableFrom(double.class)) field.set(object, Double.parseDouble(((String)value).replace(",", ".")));
|
else if (classType.isAssignableFrom(String.class)) field.set(object, value);
|
else if (classType.isArray() == true)
|
{
|
Class<?> componentType = classType.getComponentType();
|
|
if ((componentType.isAssignableFrom(Boolean.class)) || componentType.isAssignableFrom(boolean.class))
|
{
|
boolean[] array = (boolean[])field.get(object); Array.set(array, fieldArrayIndex, ((String)value).equalsIgnoreCase("true"));
|
}
|
else if ((componentType.isAssignableFrom(Byte.class)) || componentType.isAssignableFrom(byte.class))
|
{
|
byte[] array = (byte[])field.get(object); Array.set(array, fieldArrayIndex, Byte.parseByte((String)value));
|
}
|
else if ((componentType.isAssignableFrom(Short.class)) || componentType.isAssignableFrom(short.class))
|
{
|
short[] array = (short[])field.get(object); Array.set(array, fieldArrayIndex, Short.parseShort((String)value));
|
}
|
else if ((componentType.isAssignableFrom(Integer.class)) || componentType.isAssignableFrom(int.class))
|
{
|
int[] array = (int[])field.get(object); Array.set(array, fieldArrayIndex, Integer.parseInt((String)value));
|
}
|
else if ((componentType.isAssignableFrom(Long.class)) || componentType.isAssignableFrom(long.class))
|
{
|
long[] array = (long[])field.get(object); Array.set(array, fieldArrayIndex, Long.parseLong((String)value));
|
}
|
else if ((componentType.isAssignableFrom(Float.class)) || componentType.isAssignableFrom(float.class))
|
{
|
float[] array = (float[])field.get(object); Array.set(array, fieldArrayIndex, Float.parseFloat((String)value));
|
}
|
else if ((componentType.isAssignableFrom(Double.class)) || componentType.isAssignableFrom(double.class))
|
{
|
double[] array = (double[])field.get(object); Array.set(array, fieldArrayIndex, Double.parseDouble((String)value));
|
}
|
else
|
{
|
}
|
}
|
|
else field.set(object, value);
|
}
|
else
|
{
|
field.set(object, value);
|
}
|
}
|
catch (Exception e)
|
{
|
for (Method method : object.getClass().getDeclaredMethods())
|
{
|
if (method.getName().equals(name))
|
{
|
Object finalvalue = value;
|
Class classType = method.getParameterTypes()[0];
|
|
if (value instanceof String)
|
{
|
if (classType.isAssignableFrom(Boolean.class) || classType.isAssignableFrom(boolean.class)) finalvalue = new Boolean(value.toString().equalsIgnoreCase("true"));
|
else if (classType.isAssignableFrom(Byte.class) || classType.isAssignableFrom(byte.class)) finalvalue = new Byte(value.toString());
|
else if (classType.isAssignableFrom(Short.class) || classType.isAssignableFrom(short.class)) finalvalue = new Short(value.toString());
|
else if (classType.isAssignableFrom(Integer.class) || classType.isAssignableFrom(int.class)) finalvalue = new Integer(value.toString());
|
else if (classType.isAssignableFrom(Long.class) || classType.isAssignableFrom(long.class)) finalvalue = new Long(value.toString());
|
else if (classType.isAssignableFrom(Float.class) || classType.isAssignableFrom(float.class)) finalvalue = new Float(value.toString());
|
else if (classType.isAssignableFrom(Double.class) || classType.isAssignableFrom(double.class))finalvalue = new Double(value.toString());
|
else if (classType.isAssignableFrom(String.class)) finalvalue = value.toString();
|
}
|
|
method.setAccessible(true);
|
method.invoke(object, finalvalue);
|
return;
|
}
|
}
|
}
|
}
|
}
|
|
|
|
|
|
@JsonIgnore
|
public static Object getValue(Object object, String name) throws Exception
|
{
|
int indice1 = name.indexOf(".");
|
int indice2 = name.indexOf("@");
|
|
if ((indice2 > 0) && (indice2 < indice1)) // Array
|
{
|
String nombreClase = name.substring(0, indice2);
|
String nombreRestante = name.substring(indice1 + 1, name.length());
|
int index = Integer.parseInt(name.substring(indice2 + 1, indice1));
|
|
Field field = object.getClass().getDeclaredField(nombreClase);
|
Object child = Array.get(field.get(object), index);
|
return getValue(child, nombreRestante);
|
}
|
else if (indice1 > 0)
|
{
|
// Es un campo de una clase
|
|
String nombreClase = name.substring(0, indice1);
|
String nombreRestante = name.substring(indice1 + 1, name.length());
|
|
try
|
{
|
Field field = getDeclaredField(object.getClass(), nombreClase);
|
field.setAccessible(true);
|
Object child = field.get(object);
|
return getValue(child, nombreRestante);
|
}
|
catch (Exception e)
|
{
|
try
|
{
|
Method method = getDeclaredMethod(object.getClass(), nombreClase);
|
method.setAccessible(true);
|
Object child = method.invoke(object, new Object[0]);
|
return getValue(child, nombreRestante);
|
}
|
catch (Exception exception)
|
{
|
return "";
|
}
|
}
|
}
|
else
|
{
|
try
|
{
|
Field field = null;
|
int fieldArrayIndex = 0;
|
|
// Array
|
|
if (name.indexOf("@") > 0)
|
{
|
String[] tokens = name.split("@");
|
field = getDeclaredField(object.getClass(), tokens[0]);
|
fieldArrayIndex = Integer.parseInt(tokens[1]);
|
field.setAccessible(true);
|
return getValue(field.get(object), fieldArrayIndex);
|
}
|
|
field = getDeclaredField(object.getClass(), name);
|
field.setAccessible(true);
|
return field.get(object);
|
}
|
catch (Exception e)
|
{
|
try
|
{
|
Method method = getDeclaredMethod(object.getClass(), name);
|
method.setAccessible(true);
|
return method.invoke(object, new Object[0]);
|
}
|
catch (Exception exception)
|
{
|
return "";
|
}
|
}
|
}
|
}
|
|
|
|
|
|
|
|
|
|
|
public static Object getValue(Object value, int arrayIndex)
|
{
|
try
|
{
|
if(value.getClass().isArray())
|
{
|
if(value instanceof Object[])
|
{
|
return ((Object[])value)[arrayIndex];
|
}
|
else // box primitive arrays
|
{
|
return Array.get(value, arrayIndex); // automatic boxing
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
|
return null;
|
}
|
|
|
|
@JsonIgnore
|
public static Object getValue(Object object, String name, Object parameter) throws Exception
|
{
|
int indice = name.indexOf(".");
|
|
if (indice > 0)
|
{
|
// Es un campo de una clase
|
|
String nombreClase = name.substring(0, indice);
|
String nombreRestante = name.substring(indice + 1, name.length());
|
|
try
|
{
|
Method method = getDeclaredMethod(object.getClass(), nombreClase);
|
method.setAccessible(true);
|
Object child = method.invoke(object, parameter);
|
return getValue(child, nombreRestante, parameter);
|
}
|
catch (Exception exception)
|
{
|
return null;
|
}
|
}
|
else
|
{
|
try
|
{
|
Method method = getDeclaredMethod(object.getClass(), name);
|
method.setAccessible(true);
|
return method.invoke(object, parameter);
|
}
|
catch (Exception exception)
|
{
|
exception.printStackTrace();
|
return "";
|
}
|
}
|
}
|
|
|
|
|
|
}
|