samedi 17 janvier 2015

Take the type from a String

I've never posted a question so strange on StackOverflow, so sorry if it will be not so clear (comment your doubts and I will try to explain myself).


Introduction


I have to create a Simple Testing Framework (STF), where given an html file (with only and tags) of this form:


enter image description here


Where:



  1. The first row is the test table class


  2. The second row contains the names of the variables used in the test + result() which is a method that will be written by the programmer and will perform the test. In the example above, result() is:


    public float result() { return x*y; }




  3. The third one is the type of each variable + the return type of result()



  4. From the fourth row forward each line is the data of each test


From this table, I have to generate a new html file where each cell under the result() column is colored of green if the value is correct, red otherwise. The generated table of the example above is:


enter image description here


The Problem


Without going too much in details, the colored generated table must be create in two phases:


The first one generates a class which contains a method boolean check(Row row) which test return true if the data contained in row are correct or not, exploiting the result() function which I mentioned you before.


The second one will generate the new table exploting the methods of the class generated in the previous phase.


EVERYTHING ABOVE WERE PROJECT SPECIFICS, FROM NOW ON THERE ARE ONLY MY ASSUMPTIONS/IMPLEMENTATIONS/SOLUTIONS


Looking at the original example, the method check(Row row)should be something like this:



public boolean check (Row row)
{
x = row.column[0];
y = row.column[1];
if(result()==row.column[2])
return true;
else
return false;
}


As you can imagine, x and y are attributes inside the same class of check, and row is an object created by a parser.


So what's the problem?



The problem is that since the data could be any kind of data (even objects references!) I implemented the list Row.columns as a List<String>, because I cannot know previously the type of each data. So what I wrote above has no sense since x and y and the value returned by result() are float while each element inside columns are String!



The question:


There exists any kind of mechanism in order to transform each String column element in the correct type? It sounds like some reflection stuff, but I don't know how to figure it out!






Aucun commentaire:

Enregistrer un commentaire