Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import java.lang.reflect.Method;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import groovyx.gaelyk.GaelykCategory;
// scaffolds DSLD for category class to the system console
// setup
String provider = "Gaelyk"
Class category = GaelykCategory
Map<Class, List<Method>> supported = [:]
category.methods.each { Method m ->
if(m.declaringClass == category && m.parameterTypes.size() >=1 && Modifier.isStatic(m.getModifiers())){
def toBeSupported = m.parameterTypes[0]
def supportedMethods = supported[toBeSupported]
if(!supportedMethods){
supportedMethods = []
supported[toBeSupported] = supportedMethods
}
supportedMethods << m
}
}
supported.each{ Class supportedClass, List<Method> methods ->
// change it to your own pointcut definition
println "shortcutFor('${nameOf(supportedClass)}').accept {"
println " provider = '$provider'"
methods.each{ Method m ->
println " method name: '$m.name',"
println " type: '${nameOf(m.returnType)}',"
if(m.parameterTypes.size() > 1){
def i = 0
print " params: ["
print m.parameterTypes[1..-1].collect{ type -> i++; "arg$i: '${nameOf(type)}'"}.join(", ")
println "],"
}
println " doc: 'TODO'"
println()
// beware can produce duplicates
if(isPropertyMethod(m)){
println " property name: '${toPropertyName(m.name)}',"
if(isGetter(m)){
println " type: '${nameOf(m.returnType)}',"
} else if(isSetter(m)){
println " type: '${nameOf(m.parameterTypes[1])}',"
}
println " doc: 'TODO'"
println()
}
}
println "}"
println()
}
String nameOf(Class type){
type.isArray() ? (type.componentType.name + '[]' ): type.name
}
boolean isPropertyMethod(Method m){
isSetter(m) || isGetter(m)
}
boolean isSetter(Method m){
m.parameterTypes.size() == 2 && m.name.size() > 3 &&m.name.startsWith("set") && m.returnType.name == 'void'
}
boolean isGetter(Method m){
m.parameterTypes.size() == 1 && m.name.size() > 3 && (m.name.startsWith("get") || m.name.startsWith("is")) && m.returnType.name != 'void'
}
String toPropertyName(String methodName){
switch(methodName) {
case ~/(set|get).+/: return methodName[3].toLowerCase() + (methodName.size() > 4 ? methodName[4..-1] : '')
case ~/is.+/: return methodName[2].toLowerCase() + (methodName.size() > 3 ? methodName[3..-1] : '')
default: throw new IllegalArgumentException("Illegal method name: $methodName")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment