Bella
Bella

Reputation: 185

grails how to delete property from object

suppose I have MyClass domain class:

class MyClass {
  String prop1
  String prop2
  String prop3
}

I wonder is there any way to delete for example prop1 property from MyClass object ?

Upvotes: 2

Views: 2491

Answers (1)

Dónal
Dónal

Reputation: 187499

The only way to actually delete the property is to remove it from the source file. However, you can make attempts to access the property exhibit the same behaviour as an attempt to access a non-existent property.

class MyClass {

  String prop1
  String prop2
  String prop3
}

MyClass.metaClass {
  // Intercept attempts to get the property
  getProp1 = {-> throw new MissingPropertyException("can't get prop1")}
  // Intercept attempts to set the property
  setProp1 = {throw new MissingPropertyException("can't set prop1")}
}

Upvotes: 5

Related Questions