View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   */
20  package org.grouplens.grapht.reflect;
21  
22  import org.grouplens.grapht.util.Types;
23  
24  import javax.annotation.Nonnull;
25  import javax.annotation.Nullable;
26  import java.lang.annotation.Annotation;
27  import java.lang.reflect.Member;
28  import java.lang.reflect.Type;
29  import java.util.Collection;
30  import java.util.Collections;
31  
32  /**
33   * MockInjectionPoint is a simple injection point that wraps a type, qualifier, and a
34   * transient state. It has no actual injectable point but can be used when
35   * constructing ReflectionDesires on the fly for tests.
36   * 
37   * @author <a href="http://grouplens.org">GroupLens Research</a>
38   */
39  public class MockInjectionPoint implements InjectionPoint {
40      private static final long serialVersionUID = 1L;
41  
42      private final Class<?> type;
43      private final boolean nullable;
44      private final Annotation qualifier;
45      
46      public MockInjectionPoint(Class<?> type, boolean nullable) {
47          this(type, null, nullable);
48      }
49      
50      public MockInjectionPoint(Class<?> type, Annotation qualifier, boolean nullable) {
51          this.type = Types.box(type);
52          this.qualifier = qualifier;
53          this.nullable = nullable;
54      }
55      
56      @Override
57      public Member getMember() {
58          return new Member() {
59              @Override
60              public Class<?> getDeclaringClass() {
61                  return Void.class;
62              }
63  
64              @Override
65              public String getName() {
66                  return "synthetic";
67              }
68  
69              @Override
70              public int getModifiers() {
71                  return 0;
72              }
73  
74              @Override
75              public boolean isSynthetic() {
76                  return true;
77              }
78          };
79      }
80      
81      @Override
82      public Type getType() {
83          return type;
84      }
85      
86      @Override
87      public Class<?> getErasedType() {
88          return type;
89      }
90  
91      @Nullable
92      @Override
93      public Annotation getQualifier() {
94          return qualifier;
95      }
96  
97      @Nullable
98      @Override
99      public <A extends Annotation> A getAttribute(Class<A> atype) {
100         return null;
101     }
102 
103     @Nonnull
104     @Override
105     public Collection<Annotation> getAttributes() {
106         return Collections.emptySet();
107     }
108 
109     @Override
110     public boolean isNullable() {
111         return nullable;
112     }
113 
114     @Override
115     public boolean equals(Object o) {
116         if (!(o instanceof MockInjectionPoint)) {
117             return false;
118         }
119         MockInjectionPoint m = (MockInjectionPoint) o;
120         return m.type.equals(type) && 
121                m.nullable == nullable && 
122                (m.qualifier == qualifier || (qualifier != null && qualifier.equals(m.qualifier)));
123     }
124     
125     @Override
126     public int hashCode() {
127         return type.hashCode() ^ (qualifier == null ? 0 : qualifier.hashCode()) ^ (nullable ? 2 : 4);
128     }
129 }