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.

Javadoc.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * This file is part of the debugger and core tools for the AspectJ(tm)
  4. * programming language; see http://aspectj.org
  5. *
  6. * The contents of this file are subject to the Mozilla Public License
  7. * Version 1.1 (the "License"); you may not use this file except in
  8. * compliance with the License. You may obtain a copy of the License at
  9. * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is AspectJ.
  17. *
  18. * The Initial Developer of the Original Code is Xerox Corporation. Portions
  19. * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
  20. * All Rights Reserved.
  21. */
  22. package org.aspectj.tools.ajdoc.rootmakers;
  23. import java.lang.reflect.InvocationTargetException;
  24. import java.lang.reflect.Method;
  25. import org.aspectj.tools.ajdoc.ErrPrinter;
  26. /**
  27. * An abstract class for a javadoc rootmaker.
  28. * Provides some error handling and misc. functionality.
  29. *
  30. * @author Jeff Palm
  31. */
  32. public abstract class Javadoc {
  33. /** Subclasses may want this ;). */
  34. protected ErrPrinter err;
  35. /** Default ctor. */
  36. public Javadoc() {}
  37. /**
  38. * Returns the result of invoking <code>method</code>,
  39. * on target <code>owner</code>, with arguments <code>args</code>.
  40. * This method handles errors that arise.
  41. *
  42. * @param method method to invoke.
  43. * @param owner target of the invocation.
  44. * @param args arguments to pass the method.
  45. * @return result of invoking the method on
  46. * the passed in owner with the passed in
  47. * parameters.
  48. */
  49. protected final Object invoke(Method method, Object owner, Object[] args) {
  50. if (method == null || owner == null) return null;
  51. String classname = owner.getClass().getName();
  52. String methodName = method.getName();
  53. try {
  54. Thread.currentThread().setContextClassLoader
  55. (owner.getClass().getClassLoader());
  56. return method.invoke(owner, args);
  57. } catch (InvocationTargetException e) {
  58. err.invocationTargetException(e, classname, methodName);
  59. } catch (IllegalAccessException e) {
  60. err.ex(e, "method_not_accessible", classname, methodName);
  61. } catch (Exception e) {
  62. err.ex(e, "exception_thrown", "List",
  63. classname, methodName, e != null ? e.getMessage() : e+"");
  64. }
  65. return null;
  66. }
  67. /**
  68. * Returns the <code>Class</code> with name <code>classname</code>.
  69. * This may return null if <code>Class.forName</code> returns null,
  70. * but otherwise, this method handles resulting errors.
  71. *
  72. * @param classname name of the class to get.
  73. * @return Class named <code>clasname</code>.
  74. */
  75. protected final Class type(String classname) {
  76. try {
  77. return Class.forName(classname);
  78. } catch (ClassNotFoundException e) {
  79. err.ex(e, "class_not_found", "Hashtable", classname);
  80. }
  81. return null;
  82. }
  83. /**
  84. * Returns the method named <code>name</code>, whose parameters
  85. * are declared <code>params</code>, and which is declared
  86. * in <code>type</code>.
  87. *
  88. * @param name name of the method.
  89. * @param params type of the parameters.
  90. * @param type type in which the resulting method is declared.
  91. * @return the method named <code>name</code>, whose parameters
  92. * are declared <code>params</code>, and which is declared
  93. * in <code>type</code>. This may be null.
  94. */
  95. protected final Method method(String name, Class[] params, Class type) {
  96. if (type == null) return null;
  97. try {
  98. return type.getMethod(name, params);
  99. } catch (NoSuchMethodException e) {
  100. err.ex(e, "method_not_found", type.getClass().getName(), name);
  101. }
  102. return null;
  103. }
  104. /**
  105. * Returns the Object resulting from default construction
  106. * of a class named <code>classname</code>. This method
  107. * handles all errors that arise.
  108. *
  109. * @param classname class name of the resulting Object.
  110. * @return new instance made from the default
  111. * construction of a class named
  112. * <code>classname</code>. This may be null.
  113. * @see #newInstance(Class)
  114. */
  115. protected final Object newInstance(String classname) {
  116. return newInstance(type(classname));
  117. }
  118. /**
  119. * Returns the Object resulting from default construction
  120. * of a class <code>type</code>. This method
  121. * handles all errors that arise.
  122. *
  123. * @param type Class of the resulting Object.
  124. * @return new instance made from the default
  125. * construction of a class named
  126. * <code>classname</code>. This may be null.
  127. */
  128. protected final Object newInstance(Class type) {
  129. if (type == null) return null;
  130. try {
  131. return type.newInstance();
  132. } catch (InstantiationException e) {
  133. err.ex(e, "must_have_default_ctor", type.getClass().getName());
  134. return null;
  135. } catch (IllegalAccessException e) {
  136. err.ex(e, "method_not_accessible", type.getClass().getName(), "new()");
  137. return null;
  138. }
  139. }
  140. }