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 | |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.ErrorHandler; |
kevinb9n | cad2c2b | 2007-05-15 17:28:03 +0000 | [diff] [blame] | 20 | import com.google.inject.internal.GuiceFastClass; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 21 | import net.sf.cglib.reflect.FastClass; |
| 22 | import net.sf.cglib.reflect.FastConstructor; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 23 | |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 24 | import java.lang.reflect.Constructor; |
| 25 | import java.lang.reflect.InvocationTargetException; |
| 26 | import java.lang.reflect.Member; |
| 27 | import java.lang.reflect.Modifier; |
| 28 | import java.util.List; |
| 29 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 30 | /** |
limpbizkit | 009bb09 | 2008-05-07 06:36:02 +0000 | [diff] [blame] | 31 | * Default {@link ConstructionProxyFactory} implementation. Simply invokes the |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 32 | * constructor. Can be reused by other {@code ConstructionProxyFactory} |
| 33 | * implementations. |
| 34 | * |
| 35 | * @author crazybob@google.com (Bob Lee) |
| 36 | */ |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 37 | class DefaultConstructionProxyFactory implements ConstructionProxyFactory { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 38 | |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 39 | private final ErrorHandler errorHandler; |
| 40 | |
| 41 | DefaultConstructionProxyFactory(ErrorHandler errorHandler) { |
| 42 | this.errorHandler = errorHandler; |
| 43 | } |
| 44 | |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 45 | public <T> ConstructionProxy<T> get(final Constructor<T> constructor) { |
crazyboblee | 77bf3b2 | 2007-02-25 21:37:02 +0000 | [diff] [blame] | 46 | // We can't use FastConstructor if the constructor is private or protected. |
| 47 | if (Modifier.isPrivate(constructor.getModifiers()) |
| 48 | || Modifier.isProtected(constructor.getModifiers())) { |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 49 | constructor.setAccessible(true); |
| 50 | return new ConstructionProxy<T>() { |
| 51 | public T newInstance(Object... arguments) throws |
| 52 | InvocationTargetException { |
| 53 | try { |
| 54 | return constructor.newInstance(arguments); |
| 55 | } |
| 56 | catch (InstantiationException e) { |
| 57 | throw new RuntimeException(e); |
| 58 | } |
| 59 | catch (IllegalAccessException e) { |
| 60 | throw new AssertionError(e); |
| 61 | } |
| 62 | } |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 63 | public List<Parameter<?>> getParameters() { |
| 64 | return Parameter.forConstructor(errorHandler, constructor); |
| 65 | } |
| 66 | public Member getMember() { |
| 67 | return constructor; |
| 68 | } |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 69 | }; |
| 70 | } |
| 71 | |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 72 | Class<T> classToConstruct = constructor.getDeclaringClass(); |
| 73 | FastClass fastClass = GuiceFastClass.create(classToConstruct); |
| 74 | final FastConstructor fastConstructor |
| 75 | = fastClass.getConstructor(constructor); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 76 | return new ConstructionProxy<T>() { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 77 | @SuppressWarnings("unchecked") |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 78 | public T newInstance(Object... arguments) |
| 79 | throws InvocationTargetException { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 80 | return (T) fastConstructor.newInstance(arguments); |
| 81 | } |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 82 | public List<Parameter<?>> getParameters() { |
limpbizkit | 7e6659c | 2008-05-14 01:22:21 +0000 | [diff] [blame] | 83 | return Parameter.forConstructor(errorHandler, constructor); |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 84 | } |
| 85 | public Member getMember() { |
limpbizkit | 7e6659c | 2008-05-14 01:22:21 +0000 | [diff] [blame] | 86 | return constructor; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 87 | } |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 88 | }; |
| 89 | } |
| 90 | } |