You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JavadocRunner.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * Mik Kersten port to AspectJ 1.1+ code base
  13. * ******************************************************************/
  14. package org.aspectj.tools.ajdoc;
  15. import java.lang.reflect.InvocationTargetException;
  16. import java.lang.reflect.Method;
  17. import java.util.List;
  18. import java.util.Vector;
  19. import javax.tools.DocumentationTool;
  20. import javax.tools.DocumentationTool.DocumentationTask;
  21. import javax.tools.JavaFileObject;
  22. import javax.tools.StandardJavaFileManager;
  23. import javax.tools.ToolProvider;
  24. /**
  25. * @author Mik Kersten
  26. */
  27. class JavadocRunner {
  28. static boolean has14ToolsAvailable() {
  29. try {
  30. Class jdMainClass = com.sun.tools.javadoc.Main.class;
  31. Class[] paramTypes = new Class[] { String[].class };
  32. jdMainClass.getMethod("execute", paramTypes);
  33. } catch (NoClassDefFoundError e) {
  34. return false;
  35. } catch (UnsupportedClassVersionError e) {
  36. return false;
  37. } catch (NoSuchMethodException e) {
  38. return false;
  39. }
  40. return true;
  41. }
  42. static void callJavadoc(String[] javadocargs) {
  43. // final SecurityManager defaultSecurityManager = System.getSecurityManager();
  44. //
  45. // System.setSecurityManager( new SecurityManager() {
  46. // public void checkExit(int status) {
  47. // if (status == 0) {
  48. // throw new SecurityException();
  49. // }
  50. // else {
  51. // System.setSecurityManager(defaultSecurityManager);
  52. // //System.out.println("Error: javadoc exited unexpectedly");
  53. // System.exit(0);
  54. // throw new SecurityException();
  55. // }
  56. // }
  57. // public void checkPermission( java.security.Permission permission ) {
  58. // if ( defaultSecurityManager != null )
  59. // defaultSecurityManager.checkPermission( permission );
  60. // }
  61. // public void checkPermission( java.security.Permission permission,
  62. // Object context ) {
  63. // if ( defaultSecurityManager != null )
  64. // defaultSecurityManager.checkPermission( permission, context );
  65. // }
  66. // } );
  67. try {
  68. // for JDK 1.4 and above call the execute method...
  69. Class jdMainClass = com.sun.tools.javadoc.Main.class;
  70. Method executeMethod = null;
  71. try {
  72. Class[] paramTypes = new Class[] { String[].class };
  73. executeMethod = jdMainClass.getMethod("execute", paramTypes);
  74. } catch (NoSuchMethodException e) {
  75. com.sun.tools.javadoc.Main.main(javadocargs);
  76. // throw new UnsupportedOperationException("ajdoc requires a tools library from JDK 1.4 or later.");
  77. }
  78. try {
  79. executeMethod.invoke(null, new Object[] { javadocargs });
  80. } catch (IllegalArgumentException e1) {
  81. throw new RuntimeException("Failed to invoke javadoc");
  82. } catch (IllegalAccessException e1) {
  83. throw new RuntimeException("Failed to invoke javadoc");
  84. } catch (InvocationTargetException e1) {
  85. throw new RuntimeException("Failed to invoke javadoc");
  86. }
  87. // main method is documented as calling System.exit() - which stops us dead in our tracks
  88. // com.sun.tools.javadoc.Main.main( javadocargs );
  89. } catch (SecurityException se) {
  90. // Do nothing since we expect it to be thrown
  91. // System.out.println( ">> se: " + se.getMessage() );
  92. }
  93. // Set the security manager back
  94. // System.setSecurityManager(defaultSecurityManager);
  95. }
  96. public static void callJavadocViaToolProvider(Vector<String> options, List<String> files) {
  97. DocumentationTool doctool = ToolProvider.getSystemDocumentationTool();
  98. StandardJavaFileManager fm = doctool.getStandardFileManager(null, null, null);
  99. Iterable<? extends JavaFileObject> jfos = fm.getJavaFileObjects(files.toArray(new String[0]));
  100. DocumentationTask task = doctool.getTask(null/*standard System.err*/, null/*standard file manager*/,
  101. null/*default diagnostic listener*/, null/*standard doclet*/, options, jfos);
  102. task.call();
  103. }
  104. }