0 Replies Latest reply on Nov 4, 2015 11:16 AM by mrjazzy

    Roaster: Adding a Property of a generic Type with a concrete Type Parameter

    mrjazzy

      Using Roaster it is possible to add a property to the source of a java class;

       

      <code>

      final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);

      ...

      PropertySource<JavaClassSource> p = javaClass.addProperty("List<String>", "friends");

      </code>

       

      which emits

       

      <code>

      private List<String> friends;

       

      public List<String> getFriends() {

        return friends;

      }

       

      public void setFriends(List<String> friends) {

        this.friends = friends;

      }

      </code>

       

      as expected.

       

       

      However say I wanted to "keep track" of the declared type of the property to use consistently elsewhere during generation; how could I do this?

      For example the following do not work;

       

      <code>

      PropertySource<JavaClassSource> p = javaClass.addProperty("List<String>", "friends");

      PropertySource<JavaClassSource> p2 = javaClass.addProperty(p.getType(), "acquaintances"); //Does not compile; illegal cast

       

      JavaClassSource jtS = Roaster.create(JavaClassSource.class);

      jtS.setPackage("java.lang").setName("String");

       

      JavaClassSource jtL = Roaster.create(JavaClassSource.class);

      jtL.setPackage("java.util").setName("List");

      jtL.addTypeVariable().setBounds(jtS);

       

      PropertySource<JavaClassSource> p3 = javaClass.addProperty(jtL, "acquaintances"); //emits private List acquaintances; for example - Looses the Type parameter

      </code>