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