1. /*
  2. * Copyright 2001-2002,2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.compilers;
  18. import java.lang.reflect.Method;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.types.Commandline;
  22. /**
  23. * The implementation of the javac compiler for JDK 1.3
  24. * This is primarily a cut-and-paste from the original javac task before it
  25. * was refactored.
  26. *
  27. * @since Ant 1.3
  28. */
  29. public class Javac13 extends DefaultCompilerAdapter {
  30. /**
  31. * Integer returned by the "Modern" jdk1.3 compiler to indicate success.
  32. */
  33. private static final int MODERN_COMPILER_SUCCESS = 0;
  34. /**
  35. * Run the compilation.
  36. *
  37. * @exception BuildException if the compilation has problems.
  38. */
  39. public boolean execute() throws BuildException {
  40. attributes.log("Using modern compiler", Project.MSG_VERBOSE);
  41. Commandline cmd = setupModernJavacCommand();
  42. // Use reflection to be able to build on all JDKs >= 1.1:
  43. try {
  44. Class c = Class.forName ("com.sun.tools.javac.Main");
  45. Object compiler = c.newInstance ();
  46. Method compile = c.getMethod ("compile",
  47. new Class [] {(new String [] {}).getClass ()});
  48. int result = ((Integer) compile.invoke
  49. (compiler, new Object[] {cmd.getArguments()}))
  50. .intValue ();
  51. return (result == MODERN_COMPILER_SUCCESS);
  52. } catch (Exception ex) {
  53. if (ex instanceof BuildException) {
  54. throw (BuildException) ex;
  55. } else {
  56. throw new BuildException("Error starting modern compiler",
  57. ex, location);
  58. }
  59. }
  60. }
  61. }