Rutvij Shah
Rutvij Shah

Reputation: 113

Playframework Setter Encapsulation

I am using Play 2.0 for webapplication. I came to know about Play's property simulation:http://www.playframework.org/documentation/1.2.3/model#properties

When I tried same thing on my sample application setter's validation did not get called.

I re run application in debug mode and put break point on Setter method of Product class but its not getting executed.

Here is code snippet :

public class Product {

public String name;
public Integer price;

  public void setPrice(Integer price) {
     if (price < 0) {
         throw new IllegalArgumentException("Price can’t be negative!");
     }
     this.price = price;
   }
}    


public class Application extends Controller {

  public static Result index() {
   Product p=new Product();
   p.name="Test";
   p.price=-1; //I am expecting that code will throw IllegalArgumentException but its not

      return ok(index.render(p.name));
  }

}

Am I missing something here ?

Upvotes: 0

Views: 124

Answers (1)

Schleichardt
Schleichardt

Reputation: 7542

Play 2 behaves different than play 1.

It won't rewrite that expressions:

anInstance.field = newValue;

So you have to call the setter directly to validate arguments.

Source: https://groups.google.com/forum/#!topic/play-framework/S8DHAcYHh-A

Upvotes: 1

Related Questions