KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > gen > TwoModificationsTest


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.api.java.source.gen;
20
21 import com.sun.source.tree.AnnotationTree;
22 import com.sun.source.tree.ClassTree;
23 import com.sun.source.tree.CompilationUnitTree;
24 import com.sun.source.tree.ExpressionTree;
25 import com.sun.source.tree.MethodTree;
26 import com.sun.source.tree.ModifiersTree;
27 import com.sun.source.tree.Tree;
28 import com.sun.source.tree.TypeParameterTree;
29 import com.sun.source.tree.VariableTree;
30 import com.sun.tools.javac.util.List;
31 import java.io.File JavaDoc;
32 import java.util.Collections JavaDoc;
33 import javax.lang.model.element.Modifier;
34 import javax.lang.model.element.TypeElement;
35 import javax.lang.model.type.TypeKind;
36 import org.netbeans.api.java.source.CancellableTask;
37 import org.netbeans.api.java.source.JavaSource;
38 import org.netbeans.api.java.source.JavaSource.Phase;
39 import org.netbeans.api.java.source.TreeMaker;
40 import org.netbeans.api.java.source.WorkingCopy;
41 import org.netbeans.jackpot.test.TestUtilities;
42 import org.netbeans.junit.NbTestSuite;
43 import org.openide.filesystems.FileUtil;
44
45 /**
46  * Tests which do two modification in one modification task.
47  *
48  * @author Pavel Flaska
49  */

50 public class TwoModificationsTest extends GeneratorTest {
51     
52     /** Creates a new instance of TwoModificationsTest */
53     public TwoModificationsTest(String JavaDoc name) {
54         super(name);
55     }
56     
57     public static NbTestSuite suite() {
58         NbTestSuite suite = new NbTestSuite();
59         suite.addTestSuite(TwoModificationsTest.class);
60         return suite;
61     }
62     
63     /**
64      * #91265: Adding annotation to class and method to class members fails
65      */

66     public void testAddAnnAndMethod() throws Exception JavaDoc {
67         testFile = new File JavaDoc(getWorkDir(), "Test.java");
68         TestUtilities.copyStringToFile(testFile,
69             "package hierbas.del.litoral;\n\n" +
70             "import java.io.*;\n\n" +
71             "public class Test {\n" +
72             " public void taragui() {// what a ... problem\n" +
73             " }\n" +
74             "}\n"
75             );
76         String JavaDoc golden =
77             "package hierbas.del.litoral;\n\n" +
78             "import java.io.*;\n\n" +
79             "@javax.jws.WebService\n" +
80             "public class Test {\n" +
81             " public void taragui() {// what a ... problem\n" +
82             " }\n\n" +
83             " public void writeExternal(final Object arg0) throws IOException {\n" +
84             " throw new UnsupportedOperationException(\"Not supported yet.\");\n" +
85             " }\n" +
86             "}\n";
87         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
88         CancellableTask task = new CancellableTask<WorkingCopy>() {
89
90             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
91                 workingCopy.toPhase(Phase.RESOLVED);
92                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
93                 
94                 TreeMaker make = workingCopy.getTreeMaker();
95                 for (Tree typeDecl : cut.getTypeDecls()) {
96                     // ensure that it is correct type declaration, i.e. class
97
if (Tree.Kind.CLASS == typeDecl.getKind()) {
98                         ClassTree clazz = (ClassTree) typeDecl;
99                         
100                         AnnotationTree wsAnnotation = make.Annotation(
101                                 make.Identifier("javax.jws.WebService"),
102                                 //make.QualIdent(element),
103
Collections.<ExpressionTree>emptyList()
104                         );
105                         
106                         ClassTree modClass1 = make.Class(
107                             make.addModifiersAnnotation(clazz.getModifiers(), wsAnnotation),
108                             clazz.getSimpleName(),
109                             clazz.getTypeParameters(),
110                             clazz.getExtendsClause(),
111                             (List<ExpressionTree>)clazz.getImplementsClause(),
112                             clazz.getMembers());
113                         
114                         // create method modifier: public and no annotation
115
ModifiersTree methodModifiers = make.Modifiers(
116                             Collections.<Modifier>singleton(Modifier.PUBLIC),
117                             Collections.<AnnotationTree>emptyList()
118                         );
119                         
120                         // create parameter:
121
// final ObjectOutput arg0
122
VariableTree parameter = make.Variable(
123                                 make.Modifiers(
124                                     Collections.<Modifier>singleton(Modifier.FINAL),
125                                     Collections.<AnnotationTree>emptyList()
126                                 ),
127                                 "arg0", // name
128
make.Identifier("Object"), // parameter type
129
null // initializer - does not make sense in parameters.
130
);
131                         
132                         // prepare simple name to throws clause:
133
// 'throws IOException' and its import will be added
134
TypeElement element = workingCopy.getElements().getTypeElement("java.io.IOException");
135                         ExpressionTree throwsClause = make.QualIdent(element);
136                         
137                         // create method. There are two basic options:
138
// 1)
139
// make.Method() with 'BlockTree body' parameter -
140
// body has to be created, here in example code
141
// empty body block commented out
142
// 2)
143
// make.Method() with 'String body' parameter -
144
// body is added as a text. Used in our example.
145
MethodTree newMethod = make.Method(
146                             methodModifiers, // public
147
"writeExternal", // writeExternal
148
make.PrimitiveType(TypeKind.VOID), // return type "void"
149
Collections.<TypeParameterTree>emptyList(), // type parameters - none
150
Collections.<VariableTree>singletonList(parameter), // final ObjectOutput arg0
151
Collections.<ExpressionTree>singletonList(throwsClause), // throws
152
"{ throw new UnsupportedOperationException(\"Not supported yet.\") }", // body text
153
// make.Block(Collections.<StatementTree>emptyList(), false), // empty statement block
154
null // default value - not applicable here, used by annotations
155
);
156
157                         // and in the same way as interface was added to implements clause,
158
// add feature to the class:
159
ClassTree modifiedClazz = make.addClassMember(modClass1, newMethod);
160                         workingCopy.rewrite(clazz, modifiedClazz);
161                     }
162                 }
163             }
164
165             public void cancel() {
166             }
167         };
168         testSource.runModificationTask(task).commit();
169         String JavaDoc res = TestUtilities.copyFileToString(testFile);
170         System.err.println(res);
171         assertEquals(golden, res);
172     }
173     
174     String JavaDoc getGoldenPckg() {
175         return "";
176     }
177
178     String JavaDoc getSourcePckg() {
179         return "";
180     }
181
182     /**
183      * @param args the command line arguments
184      */

185     public static void main(String JavaDoc[] args) {
186         // TODO code application logic here
187
}
188     
189 }
190
Popular Tags