Lines 39-44
Link Here
|
39 |
import java.util.Collections; |
39 |
import java.util.Collections; |
40 |
import java.util.EnumSet; |
40 |
import java.util.EnumSet; |
41 |
import java.util.Map; |
41 |
import java.util.Map; |
|
|
42 |
import javax.lang.model.element.ElementKind; |
42 |
import javax.lang.model.element.Modifier; |
43 |
import javax.lang.model.element.Modifier; |
43 |
import javax.lang.model.element.TypeElement; |
44 |
import javax.lang.model.element.TypeElement; |
44 |
import javax.lang.model.type.TypeKind; |
45 |
import javax.lang.model.type.TypeKind; |
Lines 85-93
Link Here
|
85 |
@Override |
86 |
@Override |
86 |
protected void setUp() throws Exception { |
87 |
protected void setUp() throws Exception { |
87 |
super.setUp(); |
88 |
super.setUp(); |
88 |
FileObject templates = FileUtil.getConfigFile("Templates/Classes/Class.java"); |
89 |
FileObject template = FileUtil.getConfigFile("Templates/Classes/Class.java"); |
|
|
90 |
if (template != null) template.delete(); |
91 |
FileObject template2 = FileUtil.getConfigFile("Templates/Classes/package-info.java"); |
92 |
if (template2 != null) template2.delete(); |
93 |
} |
89 |
|
94 |
|
90 |
if (templates != null) templates.delete(); |
95 |
public void testNewCompilationUnitFromTemplate() throws Exception { |
|
|
96 |
testFile = new File(getWorkDir(), "Test.java"); |
97 |
|
98 |
File fakeFile = new File(getWorkDir(), "Fake.java"); |
99 |
FileObject fakeFO = FileUtil.createData(fakeFile); |
100 |
|
101 |
FileObject emptyJava = FileUtil.createData(FileUtil.getConfigRoot(), "Templates/Classes/Empty.java"); |
102 |
emptyJava.setAttribute("template", Boolean.TRUE); |
103 |
|
104 |
FileObject classJava = FileUtil.createData(FileUtil.getConfigRoot(), "Templates/Classes/Class.java"); |
105 |
classJava.setAttribute("template", Boolean.TRUE); |
106 |
classJava.setAttribute("verbatim-create-from-template", Boolean.TRUE); |
107 |
Writer w = new OutputStreamWriter(classJava.getOutputStream(), "UTF-8"); |
108 |
w.write("/*\n * License\n */\npackage zoo;\n\n/**\n * trida\n */\npublic class Template {\n}"); |
109 |
w.close(); |
110 |
|
111 |
FileObject packageJava = FileUtil.createData(FileUtil.getConfigRoot(), "Templates/Classes/package-info.java"); |
112 |
packageJava.setAttribute("template", Boolean.TRUE); |
113 |
packageJava.setAttribute("verbatim-create-from-template", Boolean.TRUE); |
114 |
Writer w2 = new OutputStreamWriter(packageJava.getOutputStream(), "UTF-8"); |
115 |
w2.write("/*\n * License\n */\npackage zoo;\n"); |
116 |
w2.close(); |
117 |
|
118 |
FileObject testSourceFO = FileUtil.createData(testFile); assertNotNull(testSourceFO); |
119 |
ClassPath sourcePath = ClassPath.getClassPath(testSourceFO, ClassPath.SOURCE); assertNotNull(sourcePath); |
120 |
FileObject[] roots = sourcePath.getRoots(); assertEquals(1, roots.length); |
121 |
final FileObject sourceRoot = roots[0]; assertNotNull(sourceRoot); |
122 |
ClassPath compilePath = ClassPath.getClassPath(testSourceFO, ClassPath.COMPILE); assertNotNull(compilePath); |
123 |
ClassPath bootPath = ClassPath.getClassPath(testSourceFO, ClassPath.BOOT); assertNotNull(bootPath); |
124 |
ClasspathInfo cpInfo = ClasspathInfo.create(bootPath, compilePath, sourcePath); |
125 |
JavaSource javaSource = JavaSource.create(cpInfo, fakeFO); |
126 |
|
127 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
128 |
|
129 |
public void cancel() { |
130 |
} |
131 |
|
132 |
public void run(WorkingCopy workingCopy) throws Exception { |
133 |
workingCopy.toPhase(JavaSource.Phase.PARSED); |
134 |
TreeMaker make = workingCopy.getTreeMaker(); |
135 |
String path = "zoo/Krtek.java"; |
136 |
GeneratorUtilities genUtils = GeneratorUtilities.get(workingCopy); |
137 |
CompilationUnitTree newTree = genUtils.createFromTemplate(sourceRoot, path, ElementKind.CLASS); |
138 |
MethodTree nju = make.Method( |
139 |
make.Modifiers(Collections.<Modifier>emptySet()), |
140 |
"m", |
141 |
make.PrimitiveType(TypeKind.VOID), // return type - void |
142 |
Collections.<TypeParameterTree>emptyList(), |
143 |
Collections.<VariableTree>emptyList(), |
144 |
Collections.<ExpressionTree>emptyList(), |
145 |
make.Block(Collections.<StatementTree>emptyList(), false), |
146 |
null // default value - not applicable |
147 |
); |
148 |
ClassTree clazz = make.Class( |
149 |
make.Modifiers(Collections.<Modifier>singleton(Modifier.PUBLIC)), |
150 |
"Krtek", |
151 |
Collections.<TypeParameterTree>emptyList(), |
152 |
null, |
153 |
Collections.<Tree>emptyList(), |
154 |
Collections.singletonList(nju) |
155 |
); |
156 |
if(newTree.getTypeDecls().isEmpty()) { |
157 |
newTree = make.addCompUnitTypeDecl(newTree, clazz); |
158 |
} else { |
159 |
Tree templateClass = newTree.getTypeDecls().get(0); |
160 |
genUtils.copyComments(templateClass, clazz, true); |
161 |
genUtils.copyComments(templateClass, clazz, false); |
162 |
newTree = make.removeCompUnitTypeDecl(newTree, 0); |
163 |
newTree = make.insertCompUnitTypeDecl(newTree, 0, clazz); |
164 |
} |
165 |
workingCopy.rewrite(null, newTree); |
166 |
|
167 |
String packagePath = "zoo/package-info.java"; |
168 |
CompilationUnitTree newPackageTree = genUtils.createFromTemplate(sourceRoot, packagePath, ElementKind.PACKAGE); |
169 |
workingCopy.rewrite(null, newPackageTree); |
170 |
} |
171 |
}; |
172 |
ModificationResult result = javaSource.runModificationTask(task); |
173 |
result.commit(); |
174 |
|
175 |
String goldenClass = |
176 |
"/*\n * License\n */\n" + |
177 |
"package zoo;\n" + |
178 |
"\n" + |
179 |
"/**\n * trida\n */\n" + |
180 |
"public class Krtek {\n" + |
181 |
"\n" + |
182 |
" void m() {\n" + |
183 |
" }\n" + |
184 |
"}"; |
185 |
String res = TestUtilities.copyFileToString(new File(getDataDir().getAbsolutePath() + "/zoo/Krtek.java")); |
186 |
assertEquals(goldenClass, res); |
187 |
|
188 |
String goldenPackage = |
189 |
"/*\n * License\n */\n" + |
190 |
"package zoo;\n"; |
191 |
res = TestUtilities.copyFileToString(new File(getDataDir().getAbsolutePath() + "/zoo/package-info.java")); |
192 |
assertEquals(goldenPackage, res); |
193 |
} |
194 |
|
195 |
public void testNewCompilationUnitFromNonExistingTemplate() throws Exception { |
196 |
testFile = new File(getWorkDir(), "Test.java"); |
197 |
|
198 |
File fakeFile = new File(getWorkDir(), "Fake.java"); |
199 |
FileObject fakeFO = FileUtil.createData(fakeFile); |
200 |
|
201 |
FileObject template = FileUtil.getConfigFile("Templates/Classes/Class.java"); |
202 |
if (template != null) template.delete(); |
203 |
template = FileUtil.getConfigFile("Templates/Classes/Empty.java"); |
204 |
if(template != null) template.delete(); |
205 |
|
206 |
FileObject testSourceFO = FileUtil.createData(testFile); assertNotNull(testSourceFO); |
207 |
ClassPath sourcePath = ClassPath.getClassPath(testSourceFO, ClassPath.SOURCE); assertNotNull(sourcePath); |
208 |
FileObject[] roots = sourcePath.getRoots(); assertEquals(1, roots.length); |
209 |
final FileObject sourceRoot = roots[0]; assertNotNull(sourceRoot); |
210 |
ClassPath compilePath = ClassPath.getClassPath(testSourceFO, ClassPath.COMPILE); assertNotNull(compilePath); |
211 |
ClassPath bootPath = ClassPath.getClassPath(testSourceFO, ClassPath.BOOT); assertNotNull(bootPath); |
212 |
ClasspathInfo cpInfo = ClasspathInfo.create(bootPath, compilePath, sourcePath); |
213 |
JavaSource javaSource = JavaSource.create(cpInfo, fakeFO); |
214 |
|
215 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
216 |
|
217 |
public void cancel() { |
218 |
} |
219 |
|
220 |
public void run(WorkingCopy workingCopy) throws Exception { |
221 |
workingCopy.toPhase(JavaSource.Phase.PARSED); |
222 |
TreeMaker make = workingCopy.getTreeMaker(); |
223 |
String path = "zoo/Krtek.java"; |
224 |
GeneratorUtilities genUtils = GeneratorUtilities.get(workingCopy); |
225 |
CompilationUnitTree newTree = genUtils.createFromTemplate(sourceRoot, path, ElementKind.CLASS); |
226 |
workingCopy.rewrite(null, newTree); |
227 |
} |
228 |
}; |
229 |
ModificationResult result = javaSource.runModificationTask(task); |
230 |
result.commit(); |
231 |
|
232 |
String res = TestUtilities.copyFileToString(new File(getDataDir().getAbsolutePath() + "/zoo/Krtek.java")); |
233 |
assertEquals(res, "package zoo;\n\n"); |
91 |
} |
234 |
} |
92 |
|
235 |
|
93 |
public void testNewCompilationUnit() throws Exception { |
236 |
public void testNewCompilationUnit() throws Exception { |