From f4ec0863cdbf68a72b0870aa316fa32662bfed10 Mon Sep 17 00:00:00 2001 From: Laird Nelson Date: Wed, 16 Oct 2024 07:50:03 -0700 Subject: [PATCH] Adds contains() method to TypeAndElementSource (#22) Signed-off-by: Laird Nelson --- README.md | 2 +- lang/src/main/java/org/microbean/lang/Lang.java | 5 +++++ .../org/microbean/lang/TypeAndElementSource.java | 16 +++++++++++++++- .../test/java/org/microbean/lang/TestErase.java | 4 ++++ .../org/microbean/lang/TestSameTypeVisitor.java | 4 ++++ .../org/microbean/lang/TestSupertypeVisitor.java | 4 ++++ .../java/org/microbean/lang/TestTypeClosure.java | 8 ++++++++ 7 files changed, 41 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a30803cc..47b73e60 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Maven dependency: org.microbean microbean-lang - 0.0.20 + 0.0.21 ``` diff --git a/lang/src/main/java/org/microbean/lang/Lang.java b/lang/src/main/java/org/microbean/lang/Lang.java index 953e3628..164a89e1 100644 --- a/lang/src/main/java/org/microbean/lang/Lang.java +++ b/lang/src/main/java/org/microbean/lang/Lang.java @@ -2239,6 +2239,11 @@ public final TypeElement boxedClass(final PrimitiveType t) { return Lang.boxedClass(t); } + @Override + public final boolean contains(final TypeMirror t, final TypeMirror s) { + return Lang.contains(t, s); + } + @Override public final DeclaredType declaredType(final TypeElement typeElement, final TypeMirror... typeArguments) { return Lang.declaredType(typeElement, typeArguments); diff --git a/lang/src/main/java/org/microbean/lang/TypeAndElementSource.java b/lang/src/main/java/org/microbean/lang/TypeAndElementSource.java index 4ac5b863..0877acc7 100644 --- a/lang/src/main/java/org/microbean/lang/TypeAndElementSource.java +++ b/lang/src/main/java/org/microbean/lang/TypeAndElementSource.java @@ -51,7 +51,21 @@ public interface TypeAndElementSource { // Note the strange positioning of payload and receiver. public boolean assignable(final TypeMirror payload, final TypeMirror receiver); - public TypeElement boxedClass(final PrimitiveType t); + public default TypeElement boxedClass(final PrimitiveType t) { + return switch (t.getKind()) { // won't cause symbol completion + case BOOLEAN -> this.typeElement("java.lang.Boolean"); + case BYTE -> this.typeElement("java.lang.Byte"); + case CHAR -> this.typeElement("java.lang.Character"); + case DOUBLE -> this.typeElement("java.lang.Double"); + case FLOAT -> this.typeElement("java.lang.Float"); + case INT -> this.typeElement("java.lang.Integer"); + case LONG -> this.typeElement("java.lang.Long"); + case SHORT -> this.typeElement("java.lang.Short"); + default -> throw new IllegalArgumentException("t: " + t); + }; + } + + public boolean contains(final TypeMirror t, final TypeMirror s); public DeclaredType declaredType(final DeclaredType enclosingType, final TypeElement typeElement, diff --git a/lang/src/test/java/org/microbean/lang/TestErase.java b/lang/src/test/java/org/microbean/lang/TestErase.java index 8eeea1ae..d44a561a 100644 --- a/lang/src/test/java/org/microbean/lang/TestErase.java +++ b/lang/src/test/java/org/microbean/lang/TestErase.java @@ -115,6 +115,10 @@ public final TypeElement boxedClass(final PrimitiveType t) { return javacModelTypes.boxedClass(t); } @Override + public final boolean contains(final TypeMirror t, final TypeMirror s) { + return javacModelTypes.contains(t, s); + } + @Override public final DeclaredType declaredType(final TypeElement typeElement, final TypeMirror... arguments) { return javacModelTypes.getDeclaredType(typeElement, arguments); } diff --git a/lang/src/test/java/org/microbean/lang/TestSameTypeVisitor.java b/lang/src/test/java/org/microbean/lang/TestSameTypeVisitor.java index 690b6dfe..7f71b8a7 100644 --- a/lang/src/test/java/org/microbean/lang/TestSameTypeVisitor.java +++ b/lang/src/test/java/org/microbean/lang/TestSameTypeVisitor.java @@ -81,6 +81,10 @@ public final TypeElement boxedClass(final PrimitiveType t) { return javacModelTypes.boxedClass(t); } @Override + public final boolean contains(final TypeMirror t, final TypeMirror s) { + return javacModelTypes.contains(t, s); + } + @Override public final DeclaredType declaredType(final TypeElement typeElement, final TypeMirror... arguments) { return javacModelTypes.getDeclaredType(typeElement, arguments); } diff --git a/lang/src/test/java/org/microbean/lang/TestSupertypeVisitor.java b/lang/src/test/java/org/microbean/lang/TestSupertypeVisitor.java index fbbcda85..b3ae323f 100644 --- a/lang/src/test/java/org/microbean/lang/TestSupertypeVisitor.java +++ b/lang/src/test/java/org/microbean/lang/TestSupertypeVisitor.java @@ -101,6 +101,10 @@ public final TypeElement boxedClass(final PrimitiveType t) { return javacModelTypes.boxedClass(t); } @Override + public final boolean contains(final TypeMirror t, final TypeMirror s) { + return javacModelTypes.contains(t, s); + } + @Override public final DeclaredType declaredType(final TypeElement typeElement, final TypeMirror... arguments) { return javacModelTypes.getDeclaredType(typeElement, arguments); } diff --git a/lang/src/test/java/org/microbean/lang/TestTypeClosure.java b/lang/src/test/java/org/microbean/lang/TestTypeClosure.java index ee9e09cd..f0fa3e6b 100644 --- a/lang/src/test/java/org/microbean/lang/TestTypeClosure.java +++ b/lang/src/test/java/org/microbean/lang/TestTypeClosure.java @@ -96,6 +96,10 @@ public final TypeElement boxedClass(final PrimitiveType t) { return javacModelTypes.boxedClass(t); } @Override + public final boolean contains(final TypeMirror t, final TypeMirror s) { + return javacModelTypes.contains(t, s); + } + @Override public final DeclaredType declaredType(final TypeElement typeElement, final TypeMirror... arguments) { return javacModelTypes.getDeclaredType(typeElement, arguments); } @@ -210,6 +214,10 @@ public final TypeElement boxedClass(final PrimitiveType t) { return javacModelTypes.boxedClass(t); } @Override + public final boolean contains(final TypeMirror t, final TypeMirror s) { + return javacModelTypes.contains(t, s); + } + @Override public final DeclaredType declaredType(final TypeElement typeElement, final TypeMirror... arguments) { return javacModelTypes.getDeclaredType(typeElement, arguments); }