View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit.internal.injection;
6   
7   import edu.umd.cs.findbugs.annotations.NonNull;
8   import edu.umd.cs.findbugs.annotations.Nullable;
9   
10  import java.lang.annotation.Annotation;
11  import java.lang.reflect.Type;
12  
13  /**
14   * Provides type, name, and value(s) for an injection point, which is either a field to be injected or a parameter in
15   * the chosen constructor of a tested class.
16   */
17  public abstract class InjectionProvider {
18      public static final Object NULL = Void.class;
19  
20      @NonNull
21      protected final Type declaredType;
22      @NonNull
23      protected final String name;
24      @Nullable
25      public InjectionProvider parent;
26  
27      protected InjectionProvider(@NonNull Type declaredType, @NonNull String name) {
28          this.declaredType = declaredType;
29          this.name = name;
30      }
31  
32      @NonNull
33      public final Type getDeclaredType() {
34          return declaredType;
35      }
36  
37      @NonNull
38      public abstract Class<?> getClassOfDeclaredType();
39  
40      @NonNull
41      public final String getName() {
42          return name;
43      }
44  
45      @NonNull
46      public Annotation[] getAnnotations() {
47          throw new UnsupportedOperationException("No annotations");
48      }
49  
50      @Nullable
51      public Object getValue(@Nullable Object owner) {
52          return null;
53      }
54  
55      public boolean isRequired() {
56          return false;
57      }
58  
59      public boolean hasAnnotation(@NonNull Class<? extends Annotation> annotationOfInterest) {
60          for (Annotation annotation : getAnnotations()) {
61              if (annotationOfInterest.isInstance(annotation)) {
62                  return true;
63              }
64          }
65  
66          return false;
67      }
68  
69      @Override
70      public String toString() {
71          Class<?> type = getClassOfDeclaredType();
72          StringBuilder description = new StringBuilder().append('"').append(type.getSimpleName()).append(' ')
73                  .append(name).append('"');
74  
75          if (parent != null) {
76              description.append("\r\n  when initializing ").append(parent);
77          }
78  
79          return description.toString();
80      }
81  }