1 18 19 package org.apache.tools.ant.taskdefs.compilers; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 import org.apache.tools.ant.taskdefs.Apt; 24 import org.apache.tools.ant.types.Commandline; 25 import org.apache.tools.ant.types.Path; 26 27 import java.io.File ; 28 import java.lang.reflect.Method ; 29 import java.util.Enumeration ; 30 import java.util.Vector ; 31 32 33 72 public class AptCompilerAdapter extends DefaultCompilerAdapter { 73 74 77 private static final int APT_COMPILER_SUCCESS = 0; 78 81 public static final String APT_ENTRY_POINT = "com.sun.tools.apt.Main"; 82 83 86 public static final String APT_METHOD_NAME = "process"; 87 88 94 protected Apt getApt() { 95 return (Apt) getJavac(); 96 } 97 98 105 static void setAptCommandlineSwitches(Apt apt, Commandline cmd) { 106 107 if (!apt.isCompile()) { 108 cmd.createArgument().setValue("-nocompile"); 109 } 110 111 String factory = apt.getFactory(); 113 if (factory != null) { 114 cmd.createArgument().setValue("-factory"); 115 cmd.createArgument().setValue(factory); 116 } 117 118 Path factoryPath = apt.getFactoryPath(); 120 if (factoryPath != null) { 121 cmd.createArgument().setValue("-factorypath"); 122 cmd.createArgument().setPath(factoryPath); 123 } 124 125 File preprocessDir = apt.getPreprocessDir(); 126 if (preprocessDir != null) { 127 cmd.createArgument().setValue("-s"); 128 cmd.createArgument().setFile(preprocessDir); 129 } 130 131 Vector options = apt.getOptions(); 133 Enumeration elements = options.elements(); 134 Apt.Option opt; 135 StringBuffer arg = null; 136 while (elements.hasMoreElements()) { 137 opt = (Apt.Option) elements.nextElement(); 138 arg = new StringBuffer (); 139 arg.append("-A").append(opt.getName()); 140 if (opt.getValue() != null) { 141 arg.append("=").append(opt.getValue()); 142 } 143 cmd.createArgument().setValue(arg.toString()); 144 } 145 } 146 147 152 protected void setAptCommandlineSwitches(Commandline cmd) { 153 Apt apt = getApt(); 154 setAptCommandlineSwitches(apt, cmd); 155 } 156 157 162 public boolean execute() throws BuildException { 163 attributes.log("Using apt compiler", Project.MSG_VERBOSE); 164 Commandline cmd = setupModernJavacCommand(); 166 setAptCommandlineSwitches(cmd); 168 169 try { 172 Class c = Class.forName(APT_ENTRY_POINT); 173 Object compiler = c.newInstance(); 174 Method compile = c.getMethod(APT_METHOD_NAME, 175 new Class []{(new String []{}).getClass()}); 176 int result = ((Integer ) compile.invoke 177 (compiler, new Object []{cmd.getArguments()})) 178 .intValue(); 179 return (result == APT_COMPILER_SUCCESS); 180 } catch (BuildException be) { 181 throw be; 183 } catch (Exception ex) { 184 throw new BuildException("Error starting apt compiler", 186 ex, location); 187 } 188 } 189 } 190 | Popular Tags |