KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > ClassFab


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.service;
16
17 /**
18  * Used when fabricating a new class. Represents a wrapper around
19  * the Javassist library.
20  *
21  * <p>
22  * The core concept of Javassist is how method bodies (as well as constructor bodies, etc.)
23  * are specified ... as a very Java-like scripting language. The
24  * {@link org.apache.hivemind.service.BodyBuilder} class is <em>very</em> useful for assembling
25  * this method bodies. Details are available at the
26  * <a HREF="http://jboss.org/products/javassist">Javassist home page</a>.
27  *
28  * <p>
29  * Method bodies look largely like Java. References to java classes must be fully qualified.
30  * Several special variables are used:
31  * <ul>
32  * <li><code>$0</code> first parameter, equivalent to <code>this</code> in Java code (and can't
33  * be used when creating a static method)
34  * <li><code>$1, $2, ...</code> actual parameters to the method
35  * <li><code>$args</code> all the parameters as an <code>Object[]</code>
36  * <li><code>$r</code> the return type of the method, typically used as <code>return ($r) ...</code>.
37  * <code>$r</code> is valid with method that return <code>void</code>. This also handles conversions
38  * between wrapper types and primitive types.
39  * <li><code>$w</code> conversion from primitive type to wrapper type, used as <code>($w) foo()</code> where
40  * <code>foo()</code> returns a primitive type and a wrapper type is needed
41  * <li>
42  * </ul>
43  *
44  * @author Howard Lewis Ship
45  */

46 public interface ClassFab
47 {
48     /**
49      * Adds the specified interface as an interface implemented by this class.
50      */

51     public void addInterface(Class JavaDoc interfaceClass);
52
53     /**
54      * Adds a new field with the given name and type. The field is
55      * added as a private field.
56      */

57
58     public void addField(String JavaDoc name, Class JavaDoc type);
59
60     public boolean canConvert(Class JavaDoc inputClass);
61     
62     /**
63      * Convenience method for checking whether the fabricated class already contains
64      * a method.
65      * @param signature the signature
66      * @return whether or not the fabricated class already contains the method
67      */

68     public boolean containsMethod( MethodSignature signature );
69     
70     /**
71      * Adds a method. The method is a public instance method.
72      * @return a method fabricator, used to add catch handlers.
73      * @param modifiers Modifiers for the method (see {@link java.lang.reflect.Modifier}).
74      * @param signature defines the name, return type, parameters and exceptions thrown
75      * @param body The body of the method.
76      * @throws org.apache.hivemind.ApplicationRuntimeException if a method with that signature has already
77      * been added, or if there is a Javassist compilation error
78      */

79
80     public MethodFab addMethod(int modifiers, MethodSignature signature, String JavaDoc body);
81
82     /**
83      * Returns a previous defined method so that it can be further enhanced
84      * (perhaps by adding additional catches, etc.).
85      *
86      * @param signature the signature of the method previously added
87      * @return the MethodFab for that method, or null if the method has not been added yet
88      */

89
90     public MethodFab getMethodFab(MethodSignature signature);
91
92     /**
93      * Adds a constructor to the class. The constructor will be public.
94      * @param parameterTypes the type of each parameter, or null if the constructor takes no parameters.
95      * @param exceptions the type of each exception, or null if the constructor throws no exceptions.
96      * @param body The body of the constructor.
97      */

98     public void addConstructor(Class JavaDoc[] parameterTypes, Class JavaDoc[] exceptions, String JavaDoc body);
99
100     /**
101      * Invoked last to create the class. This will enforce that
102      * all abstract methods have been implemented in the (concrete) class.
103      */

104     public Class JavaDoc createClass();
105 }
106
Popular Tags