Skip to content

Commit 763ef13

Browse files
snuyanzindblock
andauthored
Use getParameterCount instead of getParameterTypes (#4821)
* Use getParameterCount instead of getParameterTypes Signed-off-by: Sergey Nuyanzin <snuyanzin@gmail.com> * Update changelog Signed-off-by: Sergey Nuyanzin <snuyanzin@gmail.com> * Apply spotless Signed-off-by: Sergey Nuyanzin <snuyanzin@gmail.com> * Address feedback Signed-off-by: Sergey Nuyanzin <snuyanzin@gmail.com> * Address feedback Signed-off-by: Sergey Nuyanzin <snuyanzin@gmail.com> Signed-off-by: Sergey Nuyanzin <snuyanzin@gmail.com> Co-authored-by: Daniel (dB.) Doubrovkine <dblock@amazon.com>
1 parent db418fb commit 763ef13

File tree

8 files changed

+70
-66
lines changed

8 files changed

+70
-66
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
4848
- Added resource usage trackers for in-flight cancellation of SearchShardTask ([#4805](https://github.com/opensearch-project/OpenSearch/pull/4805))
4949
- Renamed flaky tests ([#4912](https://github.com/opensearch-project/OpenSearch/pull/4912))
5050
- Update previous release bwc version to 2.5.0 ([#5003](https://github.com/opensearch-project/OpenSearch/pull/5003))
51+
- Use getParameterCount instead of getParameterTypes ([#4821](https://github.com/opensearch-project/OpenSearch/pull/4821))
5152

5253
### Dependencies
5354
- Bumps `log4j-core` from 2.18.0 to 2.19.0

buildSrc/src/testFixtures/java/org/opensearch/gradle/test/JUnit3MethodProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public Collection<Method> getTestMethods(Class<?> suiteClass, ClassModel classMo
5959
if (m.getName().startsWith("test")
6060
&& Modifier.isPublic(m.getModifiers())
6161
&& !Modifier.isStatic(m.getModifiers())
62-
&& m.getParameterTypes().length == 0) {
62+
&& m.getParameterCount() == 0) {
6363
result.add(m);
6464
}
6565
}

client/rest-high-level/src/test/java/org/opensearch/client/RestHighLevelClientTests.java

+24-25
Original file line numberDiff line numberDiff line change
@@ -1005,37 +1005,34 @@ private static void assertSyncMethod(Method method, String apiName, List<String>
10051005
}
10061006

10071007
assertEquals("incorrect number of exceptions for method [" + method + "]", 1, method.getExceptionTypes().length);
1008+
final Class<?>[] parameterTypes = method.getParameterTypes();
10081009
// a few methods don't accept a request object as argument
10091010
if (APIS_WITHOUT_REQUEST_OBJECT.contains(apiName)) {
1010-
assertEquals("incorrect number of arguments for method [" + method + "]", 1, method.getParameterTypes().length);
1011-
assertThat(
1012-
"the parameter to method [" + method + "] is the wrong type",
1013-
method.getParameterTypes()[0],
1014-
equalTo(RequestOptions.class)
1015-
);
1011+
assertEquals("incorrect number of arguments for method [" + method + "]", 1, method.getParameterCount());
1012+
assertThat("the parameter to method [" + method + "] is the wrong type", parameterTypes[0], equalTo(RequestOptions.class));
10161013
} else {
1017-
assertEquals("incorrect number of arguments for method [" + method + "]", 2, method.getParameterTypes().length);
1014+
assertEquals("incorrect number of arguments for method [" + method + "]", 2, method.getParameterCount());
10181015
// This is no longer true for all methods. Some methods can contain these 2 args backwards because of deprecation
1019-
if (method.getParameterTypes()[0].equals(RequestOptions.class)) {
1016+
if (parameterTypes[0].equals(RequestOptions.class)) {
10201017
assertThat(
10211018
"the first parameter to method [" + method + "] is the wrong type",
1022-
method.getParameterTypes()[0],
1019+
parameterTypes[0],
10231020
equalTo(RequestOptions.class)
10241021
);
10251022
assertThat(
10261023
"the second parameter to method [" + method + "] is the wrong type",
1027-
method.getParameterTypes()[1].getSimpleName(),
1024+
parameterTypes[1].getSimpleName(),
10281025
endsWith("Request")
10291026
);
10301027
} else {
10311028
assertThat(
10321029
"the first parameter to method [" + method + "] is the wrong type",
1033-
method.getParameterTypes()[0].getSimpleName(),
1030+
parameterTypes[0].getSimpleName(),
10341031
endsWith("Request")
10351032
);
10361033
assertThat(
10371034
"the second parameter to method [" + method + "] is the wrong type",
1038-
method.getParameterTypes()[1],
1035+
parameterTypes[1],
10391036
equalTo(RequestOptions.class)
10401037
);
10411038
}
@@ -1049,39 +1046,40 @@ private static void assertAsyncMethod(Map<String, Set<Method>> methods, Method m
10491046
);
10501047
assertThat("async method [" + method + "] should return Cancellable", method.getReturnType(), equalTo(Cancellable.class));
10511048
assertEquals("async method [" + method + "] should not throw any exceptions", 0, method.getExceptionTypes().length);
1049+
final Class<?>[] parameterTypes = method.getParameterTypes();
10521050
if (APIS_WITHOUT_REQUEST_OBJECT.contains(apiName.replaceAll("_async$", ""))) {
1053-
assertEquals(2, method.getParameterTypes().length);
1054-
assertThat(method.getParameterTypes()[0], equalTo(RequestOptions.class));
1055-
assertThat(method.getParameterTypes()[1], equalTo(ActionListener.class));
1051+
assertEquals(2, parameterTypes.length);
1052+
assertThat(parameterTypes[0], equalTo(RequestOptions.class));
1053+
assertThat(parameterTypes[1], equalTo(ActionListener.class));
10561054
} else {
1057-
assertEquals("async method [" + method + "] has the wrong number of arguments", 3, method.getParameterTypes().length);
1055+
assertEquals("async method [" + method + "] has the wrong number of arguments", 3, method.getParameterCount());
10581056
// This is no longer true for all methods. Some methods can contain these 2 args backwards because of deprecation
1059-
if (method.getParameterTypes()[0].equals(RequestOptions.class)) {
1057+
if (parameterTypes[0].equals(RequestOptions.class)) {
10601058
assertThat(
10611059
"the first parameter to async method [" + method + "] should be a request type",
1062-
method.getParameterTypes()[0],
1060+
parameterTypes[0],
10631061
equalTo(RequestOptions.class)
10641062
);
10651063
assertThat(
10661064
"the second parameter to async method [" + method + "] is the wrong type",
1067-
method.getParameterTypes()[1].getSimpleName(),
1065+
parameterTypes[1].getSimpleName(),
10681066
endsWith("Request")
10691067
);
10701068
} else {
10711069
assertThat(
10721070
"the first parameter to async method [" + method + "] should be a request type",
1073-
method.getParameterTypes()[0].getSimpleName(),
1071+
parameterTypes[0].getSimpleName(),
10741072
endsWith("Request")
10751073
);
10761074
assertThat(
10771075
"the second parameter to async method [" + method + "] is the wrong type",
1078-
method.getParameterTypes()[1],
1076+
parameterTypes[1],
10791077
equalTo(RequestOptions.class)
10801078
);
10811079
}
10821080
assertThat(
10831081
"the third parameter to async method [" + method + "] is the wrong type",
1084-
method.getParameterTypes()[2],
1082+
parameterTypes[2],
10851083
equalTo(ActionListener.class)
10861084
);
10871085
}
@@ -1094,16 +1092,17 @@ private static void assertSubmitTaskMethod(
10941092
ClientYamlSuiteRestSpec restSpec
10951093
) {
10961094
String methodName = extractMethodName(apiName);
1095+
final Class<?>[] parameterTypes = method.getParameterTypes();
10971096
assertTrue("submit task method [" + method.getName() + "] doesn't have corresponding sync method", methods.containsKey(methodName));
1098-
assertEquals("submit task method [" + method + "] has the wrong number of arguments", 2, method.getParameterTypes().length);
1097+
assertEquals("submit task method [" + method + "] has the wrong number of arguments", 2, method.getParameterCount());
10991098
assertThat(
11001099
"the first parameter to submit task method [" + method + "] is the wrong type",
1101-
method.getParameterTypes()[0].getSimpleName(),
1100+
parameterTypes[0].getSimpleName(),
11021101
endsWith("Request")
11031102
);
11041103
assertThat(
11051104
"the second parameter to submit task method [" + method + "] is the wrong type",
1106-
method.getParameterTypes()[1],
1105+
parameterTypes[1],
11071106
equalTo(RequestOptions.class)
11081107
);
11091108

modules/lang-painless/src/main/java/org/opensearch/painless/Compiler.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ private static void addFactoryMethod(Map<String, Class<?>> additionalClasses, Cl
212212
}
213213

214214
additionalClasses.put(factoryClass.getName(), factoryClass);
215-
for (int i = 0; i < factoryMethod.getParameterTypes().length; ++i) {
216-
Class<?> parameterClazz = factoryMethod.getParameterTypes()[i];
215+
final Class<?>[] parameterTypes = factoryMethod.getParameterTypes();
216+
for (int i = 0; i < parameterTypes.length; ++i) {
217+
Class<?> parameterClazz = parameterTypes[i];
217218
additionalClasses.put(parameterClazz.getName(), parameterClazz);
218219
}
219220
}

modules/lang-painless/src/main/java/org/opensearch/painless/PainlessScriptEngine.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,12 @@ private <T> Type generateStatefulFactory(Loader loader, ScriptContext<T> context
195195
}
196196
}
197197

198-
for (int count = 0; count < newFactory.getParameterTypes().length; ++count) {
198+
final Class<?>[] parameterTypes = newFactory.getParameterTypes();
199+
for (int count = 0; count < parameterTypes.length; ++count) {
199200
writer.visitField(
200201
Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL,
201202
"$arg" + count,
202-
Type.getType(newFactory.getParameterTypes()[count]).getDescriptor(),
203+
Type.getType(parameterTypes[count]).getDescriptor(),
203204
null,
204205
null
205206
).visitEnd();
@@ -211,7 +212,7 @@ private <T> Type generateStatefulFactory(Loader loader, ScriptContext<T> context
211212
);
212213
org.objectweb.asm.commons.Method init = new org.objectweb.asm.commons.Method(
213214
"<init>",
214-
MethodType.methodType(void.class, newFactory.getParameterTypes()).toMethodDescriptorString()
215+
MethodType.methodType(void.class, parameterTypes).toMethodDescriptorString()
215216
);
216217

217218
GeneratorAdapter constructor = new GeneratorAdapter(
@@ -223,10 +224,10 @@ private <T> Type generateStatefulFactory(Loader loader, ScriptContext<T> context
223224
constructor.loadThis();
224225
constructor.invokeConstructor(OBJECT_TYPE, base);
225226

226-
for (int count = 0; count < newFactory.getParameterTypes().length; ++count) {
227+
for (int count = 0; count < parameterTypes.length; ++count) {
227228
constructor.loadThis();
228229
constructor.loadArg(count);
229-
constructor.putField(Type.getType("L" + className + ";"), "$arg" + count, Type.getType(newFactory.getParameterTypes()[count]));
230+
constructor.putField(Type.getType("L" + className + ";"), "$arg" + count, Type.getType(parameterTypes[count]));
230231
}
231232

232233
constructor.returnValue();
@@ -247,7 +248,7 @@ private <T> Type generateStatefulFactory(Loader loader, ScriptContext<T> context
247248
MethodType.methodType(newInstance.getReturnType(), newInstance.getParameterTypes()).toMethodDescriptorString()
248249
);
249250

250-
List<Class<?>> parameters = new ArrayList<>(Arrays.asList(newFactory.getParameterTypes()));
251+
List<Class<?>> parameters = new ArrayList<>(Arrays.asList(parameterTypes));
251252
parameters.addAll(Arrays.asList(newInstance.getParameterTypes()));
252253

253254
org.objectweb.asm.commons.Method constru = new org.objectweb.asm.commons.Method(
@@ -264,9 +265,9 @@ private <T> Type generateStatefulFactory(Loader loader, ScriptContext<T> context
264265
adapter.newInstance(WriterConstants.CLASS_TYPE);
265266
adapter.dup();
266267

267-
for (int count = 0; count < newFactory.getParameterTypes().length; ++count) {
268+
for (int count = 0; count < parameterTypes.length; ++count) {
268269
adapter.loadThis();
269-
adapter.getField(Type.getType("L" + className + ";"), "$arg" + count, Type.getType(newFactory.getParameterTypes()[count]));
270+
adapter.getField(Type.getType("L" + className + ";"), "$arg" + count, Type.getType(parameterTypes[count]));
270271
}
271272

272273
adapter.loadArgs();
@@ -334,13 +335,14 @@ private <T> T generateFactory(Loader loader, ScriptContext<T> context, Type clas
334335
}
335336
}
336337

338+
final Class<?>[] parameterTypes = reflect.getParameterTypes();
337339
org.objectweb.asm.commons.Method instance = new org.objectweb.asm.commons.Method(
338340
reflect.getName(),
339-
MethodType.methodType(reflect.getReturnType(), reflect.getParameterTypes()).toMethodDescriptorString()
341+
MethodType.methodType(reflect.getReturnType(), parameterTypes).toMethodDescriptorString()
340342
);
341343
org.objectweb.asm.commons.Method constru = new org.objectweb.asm.commons.Method(
342344
"<init>",
343-
MethodType.methodType(void.class, reflect.getParameterTypes()).toMethodDescriptorString()
345+
MethodType.methodType(void.class, parameterTypes).toMethodDescriptorString()
344346
);
345347

346348
GeneratorAdapter adapter = new GeneratorAdapter(
@@ -421,9 +423,7 @@ private <T> T generateFactory(Loader loader, ScriptContext<T> context, Type clas
421423

422424
private void writeNeedsMethods(Class<?> clazz, ClassWriter writer, Set<String> extractedVariables) {
423425
for (Method method : clazz.getMethods()) {
424-
if (method.getName().startsWith("needs")
425-
&& method.getReturnType().equals(boolean.class)
426-
&& method.getParameterTypes().length == 0) {
426+
if (method.getName().startsWith("needs") && method.getReturnType().equals(boolean.class) && method.getParameterCount() == 0) {
427427
String name = method.getName();
428428
name = name.substring(5);
429429
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);

modules/lang-painless/src/main/java/org/opensearch/painless/ScriptClassInfo.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public ScriptClassInfo(PainlessLookup painlessLookup, Class<?> baseClass) {
8888
+ "] has more than one."
8989
);
9090
}
91-
} else if (m.getName().startsWith("needs") && m.getReturnType() == boolean.class && m.getParameterTypes().length == 0) {
91+
} else if (m.getName().startsWith("needs") && m.getReturnType() == boolean.class && m.getParameterCount() == 0) {
9292
needsMethods.add(new org.objectweb.asm.commons.Method(m.getName(), NEEDS_PARAMETER_METHOD_TYPE.toMethodDescriptorString()));
9393
} else if (m.getName().startsWith("get")
9494
&& m.getName().equals("getClass") == false
@@ -124,7 +124,7 @@ public ScriptClassInfo(PainlessLookup painlessLookup, Class<?> baseClass) {
124124
FunctionTable.LocalFunction defConverter = null;
125125
for (java.lang.reflect.Method m : baseClass.getMethods()) {
126126
if (m.getName().startsWith("convertFrom")
127-
&& m.getParameterTypes().length == 1
127+
&& m.getParameterCount() == 1
128128
&& m.getReturnType() == returnType
129129
&& Modifier.isStatic(m.getModifiers())) {
130130

modules/lang-painless/src/main/java/org/opensearch/painless/lookup/PainlessLookupBuilder.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -2168,9 +2168,10 @@ private void generateBridgeMethod(PainlessClassBuilder painlessClassBuilder, Pai
21682168
bridgeMethodWriter.loadArg(0);
21692169
}
21702170

2171-
for (int typeParameterCount = 0; typeParameterCount < javaMethod.getParameterTypes().length; ++typeParameterCount) {
2171+
final Class<?>[] typeParameters = javaMethod.getParameterTypes();
2172+
for (int typeParameterCount = 0; typeParameterCount < typeParameters.length; ++typeParameterCount) {
21722173
bridgeMethodWriter.loadArg(typeParameterCount + bridgeTypeParameterOffset);
2173-
Class<?> typeParameter = javaMethod.getParameterTypes()[typeParameterCount];
2174+
Class<?> typeParameter = typeParameters[typeParameterCount];
21742175

21752176
if (typeParameter == Byte.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BYTE_IMPLICIT);
21762177
else if (typeParameter == Short.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_SHORT_IMPLICIT);

0 commit comments

Comments
 (0)