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