forked from microbean/microbean-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxySpecification.java
More file actions
208 lines (171 loc) · 6.07 KB
/
ProxySpecification.java
File metadata and controls
208 lines (171 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
*
* Copyright © 2025–2026 microBean™.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.microbean.proxy;
import java.util.List;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import org.microbean.bean.BeanTypeList;
import org.microbean.bean.Id;
import org.microbean.construct.Domain;
import static javax.lang.model.element.ElementKind.INTERFACE;
import static javax.lang.model.type.TypeKind.DECLARED;
import static org.microbean.bean.BeanTypes.proxiableBeanType;
/**
* Information about what requirements a {@link Proxy} must fulfil.
*
* @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
*/
// Deliberately not final.
public class ProxySpecification {
/*
* Instance fields.
*/
private final Domain domain;
private final DeclaredType sc;
private final List<TypeMirror> interfaces;
private final List<AnnotationMirror> annotations;
private final String name;
/*
* Constructors.
*/
/**
* Creates a new {@link ProxySpecification}.
*
* @param domain a {@link Domain}; must not be {@code null}
*
* @param id an {@link Id}; must not be {@code null}
*
* @exception NullPointerException if any argument is {@code null}
*
* @exception IllegalArgumentException if {@code types} does not represent a type that can be proxied
*
* @see org.microbean.bean.BeanTypes#proxiableBeanType(TypeMirror)
*/
public ProxySpecification(final Domain domain, final Id id) {
super();
this.domain = domain; // not nullable
this.annotations = id.annotations();
final BeanTypeList types = id.types();
final TypeMirror t = types.get(0); // putative superclass
if (t.getKind() != DECLARED || domain.javaLangObject(t) && types.size() == 1) {
throw new IllegalArgumentException("id: " + id);
} else if (((DeclaredType)t).asElement().getKind() == INTERFACE) {
this.sc = (DeclaredType)domain.javaLangObject().asType();
this.interfaces = types;
} else if (!proxiableBeanType(t)) {
throw new IllegalArgumentException("id: " + id);
} else {
this.sc = (DeclaredType)t;
this.interfaces = types.interfaces();
}
this.name = computeName(domain, this.sc, this.interfaces);
}
/*
* Instance methods.
*/
@Override // Object
public boolean equals(final Object other) {
if (other == this) {
return true;
} else if (other != null && other.getClass() == this.getClass()) {
final ProxySpecification her = (ProxySpecification)other;
if (!this.domain.equals(her.domain)) {
return false;
}
if (!this.domain.sameType(this.superclass(), her.superclass())) {
return false;
}
final List<TypeMirror> interfaces = this.interfaces();
final List<TypeMirror> herInterfaces = her.interfaces();
final int size = interfaces.size();
if (herInterfaces.size() != size) {
return false;
}
for (int i = 0; i < size; i++) {
if (!this.domain.sameType(interfaces.get(i), herInterfaces.get(i))) {
return false;
}
}
return this.annotations().equals(her.annotations());
} else {
return false;
}
}
@Override // Object
public int hashCode() {
int hashCode = 31;
hashCode = 17 * hashCode + this.domain.hashCode();
hashCode = 17 * hashCode + this.superclass().hashCode();
hashCode = 17 * hashCode + this.interfaces().hashCode();
hashCode = 17 * hashCode + this.annotations().hashCode();
return hashCode;
}
/**
* Returns an immutable {@link List} of {@link AnnotationMirror}s describing this {@link ProxySpecification}.
*
* @return a non-{@code null}, immutable {@link List} of {@link AnnotationMirror} instances
*/
public final List<AnnotationMirror> annotations() {
return this.annotations;
}
/**
* Returns the interfaces the proxy should implement.
*
* @return a non-{@code null}, immutable {@link List} of {@link TypeMirror}s
*/
public final List<TypeMirror> interfaces() {
return this.interfaces;
}
/**
* Returns the name the proxy class should have.
*
* @return a non-{@code null} {@link String}
*/
public final String name() {
return this.name;
}
/**
* Returns the superclass the proxy should specialize.
*
* @return a non-{@code null} {@link DeclaredType}
*/
public final DeclaredType superclass() {
return this.sc;
}
/*
* Static methods.
*/
static final String computeName(final Domain domain, final DeclaredType superclass, final List<TypeMirror> interfaces) {
// TODO: there will absolutely be edge cases here and we know this is not complete.
if (superclass.getKind() != DECLARED) {
throw new IllegalArgumentException("superclass: " + superclass);
}
final DeclaredType proxyClassSibling;
if (domain.javaLangObject(superclass)) {
if (interfaces.isEmpty()) {
throw new IllegalArgumentException("interfaces.isEmpty(); superclass: java.lang.Object");
}
// Interface-only. There will be at least one and it will be the most specialized.
proxyClassSibling = (DeclaredType)interfaces.get(0);
if (proxyClassSibling.getKind() != DECLARED) {
throw new IllegalArgumentException("interfaces: " + interfaces);
}
} else {
proxyClassSibling = superclass;
}
return domain.toString(domain.binaryName((TypeElement)proxyClassSibling.asElement())) + "_Proxy";
}
}