You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2023/06/27 17:03:29 UTC
[sling-org-apache-sling-models-impl] branch feature/use-java-lang-reflect-parameter created (now dcb5830)
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a change to branch feature/use-java-lang-reflect-parameter
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
at dcb5830 SLING-11917 Evaluate constructor parameter names via reflection
This branch includes the following new commits:
new dcb5830 SLING-11917 Evaluate constructor parameter names via reflection
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[sling-org-apache-sling-models-impl] 01/01: SLING-11917 Evaluate constructor parameter names via reflection
Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch feature/use-java-lang-reflect-parameter
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
commit dcb58304ef22cb68515c776029e8ea9b86fbe4e2
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Tue Jun 27 19:03:24 2023 +0200
SLING-11917 Evaluate constructor parameter names via reflection
This is available if compiled accordingly (with javac flag -parameters,
https://docs.oracle.com/en/java/javase/17/docs/specs/man/javac.html#option-parameters)
---
.../sling/models/impl/ModelAdapterFactory.java | 2 +-
.../models/impl/model/ConstructorParameter.java | 27 ++++++++++++++--------
.../models/impl/model/ModelClassConstructor.java | 21 +++++++----------
.../models/impl/injectors/SelfInjectorTest.java | 4 ++--
4 files changed, 29 insertions(+), 25 deletions(-)
diff --git a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
index 9724013..02d0541 100644
--- a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
+++ b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
@@ -1017,7 +1017,7 @@ public class ModelAdapterFactory implements AdapterFactory, Runnable, ModelFacto
private RuntimeException setConstructorParameter(ConstructorParameter constructorParameter, List<Object> parameterValues, Object value) {
if (constructorParameter.getParameterType() instanceof Class<?>) {
- Result<Object> result = adaptIfNecessary(value, (Class<?>) constructorParameter.getParameterType(), constructorParameter.getGenericType());
+ Result<Object> result = adaptIfNecessary(value, (Class<?>) constructorParameter.getParameterType(), constructorParameter.getType());
if (result.wasSuccessful() ) {
parameterValues.set(constructorParameter.getParameterIndex(), result.getValue());
return null;
diff --git a/src/main/java/org/apache/sling/models/impl/model/ConstructorParameter.java b/src/main/java/org/apache/sling/models/impl/model/ConstructorParameter.java
index 2eb525c..5700373 100644
--- a/src/main/java/org/apache/sling/models/impl/model/ConstructorParameter.java
+++ b/src/main/java/org/apache/sling/models/impl/model/ConstructorParameter.java
@@ -20,10 +20,12 @@ package org.apache.sling.models.impl.model;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.util.Arrays;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
+import org.apache.sling.models.impl.ReflectionUtil;
import org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProcessorFactory;
/**
@@ -34,23 +36,30 @@ import org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProces
public class ConstructorParameter extends AbstractInjectableElement {
private final Type parameterType;
- private final Type genericType;
private final boolean isPrimitive;
private final int parameterIndex;
+ /**
+ * Try to extract parameter names according to https://openjdk.org/jeps/118 (requires javac flag -parameters)
+ * @param parameter
+ * @param parameterIndex
+ * @param processorFactories
+ * @param defaultInjectionStrategy
+ */
+ public static ConstructorParameter of(Parameter parameter, int parameterIndex, StaticInjectAnnotationProcessorFactory[] processorFactories, DefaultInjectionStrategy defaultInjectionStrategy) {
+ Type genericType = ReflectionUtil.mapPrimitiveClasses(parameter.getParameterizedType());
+ boolean isPrimitive = (parameter.getParameterizedType() != genericType);
+ return new ConstructorParameter(parameter.getAnnotations(), parameter.getType(), genericType, isPrimitive, parameterIndex, parameter.getName(), processorFactories, defaultInjectionStrategy);
+ }
+
public ConstructorParameter(Annotation[] annotations, Type parameterType, Type genericType, boolean isPrimitive,
- int parameterIndex, StaticInjectAnnotationProcessorFactory[] processorFactories, DefaultInjectionStrategy defaultInjectionStrategy) {
- super(new FakeAnnotatedElement(annotations, parameterIndex), genericType, null, processorFactories, defaultInjectionStrategy);
+ int parameterIndex, String name, StaticInjectAnnotationProcessorFactory[] processorFactories, DefaultInjectionStrategy defaultInjectionStrategy) {
+ super(new FakeAnnotatedElement(annotations, parameterIndex), genericType, name, processorFactories, defaultInjectionStrategy);
this.parameterType = parameterType;
- this.genericType = genericType;
this.isPrimitive = isPrimitive;
this.parameterIndex = parameterIndex;
}
- public Type getGenericType() {
- return this.genericType;
- }
-
public Type getParameterType() {
return this.parameterType;
}
@@ -65,7 +74,7 @@ public class ConstructorParameter extends AbstractInjectableElement {
@Override
public String toString() {
- return "Parameter" + this.parameterIndex + "[" + this.genericType.toString() + "]";
+ return "Parameter" + this.parameterIndex + "[" + getType().toString() + "]";
}
public static class FakeAnnotatedElement implements AnnotatedElement {
diff --git a/src/main/java/org/apache/sling/models/impl/model/ModelClassConstructor.java b/src/main/java/org/apache/sling/models/impl/model/ModelClassConstructor.java
index 23460be..1f1b5b5 100644
--- a/src/main/java/org/apache/sling/models/impl/model/ModelClassConstructor.java
+++ b/src/main/java/org/apache/sling/models/impl/model/ModelClassConstructor.java
@@ -20,12 +20,12 @@ package org.apache.sling.models.impl.model;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Type;
+import java.lang.reflect.Parameter;
+import java.util.stream.IntStream;
import javax.inject.Inject;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
-import org.apache.sling.models.impl.ReflectionUtil;
import org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProcessorFactory;
public class ModelClassConstructor<M> {
@@ -38,20 +38,15 @@ public class ModelClassConstructor<M> {
this.constructor = constructor;
this.hasInjectAnnotation = constructor.isAnnotationPresent(Inject.class);
- Type[] parameterTypes = constructor.getGenericParameterTypes();
- this.constructorParametersArray = new ConstructorParameter[parameterTypes.length];
-
- for (int i = 0; i < parameterTypes.length; i++) {
- Type genericType = ReflectionUtil.mapPrimitiveClasses(parameterTypes[i]);
- boolean isPrimitive = (parameterTypes[i] != genericType);
- this.constructorParametersArray[i] = new ConstructorParameter(
- constructor.getParameterAnnotations()[i], constructor.getParameterTypes()[i], genericType, isPrimitive, i,
- processorFactories, defaultInjectionStrategy);
- }
+ Parameter[] parameters = constructor.getParameters();
+ this.constructorParametersArray = IntStream
+ .range(0, parameters.length)
+ .mapToObj(i -> ConstructorParameter.of(parameters[i], i, processorFactories, defaultInjectionStrategy))
+ .toArray(ConstructorParameter[]::new);
}
/**
- * Proxies the call to {@link Constructor#newInstance(Object...)}, checking (and
+ * Proxies the call to {@link Constructor#newInsztance(Object...)}, checking (and
* setting) accessibility first.
*
* @param parameters
diff --git a/src/test/java/org/apache/sling/models/impl/injectors/SelfInjectorTest.java b/src/test/java/org/apache/sling/models/impl/injectors/SelfInjectorTest.java
index 02a790d..4810625 100644
--- a/src/test/java/org/apache/sling/models/impl/injectors/SelfInjectorTest.java
+++ b/src/test/java/org/apache/sling/models/impl/injectors/SelfInjectorTest.java
@@ -65,9 +65,9 @@ public class SelfInjectorTest {
@Before
public void setup() {
lenient().when(modelAnnotation.defaultInjectionStrategy()).thenReturn(DefaultInjectionStrategy.REQUIRED);
- firstConstructorParameter = new ConstructorParameter(new Annotation[0], Object.class, Object.class, true, 0,
+ firstConstructorParameter = new ConstructorParameter(new Annotation[0], Object.class, Object.class, true, 0, null,
new StaticInjectAnnotationProcessorFactory[0], null);
- secondConstructorParameter = new ConstructorParameter(new Annotation[0], Object.class, Object.class, true, 1,
+ secondConstructorParameter = new ConstructorParameter(new Annotation[0], Object.class, Object.class, true, 1, null,
new StaticInjectAnnotationProcessorFactory[0], null);
}