Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » How to modify the field declaration with jdt
How to modify the field declaration with jdt [message #259174] Sat, 21 March 2009 21:38 Go to next message
Eclipse UserFriend
Hi, all

Here I am curious how to modify the field declaration with JDT
programmaticall.
I have a field declaration like this:
<pre>
public final int count = 3; // e.g. I want to change the count value
3 to 1
</pre>

Is there any way to change the count value, (e.g. 3 to 1)?

I found no proper setter method in IField.
At first, I try to do like this:
1. IField.delete(...) // delete the field first
2. IType.createField(...) // create a new field with new value
declaration.
but, it also came up with a new problem, after above steps, the new field
was created correctly, but the old field was not removed, so there's two
duplicate fields...

What ever, is there any wat to modify the field declaration with JDT?
Any clue is appreciated

--
Thanks & Best Regards!
///
(. .)
--------ooO--(_)--Ooo--------
| Nick Tan |
-----------------------------
Re: How to modify the field declaration with jdt [message #259178 is a reply to message #259174] Sun, 22 March 2009 07:55 Go to previous messageGo to next message
Eclipse UserFriend
Well, I find a way to do the modification with AST, but I DON NOT thinks
it's best practices, is there any better ways?

<code>
ICompilationUnit cu = type.getCompilationUnit();

// creation of DOM/AST from a ICompilationUnit
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(cu);
CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);

// start record of the modifications
astRoot.recordModifications();

List<TypeDeclaration> types = astRoot.types();
TypeDeclaration typeDecl = null;
for (TypeDeclaration td : types) {
if
(td.getName().getFullyQualifiedName().equals(type.getTypeQua lifiedName())) {
typeDecl = td;
break;
}
}

boolean isDirty = false;
if (typeDecl != null) {
FieldDeclaration[] fieldDeclarations = typeDecl.getFields();
for (FieldDeclaration fd : fieldDeclarations) {
List<VariableDeclarationFragment> vdfs = fd.fragments();
for (VariableDeclarationFragment vdf : vdfs) {
if (vdf.getName().getFullyQualifiedName().equals(fieldName)) {
Expression expr = vdf.getInitializer();
if (expr.getNodeType() == ASTNode.NUMBER_LITERAL) {
NumberLiteral numExpr = (NumberLiteral) expr;
numExpr.setToken(marker.getAttribute(ISTIPletMarkar.ATTR_ENT ITY_VALUE,
"")); //$NON-NLS-1$
isDirty = true;
break;
}
}
}
}
}

if (isDirty) {
String source = cu.getSource();
Document document = new Document(source);

// computation of the text edits
TextEdit edits = astRoot.rewrite(document,
cu.getJavaProject().getOptions(true));

// computation of the new source code
edits.apply(document);
String newSource = document.get();

// update of the compilation unit
cu.getBuffer().setContents(newSource);
}
</code>

And there is another flaw that when I UNDO this operation in JAVA editor,
the whole content replaced to the old one rather than the changed field one
(e.g. count = 3;), because I invoke
cu.getBuffer().setContents(newSource);
which cause the above problem, it's only a little not user-friendly, I
think.


"nick tan" <missedone@gmail.com> wrote in message
news:fe79c10855cad7bad6c51027ef4dde7b$1@www.eclipse.org...
> Hi, all
>
> Here I am curious how to modify the field declaration with JDT
> programmaticall.
> I have a field declaration like this:
> <pre>
> public final int count = 3; // e.g. I want to change the count value 3
> to 1
> </pre>
>
> Is there any way to change the count value, (e.g. 3 to 1)?
>
> I found no proper setter method in IField.
> At first, I try to do like this:
> 1. IField.delete(...) // delete the field first
> 2. IType.createField(...) // create a new field with new value
> declaration.
> but, it also came up with a new problem, after above steps, the new field
> was created correctly, but the old field was not removed, so there's two
> duplicate fields...
>
> What ever, is there any wat to modify the field declaration with JDT?
> Any clue is appreciated
>
> --
> Thanks & Best Regards!
> ///
> (. .)
> --------ooO--(_)--Ooo--------
> | Nick Tan |
> -----------------------------
>
Re: How to modify the field declaration with jdt [message #259186 is a reply to message #259178] Sun, 22 March 2009 16:47 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: alamothe.ptt.yu

Nick Tan wrote:
> And there is another flaw that when I UNDO this operation in JAVA
> editor, the whole content replaced to the old one rather than the changed
> field one (e.g. count = 3;), because I invoke
> cu.getBuffer().setContents(newSource);
> which cause the above problem, it's only a little not user-friendly, I
> think.

cu.applyTextEdit(...) should solve this problem. You still need the
document, but you need not apply the edit on it.
Re: How to modify the field declaration with jdt [message #259191 is a reply to message #259178] Sun, 22 March 2009 16:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: alamothe.ptt.yu

Also, if you already have an IField, it's easiest to find the corresponding
declaration in the AST using org.eclipse.jdt.internal.corext.dom.NodeFinder.

Nick Tan wrote:
> Well, I find a way to do the modification with AST, but I DON NOT
> thinks it's best practices, is there any better ways?
>
> <code>
> ICompilationUnit cu = type.getCompilationUnit();
>
> // creation of DOM/AST from a ICompilationUnit
> ASTParser parser = ASTParser.newParser(AST.JLS3);
> parser.setSource(cu);
> CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
>
> // start record of the modifications
> astRoot.recordModifications();
>
> List<TypeDeclaration> types = astRoot.types();
> TypeDeclaration typeDecl = null;
> for (TypeDeclaration td : types) {
> if
> (td.getName().getFullyQualifiedName().equals(type.getTypeQua lifiedName()))
> { typeDecl = td;
> break;
> }
> }
>
> boolean isDirty = false;
> if (typeDecl != null) {
> FieldDeclaration[] fieldDeclarations = typeDecl.getFields();
> for (FieldDeclaration fd : fieldDeclarations) {
> List<VariableDeclarationFragment> vdfs = fd.fragments();
> for (VariableDeclarationFragment vdf : vdfs) {
> if (vdf.getName().getFullyQualifiedName().equals(fieldName))
> { Expression expr = vdf.getInitializer();
> if (expr.getNodeType() == ASTNode.NUMBER_LITERAL) {
> NumberLiteral numExpr = (NumberLiteral) expr;
>
> numExpr.setToken(marker.getAttribute(ISTIPletMarkar.ATTR_ENT ITY_VALUE,
> "")); //$NON-NLS-1$ isDirty = true;
> break;
> }
> }
> }
> }
> }
>
> if (isDirty) {
> String source = cu.getSource();
> Document document = new Document(source);
>
> // computation of the text edits
> TextEdit edits = astRoot.rewrite(document,
> cu.getJavaProject().getOptions(true));
>
> // computation of the new source code
> edits.apply(document);
> String newSource = document.get();
>
> // update of the compilation unit
> cu.getBuffer().setContents(newSource);
> }
> </code>
>
> And there is another flaw that when I UNDO this operation in JAVA
> editor, the whole content replaced to the old one rather than the changed
> field one (e.g. count = 3;), because I invoke
> cu.getBuffer().setContents(newSource);
> which cause the above problem, it's only a little not user-friendly, I
> think.
Re: How to modify the field declaration with jdt [message #259194 is a reply to message #259191] Mon, 23 March 2009 00:03 Go to previous message
Eclipse UserFriend
awesome! It all works.
Thanks!


"Nikola Mihajlovic" <alamothe@ptt.yu> 写入消息
news:gq68d8$egp$1@build.eclipse.org...
> Also, if you already have an IField, it's easiest to find the
> corresponding declaration in the AST using
> org.eclipse.jdt.internal.corext.dom.NodeFinder.
>
> Nick Tan wrote:
>> Well, I find a way to do the modification with AST, but I DON NOT
>> thinks it's best practices, is there any better ways?
>>
>> <code>
>> ICompilationUnit cu = type.getCompilationUnit();
>>
>> // creation of DOM/AST from a ICompilationUnit
>> ASTParser parser = ASTParser.newParser(AST.JLS3);
>> parser.setSource(cu);
>> CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
>>
>> // start record of the modifications
>> astRoot.recordModifications();
>>
>> List<TypeDeclaration> types = astRoot.types();
>> TypeDeclaration typeDecl = null;
>> for (TypeDeclaration td : types) {
>> if
>> (td.getName().getFullyQualifiedName().equals(type.getTypeQua lifiedName()))
>> { typeDecl = td;
>> break;
>> }
>> }
>>
>> boolean isDirty = false;
>> if (typeDecl != null) {
>> FieldDeclaration[] fieldDeclarations = typeDecl.getFields();
>> for (FieldDeclaration fd : fieldDeclarations) {
>> List<VariableDeclarationFragment> vdfs = fd.fragments();
>> for (VariableDeclarationFragment vdf : vdfs) {
>> if (vdf.getName().getFullyQualifiedName().equals(fieldName))
>> { Expression expr = vdf.getInitializer();
>> if (expr.getNodeType() == ASTNode.NUMBER_LITERAL) {
>> NumberLiteral numExpr = (NumberLiteral) expr;
>>
>> numExpr.setToken(marker.getAttribute(ISTIPletMarkar.ATTR_ENT ITY_VALUE,
>> "")); //$NON-NLS-1$ isDirty = true;
>> break;
>> }
>> }
>> }
>> }
>> }
>>
>> if (isDirty) {
>> String source = cu.getSource();
>> Document document = new Document(source);
>>
>> // computation of the text edits
>> TextEdit edits = astRoot.rewrite(document,
>> cu.getJavaProject().getOptions(true));
>>
>> // computation of the new source code
>> edits.apply(document);
>> String newSource = document.get();
>>
>> // update of the compilation unit
>> cu.getBuffer().setContents(newSource);
>> }
>> </code>
>>
>> And there is another flaw that when I UNDO this operation in JAVA
>> editor, the whole content replaced to the old one rather than the changed
>> field one (e.g. count = 3;), because I invoke
>> cu.getBuffer().setContents(newSource);
>> which cause the above problem, it's only a little not user-friendly, I
>> think.
>
>
Previous Topic:How to get the (inner) IType of a IField
Next Topic:Files not being copied to output folder
Goto Forum:
  


Current Time: Mon Apr 21 05:26:54 EDT 2025

Powered by FUDForum. Page generated in 0.04325 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top