Skip to content

Commit be308c3

Browse files
Improve unsupported API access error message
1 parent 48a0ef6 commit be308c3

2 files changed

Lines changed: 8 additions & 11 deletions

File tree

src/main/java/com/hivemq/extension/sdk/api/services/Services.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@
6262
*/
6363
public class Services {
6464

65-
private static final String NO_ACCESS_MESSAGE =
66-
"Static class Services cannot be called from a thread \"%s\" which" +
67-
" does not have a HiveMQ extension classloader as its context classloader.";
65+
private static final @NotNull String NO_ACCESS_MESSAGE = "Only HiveMQ Extensions can access Services.";
6866

6967
//this map is filled by HiveMQ with implementations for all services
7068
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
@@ -169,17 +167,18 @@ public class Services {
169167
private static <T> @NotNull T getClassObject(final @NotNull Class<T> clazz) {
170168

171169
if (services == null) {
172-
throw new RuntimeException(String.format(NO_ACCESS_MESSAGE, Thread.currentThread().getName()));
170+
throw new RuntimeException(NO_ACCESS_MESSAGE);
173171
}
174172

175173
final Object object = services.get(clazz.getCanonicalName());
176174
if (object == null) {
177175
//don't cache this exception to keep the stacktrace, this is not expected to happen very often
178-
throw new RuntimeException(String.format(NO_ACCESS_MESSAGE, Thread.currentThread().getName()));
176+
throw new RuntimeException(NO_ACCESS_MESSAGE);
179177
}
180178
if (clazz.isInstance(object)) {
179+
//noinspection unchecked
181180
return (T) object;
182181
}
183-
throw new RuntimeException(String.format(NO_ACCESS_MESSAGE, Thread.currentThread().getName()));
182+
throw new RuntimeException(NO_ACCESS_MESSAGE);
184183
}
185184
}

src/main/java/com/hivemq/extension/sdk/api/services/builder/Builders.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@
4444
*/
4545
public class Builders {
4646

47-
private static final String NO_ACCESS_MESSAGE =
48-
"Static class Builders cannot be called from a thread \"%s\" which" +
49-
" does not have a HiveMQ extension classloader as its context classloader.";
47+
private static final @NotNull String NO_ACCESS_MESSAGE = "Only HiveMQ Extensions can access Builders.";
5048

5149
//this map is filled by HiveMQ with implementations for all builders
5250
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
@@ -95,13 +93,13 @@ public class Builders {
9593
private static <T> @NotNull Supplier<T> getClassSupplier(final @NotNull Class<T> clazz) {
9694

9795
if (builders == null) {
98-
throw new RuntimeException(String.format(NO_ACCESS_MESSAGE, Thread.currentThread().getName()));
96+
throw new RuntimeException(NO_ACCESS_MESSAGE);
9997
}
10098

10199
final Supplier<Object> objectSupplier = builders.get(clazz.getCanonicalName());
102100
if (objectSupplier == null) {
103101
//don't cache this exception to keep the stacktrace, this is not expected to happen very often
104-
throw new RuntimeException(String.format(NO_ACCESS_MESSAGE, Thread.currentThread().getName()));
102+
throw new RuntimeException(NO_ACCESS_MESSAGE);
105103
}
106104
//noinspection unchecked: HiveMQ takes care of placing the right supplier in the map
107105
return (Supplier<T>) objectSupplier;

0 commit comments

Comments
 (0)