KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > java > generating > InnerClasses


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 /*
21  * FieldElem.java
22  *
23  * Created on June 26, 2000, 9:29 AM
24  */

25
26 package org.netbeans.test.java.generating.InnerClasses;
27
28 import com.sun.source.tree.ClassTree;
29 import com.sun.source.tree.CompilationUnitTree;
30 import com.sun.source.tree.ExpressionTree;
31 import com.sun.source.tree.MethodTree;
32 import com.sun.source.tree.ModifiersTree;
33 import com.sun.source.tree.Tree;
34 import com.sun.source.tree.VariableTree;
35 import java.io.IOException JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.EnumSet JavaDoc;
39 import java.util.EnumSet JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Map JavaDoc;
43 import javax.lang.model.element.Element;
44 import javax.lang.model.element.Modifier;
45 import org.netbeans.api.java.source.CancellableTask;
46 import org.netbeans.api.java.source.JavaSource;
47 import org.netbeans.api.java.source.JavaSource.Phase;
48 import org.netbeans.api.java.source.TreeMaker;
49 import org.netbeans.api.java.source.WorkingCopy;
50 import org.openide.loaders.*;
51 import org.netbeans.junit.*;
52 import org.netbeans.test.java.Common;
53 import org.openide.filesystems.FileObject;
54
55 /** <B>Java Module General API test: InnerClasses</B>
56  * <BR><BR><I>What it tests:</I><BR>
57  * Creating and handling with ClassElement.
58  * Test is focused on checking of correctness of generated code.
59  * <BR><BR><I>How it works</I><BR>
60  * New class is created using DataObject.createFromTemplate() and also some ClassElements are created.
61  * These are customized using setters and filled with fields, methods etc.
62  * Then these ClassElements are added using ClassElement.addClass() into ClassElement.
63  * These action cause generating of .java code. This code is compared with supposed one.
64  * <BR><BR><I>Output</I><BR>
65  * Generated Java code.
66  * <BR><BR><I>Possible reasons of failure</I><BR>
67  * <U>Classes are not inserted properly:</U><BR>
68  * If there are some Initialzers in .diff file.
69  * <BR><BR><U>Classes have/return bad properies</U><BR>
70  * See .diff file to get which ones
71  * <BR><BR><U>Bad indentation</U><BR>
72  * This is probably not a bug of Java Module. (->Editor Bug)
73  * In .diff file could be some whitespaces.
74  * <BR><BR><U>Exception occured</U><BR><BR>
75  * See .log file for StackTrace
76  *
77  * @author Jan Becicka <Jan.Becicka@sun.com>
78  */

