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