KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > core > jaxws > actions > JaxWsClassesCookieImpl


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 package org.netbeans.modules.websvc.core.jaxws.actions;
20
21 import com.sun.source.tree.AnnotationTree;
22 import com.sun.source.tree.ClassTree;
23 import com.sun.source.tree.ExpressionTree;
24 import com.sun.source.tree.MethodTree;
25 import com.sun.source.tree.ModifiersTree;
26 import com.sun.source.tree.PrimitiveTypeTree;
27 import com.sun.source.tree.Tree.Kind;
28 import com.sun.source.tree.VariableTree;
29 import java.io.IOException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.List JavaDoc;
33 import javax.lang.model.element.TypeElement;
34 import javax.lang.model.type.TypeKind;
35 import org.netbeans.api.java.source.CancellableTask;
36 import org.netbeans.api.java.source.Comment;
37 import org.netbeans.api.java.source.JavaSource;
38 import org.netbeans.api.java.source.TreeMaker;
39 import org.netbeans.modules.j2ee.common.source.GenerationUtils;
40 import static org.netbeans.api.java.source.JavaSource.Phase;
41 import org.netbeans.api.java.source.WorkingCopy;
42 import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
43 import org.openide.ErrorManager;
44 import org.openide.filesystems.FileObject;
45 import org.openide.util.NbBundle;
46
47 public class JaxWsClassesCookieImpl implements JaxWsClassesCookie {
48     Service service;
49     FileObject implClassFO;
50     
51     public JaxWsClassesCookieImpl(Service service, FileObject implClassFO) {
52         this.service = service;
53         this.implClassFO = implClassFO;
54     }
55     
56     public void addOperation(final MethodTree method) {
57         JavaSource targetSource = JavaSource.forFileObject(implClassFO);
58         CancellableTask<WorkingCopy> modificationTask = new CancellableTask<WorkingCopy>() {
59             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
60                 workingCopy.toPhase(Phase.RESOLVED);
61                 TreeMaker make = workingCopy.getTreeMaker();
62                 GenerationUtils genUtils = GenerationUtils.newInstance(workingCopy);
63                 if (genUtils!=null) {
64                     ClassTree javaClass = genUtils.getClassTree();
65                     TypeElement webMethodAn = workingCopy.getElements().getTypeElement("javax.jws.WebMethod"); //NOI18N
66
TypeElement webParamAn = workingCopy.getElements().getTypeElement("javax.jws.WebParam"); //NOI18N
67

68                     AnnotationTree webMethodAnnotation = make.Annotation(
69                         make.QualIdent(webMethodAn),
70                         Collections.<ExpressionTree>emptyList()
71                     );
72                     // add @WebMethod annotation
73
ModifiersTree modifiersTree = make.addModifiersAnnotation(method.getModifiers(), webMethodAnnotation);
74                     
75                     // add @Oneway annotation
76
if (Kind.PRIMITIVE_TYPE == method.getReturnType().getKind()) {
77                         PrimitiveTypeTree primitiveType = (PrimitiveTypeTree)method.getReturnType();
78                         if (TypeKind.VOID == primitiveType.getPrimitiveTypeKind()) {
79                             TypeElement oneWayAn = workingCopy.getElements().getTypeElement("javax.jws.Oneway"); //NOI18N
80
AnnotationTree oneWayAnnotation = make.Annotation(
81                                 make.QualIdent(oneWayAn),
82                                 Collections.<ExpressionTree>emptyList()
83                             );
84                             modifiersTree = make.addModifiersAnnotation(modifiersTree, oneWayAnnotation);
85                         }
86                     }
87                     
88                     // add @WebParam annotations
89
List JavaDoc<? extends VariableTree> parameters = method.getParameters();
90                     List JavaDoc<VariableTree> newParameters = new ArrayList JavaDoc<VariableTree>();
91                     for (VariableTree param:parameters) {
92                         AnnotationTree paramAnnotation = make.Annotation(
93                             make.QualIdent(webParamAn),
94                             Collections.<ExpressionTree>singletonList(
95                                 make.Assignment(make.Identifier("name"), make.Literal(param.getName().toString()))) //NOI18N
96
);
97                         newParameters.add(genUtils.addAnnotation(param, paramAnnotation));
98                     }
99                     // create new (annotated) method
100
MethodTree annotatedMethod = make.Method(
101                                 modifiersTree,
102                                 method.getName(),
103                                 method.getReturnType(),
104                                 method.getTypeParameters(),
105                                 newParameters,
106                                 method.getThrows(),
107                                 method.getBody(),
108                                 (ExpressionTree)method.getDefaultValue());
109                     Comment comment = Comment.create(NbBundle.getMessage(JaxWsClassesCookieImpl.class, "TXT_WSOperation"));
110                     make.addComment(annotatedMethod, comment, true);
111                     
112                     ClassTree modifiedClass = make.addClassMember(javaClass,annotatedMethod);
113                     workingCopy.rewrite(javaClass, modifiedClass);
114                 }
115             }
116             public void cancel() {}
117         };
118         try {
119             targetSource.runModificationTask(modificationTask).commit();
120         } catch (IOException JavaDoc ex) {
121             ErrorManager.getDefault().notify(ex);
122         }
123     }
124 // Retouche
125
// private JavaClass getImplBeanClass() {
126
// String implBean = service.getImplementationClass();
127
// if(implBean != null) {
128
// return JMIUtils.findClass(implBean, implClassFO);
129
// }
130
// return null;
131
// }
132
//
133
// private String getImplicitValue(Type t) {
134
// // TODO - should enhance to enum types, annotation types
135
// String ret = "return ";
136
//
137
// if (t instanceof PrimitiveType) {
138
//
139
// if (((PrimitiveType)t).getKind().equals(PrimitiveTypeKindEnum.BOOLEAN)) {
140
// return "false"; //NOI18N
141
// }
142
// if (((PrimitiveType)t).getKind().equals(PrimitiveTypeKindEnum.CHAR)) {
143
// return "''"; //NOI18N
144
// }
145
// return "0"; //NOI18N
146
//
147
// } else if (t instanceof JavaClass) {
148
// return "null"; //NOI18N
149
//
150
// } else if (t instanceof Array) {
151
// return "new " + t.getName() + " { }"; //NOI18N
152
// }
153
//
154
// return null;
155
// }
156
//
157
// public void addOperation(Method m) {
158
// JavaClass implClass = null;
159
// boolean rollback = true;
160
// JMIUtils.beginJmiTransaction(true);
161
// try {
162
// m.setJavadocText(NbBundle.getMessage(JaxWsClassesCookieImpl.class, "TXT_WSOperation"));
163
//
164
// implClass = getImplBeanClass();
165
//
166
// String body = NbBundle.getMessage(JaxWsClassesCookieImpl.class, "TXT_VoidOperation"); //NOI18N
167
//
168
// Type returnType = m.getType();
169
// boolean voidType = true;
170
// if (!((returnType instanceof PrimitiveType) && ((PrimitiveType) returnType).getKind().equals(PrimitiveTypeKindEnum.VOID))) {
171
// voidType=false;
172
// String retString = getImplicitValue(m.getType());
173
// if (retString != null) {
174
// body += "return " + retString + ";"; //NOI18N
175
// }
176
// }
177
//
178
// if (implClass != null) {
179
// implClass.getContents().add(m);
180
//
181
// // #61178
182
// JMIUtils.fixImports(implClass);
183
// //JMIUtils.fixImports(seiClass);
184
//
185
// m.setBodyText(body);
186
//
187
// // add javax.jws.WebMethod annotation
188
// m.getAnnotations().add(JMIGenerationUtil.createAnnotation(implClass,"javax.jws.WebMethod",new ArrayList())); //NOI18N
189
// if (voidType && m.getExceptionNames().size()==0) m.getAnnotations().add(JMIGenerationUtil.createAnnotation(implClass,"javax.jws.Oneway",new ArrayList())); //NOI18N
190
// List parameters = m.getParameters();
191
// for (int i=0;i<parameters.size();i++) {
192
// Parameter param = (Parameter)parameters.get(i);
193
// String paramName = param.getName();
194
// List attrValues = new ArrayList();
195
// attrValues.add(JMIGenerationUtil.createAttributeValue(implClass,"name",paramName));
196
// param.getAnnotations().add(JMIGenerationUtil.createAnnotation(implClass,"javax.jws.WebParam",attrValues)); //NOI18N
197
// }
198
// rollback = false;
199
// }
200
// } catch (Exception e) {
201
// ErrorManager.getDefault().notify(e);
202
// } finally {
203
// JMIUtils.endJmiTransaction(rollback);
204
// }
205
// JMIUtils.openInEditor(m);
206
// }
207

208 }
209
210
Popular Tags