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.internal;
21  
22  import org.grouplens.grapht.reflect.InjectionPoint;
23  import org.grouplens.grapht.util.MethodProxy;
24  import org.grouplens.grapht.util.Preconditions;
25  
26  import javax.annotation.Nonnull;
27  import javax.annotation.Nullable;
28  import java.io.InvalidObjectException;
29  import java.io.ObjectInputStream;
30  import java.io.ObjectStreamException;
31  import java.io.Serializable;
32  import java.lang.annotation.Annotation;
33  import java.lang.reflect.Method;
34  import java.lang.reflect.Type;
35  import java.util.Collection;
36  import java.util.Collections;
37  
38  public class NoArgumentInjectionPoint implements InjectionPoint, Serializable {
39      private static final long serialVersionUID = -1L;
40      private final transient Method method;
41  
42      /**
43       * Create a NoArgumentInjectionPoint that wraps the given no-argument
44       * method.
45       *
46       * @param method The method to invoke without arguments
47       */
48      public NoArgumentInjectionPoint(@Nonnull Method method) {
49          Preconditions.notNull("method", method);
50          if (method.getParameterTypes().length != 0) {
51              throw new IllegalArgumentException("Method takes arguments: " + method);
52          }
53  
54          this.method = method;
55      }
56  
57      /**
58       * @return The setter method wrapped by this injection point
59       */
60      @Override @Nonnull
61      public Method getMember() {
62          return method;
63      }
64  
65      @Override
66      public boolean isNullable() {
67          return true;
68      }
69  
70      @Override
71      public Type getType() {
72          return Void.class;
73      }
74  
75      @Override
76      public Class<?> getErasedType() {
77          return Void.class;
78      }
79  
80      @Nullable
81      @Override
82      public Annotation getQualifier() {
83          return null;
84      }
85  
86      @Nullable
87      @Override
88      public <A extends Annotation> A getAttribute(Class<A> atype) {
89          return null;
90      }
91  
92      @Override
93      public Collection<Annotation> getAttributes() {
94          return Collections.emptyList();
95      }
96  
97      @Override
98      public boolean equals(Object o) {
99          if (!(o instanceof NoArgumentInjectionPoint)) {
100             return false;
101         }
102         NoArgumentInjectionPoint p = (NoArgumentInjectionPoint) o;
103         return p.method.equals(method) && p.method == method;
104     }
105 
106     @Override
107     public int hashCode() {
108         return method.hashCode();
109     }
110 
111     @Override
112     public String toString() {
113         return method.toString();
114     }
115 
116     private Object writeReplace() {
117         return new SerialProxy(method);
118     }
119 
120     private void readObject(ObjectInputStream stream) throws ObjectStreamException {
121         throw new InvalidObjectException("Serialization proxy required");
122     }
123 
124     private static class SerialProxy implements Serializable {
125         private static final long serialVersionUID = 1L;
126         private final MethodProxy method;
127         public SerialProxy(Method m) {
128             method = MethodProxy.of(m);
129         }
130 
131         private Object readResolve() throws ObjectStreamException {
132             try {
133                 return new NoArgumentInjectionPoint(method.resolve());
134             } catch (ClassNotFoundException e) {
135                 InvalidObjectException ex =
136                         new InvalidObjectException("no class for " + method.toString());
137                 ex.initCause(e);
138                 throw ex;
139             } catch (NoSuchMethodException e) {
140                 InvalidObjectException ex =
141                         new InvalidObjectException("cannot resolve " + method.toString());
142                 ex.initCause(e);
143                 throw ex;
144             }
145         }
146     }
147 }