1   /*
2    * The Apache Software License, Version 1.1
3    *
4    * Copyright (c) 2002 The Apache Software Foundation.  All rights
5    * reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 1. Redistributions of source code must retain the above copyright
12   *    notice, this list of conditions and the following disclaimer.
13   *
14   * 2. Redistributions in binary form must reproduce the above copyright
15   *    notice, this list of conditions and the following disclaimer in
16   *    the documentation and/or other materials provided with the
17   *    distribution.
18   *
19   * 3. The end-user documentation included with the redistribution,
20   *    if any, must include the following acknowledgment:
21   *       "This product includes software developed by the
22   *        Apache Software Foundation (http://www.apache.org/)."
23   *    Alternately, this acknowledgment may appear in the software itself,
24   *    if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The names "Apache" and "Apache Software Foundation" must
27   *    not be used to endorse or promote products derived from this
28   *    software without prior written permission. For written
29   *    permission, please contact apache@apache.org.
30   *
31   * 5. Products derived from this software may not be called "Apache",
32   *    nor may "Apache" appear in their name, without prior written
33   *    permission of the Apache Software Foundation.
34   *
35   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46   * SUCH DAMAGE.
47   * ====================================================================
48   *
49   * This software consists of voluntary contributions made by many
50   * individuals on behalf of the Apache Software Foundation.  For more
51   * information on the Apache Software Foundation, please see
52   * <http://www.apache.org/>.
53   */
54  package net.sf.cglib.reflect;
55  
56  import net.sf.cglib.core.*;
57  import java.lang.reflect.Constructor;
58  import java.lang.reflect.InvocationTargetException;
59  import java.lang.reflect.Method;
60  import org.objectweb.asm.ClassVisitor;
61  import org.objectweb.asm.Type;
62  
63  abstract public class FastClass
64  {
65      private Class type;
66  
67      protected FastClass(Class type) {
68          this.type = type;
69      }
70  
71      public static FastClass create(Class type) {
72          Generator gen = new Generator();
73          gen.setType(type);
74          return gen.create();
75      }
76  
77      public static class Generator extends AbstractClassGenerator
78      {
79          private static final Source SOURCE = new Source(FastClass.class.getName());
80          private Class type;
81          
82          public Generator() {
83              super(SOURCE);
84          }
85  
86          public void setType(Class type) {
87              this.type = type;
88          }
89          
90          public FastClass create() {
91              setNamePrefix(type.getName());
92              return (FastClass)super.create(type.getName());
93          }
94  
95          protected ClassLoader getDefaultClassLoader() {
96              return type.getClassLoader();
97          }
98  
99          public void generateClass(ClassVisitor v) throws Exception {
100             new FastClassEmitter(v, getClassName(), type);
101         }
102 
103         protected Object firstInstance(Class type) {
104             return ReflectUtils.newInstance(type,
105                                             new Class[]{ Class.class },
106                                             new Object[]{ this.type });
107         }
108 
109         protected Object nextInstance(Object instance) {
110             return instance;
111         }
112     }
113     
114     public Object invoke(String name, Class[] parameterTypes, Object obj, Object[] args) throws InvocationTargetException {
115         return invoke(getIndex(name, parameterTypes), obj, args);
116     }
117 
118     public Object newInstance() throws InvocationTargetException {
119         return newInstance(getIndex(Constants.EMPTY_CLASS_ARRAY), null);
120     }
121 
122     public Object newInstance(Class[] parameterTypes, Object[] args) throws InvocationTargetException {
123         return newInstance(getIndex(parameterTypes), args);
124     }
125     
126     public FastMethod getMethod(Method method) {
127         return new FastMethod(this, method);
128     }
129 
130     public FastConstructor getConstructor(Constructor constructor) {
131         return new FastConstructor(this, constructor);
132     }
133 
134     public FastMethod getMethod(String name, Class[] parameterTypes) {
135         try {
136             return getMethod(type.getMethod(name, parameterTypes));
137         } catch (NoSuchMethodException e) {
138             throw new NoSuchMethodError(e.getMessage());
139         }
140     }
141 
142     public FastConstructor getConstructor(Class[] parameterTypes) {
143         try {
144             return getConstructor(type.getConstructor(parameterTypes));
145         } catch (NoSuchMethodException e) {
146             throw new NoSuchMethodError(e.getMessage());
147         }
148     }
149 
150     public String getName() {
151         return type.getName();
152     }
153 
154     public Class getJavaClass() {
155         return type;
156     }
157 
158     public String toString() {
159         return type.toString();
160     }
161 
162     public int hashCode() {
163         return type.hashCode();
164     }
165 
166     public boolean equals(Object o) {
167         if (o == null || !(o instanceof FastClass)) {
168             return false;
169         }
170         return type.equals(((FastClass)o).type);
171     }
172 
173     /***
174      * Return the index of the matching method. The index may be used
175      * later to invoke the method with less overhead. If more than one
176      * method matches (i.e. they differ by return type only), one is
177      * chosen arbitrarily.
178      * @see #invoke(int, Object, Object[])
179      * @param name the method name
180      * @param parameterTypes the parameter array
181      * @return the index, or <code>-1</code> if none is found.
182      */
183     abstract public int getIndex(String name, Class[] parameterTypes);
184 
185     /***
186      * Return the index of the matching constructor. The index may be used
187      * later to create a new instance with less overhead.
188      * @see #newInstance(int, Object[])
189      * @param parameterTypes the parameter array
190      * @return the constructor index, or <code>-1</code> if none is found.
191      */
192     abstract public int getIndex(Class[] parameterTypes);
193 
194     /***
195      * Invoke the method with the specified index.
196      * @see getIndex(name, Class[])
197      * @param index the method index
198      * @param obj the object the underlying method is invoked from
199      * @param args the arguments used for the method call
200      * @throws java.lang.reflect.InvocationTargetException if the underlying method throws an exception
201      */
202     abstract public Object invoke(int index, Object obj, Object[] args) throws InvocationTargetException;
203 
204     /***
205      * Create a new instance using the specified constructor index and arguments.
206      * @see getIndex(Class[])
207      * @param index the constructor index
208      * @param args the arguments passed to the constructor
209      * @throws java.lang.reflect.InvocationTargetException if the constructor throws an exception
210      */
211     abstract public Object newInstance(int index, Object[] args) throws InvocationTargetException;
212 
213     abstract public int getIndex(Signature sig);
214 
215     /***
216      * Returns the maximum method index for this class.
217      */
218     abstract public int getMaxIndex();
219 
220     protected static String getSignatureWithoutReturnType(String name, Class[] parameterTypes) {
221         StringBuffer sb = new StringBuffer();
222         sb.append(name);
223         sb.append('(');
224         for (int i = 0; i < parameterTypes.length; i++) {
225             sb.append(Type.getDescriptor(parameterTypes[i]));
226         }
227         sb.append(')');
228         return sb.toString();
229     }
230 }
This page was automatically generated by Maven