diff --git a/core-groovy-2/src/main/groovy/com/baeldung/metaprogramming/Employee.groovy b/core-groovy-2/src/main/groovy/com/baeldung/metaprogramming/Employee.groovy new file mode 100644 index 0000000000..f49d0f906b --- /dev/null +++ b/core-groovy-2/src/main/groovy/com/baeldung/metaprogramming/Employee.groovy @@ -0,0 +1,45 @@ +package com.baeldung.metaprogramming + +import groovy.transform.AutoClone +import groovy.transform.Canonical +import groovy.transform.EqualsAndHashCode +import groovy.transform.ToString +import groovy.transform.TupleConstructor +import groovy.util.logging.* + +@Canonical +@TupleConstructor +@EqualsAndHashCode +@ToString(includePackage=false, excludes=['id']) +@Log +@AutoClone +class Employee { + + long id + String firstName + String lastName + int age + + //method to catch missing property's getter + def propertyMissing(String propertyName) { + log.info "$propertyName is not available" + "property '$propertyName' is not available" + } + + //method to catch missing property's setter + def propertyMissing(String propertyName, propertyValue) { + println "property '$propertyName' is not available" + log.info "$propertyName is not available" + "property '$propertyName' is not available" + } + + def methodMissing(String methodName, def methodArgs) { + log.info "$methodName is not defined" + "method '$methodName' is not defined" + } + + def logEmp() { + log.info "Employee: $lastName, $firstName is of $age years age" + } + +} \ No newline at end of file diff --git a/core-groovy-2/src/main/groovy/com/baeldung/metaprogramming/extension/BasicExtensions.groovy b/core-groovy-2/src/main/groovy/com/baeldung/metaprogramming/extension/BasicExtensions.groovy new file mode 100644 index 0000000000..65591cae8d --- /dev/null +++ b/core-groovy-2/src/main/groovy/com/baeldung/metaprogramming/extension/BasicExtensions.groovy @@ -0,0 +1,30 @@ +package com.baeldung.metaprogramming.extension + +import com.baeldung.metaprogramming.Employee + +class BasicExtensions { + + static int getYearOfBirth(Employee self) { + return (new Date().getYear() + 1900) - self.age; + } + + static String capitalize(String self) { + return self.substring(0, 1).toUpperCase() + self.substring(1) + } + + static void printCounter(Integer self) { + while (self>0) { + println self + self-- + } + } + + static Long square(Long self) { + return self*self + } + + static BigDecimal cube(BigDecimal self) { + return self*self*self + } + +} \ No newline at end of file diff --git a/core-groovy-2/src/main/groovy/com/baeldung/metaprogramming/extension/StaticEmployeeExtension.groovy b/core-groovy-2/src/main/groovy/com/baeldung/metaprogramming/extension/StaticEmployeeExtension.groovy new file mode 100644 index 0000000000..ab2aac38ea --- /dev/null +++ b/core-groovy-2/src/main/groovy/com/baeldung/metaprogramming/extension/StaticEmployeeExtension.groovy @@ -0,0 +1,10 @@ +package com.baeldung.metaprogramming.extension + +import com.baeldung.metaprogramming.Employee + +class StaticEmployeeExtension { + + static Employee getDefaultObj(Employee self) { + return new Employee(firstName: "firstName", lastName: "lastName", age: 20) + } +} \ No newline at end of file diff --git a/core-groovy-2/src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule b/core-groovy-2/src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule new file mode 100644 index 0000000000..967108b846 --- /dev/null +++ b/core-groovy-2/src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule @@ -0,0 +1,4 @@ +moduleName=core-groovy-2 +moduleVersion=1.0-SNAPSHOT +extensionClasses=com.baeldung.metaprogramming.extension.BasicExtensions +staticExtensionClasses=com.baeldung.metaprogramming.extension.StaticEmployeeExtension diff --git a/core-groovy-2/src/test/groovy/com/baeldung/metaprogramming/MetaprogrammingUnitTest.groovy b/core-groovy-2/src/test/groovy/com/baeldung/metaprogramming/MetaprogrammingUnitTest.groovy new file mode 100644 index 0000000000..8066b10f9b --- /dev/null +++ b/core-groovy-2/src/test/groovy/com/baeldung/metaprogramming/MetaprogrammingUnitTest.groovy @@ -0,0 +1,118 @@ +package com.baeldung.metaprogramming + +import groovy.time.TimeCategory + +class MetaprogrammingUnitTest extends GroovyTestCase { + + Employee emp = new Employee(firstName: "Norman", lastName: "Lewis") + + void testPropertyMissing() { + assert emp.address == "property 'address' is not available" + } + + void testMethodMissing() { + Employee emp = new Employee() + try { + emp.getFullName() + } catch(MissingMethodException e) { + println "method is not defined" + } + assert emp.getFullName() == "method 'getFullName' is not defined" + } + + void testMetaClassProperty() { + Employee.metaClass.address = "" + emp = new Employee(firstName: "Norman", lastName: "Lewis", address: "US") + assert emp.address == "US" + } + + void testMetaClassMethod() { + emp.metaClass.getFullName = { + "$lastName, $firstName" + } + assert emp.getFullName() == "Lewis, Norman" + } + + void testMetaClassConstructor() { + try { + Employee emp = new Employee("Norman") + } catch(GroovyRuntimeException e) { + assert e.message == "Could not find matching constructor for: com.baeldung.metaprogramming.Employee(String)" + } + + Employee.metaClass.constructor = { String firstName -> + new Employee(firstName: firstName) + } + + Employee norman = new Employee("Norman") + assert norman.firstName == "Norman" + assert norman.lastName == null + } + + void testJavaMetaClass() { + String.metaClass.capitalize = { String str -> + str.substring(0, 1).toUpperCase() + str.substring(1); + } + assert "norman".capitalize() == "Norman" + } + + void testEmployeeExtension() { + Employee emp = new Employee(age: 28) + assert emp.getYearOfBirth() == 1991 + } + + void testJavaClassesExtensions() { + 5.printCounter() + + assert 40l.square() == 1600l + + assert (2.98).cube() == 26.463592 + } + + void testStaticEmployeeExtension() { + assert Employee.getDefaultObj().firstName == "firstName" + assert Employee.getDefaultObj().lastName == "lastName" + assert Employee.getDefaultObj().age == 20 + } + + void testToStringAnnotation() { + Employee employee = new Employee() + employee.id = 1 + employee.firstName = "norman" + employee.lastName = "lewis" + employee.age = 28 + + assert employee.toString() == "Employee(norman, lewis, 28)" + } + + void testTupleConstructorAnnotation() { + Employee norman = new Employee(1, "norman", "lewis", 28) + assert norman.toString() == "Employee(norman, lewis, 28)" + + Employee snape = new Employee(2, "snape") + assert snape.toString() == "Employee(snape, null, 0)" + + } + + void testEqualsAndHashCodeAnnotation() { + Employee norman = new Employee(1, "norman", "lewis", 28) + Employee normanCopy = new Employee(1, "norman", "lewis", 28) + assert norman.equals(normanCopy) + assert norman.hashCode() == normanCopy.hashCode() + } + + void testAutoCloneAnnotation() { + try { + Employee norman = new Employee(1, "norman", "lewis", 28) + def normanCopy = norman.clone() + assert norman == normanCopy + } catch(CloneNotSupportedException e) { + e.printStackTrace() + } + } + + void testLoggingAnnotation() { + Employee employee = new Employee(1, "Norman", "Lewis", 28) + employee.logEmp() + } +} \ No newline at end of file