java - How do I create a FieldDeclaration given an IField (Eclipe plugin) -
given have access ifield field (parsed java file), how create fielddeclaration in order add ast?
string varname = field.getelementname(); string typename = signature.tostring(field.gettypesignature()); variabledeclarationfragment fieldfrag = ast.newvariabledeclarationfragment(); fieldfrag.setname(ast.newsimplename(varname)); fielddeclaration field = ast.newfielddeclaration(fieldfrag); type fieldtype = ast.newsimpletype(ast.newsimplename(typename)); field.settype(fieldtype); field.modifiers().add(ast.newmodifier(modifierkeyword));
the above
type fieldtype = ast.newsimpletype(ast.newsimplename(typename));
only works if typename not java keyword. there way create fielddeclaration ifield info (modifier, type, variable)
thanks
i've found way using copysubtree:
ast ast = targetcompilationunit.getast(); fielddeclaration oldfielddeclaration = astnodesearchutil.getfielddeclarationnode(field, sourcecompilationunit); type oldtype = oldfielddeclaration.gettype(); type newtype = (type) astnode.copysubtree(ast, oldtype);
then newtype can used plug fielddeclaration
Comments
Post a Comment