mercredi 17 août 2022

JavaParser to modify attributes and methods of a java class

I am trying to make a program that corrects me a certain Java class according to the input data type.

I was given a .java file that defines Animals, it has the following form:

Animals.java

public class Animal {
  Mammal mammal;
  Bird bird;
  Fish fish;
  ...
  
  class Mammal {
    Dog dog;
    Cat cat;
    Horse horse;
    ...

    class Dog {
      String name;
      String weight;

      void setWeight(String name){...}
      String getWeight() {...}
    }

    class Cat {
      String name;
      Double weight;

      void setWeight(Double name){...}
      Double getWeight() {...}
    }
    ...
  }
  class Bird {...}
  class Fish {...}
  ...
}

And text files containing:

a.getMammal().getDog().setWeight("50");
a.getMammal().getDog().setName("Fluffy");
a.getMammal().getCat().setWeight("25");
a.getMammal().getCat().setName("Kitty");
...
...
...
...

Since I have tons of text files with tons of lines in it, it is infeasible (manually) verifiying that the input parameter (always is a String) matches with the required datatype of the setter, and if not changing the class that matches the input parameter.

So I started searching for some tools to change a Java class (methods and attributes) programatically.

What I want to achive in pseudocode would be:

1) OPEN A TXT FILE
2) READ LINES
3) IF DATATYPE OF METHOD SIGNATURE != DATATYPE INPUT
4) THEN FIX .Java FILE
5) ELSE CONTINUE

I already have steps 1 - 3, I used Reflections library, but I don't know how to proceed to Step 4 (I don't even know if it is possible to modify a java file programmatically)

To clarify, an example:

lines.txt
----------------------------------------
1   a.getMammal().getDog().setWeight("50");
2   a.getMammal().getCat().setWeight("23");
----------------------------------------

open lines.txt

read line -> line = a.getMammal().getDog().setWeight("50");

get params type -> 
    methodType = String  (a.getMammal().getDog().setWeight(), the attribute weight in Dog is defined as String)
    inputType = String   ("50" is a String)

compare types -> Since both coincide, do nothing to Animals.java and read next line

read line -> a.getMammal().getCat().setWeight("23");

get params type -> 
    methodType = Double  (a.getMammal().getCat().setWeight(), the attribute weight in Cat is defined as Double)
    inputType = String   ("23" is a String)

compare types -> Since both  no coincide, fix and update Animals.java

end program

At the end of the execution, the file Animals.java would look like this:

Animals.java

public class Animal {
  Mammal mammal;
  Bird bird;
  Fish fish;
  ...
  
  class Mammal {
    Dog dog;
    Cat cat;
    Horse horse;
    ...

    class Dog {
      String name;
      String weight;

      void setWeight(String name){...}
      String getWeight() {...}
    }

    class Cat {
      String name;
      **String** weight;

      void setWeight(**String** name){...}
      **String** getWeight() {...}
    }
    ...
  }
  class Bird {...}
  class Fish {...}
  ...
}




Aucun commentaire:

Enregistrer un commentaire