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

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -