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 | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.BytecodeGen; |
limpbizkit | 47062f1 | 2008-07-03 20:53:24 +0000 | [diff] [blame] | 20 | import com.google.inject.internal.BytecodeGen.Visibility; |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 21 | import com.google.inject.internal.ImmutableMap; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 22 | import com.google.inject.spi.InjectionPoint; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 23 | import java.lang.reflect.Constructor; |
| 24 | import java.lang.reflect.InvocationTargetException; |
limpbizkit | 696c5cd | 2008-12-30 21:48:17 +0000 | [diff] [blame] | 25 | import java.lang.reflect.Method; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 26 | import java.lang.reflect.Modifier; |
limpbizkit | 696c5cd | 2008-12-30 21:48:17 +0000 | [diff] [blame] | 27 | import java.util.List; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 28 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 29 | /** |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 30 | * Produces construction proxies that invoke the class constructor. |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 31 | * |
| 32 | * @author crazybob@google.com (Bob Lee) |
| 33 | */ |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 34 | class DefaultConstructionProxyFactory<T> implements ConstructionProxyFactory<T> { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 35 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 36 | private final InjectionPoint injectionPoint; |
| 37 | |
| 38 | /** |
| 39 | * @param injectionPoint an injection point whose member is a constructor of {@code T}. |
| 40 | */ |
| 41 | DefaultConstructionProxyFactory(InjectionPoint injectionPoint) { |
| 42 | this.injectionPoint = injectionPoint; |
| 43 | } |
| 44 | |
| 45 | public ConstructionProxy<T> create() { |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 46 | @SuppressWarnings("unchecked") // the injection point is for a constructor of T |
| 47 | final Constructor<T> constructor = (Constructor<T>) injectionPoint.getMember(); |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 48 | |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 49 | // Use FastConstructor if the constructor is public. |
| 50 | if (Modifier.isPublic(constructor.getModifiers())) { |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 51 | /*if[AOP]*/ |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 52 | return new ConstructionProxy<T>() { |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 53 | Class<T> classToConstruct = constructor.getDeclaringClass(); |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 54 | final net.sf.cglib.reflect.FastConstructor fastConstructor |
| 55 | = BytecodeGen.newFastClass(classToConstruct, Visibility.forMember(constructor)) |
| 56 | .getConstructor(constructor); |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 57 | |
| 58 | @SuppressWarnings("unchecked") |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 59 | public T newInstance(Object... arguments) throws InvocationTargetException { |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 60 | return (T) fastConstructor.newInstance(arguments); |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 61 | } |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 62 | public InjectionPoint getInjectionPoint() { |
| 63 | return injectionPoint; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 64 | } |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 65 | public Constructor<T> getConstructor() { |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 66 | return constructor; |
| 67 | } |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 68 | public ImmutableMap<Method, List<org.aopalliance.intercept.MethodInterceptor>> |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 69 | getMethodInterceptors() { |
limpbizkit | 696c5cd | 2008-12-30 21:48:17 +0000 | [diff] [blame] | 70 | return ImmutableMap.of(); |
| 71 | } |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 72 | }; |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 73 | /*end[AOP]*/ |
| 74 | } else { |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 75 | constructor.setAccessible(true); |
| 76 | } |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 77 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 78 | return new ConstructionProxy<T>() { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 79 | public T newInstance(Object... arguments) throws InvocationTargetException { |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 80 | try { |
| 81 | return constructor.newInstance(arguments); |
| 82 | } catch (InstantiationException e) { |
| 83 | throw new AssertionError(e); // shouldn't happen, we know this is a concrete type |
| 84 | } catch (IllegalAccessException e) { |
| 85 | throw new AssertionError(e); // a security manager is blocking us, we're hosed |
| 86 | } |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 87 | } |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 88 | public InjectionPoint getInjectionPoint() { |
| 89 | return injectionPoint; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 90 | } |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 91 | public Constructor<T> getConstructor() { |
limpbizkit | 7e6659c | 2008-05-14 01:22:21 +0000 | [diff] [blame] | 92 | return constructor; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 93 | } |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 94 | /*if[AOP]*/ |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 95 | public ImmutableMap<Method, List<org.aopalliance.intercept.MethodInterceptor>> |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 96 | getMethodInterceptors() { |
limpbizkit | 696c5cd | 2008-12-30 21:48:17 +0000 | [diff] [blame] | 97 | return ImmutableMap.of(); |
| 98 | } |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 99 | /*end[AOP]*/ |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 100 | }; |
| 101 | } |
| 102 | } |