[英]CodeModel How do i get value at specific index with com.sun.codemodel
I'm using com.sun.codemodel for generating my classes. 我正在使用com.sun.codemodel生成我的类。 I need to dynamically get array value depending on index argument.
我需要根据索引参数动态获取数组值。
So i have an invocation that suppose to return an array 所以我有一个调用,假设返回一个数组
JcodeModel model = new JCodeModel();
JPackage jPackage = codeModel._package(packageName);
.....
JType personType = codeModel._ref(Person.class);
jVar personObject = method.decl(personType, "person", JExpr._new(personType));
personObject.invoke("getRoles");???
In this case getRoles() returns an array and i want to get an object at a specific index. 在这种情况下,getRoles()返回一个数组,我想在特定索引处获取一个对象。 Something like this
像这样
int index = 0;
Person person = new Person();
String role = person.getRoles()[index];
What should i do? 我该怎么办?
Don't know whether I'm right, but maybe following will work for you: 不知道我是否正确,但是以下方法可能对您有用:
JInvocation invocation = personObject.invoke("getRoles");
JArrayCompRef arrayCompRef = invocation.component(indexExpression);
Found this in following JavaDoc: http://codemodel.java.net/nonav/apidocs/com/sun/codemodel/JExpressionImpl.html#component(com.sun.codemodel.JExpression ) 在以下JavaDoc中找到了此代码: http ://codemodel.java.net/nonav/apidocs/com/sun/codemodel/JExpressionImpl.html#component( com.sun.codemodel.JExpression )
Hope this helps... 希望这可以帮助...
最坏的情况下,您始终可以使用JBlock.directStatement()...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.