blob: d414a5322078dcaed8bf03002712ca44bd9e734d [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
19import com.google.inject.util.GuiceFastClass;
crazybobleee3adfd62007-02-02 21:30:08 +000020import java.lang.reflect.Constructor;
21import java.lang.reflect.InvocationTargetException;
kevinb9na99dca72007-02-11 04:48:57 +000022import net.sf.cglib.reflect.FastClass;
23import net.sf.cglib.reflect.FastConstructor;
crazybobleee3adfd62007-02-02 21:30:08 +000024
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 */
kevinb9na99dca72007-02-11 04:48:57 +000032class DefaultConstructionProxyFactory implements ConstructionProxyFactory {
crazybobleee3adfd62007-02-02 21:30:08 +000033
34 public <T> ConstructionProxy<T> get(Constructor<T> constructor) {
kevinb9na99dca72007-02-11 04:48:57 +000035 Class<T> classToConstruct = constructor.getDeclaringClass();
36 FastClass fastClass = GuiceFastClass.create(classToConstruct);
37 final FastConstructor fastConstructor
38 = fastClass.getConstructor(constructor);
crazybobleee3adfd62007-02-02 21:30:08 +000039 return new ConstructionProxy<T>() {
kevinb9na99dca72007-02-11 04:48:57 +000040 @SuppressWarnings("unchecked")
crazybobleee3adfd62007-02-02 21:30:08 +000041 public T newInstance(Object... arguments)
42 throws InvocationTargetException {
43 return (T) fastConstructor.newInstance(arguments);
44 }
45 };
46 }
47}