KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > tools > ajdoc > rootmakers > Javadoc


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
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26
27 import org.aspectj.tools.ajdoc.ErrPrinter;
28
29 /**
30  * An abstract class for a javadoc rootmaker.
31  * Provides some error handling and misc. functionality.
32  *
33  * @author Jeff Palm
34  */

35 public abstract class Javadoc {
36
37     /** Subclasses may want this ;). */
38     protected ErrPrinter err;
39
40     /** Default ctor. */
41     public Javadoc() {}
42     
43
44     /**
45      * Returns the result of invoking <code>method</code>,
46      * on target <code>owner</code>, with arguments <code>args</code>.
47      * This method handles errors that arise.
48      *
49      * @param method method to invoke.
50      * @param owner target of the invocation.
51      * @param args arguments to pass the method.
52      * @return result of invoking the method on
53      * the passed in owner with the passed in
54      * parameters.
55      */

56     protected final Object JavaDoc invoke(Method JavaDoc method, Object JavaDoc owner, Object JavaDoc[] args) {
57         if (method == null || owner == null) return null;
58         String JavaDoc classname = owner.getClass().getName();
59         String JavaDoc methodName = method.getName();
60         try {
61             Thread.currentThread().setContextClassLoader
62                 (owner.getClass().getClassLoader());
63             return method.invoke(owner, args);
64         } catch (InvocationTargetException JavaDoc e) {
65             err.invocationTargetException(e, classname, methodName);
66         } catch (IllegalAccessException JavaDoc e) {
67             err.ex(e, "method_not_accessible", classname, methodName);
68         } catch (Exception JavaDoc e) {
69             err.ex(e, "exception_thrown", "List",
70                    classname, methodName, e != null ? e.getMessage() : e+"");
71         }
72         return null;
73     }
74
75     /**
76      * Returns the <code>Class</code> with name <code>classname</code>.
77      * This may return null if <code>Class.forName</code> returns null,
78      * but otherwise, this method handles resulting errors.
79      *
80      * @param classname name of the class to get.
81      * @return Class named <code>clasname</code>.
82      */

83     protected final Class JavaDoc type(String JavaDoc classname) {
84         try {
85             return Class.forName(classname);
86         } catch (ClassNotFoundException JavaDoc e) {
87             err.ex(e, "class_not_found", "Hashtable", classname);
88         }
89         return null;
90     }
91
92     /**
93      * Returns the method named <code>name</code>, whose parameters
94      * are declared <code>params</code>, and which is declared
95      * in <code>type</code>.
96      *
97      * @param name name of the method.
98      * @param params type of the parameters.
99      * @param type type in which the resulting method is declared.
100      * @return the method named <code>name</code>, whose parameters
101      * are declared <code>params</code>, and which is declared
102      * in <code>type</code>. This may be null.
103      */

104     protected final Method JavaDoc method(String JavaDoc name, Class JavaDoc[] params, Class JavaDoc type) {
105         if (type == null) return null;
106         try {
107             return type.getMethod(name, params);
108         } catch (NoSuchMethodException JavaDoc e) {
109             err.ex(e, "method_not_found", type.getClass().getName(), name);
110         }
111         return null;
112     }
113
114     /**
115      * Returns the Object resulting from default construction
116      * of a class named <code>classname</code>. This method
117      * handles all errors that arise.
118      *
119      * @param classname class name of the resulting Object.
120      * @return new instance made from the default
121      * construction of a class named
122      * <code>classname</code>. This may be null.
123      * @see #newInstance(Class)
124      */

125     protected final Object JavaDoc newInstance(String JavaDoc classname) {
126         return newInstance(type(classname));
127     }
128     
129     /**
130      * Returns the Object resulting from default construction
131      * of a class <code>type</code>. This method
132      * handles all errors that arise.
133      *
134      * @param type Class of the resulting Object.
135      * @return new instance made from the default
136      * construction of a class named
137      * <code>classname</code>. This may be null.
138      */

139     protected final Object JavaDoc newInstance(Class JavaDoc type) {
140         if (type == null) return null;
141         try {
142             return type.newInstance();
143         } catch (InstantiationException JavaDoc e) {
144             err.ex(e, "must_have_default_ctor", type.getClass().getName());
145             return null;
146         } catch (IllegalAccessException JavaDoc e) {
147             err.ex(e, "method_not_accessible", type.getClass().getName(), "new()");
148             return null;
149         }
150     }
151 }
152
Popular Tags