79
80
81 public class InnerClasses extends org.netbeans.test.java.XRunner {
82     
83     public static void main(java.lang.String JavaDoc[] args) {
84         junit.textui.TestRunner.run(suite());
85     }
86     
87     public InnerClasses() {
88         super("");
89     }
90     
91     public InnerClasses(java.lang.String JavaDoc testName) {
92         super(testName);
93     }
94     
95     public static NbTest suite() {
96         return new NbTestSuite(InnerClasses.class);
97     }
98     
99     /** "body" of this TestCase
100      * @param o SourceElement - target for generating
101      * @param log log is used for logging StackTraces
102      * @throws Exception
103      * @return true if test passed
104      * false if failed
105      */

106     public boolean go(Object JavaDoc o, java.io.PrintWriter JavaDoc log) throws Exception JavaDoc {
107         
108         boolean passed = true;
109         
110         FileObject fo = (FileObject) o;
111         JavaSource js = JavaSource.forFileObject(fo);
112         createInnerClass(js);
113         
114         // ClassElement innerClass = new ClassElement();
115
//
116
// simpleJavaSourceEtalonGenerator(innerClass);
117
//
118
// innerClass.setModifiers(Modifier.PUBLIC | Modifier.SYNCHRONIZED);
119
// innerClass.setSuperclass(Identifier.create("Object"));
120
// innerClass.setName(Identifier.create("InnerClass","InnerClass"));
121
// clazz.addClass(innerClass);
122
//
123
// innerClass = clazz.getClass(Identifier.create("InnerClass"));
124
//
125
// if (!(innerClass.getDeclaringClass().getName().getFullName().equals(packageName + "." + name))){
126
// passed = false;
127
// log("getDeclaringClass failed");
128
// }
129
//
130
// DataObject DO = (DataObject) clazz.getSource().getCookie(DataObject.class);
131
// //DO.rename("RenamedJavaTestSource");
132
// clazz.setName(Identifier.create("RenamedJavaTestSource"));
133
//
134
// if (!(innerClass.getDeclaringClass().getName().getFullName().equals(packageName + ".RenamedJavaTestSource"))){
135
// passed = false;
136
// log("getDeclaringClass failed");
137
// }
138
//
139
// if (!(innerClass.getMethods()[0].getDeclaringClass().getName().getFullName().equals(packageName + ".RenamedJavaTestSource.InnerClass"))){
140
// passed = false;
141
// log("getDeclaringClass failed: " + innerClass.getMethods()[0].getDeclaringClass().getName().getFullName());
142
// }
143
//
144
// clazz.setName(Identifier.create("Foo"));
145
// if (!(innerClass.getMethods()[0].getDeclaringClass().getName().getFullName().equals(packageName + ".Foo.InnerClass"))){
146
// passed = false;
147
// log("getDeclaringClass failed:" + innerClass.getMethods()[0].getDeclaringClass().getName().getFullName());
148
// }
149
//
150
// clazz.setName(Identifier.create("RenamedJavaTestSource"));
151

152         return passed;
153     }
154     
155     /**
156      */

157     protected void setUp() {
158         super.setUp();
159         name = "JavaTestSourceInnerClasses";
160         packageName = "org.netbeans.test.java.testsources";
161     }
162     
163     
164     public void createInnerClass(JavaSource js) throws IOException JavaDoc {
165         CancellableTask task = new CancellableTask<WorkingCopy>() {
166             public void cancel() {
167                 throw new UnsupportedOperationException JavaDoc("Not supported yet.");
168             }
169             
170             public void run(WorkingCopy workingCopy) throws Exception JavaDoc {
171                 workingCopy.toPhase(Phase.RESOLVED);
172                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
173                 TreeMaker make = workingCopy.getTreeMaker();
174                 ClassTree clazz = null;
175                 for (Tree typeDecl : cut.getTypeDecls()) {
176                     if (Tree.Kind.CLASS == typeDecl.getKind()) {
177                         clazz = (ClassTree) typeDecl;
178                     }
179                 } // end for
180
EnumSet JavaDoc<Modifier> modifiers = EnumSet.of(Modifier.STATIC,Modifier.PUBLIC);
181                 Tree extendsTree = make.QualIdent(workingCopy.getElements().getTypeElement("java.util.List"));
182                 List JavaDoc<ExpressionTree> implementsList = Collections.<ExpressionTree> singletonList(make.Identifier("Serializable"));
183                 Map JavaDoc<String JavaDoc,String JavaDoc> params = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
184                 params.put("param1", "String");
185                 MethodTree mt = Common.createMethod(make, "method", params);
186                 VariableTree vt = Common.createField(make, "variable", EnumSet.of(Modifier.PROTECTED), "double");
187                 List JavaDoc<? extends Tree> members = new ArrayList JavaDoc<Tree>();
188                 ClassTree innerClass = make.Class(
189                         make.Modifiers(modifiers),
190                         "MyInner",
191                         Collections.EMPTY_LIST,
192                         extendsTree,
193                         implementsList,
194                         Collections.EMPTY_LIST);
195                 innerClass = make.addClassMember(innerClass, mt);
196                 innerClass = make.addClassMember(innerClass, vt);
197                 innerClass = make.addClassMember(innerClass, make.Block(Collections.EMPTY_LIST, false));
198                 ClassTree modifiedClazz = make.addClassMember(clazz, innerClass);
199                 workingCopy.rewrite(clazz, modifiedClazz);
200             }
201         };
202         js.runModificationTask(task).commit();
203     }
204     
205     /** Generates SimpleJavaTestSourceEtalon
206      * @param clazz where to generate
207      * @throws Exception
208      */

209     // public static void simpleJavaSourceEtalonGenerator(org.openide.src.ClassElement clazz) throws Exception {
210
//
211
// clazz.removeConstructors(clazz.getConstructors());
212
// clazz.addMethod(Common.createMethod("method1", Modifier.PUBLIC | Modifier.STATIC, Type.INT, Common.PARS1));
213
// clazz.addField(Common.createField("field1",Modifier.PUBLIC | Modifier.STATIC, Type.FLOAT));
214
// clazz.addInitializer(Common.createInitializer());
215
// clazz.addMethod(Common.createMethod("method1", Modifier.PUBLIC | Modifier.STATIC, Type.INT, Common.PARS2));
216
// clazz.addField(Common.createField("field2",Modifier.PUBLIC | Modifier.STATIC, Type.INT));
217
// clazz.addConstructor(Common.createConstructor(clazz.getName()));
218
// clazz.addConstructor(Common.createConstructor(clazz.getName(),Common.PARS1));
219
//
220
// clazz.addMethod(Common.createMethod("method1", Modifier.PUBLIC | Modifier.STATIC, Type.INT, Common.PARS3));
221
// clazz.addInitializer(Common.createInitializer());
222
// clazz.addMethod(Common.createMethod("method2", Modifier.PUBLIC | Modifier.STATIC, Type.INT, Common.PARS1));
223
// clazz.addField(Common.createField("field3",Modifier.PUBLIC | Modifier.STATIC, Type.SHORT));
224
// clazz.addConstructor(Common.createConstructor(clazz.getName(),Common.PARS2));
225
// clazz.addMethod(Common.createMethod("method2", Modifier.PUBLIC | Modifier.STATIC, Type.SHORT, Common.PARS2));
226
// clazz.addInitializer(Common.createInitializer());
227
// clazz.addMethod(Common.createMethod("method2", Modifier.PUBLIC | Modifier.STATIC, Type.LONG, Common.PARS3));
228
//
229
// }
230

231 }
232
Popular Tags