Skip to content

Commit 0fb18c4

Browse files
authored
Adds type method to Domain. Refreshes dependencies. Sets Java release version at 21. (#22)
Signed-off-by: Laird Nelson <[email protected]>
1 parent f4acd74 commit 0fb18c4

File tree

4 files changed

+147
-27
lines changed

4 files changed

+147
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ as a Maven dependency:
3131
Always check https://search.maven.org/artifact/org.microbean/microbean-construct
3232
for up-to-date available versions.
3333
-->
34-
<version>0.0.10</version>
34+
<version>0.0.11</version>
3535
</dependency>
3636
```
3737

pom.xml

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<java.util.logging.config.file>${project.basedir}/src/test/java/logging.properties</java.util.logging.config.file>
7474

7575
<!-- maven-compiler-plugin properties -->
76-
<maven.compiler.release>23</maven.compiler.release>
76+
<maven.compiler.release>21</maven.compiler.release>
7777
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
7878
<maven.compiler.showWarnings>true</maven.compiler.showWarnings>
7979

@@ -119,7 +119,7 @@
119119
<dependency>
120120
<groupId>org.junit</groupId>
121121
<artifactId>junit-bom</artifactId>
122-
<version>5.11.3</version>
122+
<version>5.13.1</version>
123123
<type>pom</type>
124124
<scope>import</scope>
125125
</dependency>
@@ -136,11 +136,13 @@
136136
<artifactId>junit-jupiter-api</artifactId>
137137
<scope>test</scope>
138138
</dependency>
139+
139140
<dependency>
140141
<groupId>org.junit.jupiter</groupId>
141142
<artifactId>junit-jupiter-engine</artifactId>
142143
<scope>test</scope>
143144
</dependency>
145+
144146
</dependencies>
145147

146148
<build>
@@ -160,7 +162,7 @@
160162
</plugin>
161163
<plugin>
162164
<artifactId>maven-clean-plugin</artifactId>
163-
<version>3.4.0</version>
165+
<version>3.5.0</version>
164166
<configuration>
165167
<filesets>
166168
<fileset>
@@ -175,28 +177,21 @@
175177
</plugin>
176178
<plugin>
177179
<artifactId>maven-compiler-plugin</artifactId>
178-
<version>3.13.0</version>
180+
<version>3.14.0</version>
179181
<configuration>
180182
<compilerArgs>
181183
<arg>-Xlint:all</arg>
182184
<arg>-parameters</arg>
183185
</compilerArgs>
184186
</configuration>
185-
<dependencies>
186-
<dependency>
187-
<groupId>org.codehaus.plexus</groupId>
188-
<artifactId>plexus-java</artifactId>
189-
<version>1.3.0</version>
190-
</dependency>
191-
</dependencies>
192187
</plugin>
193188
<plugin>
194189
<artifactId>maven-dependency-plugin</artifactId>
195190
<version>3.8.1</version>
196191
</plugin>
197192
<plugin>
198193
<artifactId>maven-deploy-plugin</artifactId>
199-
<version>3.1.3</version>
194+
<version>3.1.4</version>
200195
</plugin>
201196
<plugin>
202197
<artifactId>maven-enforcer-plugin</artifactId>
@@ -209,15 +204,15 @@
209204
</plugin>
210205
<plugin>
211206
<artifactId>maven-install-plugin</artifactId>
212-
<version>3.1.3</version>
207+
<version>3.1.4</version>
213208
</plugin>
214209
<plugin>
215210
<artifactId>maven-jar-plugin</artifactId>
216211
<version>3.4.2</version>
217212
</plugin>
218213
<plugin>
219214
<artifactId>maven-javadoc-plugin</artifactId>
220-
<version>3.11.1</version>
215+
<version>3.11.2</version>
221216
<configuration>
222217
<docfilessubdirs>true</docfilessubdirs>
223218
</configuration>
@@ -228,7 +223,7 @@
228223
</plugin>
229224
<plugin>
230225
<artifactId>maven-project-info-reports-plugin</artifactId>
231-
<version>3.8.0</version>
226+
<version>3.9.0</version>
232227
</plugin>
233228
<plugin>
234229
<artifactId>maven-release-plugin</artifactId>
@@ -265,14 +260,7 @@
265260
</plugin>
266261
<plugin>
267262
<artifactId>maven-surefire-plugin</artifactId>
268-
<version>3.5.2</version>
269-
<dependencies>
270-
<dependency>
271-
<groupId>org.apache.maven.surefire</groupId>
272-
<artifactId>surefire-junit-platform</artifactId>
273-
<version>3.5.2</version>
274-
</dependency>
275-
</dependencies>
263+
<version>3.5.3</version>
276264
</plugin>
277265
<plugin>
278266
<artifactId>maven-toolchains-plugin</artifactId>
@@ -281,7 +269,7 @@
281269
<plugin>
282270
<groupId>com.github.spotbugs</groupId>
283271
<artifactId>spotbugs-maven-plugin</artifactId>
284-
<version>4.8.6.6</version>
272+
<version>4.9.3.0</version>
285273
</plugin>
286274
<plugin>
287275
<groupId>org.codehaus.mojo</groupId>
@@ -291,7 +279,7 @@
291279
<plugin>
292280
<groupId>io.smallrye</groupId>
293281
<artifactId>jandex-maven-plugin</artifactId>
294-
<version>3.2.3</version>
282+
<version>3.3.1</version>
295283
</plugin>
296284
<plugin>
297285
<groupId>org.sonatype.plugins</groupId>

src/main/java/org/microbean/construct/Domain.java

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
*/
1414
package org.microbean.construct;
1515

16+
import java.lang.reflect.Constructor;
17+
import java.lang.reflect.Executable;
18+
import java.lang.reflect.GenericArrayType;
19+
import java.lang.reflect.GenericDeclaration;
20+
import java.lang.reflect.Method;
21+
import java.lang.reflect.ParameterizedType;
22+
import java.lang.reflect.Type;
23+
1624
import java.util.List;
1725
import java.util.Objects;
1826

@@ -407,6 +415,35 @@ public default TypeMirror elementType(final TypeMirror t) {
407415
*/
408416
public <T extends TypeMirror> T erasure(final T t);
409417

418+
/**
419+
* Returns an {@link ExecutableElement} corresponding to the supplied {@link Executable}.
420+
*
421+
* @param e an {@link Executable}; must not be {@code null}
422+
*
423+
* @return an {@link ExecutableElement} corresponding to the supplied {@link Executable}; never {@code null}
424+
*
425+
* @exception NullPointerException if {@code e} is {@code null}
426+
*
427+
* @exception IllegalArgumentException if somehow {@code e} is neither a {@link Constructor} nor a {@link Method}
428+
*/
429+
public default ExecutableElement executableElement(final Executable e) {
430+
return switch (e) {
431+
case null -> throw new NullPointerException("e");
432+
case Constructor<?> c ->
433+
this.executableElement(this.typeElement(c.getDeclaringClass().getCanonicalName()),
434+
this.noType(TypeKind.VOID),
435+
"<init>",
436+
this.types(c.getParameterTypes()));
437+
case Method m ->
438+
this.executableElement(this.typeElement(m.getDeclaringClass().getCanonicalName()),
439+
this.type(m.getReturnType()),
440+
m.getName(),
441+
this.types(m.getParameterTypes()));
442+
default -> throw new IllegalArgumentException("e: " + e);
443+
};
444+
}
445+
446+
410447
/**
411448
* A convenience method that returns an {@link ExecutableElement} representing the static initializer, constructor or
412449
* method described by the supplied arguments, <strong>or {@code null} if no such {@link ExecutableElement}
@@ -763,6 +800,26 @@ public default TypeElement javaLangObject() {
763800
*/
764801
public PackageElement packageElement(final ModuleElement asSeenFrom, final CharSequence canonicalName);
765802

803+
/**
804+
* Returns a {@link Parameterizable} corresponding to the supplied (reflective) {@link GenericDeclaration}.
805+
*
806+
* @param gd a {@link GenericDeclaration}; must not be {@code null}
807+
*
808+
* @return a {@link Parameterizable} corresponding to the supplied {@link GenericDeclaration}; never {@code null}
809+
*
810+
* @exception NullPointerException if {@code gd} is {@code null}
811+
*
812+
* @exception IllegalArgumentException if {@code gd} is neither a {@link Class} nor an {@link Executable}
813+
*/
814+
public default Parameterizable parameterizable(final GenericDeclaration gd) {
815+
return switch (gd) {
816+
case null -> throw new NullPointerException("gd");
817+
case Class<?> c -> this.typeElement(c.getCanonicalName());
818+
case Executable e -> this.executableElement(e);
819+
default -> throw new IllegalArgumentException("gd: " + gd);
820+
};
821+
}
822+
766823
/**
767824
* A convenience method that returns {@code true} if and only if {@code t} is a {@link DeclaredType}, {@linkplain
768825
* TypeMirror#getKind() has a <code>TypeKind</code>} of {@link TypeKind#DECLARED DECLARED}, and {@linkplain
@@ -1082,6 +1139,81 @@ public default String toString(final CharSequence name) {
10821139
};
10831140
}
10841141

1142+
/**
1143+
* A convenience method that returns the {@link TypeMirror} corresponding to the supplied (reflective) {@link Type}.
1144+
*
1145+
* @param t a {@link Type}; must not be {@code null}
1146+
*
1147+
* @return the {@link TypeMirror} corresponding to the supplied {@link Type}; never {@code null}
1148+
*
1149+
* @exception NullPointerException if {@code t} is {@code null}
1150+
*
1151+
* @exception IllegalArgumentException if {@code t} is not a {@link Class}, {@link GenericArrayType}, {@link
1152+
* ParameterizedType}, {@link java.lang.reflect.TypeVariable} or {@link java.lang.reflect.WildcardType}
1153+
*/
1154+
public default TypeMirror type(final Type t) {
1155+
// TODO: anywhere there is domain.declaredType(), consider passing
1156+
// domain.moduleElement(this.getClass().getModule().getName()) as the first argument. Not sure how this works
1157+
// exactly but I think it might be necessary.
1158+
return switch (t) {
1159+
case null -> throw new NullPointerException("t");
1160+
case Class<?> c when t == boolean.class -> this.primitiveType(TypeKind.BOOLEAN);
1161+
case Class<?> c when t == byte.class -> this.primitiveType(TypeKind.BYTE);
1162+
case Class<?> c when t == char.class -> this.primitiveType(TypeKind.CHAR);
1163+
case Class<?> c when t == double.class -> this.primitiveType(TypeKind.DOUBLE);
1164+
case Class<?> c when t == float.class -> this.primitiveType(TypeKind.FLOAT);
1165+
case Class<?> c when t == int.class -> this.primitiveType(TypeKind.INT);
1166+
case Class<?> c when t == long.class -> this.primitiveType(TypeKind.LONG);
1167+
case Class<?> c when t == short.class -> this.primitiveType(TypeKind.SHORT);
1168+
case Class<?> c when t == void.class -> this.noType(TypeKind.VOID);
1169+
case Class<?> c when t == Object.class -> this.javaLangObject().asType(); // cheap and easy optimization
1170+
case Class<?> c when c.isArray() -> this.arrayTypeOf(this.type(c.getComponentType()));
1171+
case Class<?> c -> this.declaredType(c.getCanonicalName());
1172+
case GenericArrayType g -> this.arrayTypeOf(this.type(g.getGenericComponentType()));
1173+
case ParameterizedType pt when pt.getOwnerType() == null ->
1174+
this.declaredType(this.typeElement(((Class<?>)pt.getRawType()).getCanonicalName()),
1175+
this.types(pt.getActualTypeArguments()));
1176+
case ParameterizedType pt ->
1177+
this.declaredType((DeclaredType)this.type(pt.getOwnerType()),
1178+
this.typeElement(((Class<?>)pt.getRawType()).getCanonicalName()),
1179+
this.types(pt.getActualTypeArguments()));
1180+
case java.lang.reflect.TypeVariable<?> tv -> this.typeVariable(this.parameterizable(tv.getGenericDeclaration()), tv.getName());
1181+
case java.lang.reflect.WildcardType w when w.getLowerBounds().length <= 0 -> this.wildcardType(this.type(w.getUpperBounds()[0]), null);
1182+
case java.lang.reflect.WildcardType w -> this.wildcardType(null, this.type(w.getLowerBounds()[0]));
1183+
default -> throw new IllegalArgumentException("t: " + t);
1184+
};
1185+
}
1186+
1187+
/**
1188+
* A convenience method that returns an array of {@link TypeMirror}s whose elements correspond, in order, to the
1189+
* elements in the supplied {@link Type} array.
1190+
*
1191+
* @param ts an array of {@link Type}s; must not be {@code null}
1192+
*
1193+
* @return an array of {@link TypeMirror}s whose elements correspond, in order, to the elements in the supplied {@link
1194+
* Type} array; never {@code null}
1195+
*
1196+
* @exception NullPointerException if {@code ts} is {@code null} or contains {@code null} elements
1197+
*
1198+
* @exception IllegalArgumentException if any element of {@code ts} is deemed illegal by the {@link #type(Type)}
1199+
* method
1200+
*
1201+
* @see #type(Type)
1202+
*/
1203+
public default TypeMirror[] types(final Type[] ts) {
1204+
return switch (ts.length) {
1205+
case 0 -> new TypeMirror[0];
1206+
case 1 -> new TypeMirror[] { this.type(ts[0]) };
1207+
default -> {
1208+
final TypeMirror[] rv = new TypeMirror[ts.length];
1209+
for (int i = 0; i < ts.length; i++) {
1210+
rv[i] = this.type(ts[i]);
1211+
}
1212+
yield rv;
1213+
}
1214+
};
1215+
}
1216+
10851217
/**
10861218
* Returns a {@link TypeElement} representing the element bearing the supplied <dfn>canonical name</dfn>, <strong>or
10871219
* {@code null} if there is no such {@link TypeElement}</strong>.

src/site/site.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<skin>
1414
<groupId>org.apache.maven.skins</groupId>
1515
<artifactId>maven-fluido-skin</artifactId>
16-
<version>2.0.0</version>
16+
<version>2.1.0</version>
1717
</skin>
1818
<body>
1919
<menu name="Documentation">

0 commit comments

Comments
 (0)