crazyboblee | b8cf1e5 | 2007-02-02 21:48:16 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2006 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 16 | |
crazyboblee | 10a3b02 | 2007-02-10 01:49:38 +0000 | [diff] [blame] | 17 | package com.google.inject; |
| 18 | |
| 19 | import com.google.inject.util.GuiceFastClass; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 20 | import java.lang.reflect.Constructor; |
| 21 | import java.lang.reflect.InvocationTargetException; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 22 | import net.sf.cglib.reflect.FastClass; |
| 23 | import net.sf.cglib.reflect.FastConstructor; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Default {@link ConstructionProxyFactory} implementation. Simply invokes the |
| 27 | * constructor. Can be reused by other {@code ConstructionProxyFactory} |
| 28 | * implementations. |
| 29 | * |
| 30 | * @author crazybob@google.com (Bob Lee) |
| 31 | */ |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 32 | class DefaultConstructionProxyFactory implements ConstructionProxyFactory { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 33 | |
| 34 | public <T> ConstructionProxy<T> get(Constructor<T> constructor) { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 35 | Class<T> classToConstruct = constructor.getDeclaringClass(); |
| 36 | FastClass fastClass = GuiceFastClass.create(classToConstruct); |
| 37 | final FastConstructor fastConstructor |
| 38 | = fastClass.getConstructor(constructor); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 39 | return new ConstructionProxy<T>() { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 40 | @SuppressWarnings("unchecked") |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 41 | public T newInstance(Object... arguments) |
| 42 | throws InvocationTargetException { |
| 43 | return (T) fastConstructor.newInstance(arguments); |
| 44 | } |
| 45 | }; |
| 46 | } |
| 47 | } |