Add public variant getDeclaredMethods method

Add a public variant of `getDeclaredMethods` that defensively copies the
cached methods array. This is often more faster and more convenient
for users than calling `doWithLocalMethods`. We still retain most of the
benefits of the cache, namely fewer security manager calls and not as
many `Method` instances being created.

Closes gh-22580
This commit is contained in:
Phillip Webb
2019-03-08 17:20:32 -08:00
committed by Juergen Hoeller
parent 5044ee8fe6
commit 8ef609a1b7
2 changed files with 25 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -360,6 +360,13 @@ public class ReflectionUtilsTests {
assertThat(totalMs, Matchers.lessThan(10L));
}
@Test
public void getDecalredMethodsReturnsCopy() {
Method[] m1 = ReflectionUtils.getDeclaredMethods(A.class);
Method[] m2 = ReflectionUtils.getDeclaredMethods(A.class);
assertThat(m1, not(sameInstance(m2)));
}
private static class ListSavingMethodCallback implements ReflectionUtils.MethodCallback {
private List<String> methodNames = new LinkedList<>();