1 22 package org.jboss.aop.ant; 23 24 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.DirectoryScanner; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.taskdefs.Java; 29 import org.apache.tools.ant.taskdefs.MatchingTask; 30 import org.apache.tools.ant.types.CommandlineJava; 31 import org.apache.tools.ant.types.Path; 32 import org.apache.tools.ant.types.Reference; 33 34 import java.io.File ; 35 import java.util.ArrayList ; 36 37 44 public class AnnotationC extends MatchingTask 45 { 46 private static String lSep = System.getProperty("line.separator"); 47 48 private Path classpath; 49 private Path output; 50 private Path compilerClasspath; 51 private Path compileSourcepath; 52 boolean xml = false; 53 boolean bytecode = false; 54 55 58 protected boolean failOnError = true; 59 60 public void setXml(boolean i) 61 { 62 xml = i; 63 } 64 65 public boolean getXml() 66 { 67 return xml; 68 } 69 70 public void setBytecode(boolean i) 71 { 72 bytecode = i; 73 } 74 75 public boolean getBytecode() 76 { 77 return xml; 78 } 79 80 84 public void setFailonerror(boolean fail) 85 { 86 failOnError = fail; 87 } 88 89 92 public boolean getFailonerror() 93 { 94 return failOnError; 95 } 96 97 100 public void setClasspath(Path cp) 101 { 102 if (classpath == null) 103 { 104 classpath = cp; 105 } 106 else 107 { 108 classpath.append(cp); 109 } 110 } 111 112 115 public Path createClasspath() 116 { 117 if (classpath == null) 118 { 119 classpath = new Path(getProject()); 120 } 121 return classpath.createPath(); 122 } 123 124 127 public void setClasspathRef(Reference r) 128 { 129 createClasspath().setRefid(r); 130 } 131 132 public Path getClasspath() 133 { 134 return classpath; 135 } 136 137 140 public void setCompilerClasspathRef(Reference r) 141 { 142 if (compilerClasspath == null) compilerClasspath = new Path(getProject()); 143 compilerClasspath.setRefid(r); 144 } 145 146 149 public void setCompilerclasspath(Path cp) 150 { 151 if (compilerClasspath == null) 152 { 153 compilerClasspath = cp; 154 } 155 else 156 { 157 compilerClasspath.append(cp); 158 } 159 } 160 161 164 public Path getCompilerclasspath() 165 { 166 return compilerClasspath; 167 } 168 169 172 public Path createCompilerclasspath() 173 { 174 if (compilerClasspath == null) 175 { 176 compilerClasspath = new Path(getProject()); 177 } 178 return compilerClasspath.createPath(); 179 } 180 181 186 public Path createSrc() 187 { 188 if (compileSourcepath == null) 189 { 190 compileSourcepath = new Path(getProject()); 191 } 192 return compileSourcepath.createPath(); 193 } 194 195 public Path createOutput() 196 { 197 if (output == null) 198 { 199 output = new Path(getProject()); 200 } 201 return output.createPath(); 202 } 203 204 207 public void setOutput(Path cp) 208 { 209 if (output == null) 210 { 211 output = cp; 212 } 213 else 214 { 215 output.append(cp); 216 } 217 } 218 219 222 public Path getOutput() 223 { 224 return output; 225 } 226 227 public void execute() 228 throws BuildException 229 { 230 CommandlineJava cmd = new CommandlineJava(); 231 232 if (output != null) 233 { 234 cmd.createArgument().setValue("-o"); 235 cmd.createArgument().setValue(output.toString()); 236 } 237 if (xml) 238 { 239 cmd.createArgument().setValue("-xml"); 240 } 241 if (bytecode) 242 { 243 cmd.createArgument().setValue("-bytecode"); 244 } 245 logAndAddFilesToCompile(cmd); 246 try 247 { 248 classpath.append(compilerClasspath); 251 Java java = (Java) (getProject().createTask("java")); 252 if (getClasspath() != null) 253 { 254 getProject().log("using user supplied classpath: " 255 + getClasspath(), Project.MSG_DEBUG); 256 java.setClasspath(getClasspath() 257 .concatSystemClasspath("ignore")); 258 } 259 else 260 { 261 Path classpath = new Path(getProject()); 262 classpath = classpath.concatSystemClasspath("only"); 263 getProject().log("using system classpath: " + classpath, 264 Project.MSG_DEBUG); 265 java.setClasspath(classpath); 266 } 267 java.setDir(getProject().getBaseDir()); 268 java.setClassname("org.jboss.aop.annotation.compiler.AnnotationCompiler"); 269 String args[] = cmd.getJavaCommand().getArguments(); 271 for (int i = 0; i < args.length; i++) 272 { 273 java.createArg().setValue(args[i]); 274 } 275 java.setFailonerror(getFailonerror()); 276 java.setFork(false); 279 java.setTaskName("annotationc"); 280 java.execute(); 281 } 282 catch (Exception ex) 283 { 284 if (ex instanceof BuildException) 285 { 286 throw (BuildException) ex; 287 } 288 else 289 { 290 throw new BuildException("Error running aopc compiler: ", 291 ex, getLocation()); 292 } 293 } 294 } 295 296 protected void logAndAddFilesToCompile(CommandlineJava cmd) 297 { 298 String [] list = compileSourcepath.list(); 301 ArrayList compilingFiles = new ArrayList (); 302 for (int i = 0; i < list.length; i++) 303 { 304 File srcDir = getProject().resolveFile(list[i]); 305 if (!srcDir.exists()) 306 { 307 throw new BuildException("srcdir \"" 308 + srcDir.getPath() 309 + "\" does not exist!", getLocation()); 310 } 311 312 DirectoryScanner ds = this.getDirectoryScanner(srcDir); 313 String [] files = ds.getIncludedFiles(); 314 for (int j = 0; j < files.length; j++) 315 { 316 if (files[j].endsWith(".java")) 317 { 318 File f = new File (srcDir, files[j]); 319 compilingFiles.add(f.getAbsolutePath()); 320 } 321 } 322 } 323 324 325 StringBuffer niceSourceList = new StringBuffer ("File"); 326 for (int i = 0; i < compilingFiles.size(); i++) 327 { 328 String file = (String ) compilingFiles.get(i); 329 cmd.createArgument().setValue(file); 330 niceSourceList.append(" " + file + lSep); 331 } 332 } 334 335 } 336 | Popular Tags |