MyDevLife (257) 썸네일형 리스트형 14. MOP - Runtime MOP (Meta-Object-Protocol) 는 메소드 호출에 대한 요청이 Groovy 런타임 시스템에서 처리되는 방법과 중간 계층을 제어하는 방법에 대한 규칙 모음이다. https://www.slideshare.net/AliTanwir/metaprogramming-with-groovy-64544282 보통 MOP 는 런타임과 컴파일 타임에서 사용되는 형식이 다르며 컴파일 타임 MOP 는 어노테이션 파트로 https://groovy-lang.org/metaprogramming.html 다음장에서 살펴보자. POGO Groovy 는 오브젝트를 크게 3가지 오브젝트로 분할하여 관리한다. 이중 GroovyObject 인터페이스를 구현한 클래스를 POGO 오브젝트라고 한다. 통상 Groovy 로 선언한 모든.. 13. OOP JAVA 에 비해서 Groovy 의 Class 는 상대적으로 쉽고 간단히 Class 를 생성할 수 있다. Class 일단 Groovy 의 class 에서는 public private 를 넣지 않는데 Groovy 의 모든 클래스는 public 이다. 필요하다면 private 을 넣어 선언해도 상관없으므로 private 클래스인 경우에는 private 을 넣으면 된다. class Person { } public class Person implements GroovyObject { @Generated public Person() { CallSite[] var1 = $getCallSiteArray(); super(); MetaClass var2 = this.$getStaticMetaClass(); this.me.. 12. Exception Exception 은 Java 와 비슷하지만 throws 를 따로 작성하지 않아도 된다. // Exception /* public void foo() throws Exception{ throw new Exception() } */ def foo() { throw new Exception("Foo Exception") } List log = [] try { foo() } catch (Exception e) { log 11. Conditional Statement 이번에는 Groovy 에서 사용되는 제어문들에 대해 알아보자. Boolean 데이터 타입에 따라 true / false 로 판단할 수 있는 기준이 다르다. 기본 타입을 제외하면 null 체크가 된다. If statement java 에서 사용되는 if ~ else if ~ else 를 그대로 사용한다. //-------------------------------------------- // if 제어문 // if(boolean expression} {//logic} //-------------------------------------------- if(true){ println "true" } def age = 35 if( age > 5 && age = numbers.size()) { break } p.. 10. Closures http://groovy-lang.org/closures.html 그루비 클로저는 이름없는 메서드에 대한 포인터에 해당하며 함수를 함수 포인터에 저장하는것에 불과하다. 클로저 정의는 중괄호 { } 를 이용해서 하며 호출시는 .call() 또는 일반 메서드 처럼 ( ) 으로 실행할 수 있다. Groovy 에서는 아주 많은 부분에서 클로저를 사용하기 때문에 반드시 알고 활용할 수 있어야 한다. Basic def c = {} println c.class.name Test$_run_closure1 def exMap = [:] def exClosure = { println "Hello" } exMap.closureProp = exClosure exMap.closureProp() Hello 기본적으로 함수와 비슷하.. 09. Collection Groovy 의 Collection 은 JAVA 의 Collection 보다 사용이 상당히 편하다. Range 이론 부분은 http://groovy-lang.org/api.html 에서 Range 로 검색해보면 해당 클래스는 인터페이스로 사용자 입력에 따라 IntRange 등으로 변형된다. 사용자의 Range 입력은 From .. To 형식으로 입력하면 되며 부등호를 사용할 수 있다. 선언한 Range r = 1..10 println r println r.class println r.from println r.to 1..10 class groovy.lang.IntRange 1 10 Range r2 = 1.. 08. RegExp Groovy 에서 정규 표현식을 사용할 수 있으며 기존 다른 언어에서 사용하는 방식과 크게 다르지 않다. Regexp Operator Groovy 에서는 regexp 를 위해 아래 operator 를 사용한다. Pattern 오퍼레이터와 같이 사용되는 pattern 은 기존 regexp 에서 사용하는 그대로 사용한다. 정규식을 이용하는 좀 더 자세한 사항은 아래 레퍼런스 참조하자. https://www.regular-expressions.info/refquick.html Example def text = 'Some 42 number #12 more' // ~ 는 패턴식을 정의하기 위해 사용 def pattern = ~/\d+/ // =~ 는 find def matchedStrings = (text =~ .. 07. String Groovy 의 String 은 기본적으로 JAVA 의 String 과 동일하지만 변수로 사용될 때는 ${변수명} 를 사용할 수 있다. 단 쌍따옴표를 사용하는 경우만 변수로 치환된다. 멀티라인 스트링을 사용하는 경우 쌍따옴표 3개 혹은 홑 따옴표 3 개를 연달아 쓰면 되겠다. //String Interpolation def name = "Dan" def message = "Hello ${name}" def message2 = 'Hello ${name}' println 'Message :' + message println 'Message2 :' + message2 //Multiple String And Interpolation def message3 = """ Hello ${name} a b c """ d.. 이전 1 ··· 21 22 23 24 25 26 27 ··· 33 다음