01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */package com.bm.ejb3guice.inject;
16:
17: import com.bm.ejb3guice.internal.GuiceFastClass;
18: import com.bm.ejb3guice.internal.Objects;
19:
20: import java.lang.reflect.Constructor;
21: import java.lang.reflect.InvocationTargetException;
22: import java.lang.reflect.Modifier;
23: import net.sf.cglib.reflect.FastClass;
24: import net.sf.cglib.reflect.FastConstructor;
25:
26: /**
27: * Default {@link ConstructionProxyFactory} implementation. Simply invokes the
28: * constructor. Can be reused by other {@code ConstructionProxyFactory}
29: * implementations.
30: *
31: * @author crazybob@google.com (Bob Lee)
32: */
33: class DefaultConstructionProxyFactory implements
34: ConstructionProxyFactory {
35:
36: public <T> ConstructionProxy<T> get(final Constructor<T> constructor) {
37: // We can't use FastConstructor if the constructor is private or protected.
38: if (Modifier.isPrivate(constructor.getModifiers())
39: || Modifier.isProtected(constructor.getModifiers())) {
40: constructor.setAccessible(true);
41: return new ConstructionProxy<T>() {
42: public T newInstance(Object... arguments)
43: throws InvocationTargetException {
44: Objects.assertNoNulls(arguments);
45: try {
46: return constructor.newInstance(arguments);
47: } catch (InstantiationException e) {
48: throw new RuntimeException(e);
49: } catch (IllegalAccessException e) {
50: throw new AssertionError(e);
51: }
52: }
53: };
54: }
55:
56: Class<T> classToConstruct = constructor.getDeclaringClass();
57: FastClass fastClass = GuiceFastClass.create(classToConstruct);
58: final FastConstructor fastConstructor = fastClass
59: .getConstructor(constructor);
60: return new ConstructionProxy<T>() {
61: @SuppressWarnings("unchecked")
62: public T newInstance(Object... arguments)
63: throws InvocationTargetException {
64: Objects.assertNoNulls(arguments);
65: return (T) fastConstructor.newInstance(arguments);
66: }
67: };
68: }
69: }
|