1 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 ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 import java.util.logging.Level ; 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 45 public class RemoveFinalModifier implements Fix { 46 private FileObject fileObject; 47 private ElementHandle<TypeElement> classHandle; 48 49 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 { 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 <Modifier> flags = new HashSet <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 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 o){ 90 return super.equals(o); 92 } 93 94 public String getText(){ 95 return NbBundle.getMessage(CreatePersistenceUnit.class, "LBL_RemoveFinalModifier"); 96 } 97 } 98 | Popular Tags |