संक्षिप्त उत्तर आप नहीं कर सकते हैं। लंबे जवाब है:
क्या आप के लिए कोड की जरूरत पर निर्भर करता है, तो आप शायद
// file: example1.groovy
def a = { it.twice() }
println a.metaClass.classNode.getDeclaredMethods("doCall")[0].code.text
// prints: { return it.twice() }
लेकिन
के साथ भाग मिल सकता है के रूप में
में विस्तार से बताया आप स्क्रिप्ट रनटाइम पर classpath में उपलब्ध के स्रोत कोड की आवश्यकता होगी
groovy.lang.MetaClass # getClassNode()
"MetaClass के लिए मूल एएसटी के लिए एक संदर्भ मिल गया तो यह उपलब्ध है क्रम
०१२३५१६४१०६१ पर मूल एएसटी या नल @return अगर यह नहीं किया जा सकता ,
और
पाठ चाल लौटे "वास्तव में एक ही कोड, बस एएसटी के प्रतिनिधित्व की तरह एक कोड प्रदान नहीं करता के रूप में किया जा सकता है
// file: example2.groovy
def b = {p-> p.twice() * "p"}
println b.metaClass.classNode.getDeclaredMethods("doCall")[0].code.text
// prints: { return (p.twice() * p) }
अभी भी इस स्क्रिप्ट में देखा है, यह उपयोगी यह है के रूप में अगर तुम सिर्फ शीघ्रता से अवलोकन
ले जाना चाहते हो सकता है और, अगर आप अपने हाथों पर बहुत अधिक समय है और आप अपने खुद के org.codehaus.groovy.ast.GroovyCodeVisitor
लिख सकता है सुंदर मुद्रित करने के लिए यह
या, जैसे groovy.inspect.swingui.AstNodeToScriptVisitor
// file: example3.groovy
def c = {w->
[1,2,3].each {
println "$it"
(1..it).each {x->
println 'this seems' << ' somewhat closer' << ''' to the
original''' << " $x"
}
}
}
def node = c.metaClass.classNode.getDeclaredMethods("doCall")[0].code
def writer = new StringWriter()
node.visit new groovy.inspect.swingui.AstNodeToScriptVisitor(writer)
println writer
// prints: return [1, 2, 3].each({
// this.println("$it")
// return (1.. it).each({ java.lang.Object x ->
// return this.println('this seems' << ' somewhat closer' << ' to the \n original' << " $x")
// })
// })
पहले से मौजूद किसी अब चोरी क्या करना है पता नहीं है।
अगर आप मूल, सटीक, runnable कोड चाहते हैं ... आप
मेरा मतलब, आप स्रोत लाइन जानकारी इस्तेमाल कर सकते हैं भाग्य से बाहर हैं, लेकिन पिछली बार मैं जाँच की, यह वास्तव में उन्हें सही हो रही है नहीं कर रहा था
// file: example1.groovy
....
def code = a.metaClass.classNode.getDeclaredMethods("doCall")[0].code
println "$code.lineNumber $code.columnNumber $code.lastLineNumber $code.lastColumnNumber"
new File('example1.groovy').readLines()
... etc etc you get the idea.
लाइन नंबर कम से कम मूल कोड हालांकि
यह शायद अपने हालात से मेल नहीं खाएगी पास हो shuld, लेकिन आप अन्य रास्ते जाने के लिए सक्षम हो सकता है। ग्रोवीशेल के बाद से।मूल्यांकन() एक स्ट्रिंग निष्पादित करता है (उचित var बाइंडिंग के साथ), अगर बंद करना केवल एक स्ट्रिंग था? –