[英]CodeModel How do i add @Pattern.List annotations com.sun.codemodel
[英]CodeModel How do i get value at specific index with com.sun.codemodel
我正在使用com.sun.codemodel生成我的類。 我需要根據索引參數動態獲取數組值。
所以我有一個調用,假設返回一個數組
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");???
在這種情況下,getRoles()返回一個數組,我想在特定索引處獲取一個對象。 像這樣
int index = 0;
Person person = new Person();
String role = person.getRoles()[index];
我該怎么辦?
不知道我是否正確,但是以下方法可能對您有用:
JInvocation invocation = personObject.invoke("getRoles");
JArrayCompRef arrayCompRef = invocation.component(indexExpression);
在以下JavaDoc中找到了此代碼: http ://codemodel.java.net/nonav/apidocs/com/sun/codemodel/JExpressionImpl.html#component( com.sun.codemodel.JExpression )
希望這可以幫助...
最壞的情況下,您始終可以使用JBlock.directStatement()...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.