Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid duplicate calls to getParameterTypes and getGenericParameterTypes #21794

Merged
merged 1 commit into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public ReflectiveMethodBuildItem(MethodInfo methodInfo) {
}

public ReflectiveMethodBuildItem(Method method) {
String[] params = new String[method.getParameterCount()];
for (int i = 0; i < params.length; ++i) {
params[i] = method.getParameterTypes()[i].getName();
this.params = new String[method.getParameterCount()];
if (method.getParameterCount() > 0) {
Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < params.length; ++i) {
params[i] = parameterTypes[i].getName();
}
}
this.params = params;
this.name = method.getName();
this.declaringClass = method.getDeclaringClass().getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ private void doDefineClass() {
ctor.returnValue(null);
}

Class<?>[] parameterTypes = injectConstructor.getParameterTypes();
List<Class<?>> args = new ArrayList<>();
args.add(InvocationHandler.class);
args.addAll(Arrays.asList(injectConstructor.getParameterTypes()));
args.addAll(Arrays.asList(parameterTypes));
try (MethodCreator ctor = cc
.getMethodCreator(MethodDescriptor.ofConstructor(proxyName, args.toArray(Class[]::new)))) {
List<ResultHandle> params = new ArrayList<>();
Expand All @@ -201,7 +202,7 @@ private void doDefineClass() {
}
ctor.invokeSpecialMethod(
MethodDescriptor.ofConstructor(injectConstructor.getDeclaringClass(),
injectConstructor.getParameterTypes()),
parameterTypes),
ctor.getThis(),
params.toArray(ResultHandle[]::new));
ctor.writeInstanceField(invocationHandlerField, ctor.getThis(), ctor.getMethodParam(0));
Expand Down Expand Up @@ -268,8 +269,9 @@ public T newInstance(InvocationHandler handler) throws IllegalAccessException, I
defineClass();
Object[] args = new Object[constructor.getParameterCount()];
args[0] = handler;
Class<?>[] parameterTypes = this.constructor.getParameterTypes();
for (int i = 1; i < constructor.getParameterCount(); ++i) {
Constructor<?> ctor = this.constructor.getParameterTypes()[i].getConstructor();
Constructor<?> ctor = parameterTypes[i].getConstructor();
ctor.setAccessible(true);
args[i] = ctor.newInstance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static io.quarkus.gizmo.MethodDescriptor.ofMethod;

import java.io.Closeable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -497,9 +498,11 @@ ResultHandle createValue(MethodContext context, MethodCreator method, ResultHand
//for every parameter that was passed into the method we create a deferred value
//this will allocate a space in the array, so the value can be deserialized correctly
//even if the code for an invocation is split over several methods
Class<?>[] parameterTypes = call.method.getParameterTypes();
Annotation[][] parameterAnnotations = call.method.getParameterAnnotations();
for (int i = 0; i < call.parameters.length; ++i) {
call.deferredParameters[i] = loadObjectInstance(call.parameters[i], parameterMap,
call.method.getParameterTypes()[i], Arrays.stream(call.method.getParameterAnnotations()[i])
parameterTypes[i], Arrays.stream(parameterAnnotations[i])
.anyMatch(s -> s.annotationType() == RelaxedValidation.class));
}
} catch (Exception e) {
Expand Down Expand Up @@ -1148,10 +1151,11 @@ public void prepare(MethodContext context) {
}
int count = 0;
nonDefaultConstructorHandles = new DeferredParameter[params.size()];
Class<?>[] parameterTypes = nonDefaultConstructorHolder.constructor.getParameterTypes();
for (int i = 0; i < params.size(); i++) {
Object obj = params.get(i);
nonDefaultConstructorHandles[i] = loadObjectInstance(obj, existing,
nonDefaultConstructorHolder.constructor.getParameterTypes()[count++], relaxedValidation);
parameterTypes[count++], relaxedValidation);
}
} else if (classesToUseRecorableConstructor.contains(param.getClass())) {
Constructor<?> current = null;
Expand Down Expand Up @@ -1181,9 +1185,13 @@ public void prepare(MethodContext context) {
if (ctor.isAnnotationPresent(RecordableConstructor.class)) {
nonDefaultConstructorHolder = new NonDefaultConstructorHolder(ctor, null);
nonDefaultConstructorHandles = new DeferredParameter[ctor.getParameterCount()];
for (int i = 0; i < ctor.getParameterCount(); ++i) {
String name = ctor.getParameters()[i].getName();
constructorParamNameMap.put(name, i);

if (ctor.getParameterCount() > 0) {
Parameter[] ctorParameters = ctor.getParameters();
for (int i = 0; i < ctor.getParameterCount(); ++i) {
String name = ctorParameters[i].getName();
constructorParamNameMap.put(name, i);
}
}
break;
}
Expand Down Expand Up @@ -1323,10 +1331,12 @@ public void prepare(MethodContext context) {

for (Method m : param.getClass().getMethods()) {
if (m.getName().equals(i.getWriteMethod().getName())) {
if (m.getParameterCount() > 0
&& m.getParameterTypes()[0].isAssignableFrom(param.getClass())) {
propertyType = m.getParameterTypes()[0];
break;
if (m.getParameterCount() > 0) {
Class<?>[] parameterTypes = m.getParameterTypes();
if (parameterTypes[0].isAssignableFrom(param.getClass())) {
propertyType = parameterTypes[0];
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,17 @@ void processProvidedLambda(Optional<ProvidedAmazonLambdaHandlerBuildItem> provid
// for reflection. Shouldn't have to do this.
for (Method method : handlerClass.getMethods()) {
if (method.getName().equals("handleRequest")
&& method.getParameterCount() == 2
&& !method.getParameterTypes()[0].equals(Object.class)) {
reflectiveClassBuildItemBuildProducer
.produce(new ReflectiveClassBuildItem(true, true, true, method.getParameterTypes()[0].getName()));
reflectiveClassBuildItemBuildProducer
.produce(new ReflectiveClassBuildItem(true, true, true, method.getReturnType().getName()));
reflectiveClassBuildItemBuildProducer.produce(new ReflectiveClassBuildItem(true, true, true,
DateTime.class));
break;
&& method.getParameterCount() == 2) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (!parameterTypes[0].equals(Object.class)) {
reflectiveClassBuildItemBuildProducer
.produce(new ReflectiveClassBuildItem(true, true, true, parameterTypes[0].getName()));
reflectiveClassBuildItemBuildProducer
.produce(new ReflectiveClassBuildItem(true, true, true, method.getReturnType().getName()));
reflectiveClassBuildItemBuildProducer.produce(new ReflectiveClassBuildItem(true, true, true,
DateTime.class));
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ public void setHandlerClass(Class<? extends RequestHandler<?, ?>> handler, BeanC
beanContainer = container;
ObjectMapper objectMapper = AmazonLambdaMapperRecorder.objectMapper;
Method handlerMethod = discoverHandlerMethod(handlerClass);
if (handlerMethod.getParameterTypes()[0].equals(S3Event.class)) {
Class<?> parameterType = handlerMethod.getParameterTypes()[0];
if (parameterType.equals(S3Event.class)) {
objectReader = new S3EventInputReader(objectMapper);
} else {
objectReader = new JacksonInputReader(objectMapper.readerFor(handlerMethod.getParameterTypes()[0]));
objectReader = new JacksonInputReader(objectMapper.readerFor(parameterType));
}
objectWriter = new JacksonOutputWriter(objectMapper.writerFor(handlerMethod.getReturnType()));
}
Expand Down Expand Up @@ -85,9 +86,11 @@ private Method discoverHandlerMethod(Class<? extends RequestHandler<?, ?>> handl
Method method = null;
for (int i = 0; i < methods.length && method == null; i++) {
if (methods[i].getName().equals("handleRequest")) {
final Class<?>[] types = methods[i].getParameterTypes();
if (types.length == 2 && !types[0].equals(Object.class)) {
method = methods[i];
if (methods[i].getParameterCount() == 2) {
final Class<?>[] types = methods[i].getParameterTypes();
if (!types[0].equals(Object.class)) {
method = methods[i];
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ public FunctionInvoker(String name, Class<?> targetClass, Method method) {
this.method = method;
if (method.getParameterCount() > 0) {
parameterInjectors = new ArrayList<>(method.getParameterCount());
Class<?>[] parameterTypes = method.getParameterTypes();
Type[] genericParameterTypes = method.getGenericParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (int i = 0; i < method.getParameterCount(); i++) {
Type type = method.getGenericParameterTypes()[i];
Class clz = method.getParameterTypes()[i];
Annotation[] annotations = method.getParameterAnnotations()[i];
Type type = genericParameterTypes[i];
Class<?> clz = parameterTypes[i];
Annotation[] annotations = parameterAnnotations[i];
ValueInjector injector = ParameterInjector.createInjector(type, clz, annotations);
if (injector instanceof InputValueInjector) {
inputType = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ static void setDelegates(String selectedDelegate, String selectedRawDelegate) {
if (method.getName().equals("accept")) {
// the first parameter of the accept method is the event, we need to register it's type to
// be able to deserialize to it to mimic what a BackgroundFunction does
if (method.getParameterTypes()[0] != Object.class) {// FIXME we have two accept methods !!!
parameterType = method.getParameterTypes()[0];
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes[0] != Object.class) {// FIXME we have two accept methods !!!
parameterType = parameterTypes[0];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,12 @@ private void processParticipantMethod(Method method) {

verifyReturnType(method);

Class<?>[] parameterTypes = method.getParameterTypes();

if (parameterTypes.length > 2) {
if (method.getParameterCount() > 2) {
throw new IllegalStateException(String.format("%s: %s",
method.toGenericString(), "Participant method cannot have more than 2 arguments"));
}

Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length > 0 && !parameterTypes[0].equals(URI.class)) {
throw new IllegalStateException(String.format("%s: %s",
method.toGenericString(), "Invalid argument type in LRA participant method: " + parameterTypes[0]));
Expand All @@ -146,12 +145,12 @@ private boolean setAfterLRAAnnotation(Method method) {
method.toGenericString(), "Invalid return type for @AfterLRA method: " + method.getReturnType()));
}

Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length > 2) {
if (method.getParameterCount() > 2) {
throw new IllegalStateException(String.format("%s: %s",
method.toGenericString(), "@AfterLRA method cannot have more than 2 arguments"));
}

Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length > 0 && !parameterTypes[0].equals(URI.class)) {
throw new IllegalStateException(String.format("%s: %s",
method.toGenericString(), "Invalid first argument of @AfterLRA method: " + parameterTypes[0]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ public CompletionStage<Object> getValue(Object instance) {
for (Method method : methods) {
try {
if (params.parameterTypesMatch(method.isVarArgs(), method.getParameterTypes())) {
Class<?>[] parameterTypes = method.getParameterTypes();
Object[] args = new Object[method.getParameterCount()];
for (int i = 0; i < args.length; i++) {
if (method.isVarArgs() && (i == args.length - 1)) {
Class<?> lastParam = method.getParameterTypes()[i];
Class<?> lastParam = parameterTypes[i];
args[i] = params.getVarargsResults(method.getParameterCount(),
lastParam.getComponentType());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,15 @@ public RuntimeResource buildResourceMethod(ResourceClass clazz,
Method javaMethod = lazyMethod.getMethod();
// Workaround our lack of support for generic params by not doing this init if there are not runtime
// param converter providers
converter.init(paramConverterProviders, javaMethod.getParameterTypes()[i],
javaMethod.getGenericParameterTypes()[i],
javaMethod.getParameterAnnotations()[i]);
Class<?>[] parameterTypes = javaMethod.getParameterTypes();
Type[] genericParameterTypes = javaMethod.getGenericParameterTypes();
Annotation[][] parameterAnnotations = javaMethod.getParameterAnnotations();
converter.init(paramConverterProviders, parameterTypes[i], genericParameterTypes[i],
parameterAnnotations[i]);
// make sure we give the user provided resolvers the chance to convert
converter = new RuntimeResolvedConverter(converter);
converter.init(paramConverterProviders, javaMethod.getParameterTypes()[i],
javaMethod.getGenericParameterTypes()[i],
javaMethod.getParameterAnnotations()[i]);
converter.init(paramConverterProviders, parameterTypes[i], genericParameterTypes[i],
parameterAnnotations[i]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ public Object[] apply(Method method, Object creationalContext) {
if (beanManager == null) {
return values;
}
Class<?>[] parameterTypes = method.getParameterTypes();
try {
// obtain the same method definition but from the TCCL
method = getClass().getClassLoader()
.loadClass(method.getDeclaringClass().getName())
.getMethod(method.getName(), convertToCL(method.getParameterTypes(), getClass().getClassLoader()));
.getMethod(method.getName(), convertToCL(parameterTypes, getClass().getClassLoader()));
} catch (Throwable t) {
throw new RuntimeException(t);
}
Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
try {
values[i] = getInstanceByType(beanManager, i, method, (CreationalContext<?>) creationalContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public Annotated getAnnotated() {
}

private Type findTypeOrGenericType() {
if (method.getGenericParameterTypes().length > 0) {
return method.getGenericParameterTypes()[position];
Type[] genericParameterTypes = method.getGenericParameterTypes();
if (genericParameterTypes.length > 0) {
return genericParameterTypes[position];
}
return method.getParameterTypes()[position];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.nio.file.Path;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -869,11 +870,12 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocation
//TODO: make this more pluggable
List<Object> originalArguments = invocationContext.getArguments();
List<Object> argumentsFromTccl = new ArrayList<>();
Parameter[] parameters = invocationContext.getExecutable().getParameters();
for (int i = 0; i < originalArguments.size(); i++) {
Object arg = originalArguments.get(i);
boolean cloneRequired = false;
Object replacement = null;
Class<?> argClass = invocationContext.getExecutable().getParameters()[i].getType();
Class<?> argClass = parameters[i].getType();
if (arg != null) {
Class<?> theclass = argClass;
while (theclass.isArray()) {
Expand Down