KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22 import java.io.*;
23 import org.netbeans.api.java.source.*;
24 import static org.netbeans.api.java.source.JavaSource.*;
25 import org.netbeans.junit.NbTestSuite;
26
27 /**
28  * Some test with type parameters.
29  *
30  * @author Pavel Flaska
31  */

32 public class TypeParameterTest extends GeneratorTestMDRCompat {
33     
34     /** Creates a new instance of TypeParameterTest */
35     public TypeParameterTest(String JavaDoc name) {
36         super(name);
37     }
38     
39     public static NbTestSuite suite() {
40         NbTestSuite suite = new NbTestSuite();
41         suite.addTestSuite(TypeParameterTest.class);
42         return suite;
43     }
44     
45     /**
46      * #92531: rename in type parameter in class decl:
47      * public MyList<Ex> extends ArrayList<Ex> implements List<Ex> { ...
48      */

49     public void testRenameTypeParamInClassDecl() throws Exception JavaDoc {
50         testFile = new File(getWorkDir(), "Test.java");
51         TestUtilities.copyStringToFile(testFile,
52             "package hierbas.del.litoral;\n\n" +
53             "import java.util.*;\n\n" +
54             "public class MyList<Ex> extends ArrayList<Ex> implements List<Ex> {\n" +
55             "}\n"
56             );
57         String JavaDoc golden =
58             "package hierbas.del.litoral;\n\n" +
59             "import java.util.*;\n\n" +
60             "public class MyList<E> extends ArrayList<E> implements List<E> {\n" +
61             "}\n";
62         JavaSource src = getJavaSource(testFile);
63         
64         CancellableTask task = new CancellableTask<WorkingCopy>() {
65
66             public void run(WorkingCopy workingCopy) throws IOException {
67                 workingCopy.toPhase(Phase.RESOLVED);
68                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
69                 TreeMaker make = workingCopy.getTreeMaker();
70
71                 for (Tree typeDecl : cut.getTypeDecls()) {
72                     // ensure that it is correct type declaration, i.e. class
73
if (Tree.Kind.CLASS == typeDecl.getKind()) {
74                         ClassTree clazz = (ClassTree) typeDecl;
75                         // name
76
TypeParameterTree tpt = clazz.getTypeParameters().get(0);
77                         workingCopy.rewrite(tpt, make.setLabel(tpt, "E"));
78                         // extends
79
ParameterizedTypeTree ptt = (ParameterizedTypeTree) clazz.getExtendsClause();
80                         IdentifierTree it = (IdentifierTree) ptt.getTypeArguments().get(0);
81                         workingCopy.rewrite(it, make.Identifier("E"));
82                         // implements
83
ptt = (ParameterizedTypeTree) clazz.getImplementsClause().get(0);
84                         it = (IdentifierTree) ptt.getTypeArguments().get(0);
85                         workingCopy.rewrite(it, make.Identifier("E"));
86                     }
87                 }
88             }
89
90             public void cancel() {
91             }
92         };
93         src.runModificationTask(task).commit();
94         String JavaDoc res = TestUtilities.copyFileToString(testFile);
95         System.err.println(res);
96         assertEquals(golden, res);
97     }
98     
99     /**
100      */

101     public void testRenameWildCard() throws Exception JavaDoc {
102         testFile = new File(getWorkDir(), "Test.java");
103         TestUtilities.copyStringToFile(testFile,
104             "package hierbas.del.litoral;\n\n" +
105             "import java.util.*;\n\n" +
106             "public class MyList<Ex> {\n" +
107             " public Object method(Collection<? extends Ex> c) {\n" +
108             " }\n" +
109             "}\n"
110             );
111         String JavaDoc golden =
112             "package hierbas.del.litoral;\n\n" +
113             "import java.util.*;\n\n" +
114             "public class MyList<E> {\n" +
115             " public Object method(Collection<? extends E> c) {\n" +
116             " }\n" +
117             "}\n";
118         JavaSource src = getJavaSource(testFile);
119         
120         CancellableTask task = new CancellableTask<WorkingCopy>() {
121
122             public void run(WorkingCopy workingCopy) throws IOException {
123                 workingCopy.toPhase(Phase.RESOLVED);
124                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
125                 TreeMaker make = workingCopy.getTreeMaker();
126                 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
127                 // name
128
TypeParameterTree tpt = clazz.getTypeParameters().get(0);
129                 workingCopy.rewrite(tpt, make.setLabel(tpt, "E"));
130                 // method
131
MethodTree method = (MethodTree) clazz.getMembers().get(1);
132                 VariableTree vt = method.getParameters().get(0);
133                 ParameterizedTypeTree ptt = (ParameterizedTypeTree) vt.getType();
134                 WildcardTree wct = (WildcardTree) ptt.getTypeArguments().get(0);
135                 workingCopy.rewrite(wct.getBound(), make.Identifier("E"));
136             }
137
138             public void cancel() {
139             }
140         };
141         src.runModificationTask(task).commit();
142         String JavaDoc res = TestUtilities.copyFileToString(testFile);
143         System.err.println(res);
144         assertEquals(golden, res);
145     }
146     
147     String JavaDoc getGoldenPckg() {
148         return "";
149     }
150
151     String JavaDoc getSourcePckg() {
152         return "";
153     }
154
155 }
156
Popular Tags