blob: 1c6f95990cb9ffa23d86c373497fbc199b656c00 [file] [log] [blame]
limpbizkit916f5482008-04-16 20:51:14 +00001/**
2 * Copyright (C) 2008 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 */
16
17
limpbizkit8b237452008-04-22 06:47:36 +000018package com.google.inject;
limpbizkit916f5482008-04-16 20:51:14 +000019
kevinb9n1601ae52008-06-03 22:21:04 +000020import static com.google.common.base.Preconditions.checkNotNull;
limpbizkitdf98fcd2008-06-14 05:02:15 +000021import com.google.inject.internal.ErrorHandler;
22import com.google.inject.internal.ErrorMessage;
23import com.google.inject.internal.StackTraceElements;
limpbizkit916f5482008-04-16 20:51:14 +000024import java.lang.reflect.Constructor;
25
26/**
27 * @author jessewilson@google.com (Jesse Wilson)
28 */
limpbizkit8b237452008-04-22 06:47:36 +000029class RuntimeReflectionFactory implements Reflection.Factory {
limpbizkit916f5482008-04-16 20:51:14 +000030 public Reflection create(ErrorHandler errorHandler,
31 ConstructionProxyFactory constructionProxyFactory) {
32 return new RuntimeReflection(errorHandler, constructionProxyFactory);
33 }
34
35 private static class RuntimeReflection implements Reflection {
36 private final ErrorHandler errorHandler;
37 private final ConstructionProxyFactory constructionProxyFactory;
38
39 private RuntimeReflection(ErrorHandler errorHandler,
40 ConstructionProxyFactory constructionProxyFactory) {
kevinb9n1601ae52008-06-03 22:21:04 +000041 this.errorHandler = checkNotNull(errorHandler, "errorHandler");
42 this.constructionProxyFactory = checkNotNull(constructionProxyFactory, "constructionProxyFatory");
limpbizkit916f5482008-04-16 20:51:14 +000043 }
44
45 public <T> ConstructionProxy<T> getConstructionProxy(Class<T> implementation) {
46 return constructionProxyFactory.get(findConstructorIn(implementation));
47 }
48
49 private <T> Constructor<T> findConstructorIn(Class<T> implementation) {
50 Constructor<T> found = null;
51 @SuppressWarnings("unchecked")
52 Constructor<T>[] constructors
53 = (Constructor<T>[]) implementation.getDeclaredConstructors();
54 for (Constructor<T> constructor : constructors) {
55 Inject inject = constructor.getAnnotation(Inject.class);
56 if (inject != null) {
57 if (inject.optional()) {
58 errorHandler.handle(
59 StackTraceElements.forMember(constructor),
limpbizkitdf98fcd2008-06-14 05:02:15 +000060 ErrorMessage.optionalConstructor());
limpbizkit916f5482008-04-16 20:51:14 +000061 }
62
63 if (found != null) {
64 errorHandler.handle(
65 StackTraceElements.forMember(found),
limpbizkitdf98fcd2008-06-14 05:02:15 +000066 ErrorMessage.tooManyConstructors());
limpbizkit916f5482008-04-16 20:51:14 +000067 return invalidConstructor();
68 }
69 found = constructor;
70 }
71 }
72 if (found != null) {
73 return found;
74 }
75
76 // If no annotated constructor is found, look for a no-arg constructor
77 // instead.
78 try {
79 return implementation.getDeclaredConstructor();
80 }
81 catch (NoSuchMethodException e) {
82 errorHandler.handle(
83 StackTraceElements.forMember(
84 implementation.getDeclaredConstructors()[0]),
limpbizkitdf98fcd2008-06-14 05:02:15 +000085 ErrorMessage.missingConstructor(implementation));
limpbizkit916f5482008-04-16 20:51:14 +000086 return invalidConstructor();
87 }
88 }
89
90 /**
91 * A placeholder. This enables us to continue processing and gather more
92 * errors but blows up if you actually try to use it.
93 */
94 static class InvalidConstructor {
95 InvalidConstructor() {
96 throw new AssertionError();
97 }
98 }
99
100 @SuppressWarnings("unchecked")
101 static <T> Constructor<T> invalidConstructor() {
102 try {
103 return (Constructor<T>) InvalidConstructor.class.getDeclaredConstructor();
104 }
105 catch (NoSuchMethodException e) {
106 throw new AssertionError(e);
107 }
108 }
109 }
110}