|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ClassFab.java | - | - | - | - |
|
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 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 name, Class type); | |
59 | ||
60 | /** | |
61 | * Convenience method for checking whether the fabricated class already contains | |
62 | * a method. | |
63 | * @param signature the signature | |
64 | * @return whether or not the fabricated class already contains the method | |
65 | */ | |
66 | public boolean containsMethod( MethodSignature signature ); | |
67 | ||
68 | /** | |
69 | * Adds a method. The method is a public instance method. | |
70 | * @return a method fabricator, used to add catch handlers. | |
71 | * @param modifiers Modifiers for the method (see {@link java.lang.reflect.Modifier}). | |
72 | * @param signature defines the name, return type, parameters and exceptions thrown | |
73 | * @param body The body of the method. | |
74 | * @throws ApplicationRuntimeException if a method with that signature has already | |
75 | * been added, or if there is a Javassist compilation error | |
76 | */ | |
77 | ||
78 | public MethodFab addMethod(int modifiers, MethodSignature signature, String body); | |
79 | ||
80 | /** | |
81 | * Returns a previous defined method so that it can be further enhanced | |
82 | * (perhaps by adding additional catches, etc.). | |
83 | * | |
84 | * @param signature the signature of the method previously added | |
85 | * @return the MethodFab for that method, or null if the method has not been added yet | |
86 | */ | |
87 | ||
88 | public MethodFab getMethodFab(MethodSignature signature); | |
89 | ||
90 | /** | |
91 | * Adds a constructor to the class. The constructor will be public. | |
92 | * @param parameterTypes the type of each parameter, or null if the constructor takes no parameters. | |
93 | * @param exceptions the type of each exception, or null if the constructor throws no exceptions. | |
94 | * @param body The body of the constructor. | |
95 | */ | |
96 | public void addConstructor(Class[] parameterTypes, Class[] exceptions, String body); | |
97 | ||
98 | /** | |
99 | * Invoked last to create the class. This will enforce that | |
100 | * all abstract methods have been implemented in the (concrete) class. | |
101 | */ | |
102 | public Class createClass(); | |
103 | } |
|