blob: bf046963b0a626b1781568a5dfa7d42e554134d2 [file] [log] [blame]
crazybobleeb8cf1e52007-02-02 21:48:16 +00001/**
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 */
crazybobleee3adfd62007-02-02 21:30:08 +000016
crazyboblee10a3b022007-02-10 01:49:38 +000017package com.google.inject;
18
limpbizkit916f5482008-04-16 20:51:14 +000019import com.google.inject.internal.ErrorHandler;
kevinb9ncad2c2b2007-05-15 17:28:03 +000020import com.google.inject.internal.GuiceFastClass;
kevinb9na99dca72007-02-11 04:48:57 +000021import net.sf.cglib.reflect.FastClass;
22import net.sf.cglib.reflect.FastConstructor;
crazybobleee3adfd62007-02-02 21:30:08 +000023
limpbizkit916f5482008-04-16 20:51:14 +000024import java.lang.reflect.Constructor;
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Member;
27import java.lang.reflect.Modifier;
28import java.util.List;
29
crazybobleee3adfd62007-02-02 21:30:08 +000030/**
limpbizkit009bb092008-05-07 06:36:02 +000031 * Default {@link ConstructionProxyFactory} implementation. Simply invokes the
crazybobleee3adfd62007-02-02 21:30:08 +000032 * constructor. Can be reused by other {@code ConstructionProxyFactory}
33 * implementations.
34 *
35 * @author crazybob@google.com (Bob Lee)
36 */
kevinb9na99dca72007-02-11 04:48:57 +000037class DefaultConstructionProxyFactory implements ConstructionProxyFactory {
crazybobleee3adfd62007-02-02 21:30:08 +000038
limpbizkit916f5482008-04-16 20:51:14 +000039 private final ErrorHandler errorHandler;
40
41 DefaultConstructionProxyFactory(ErrorHandler errorHandler) {
42 this.errorHandler = errorHandler;
43 }
44
crazyboblee0b3189c2007-02-24 00:14:51 +000045 public <T> ConstructionProxy<T> get(final Constructor<T> constructor) {
crazyboblee77bf3b22007-02-25 21:37:02 +000046 // We can't use FastConstructor if the constructor is private or protected.
47 if (Modifier.isPrivate(constructor.getModifiers())
48 || Modifier.isProtected(constructor.getModifiers())) {
crazyboblee0b3189c2007-02-24 00:14:51 +000049 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 }
limpbizkit916f5482008-04-16 20:51:14 +000063 public List<Parameter<?>> getParameters() {
64 return Parameter.forConstructor(errorHandler, constructor);
65 }
66 public Member getMember() {
67 return constructor;
68 }
crazyboblee0b3189c2007-02-24 00:14:51 +000069 };
70 }
71
kevinb9na99dca72007-02-11 04:48:57 +000072 Class<T> classToConstruct = constructor.getDeclaringClass();
73 FastClass fastClass = GuiceFastClass.create(classToConstruct);
74 final FastConstructor fastConstructor
75 = fastClass.getConstructor(constructor);
crazybobleee3adfd62007-02-02 21:30:08 +000076 return new ConstructionProxy<T>() {
kevinb9na99dca72007-02-11 04:48:57 +000077 @SuppressWarnings("unchecked")
crazybobleee3adfd62007-02-02 21:30:08 +000078 public T newInstance(Object... arguments)
79 throws InvocationTargetException {
crazybobleee3adfd62007-02-02 21:30:08 +000080 return (T) fastConstructor.newInstance(arguments);
81 }
limpbizkit916f5482008-04-16 20:51:14 +000082 public List<Parameter<?>> getParameters() {
limpbizkit7e6659c2008-05-14 01:22:21 +000083 return Parameter.forConstructor(errorHandler, constructor);
limpbizkit916f5482008-04-16 20:51:14 +000084 }
85 public Member getMember() {
limpbizkit7e6659c2008-05-14 01:22:21 +000086 return constructor;
limpbizkit916f5482008-04-16 20:51:14 +000087 }
crazybobleee3adfd62007-02-02 21:30:08 +000088 };
89 }
90}