KickJava   Java API By Example, From Geeks To Geeks.

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


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.ExpressionTree;
24 import java.io.IOException JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import javax.lang.model.element.TypeElement;
27 import org.netbeans.api.java.source.CancellableTask;
28 import org.netbeans.api.java.source.ElementHandle;
29 import org.netbeans.api.java.source.JavaSource;
30 import org.netbeans.api.java.source.TreeMaker;
31 import org.netbeans.api.java.source.WorkingCopy;
32 import org.netbeans.modules.j2ee.jpa.verification.JPAProblemFinder;
33 import org.netbeans.spi.editor.hints.ChangeInfo;
34 import org.netbeans.spi.editor.hints.Fix;
35 import org.openide.filesystems.FileObject;
36 import org.openide.util.NbBundle;
37
38 /**
39  * @see org.netbeans.modules.j2ee.jpa.verification.rules.entity.SerializableClass
40  * @author Tomasz.Slota@Sun.COM
41  */

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