Skip to content

Commit 5c77085

Browse files
committed
Refactor creating MessageBuffer instance
1 parent ee8c341 commit 5c77085

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBuffer.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public static MessageBuffer allocate(int size)
198198
}
199199

200200
/**
201-
* Wraps a ByteBuffer into a MessageBuffer.
201+
* Wraps a byte array into a MessageBuffer.
202202
*
203203
* The new MessageBuffer will be backed by the given byte array. Modifications to the new MessageBuffer will cause the byte array to be modified and vice versa.
204204
*
@@ -214,7 +214,7 @@ public static MessageBuffer wrap(byte[] array)
214214
}
215215

216216
/**
217-
* Wraps a ByteBuffer into a MessageBuffer.
217+
* Wraps a byte array into a MessageBuffer.
218218
*
219219
* The new MessageBuffer will be backed by the given byte array. Modifications to the new MessageBuffer will cause the byte array to be modified and vice versa.
220220
*
@@ -264,28 +264,7 @@ public static MessageBuffer wrap(ByteBuffer bb)
264264
private static MessageBuffer newMessageBuffer(byte[] arr, int off, int len)
265265
{
266266
checkNotNull(arr);
267-
try {
268-
return (MessageBuffer) mbArrConstructor.newInstance(arr, off, len);
269-
}
270-
catch (InstantiationException e) {
271-
// should never happen
272-
throw new IllegalStateException(e);
273-
}
274-
catch (IllegalAccessException e) {
275-
// should never happen unless security manager restricts this reflection
276-
throw new IllegalStateException(e);
277-
}
278-
catch (InvocationTargetException e) {
279-
if (e.getCause() instanceof RuntimeException) {
280-
// underlaying constructor may throw RuntimeException
281-
throw (RuntimeException) e.getCause();
282-
}
283-
else if (e.getCause() instanceof Error) {
284-
throw (Error) e.getCause();
285-
}
286-
// should never happen
287-
throw new IllegalStateException(e.getCause());
288-
}
267+
return newInstance(mbArrConstructor, arr, off, len);
289268
}
290269

291270
/**
@@ -297,10 +276,21 @@ else if (e.getCause() instanceof Error) {
297276
private static MessageBuffer newMessageBuffer(ByteBuffer bb)
298277
{
299278
checkNotNull(bb);
279+
return newInstance(mbBBConstructor, bb);
280+
}
281+
282+
/**
283+
* Creates a new MessageBuffer instance
284+
*
285+
* @param constructor A MessageBuffer constructor
286+
* @return new MessageBuffer instance
287+
*/
288+
private static MessageBuffer newInstance(Constructor<?> constructor, Object... args)
289+
{
300290
try {
301291
// We need to use reflection to create MessageBuffer instances in order to prevent TypeProfile generation for getInt method. TypeProfile will be
302292
// generated to resolve one of the method references when two or more classes overrides the method.
303-
return (MessageBuffer) mbBBConstructor.newInstance(bb);
293+
return (MessageBuffer) constructor.newInstance(args);
304294
}
305295
catch (InstantiationException e) {
306296
// should never happen

0 commit comments

Comments
 (0)