KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jpa > verification > fixes > RemoveFinalModifier


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.jpa.verification.fixes;
21
22 import com.sun.source.tree.ClassTree;
23 import com.sun.source.tree.ModifiersTree;
24 import java.io.IOException JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import javax.lang.model.element.Modifier;
29 import javax.lang.model.element.TypeElement;
30 import org.netbeans.api.java.source.CancellableTask;
31 import org.netbeans.api.java.source.ElementHandle;
32 import org.netbeans.api.java.source.JavaSource;
33 import org.netbeans.api.java.source.TreeMaker;
34 import org.netbeans.api.java.source.WorkingCopy;
35 import org.netbeans.modules.j2ee.jpa.verification.JPAProblemFinder;
36 import org.netbeans.spi.editor.hints.ChangeInfo;
37 import org.netbeans.spi.editor.hints.Fix;
38 import org.openide.filesystems.FileObject;
39 import org.openide.util.NbBundle;
40
41 /**
42  * @see org.netbeans.modules.j2ee.jpa.verification.rules.entity.SerializableClass
43  * @author Tomasz.Slota@Sun.COM
44  */

45 public class RemoveFinalModifier implements Fix {
46         private FileObject fileObject;
47     private ElementHandle<TypeElement> classHandle;
48     
49     /** Creates a new instance of ImplementSerializable */
50     public RemoveFinalModifier(FileObject fileObject, ElementHandle<TypeElement> classHandle) {
51         this.classHandle = classHandle;
52         this.fileObject = fileObject;
53     }
54     
55     public ChangeInfo implement(){
56         CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>(){
57             public void cancel() {}
58             
59             public void run(WorkingCopy workingCopy) throws Exception JavaDoc {
60                 workingCopy.toPhase(JavaSource.Phase.RESOLVED);
61                 TypeElement clazz = classHandle.resolve(workingCopy);
62                 
63                 if (clazz != null){
64                     ClassTree clazzTree = workingCopy.getTrees().getTree(clazz);
65                     TreeMaker make = workingCopy.getTreeMaker();
66                     
67                     Set JavaDoc<Modifier> flags = new HashSet JavaDoc<Modifier>(clazzTree.getModifiers().getFlags());
68                     flags.remove(Modifier.FINAL);
69                     ModifiersTree newModifiers = make.Modifiers(flags, clazzTree.getModifiers().getAnnotations());
70                     workingCopy.rewrite(clazzTree.getModifiers(), newModifiers);
71                 }
72             }
73         };
74         
75         JavaSource javaSource = JavaSource.forFileObject(fileObject);
76         
77         try{
78             javaSource.runModificationTask(task).commit();
79         } catch (IOException JavaDoc e){
80             JPAProblemFinder.LOG.log(Level.SEVERE, e.getMessage(), e);
81         }
82         return null;
83     }
84     
85     public int hashCode(){
86         return 1;
87     }
88     
89     public boolean equals(Object JavaDoc o){
90         // TODO: implement equals properly
91
return super.equals(o);
92     }
93     
94     public String JavaDoc getText(){
95         return NbBundle.getMessage(CreatePersistenceUnit.class, "LBL_RemoveFinalModifier");
96     }
97 }
98
Popular Tags