星期五, 九月 10, 2004

Groovy 中文 FAQ

QA使用Groovy Tips:
PreConditon:Groovy 1.0 Beta 6 jvm 1.4.2_04 windows XP

1. 如何声明一个Groovy 类似的方法或函数调用
Groovy没有明确的关键词类似Function或类似语法。
Groovy通过Closure块来实现常用功能库.
Example 1:
A={println “Hello,Wolrd”;
Println “String”.getBytes();
}
A();
Example 2:使用参数
ClosureA={
Name | println “你是”+Name;
}
ClosureA(“ Peter”);

2. 如何动态运行Groovy代码?
Q:如果把Groovy代码放在文件中或数据库中,或从用户输入,如何执行呢?
A:Groovy 代码放在文件中
可以通过CMI:groovy.bat 文件.groovy来运行.
如果是从Java语言中运行存放在文件中的groovy代码.必须用以下语法
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;
import groovy.lang.Binding;
import groovy.lang.Closure;
// Create a class loader
GroovyClassLoader groovyLoader =
new GroovyClassLoader();
// Create the shell
GroovyShell shell =
new GroovyShell( groovyLoader, new Binding() );
如果定义的Groovy class就要用:
Class closureClass=grrovyLoader.loadClass(“groovy file.”);
如果groovy是业务逻辑代码:
GroovyShell shell = new GroovyShell( new Binding() );
shell.evaluate( new File( "GroovyXML4.groovy" ) );
// Get the calculateAmounts closure
Closure calc =
(Closure)shell.getVariable( "calculateAmounts" );
3. 如何判断一段Groovy代码是否语法正确?
Import groovy.lang.GroovyShell;
Import groovy.lang.Binding;
GroovyShell shell = new GroovyShell(new Binding());
String groovyScript=””;
try
{
shell.parse(groovyScript); //解析是否正确
}catch(Exception e)
{
return false;
}
return true;

4. 如何把字符串当作groovy代码看待?
GroovyShell shell =new GroovyShell(new Binding() );
codeString="import groovy.lang.*;
println 'this is test';
";
Script script = shell.parse(codeString);
script.run();

5. 用字符串构造groovy closure
codeString=”{ |basicinfo,employment,attendence| println “basicinfo:”+basicinfo;
println “employment:”+employment;
println “attendence”+attendence;
}
“;
block=shell.evaluate(codeString);
block("william","aaa",12);//Cool!!

6. 在Java中如何使用Groovy 对象?
GroovyShell _shell=new GroovyShell(new Binding());
//Test 1 From String 2 Closure Object
StringBuffer groovyCode=new StringBuffer().append("{"};
groovyCode.append("|name,sex,high|");//Parameter of Closure
groovyCode.append(" println \"你的名字:\"+name;");
groovyCode.append(" println \"性别:\"+sex;");
groovyCode.append(" println \"身高:\"+high;");
groovyCode.append(")");
Object[] params={new String("William"),new String("Male"),new Integer(10)};
Closure temp =(Closure) _shell.evaluate(groovyCode.toString());
temp.call(params);

7. 如何实现复杂对象传递.
Groovy中的Map对象为java.util.HashMap对象。
Map的初始化为[:]( list的初始化为[])

经典代码:
Query_SQL="select distinct contactphone,a.empid,businessrule from emp_M_basicInfo a inner join emp_M_SalaryBasicInfo a1,pay_M_catagoryitem b1 on a.factorycode=a1.factorycode and a.empid = a1.empid and a1.paycatagorycode=b1.paycatagorycode and a1.factorycode=b1.factorycode "
sql = Sql.newInstance("jdbc:mysql://localhost:3306/PCD","root","","org.gjt.mm.mysql.Driver");

rsetmap=[:]

sql.eachRow(Query_SQL,{rset|
metadata=rset.getMetaData();
tempmap=[:]

for ( index in 1..metadata.getColumnCount())
{
tempmap.put("${metadata.getColumnName(index).toLowerCase()}","${rset.getObject(index)}");
}
println tempmap
rsetmap.put(rset.getRow(),tempmap);
});
//println rsetmap.values().each{println it} //ok.
//println rsetmap.size();



testa=rsetmap.values();
try{
testa.each {println it.empid}
} catch(Exception e) {println "erro"+e.getMessage();}