1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
44
45
46
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
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 }