This one is going to be tough to explain but I'll give it a shot. I am trying to dynamically create a database table that mimics java objects. So it will have each column for each defined field in a particular object. These java objects can have defined primitives, arrays and other objects inside them. I am generating the SQLite create statement from a recursive function I created. More specifically this function will create the column/type pairs in the DB create table query.
The issue I run into is trying to determine array an array size. Here is my function that generates the column/type pairs.
public String processColumnData(String topicName)
{
Field fieldList[] = null;
String query = "";
try
{
fieldList = Class.forName(topicName).getDeclaredFields();
for (Field a : fieldList)
{
if ((a.getType().getName().equals("int")) || (a.getType().getName().equals("long")) || (a.getType().getName().equals("short")) || (a.getType().getName().equals("byte"))
|| (a.getType().getName().equals("boolean")) || (com.rti.dds.util.Enum.class.isAssignableFrom(a.getType())))
{
query = query + "'" + a.getName() + "'" + " INTEGER,";
} else if (a.getType().getName().equals("java.lang.String"))
{
query = query + "'" + a.getName() + "'" + " TEXT,";
} else if (a.getType().getName().equals("float"))
{
query = query + "'" + a.getName() + "'" + " REAL,";
} else if (a.getType().isArray() && ((a.getType().getName().startsWith("["))))
{
if (a.getType().getName().equals("[F"))
{
for (int i = 0; i < Array.getLength(a.get(Class.forName(topicName))); i++)
{
query = query + "'" + a.getName() + "[" + i + "]' REAL,";
}
} else if ((a.getType().getName().equals("[Ljava.lang.String;")))
{
for (int i = 0; i < Array.getLength(a.get(Class.forName(topicName))); i++)
{
query = query + "'" + a.getName() + "[" + i + "]' TEXT,";
}
} else if (a.getType().getName().equals("[I") || a.getType().getName().equals("[S") || a.getType().getName().equals("[B")
|| (com.rti.dds.util.Enum.class.isAssignableFrom(Class.forName(a.getType().getName().replace(";", "").replace("[L", "")))))
{
for (int i = 0; i < Array.getLength(a.get(Class.forName(topicName))); i++)
{
query = query + "'" + a.getName() + "[" + i + "]' INTEGER,";
}
} else
{
// TODO: Arrays of objects not enums.
}
} else
{
processColumnData(a.getType().getName());
}
}
} catch (SecurityException e)
{
// TODO Auto-generated catch block
System.out.println("Security Exception");
return null;
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
System.out.println("Class not found " + topicName);
return null;
} catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(query);
return query;
}
The primitives are straight to the point but when I get to arrays, it will not let me determine the size. I know that I should be passing in an instance of the class to the get() instead of the class itself but I do not currently have an instance. Below is an example of an object with an array that can come into the above function:
public class jOverLoad implements Copyable, Serializable{
public int machineId= 0;
public int motorIndex= 0;
public int motorDescriptionTidx= 0;
public jTimeType timeOfLastThermalTrip = (jTimeType)jTimeType.create();
public float thermalTimer= 0;
public boolean thermalWarning= false;
public boolean thermalTrip= false;
public boolean jamWarning= false;
public boolean jamTrip= false;
public int jamResetTimer= 0;
public float phaseImbalance= 0;
public boolean phaseImbalanceTrip= false;
public boolean phaseAOK= false;
public boolean phaseBOK= false;
public boolean phaseCOK= false;
public jRtdStatus [] rtdTripStatus= new jRtdStatus [(J_MAX_MOTOR_RTDS.VALUE)];
I figured since the size is defined in the instantiation of the array, I should somehow be able to get that value. Is there a way?
Aucun commentaire:
Enregistrer un commentaire