Skip to content

Commit b751304

Browse files
committed
#128 - Track interface implementors
1 parent 17402b1 commit b751304

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

hibernate-models/src/main/java/org/hibernate/models/internal/AbstractClassDetailsRegistry.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public Set<ClassDetails> getDirectSubtypes(String typeName) {
7474
}
7575

7676
@Override
77-
public void forEachDirectSubType(String superTypeName, ClassDetailsConsumer consumer) {
78-
final List<ClassDetails> directSubTypes = getDirectSubTypes( superTypeName );
77+
public void forEachDirectSubtype(String typeName, ClassDetailsConsumer consumer) {
78+
final List<ClassDetails> directSubTypes = getDirectSubTypes( typeName );
7979
if ( directSubTypes == null ) {
8080
return;
8181
}
@@ -128,7 +128,7 @@ public void walkImplementors(String base, boolean includeBase, ClassDetailsConsu
128128
consumer.consume( baseDetails );
129129
}
130130

131-
forEachDirectSubType( base, (subType) -> {
131+
forEachDirectSubtype( base, (subType) -> {
132132
consumer.consume( subType );
133133
walkSubtypes( subType, consumer );
134134
} );
@@ -140,7 +140,7 @@ public void walkImplementors(String base, boolean includeBase, ClassDetailsConsu
140140
}
141141

142142
private void walkSubtypes(ClassDetails base, ClassDetailsConsumer consumer) {
143-
forEachDirectSubType( base.getName(), (subType) -> {
143+
forEachDirectSubtype( base.getName(), (subType) -> {
144144
consumer.consume( subType );
145145
walkSubtypes( subType, consumer );
146146
} );
@@ -156,7 +156,7 @@ private void walkInterfaceImplementors(ClassDetails implementor, ClassDetailsCon
156156
}
157157
else {
158158
// the direct interface implementor is itself a class...
159-
forEachDirectSubType( implementor.getName(), (subtype) -> {
159+
forEachDirectSubtype( implementor.getName(), (subtype) -> {
160160
consumer.consume( subtype );
161161
walkSubtypes( subtype, consumer );
162162
} );

hibernate-models/src/main/java/org/hibernate/models/spi/ClassDetailsRegistry.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,18 @@ default ClassDetails getClassDetails(String name) {
7070

7171
/**
7272
* Visit each direct subtype of the named managed-class
73+
*
74+
* @deprecated Use {@linkplain #forEachDirectSubtype} instead.
7375
*/
74-
@SuppressWarnings("unused")
75-
void forEachDirectSubType(String superTypeName, ClassDetailsConsumer consumer);
76+
@Deprecated
77+
default void forEachDirectSubType(String typeName, ClassDetailsConsumer consumer) {
78+
forEachDirectSubtype( typeName, consumer );
79+
}
80+
81+
/**
82+
* Visit each direct subtype of the named managed-class
83+
*/
84+
void forEachDirectSubtype(String typeName, ClassDetailsConsumer consumer);
7685

7786
/**
7887
* Get the list of all direct implementors for the named interface.
@@ -89,7 +98,6 @@ default ClassDetails getClassDetails(String name) {
8998
* @apiNote Does not verify that {@code interfaceName} actually names an
9099
* interface. If it does not, no callbacks will happen.
91100
*/
92-
@SuppressWarnings("unused")
93101
void forEachDirectImplementor(String interfaceName, ClassDetailsConsumer consumer);
94102

95103
/**
@@ -156,9 +164,12 @@ default Set<ClassDetails> collectImplementors(String base, boolean includeBase)
156164
*/
157165
ClassDetailsBuilder getClassDetailsBuilder();
158166

167+
@SuppressWarnings("unchecked")
159168
default <S> S as(Class<S> type) {
160-
//noinspection unchecked
161-
return (S) this;
169+
if ( type.isInstance( this ) ) {
170+
return (S) this;
171+
}
172+
throw new UnsupportedOperationException( "Unsure how to cast " + this + " to " + type.getName() );
162173
}
163174

164175
@FunctionalInterface

hibernate-models/src/test/java/org/hibernate/models/testing/tests/classes/InheritanceTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,19 @@ void testForEachDirectSubType() {
148148
final ClassDetailsRegistry classDetailsRegistry = modelsContext.getClassDetailsRegistry();
149149

150150
final List<ClassDetails> subTypes = new ArrayList<>();
151-
classDetailsRegistry.forEachDirectSubType( RootClass.class.getName(), subTypes::add );
151+
classDetailsRegistry.forEachDirectSubtype( RootClass.class.getName(), subTypes::add );
152152
assertThat( subTypes ).hasSize( 1 );
153153
subTypes.clear();
154154

155-
classDetailsRegistry.forEachDirectSubType( TrunkClass.class.getName(), subTypes::add );
155+
classDetailsRegistry.forEachDirectSubtype( TrunkClass.class.getName(), subTypes::add );
156156
assertThat( subTypes ).hasSize( 1 );
157157
subTypes.clear();
158158

159-
classDetailsRegistry.forEachDirectSubType( BranchClass.class.getName(), subTypes::add );
159+
classDetailsRegistry.forEachDirectSubtype( BranchClass.class.getName(), subTypes::add );
160160
assertThat( subTypes ).hasSize( 1 );
161161
subTypes.clear();
162162

163-
classDetailsRegistry.forEachDirectSubType( LeafClass.class.getName(), subTypes::add );
163+
classDetailsRegistry.forEachDirectSubtype( LeafClass.class.getName(), subTypes::add );
164164
assertThat( subTypes ).hasSize( 0 );
165165
}
166166
}

0 commit comments

Comments
 (0)