• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2012 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 package com.google.inject.throwingproviders;
18 
19 import com.google.inject.Binder;
20 import com.google.inject.TypeLiteral;
21 import com.google.inject.internal.Annotations;
22 import com.google.inject.internal.Errors;
23 import com.google.inject.spi.Message;
24 
25 import java.lang.annotation.Annotation;
26 import java.lang.reflect.AnnotatedElement;
27 import java.lang.reflect.Constructor;
28 
29 /**
30  * Utilities for the throwing provider module.
31  *
32  * @author sameb@google.com (Sam Berlin)
33  */
34 class CheckedProvideUtils {
35 
CheckedProvideUtils()36   private CheckedProvideUtils() {}
37 
38   private static final String CONSTRUCTOR_RULES =
39       "Classes must have either one (and only one) constructor annotated with @ThrowingInject.";
40 
41   @SuppressWarnings("unchecked") // safe because it's a constructor of the typeLiteral
findThrowingConstructor( TypeLiteral<? extends T> typeLiteral, Binder binder)42   static <T> Constructor<? extends T> findThrowingConstructor(
43       TypeLiteral<? extends T> typeLiteral, Binder binder) {
44 
45     Class<?> rawType = typeLiteral.getRawType();
46     Errors errors = new Errors(rawType);
47     Constructor<?> cxtor = null;
48     for (Constructor<?> constructor : rawType.getDeclaredConstructors()) {
49       if (constructor.isAnnotationPresent(ThrowingInject.class)) {
50         if (cxtor != null) {
51           errors.addMessage("%s has more than one constructor annotated with @ThrowingInject. "
52               + CONSTRUCTOR_RULES, rawType);
53         }
54 
55         cxtor = constructor;
56         Annotation misplacedBindingAnnotation = Annotations.findBindingAnnotation(
57             errors, cxtor, ((AnnotatedElement) cxtor).getAnnotations());
58         if (misplacedBindingAnnotation != null) {
59           errors.misplacedBindingAnnotation(cxtor, misplacedBindingAnnotation);
60         }
61       }
62     }
63 
64     if (cxtor == null) {
65       errors.addMessage(
66           "Could not find a suitable constructor in %s. " + CONSTRUCTOR_RULES, rawType);
67     }
68 
69     for (Message msg : errors.getMessages()) {
70       binder.addError(msg);
71     }
72     return (Constructor<? extends T>) cxtor;
73   }
74 
75   /** Adds errors to the binder if the exceptions aren't valid. */
validateExceptions(Binder binder, Iterable<TypeLiteral<?>> actualExceptionTypes, Iterable<Class<? extends Throwable>> expectedExceptionTypes, Class<? extends CheckedProvider> checkedProvider)76   static void validateExceptions(Binder binder,
77       Iterable<TypeLiteral<?>> actualExceptionTypes,
78       Iterable<Class<? extends Throwable>> expectedExceptionTypes,
79       Class<? extends CheckedProvider> checkedProvider) {
80     // Validate the exceptions in the method match the exceptions
81     // in the CheckedProvider.
82     for (TypeLiteral<?> exType : actualExceptionTypes) {
83       Class<?> exActual = exType.getRawType();
84       // Ignore runtime exceptions & errors.
85       if (RuntimeException.class.isAssignableFrom(exActual)
86           || Error.class.isAssignableFrom(exActual)) {
87         continue;
88       }
89 
90       boolean notAssignable = true;
91       for (Class<? extends Throwable> exExpected : expectedExceptionTypes) {
92         if (exExpected.isAssignableFrom(exActual)) {
93           notAssignable = false;
94           break;
95         }
96       }
97       if (notAssignable) {
98         binder.addError(
99             "%s is not compatible with the exceptions (%s) declared in "
100             + "the CheckedProvider interface (%s)",
101             exActual, expectedExceptionTypes, checkedProvider);
102       }
103     }
104   }
105 
106 }
107