KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > testsupport > MethodFactory


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package test.jmock.core.testsupport;
4
5 import java.lang.reflect.Method JavaDoc;
6 import org.objectweb.asm.ClassWriter;
7 import org.objectweb.asm.Constants;
8 import org.objectweb.asm.Type;
9
10
11 public class MethodFactory extends ClassLoader JavaDoc
12 {
13     public static Class JavaDoc[] NO_ARGUMENTS = {};
14     public static Class JavaDoc[] NO_EXCEPTIONS = {};
15
16
17     public Method JavaDoc newMethodReturning( Class JavaDoc returnType ) {
18         return newMethod("ignoredMethodName", NO_ARGUMENTS, returnType, NO_EXCEPTIONS);
19     }
20
21     public Method JavaDoc newMethod( final String JavaDoc methodName,
22                              final Class JavaDoc[] argTypes,
23                              final Class JavaDoc returnType,
24                              final Class JavaDoc[] exceptionTypes )
25     {
26         ClassLoader JavaDoc classLoader = new ClassLoader JavaDoc()
27         {
28             protected Class JavaDoc findClass( String JavaDoc interfaceName ) {
29                 ClassWriter writer = new ClassWriter(true);
30
31                 writer.visit(Constants.ACC_PUBLIC | Constants.ACC_INTERFACE,
32                              nameToClassFormat(interfaceName),
33                              "java/lang/Object",
34                              null, /* interfaces */
35                              null /* source file */);
36
37                 writer.visitMethod(Constants.ACC_PUBLIC | Constants.ACC_ABSTRACT,
38                                    methodName,
39                                    methodDescriptor(returnType, argTypes),
40                                    classNamesInClassFormat(exceptionTypes),
41                                    null /* no attributes */);
42
43                 byte[] classAsBytes = writer.toByteArray();
44
45                 return defineClass(interfaceName, classAsBytes, 0, classAsBytes.length);
46             }
47         };
48
49         try {
50             Class JavaDoc interfaceClass = classLoader.loadClass("InterfaceDefining_" + methodName);
51             return interfaceClass.getMethod(methodName, argTypes);
52         }
53         catch (ClassNotFoundException JavaDoc ex) {
54             throw new Error JavaDoc(ex);
55         }
56         catch (NoSuchMethodException JavaDoc ex) {
57             throw new Error JavaDoc(ex);
58         }
59     }
60
61     static String JavaDoc nameToClassFormat( String JavaDoc name ) {
62         return name.replace('.', '/');
63     }
64
65     static String JavaDoc[] classNamesInClassFormat( Class JavaDoc[] classes ) {
66         String JavaDoc[] namesInClassFormat = new String JavaDoc[classes.length];
67
68         for (int i = 0; i < classes.length; i++) {
69             namesInClassFormat[i] = nameToClassFormat(classes[i].getName());
70         }
71
72         return namesInClassFormat;
73     }
74
75     static String JavaDoc methodDescriptor( Class JavaDoc returnClass, Class JavaDoc[] argClasses ) {
76         return Type.getMethodDescriptor(Type.getType(returnClass), classesToTypes(argClasses));
77     }
78
79     private static Type[] classesToTypes( Class JavaDoc[] classes ) {
80         Type[] types = new Type[classes.length];
81
82         for (int i = 0; i < classes.length; i++) {
83             types[i] = Type.getType(classes[i]);
84         }
85
86         return types;
87     }
88 }
89
Popular Tags