KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > action > CmFieldGenerator


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbcore.action;
21
22 import com.sun.source.tree.ClassTree;
23 import com.sun.source.tree.MethodTree;
24 import java.io.IOException JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.lang.model.element.Modifier;
30 import javax.lang.model.element.TypeElement;
31 import org.netbeans.api.java.source.JavaSource;
32 import org.netbeans.api.java.source.WorkingCopy;
33 import org.netbeans.modules.j2ee.common.method.MethodModel;
34 import org.netbeans.modules.j2ee.common.method.MethodModelSupport;
35 import org.netbeans.modules.j2ee.common.source.AbstractTask;
36 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField;
37 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
38 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil;
39 import org.openide.filesystems.FileObject;
40
41 /**
42  *
43  * @author Martin Adamek
44  */

45 public class CmFieldGenerator extends AbstractMethodGenerator {
46     
47     private static final Set JavaDoc<Modifier> PUBLIC_ABSTRACT = new HashSet JavaDoc<Modifier>(Arrays.asList(new Modifier[] {
48         Modifier.PUBLIC,
49         Modifier.ABSTRACT
50     }));
51     
52     private CmFieldGenerator(Entity entity, FileObject ejbClassFileObject, FileObject ddFileObject) {
53         super(entity, ejbClassFileObject, ddFileObject);
54     }
55     
56     public static CmFieldGenerator create(Entity entity, FileObject ejbClassFileObject, FileObject ddFileObject) {
57         return new CmFieldGenerator(entity, ejbClassFileObject, ddFileObject);
58     }
59     
60     public void addCmpField(MethodModel.Variable field, boolean localGetter, boolean localSetter,
61             boolean remoteGetter, boolean remoteSetter, String JavaDoc description) throws IOException JavaDoc {
62         addFieldToClass(field, localGetter, localSetter, remoteGetter, remoteSetter);
63         Entity entity = (Entity) ejb;
64         CmpField cmpField = entity.newCmpField();
65         cmpField.setFieldName(field.getName());
66         cmpField.setDescription(description);
67         entity.addCmpField(cmpField);
68         saveXml();
69     }
70     
71     public void addFieldToClass(final MethodModel.Variable variable, final boolean localGetter, final boolean localSetter,
72             final boolean remoteGetter, final boolean remoteSetter) throws IOException JavaDoc {
73         
74         // ejb class
75
JavaSource javaSource = JavaSource.forFileObject(ejbClassFileObject);
76         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
77             public void run(WorkingCopy workingCopy) throws Exception JavaDoc {
78                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
79                 TypeElement typeElement = workingCopy.getElements().getTypeElement(ejb.getEjbClass());
80                 MethodTree getterTree = createGetter(workingCopy, variable);
81                 MethodTree setterTree = createSetter(workingCopy, variable);
82                 ClassTree classTree = workingCopy.getTrees().getTree(typeElement);
83                 ClassTree newClassTree = workingCopy.getTreeMaker().addClassMember(classTree, getterTree);
84                 newClassTree = workingCopy.getTreeMaker().addClassMember(newClassTree, setterTree);
85                 workingCopy.rewrite(classTree, newClassTree);
86             }
87         }).commit();
88         
89         // local interface
90
if (localGetter || localSetter) {
91             FileObject localFileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getLocal());
92             javaSource = JavaSource.forFileObject(localFileObject);
93             javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
94                 public void run(WorkingCopy workingCopy) throws Exception JavaDoc {
95                     workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
96                     TypeElement typeElement = workingCopy.getElements().getTypeElement(ejb.getLocal());
97                     ClassTree classTree = workingCopy.getTrees().getTree(typeElement);
98                     ClassTree newClassTree = classTree;
99                     if (localGetter) {
100                         MethodTree getterTree = createGetter(workingCopy, variable);
101                         newClassTree = workingCopy.getTreeMaker().addClassMember(newClassTree, getterTree);
102                     }
103                     if (localSetter) {
104                         MethodTree setterTree = createSetter(workingCopy, variable);
105                         newClassTree = workingCopy.getTreeMaker().addClassMember(newClassTree, setterTree);
106                     }
107                     workingCopy.rewrite(classTree, newClassTree);
108                 }
109             }).commit();
110         }
111         
112         // remote interface
113
if (remoteGetter || remoteSetter) {
114             FileObject remoteFileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getRemote());
115             javaSource = JavaSource.forFileObject(remoteFileObject);
116             javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
117                 public void run(WorkingCopy workingCopy) throws Exception JavaDoc {
118                     workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
119                     TypeElement typeElement = workingCopy.getElements().getTypeElement(ejb.getRemote());
120                     ClassTree classTree = workingCopy.getTrees().getTree(typeElement);
121                     ClassTree newClassTree = classTree;
122                     if (remoteGetter) {
123                         MethodTree getterTree = createGetter(workingCopy, variable);
124                         newClassTree = workingCopy.getTreeMaker().addClassMember(newClassTree, getterTree);
125                     }
126                     if (remoteSetter) {
127                         MethodTree setterTree = createSetter(workingCopy, variable);
128                         newClassTree = workingCopy.getTreeMaker().addClassMember(newClassTree, setterTree);
129                     }
130                     workingCopy.rewrite(classTree, newClassTree);
131                 }
132             }).commit();
133         }
134         
135     }
136     
137     private static MethodTree createGetter(WorkingCopy workingCopy, MethodModel.Variable field) {
138         MethodModel methodModel = MethodModel.create(
139                 "get" + Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1),
140                 field.getType(),
141                 null,
142                 Collections.<MethodModel.Variable>emptyList(),
143                 Collections.<String JavaDoc>emptyList(),
144                 PUBLIC_ABSTRACT
145                 );
146         return MethodModelSupport.createMethodTree(workingCopy, methodModel);
147     }
148     
149     private static MethodTree createSetter(WorkingCopy workingCopy, MethodModel.Variable field) {
150         MethodModel methodModel = MethodModel.create(
151                 "set" + Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1),
152                 "void",
153                 null,
154                 Collections.singletonList(MethodModel.Variable.create(field.getType(), field.getName())),
155                 Collections.<String JavaDoc>emptyList(),
156                 PUBLIC_ABSTRACT
157                 );
158         return MethodModelSupport.createMethodTree(workingCopy, methodModel);
159     }
160     
161 }
162
Popular Tags