Created
August 4, 2016 05:39
-
-
Save fmamud/23845248def4d15fc38dbd4d2608d6d8 to your computer and use it in GitHub Desktop.
Groovy compiler customization with type checking and static compilation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beforeVisitMethod { methodNode -> | |
println "calling ${methodNode.name}(${methodNode.parameters.join(',')})" | |
} | |
afterMethodCall { call -> | |
if (getTargetMethod(call).name=='plus') { | |
addStaticTypeError('plus() not allowed',call) | |
handled = true | |
} | |
if (getTargetMethod(call).name=='somar') { | |
if (getTargetMethod(call).returnType.typeClass != int) { | |
addStaticTypeError('somar() must returns int', call) | |
handled = true | |
} | |
if (getTargetMethod(call).parameters*.type*.typeClass != [int, int]) { | |
addStaticTypeError('somar() must be int, int parameters', call) | |
handled = true | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int somar(int a, int b) { | |
a + b | |
} | |
println "Resultado: ${somar(2, 2)}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.CompileStatic | |
withConfig(configuration) { | |
ast(extensions: ['MyExtension.groovy'], CompileStatic) | |
inline(phase:'CONVERSION') { source, context, classNode -> | |
println ">> CONVERSION $classNode" | |
} | |
imports { | |
star 'java.time' | |
normal 'java.util.function.Function', 'java.util.function.Supplier' | |
} | |
secureAst { | |
closuresAllowed = false | |
methodDefinitionAllowed = true | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
groovy --configscript rules.groovy MyTypedScript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment