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 9a73bac6..44e8ca37 100644 --- a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java +++ b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java @@ -27,8 +27,24 @@ import java.lang.ref.PhantomReference; import java.lang.ref.ReferenceQueue; import java.lang.ref.SoftReference; -import java.lang.reflect.*; -import java.util.*; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Proxy; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -1103,7 +1119,7 @@ private RuntimeException setConstructorParameter( ConstructorParameter constructorParameter, List parameterValues, Object value) { if (constructorParameter.getParameterType() instanceof Class) { Result result = adaptIfNecessary( - value, (Class) constructorParameter.getParameterType(), constructorParameter.getGenericType()); + 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 5394dbd8..02050981 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 @@ 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,34 +36,54 @@ 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, + String name, StaticInjectAnnotationProcessorFactory[] processorFactories, DefaultInjectionStrategy defaultInjectionStrategy) { super( new FakeAnnotatedElement(annotations, parameterIndex), genericType, - null, + 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; } @@ -76,7 +98,7 @@ public int getParameterIndex() { @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 f93c6f8b..35fed655 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 @@ -22,7 +22,8 @@ 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 org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.impl.ReflectionUtil; @@ -41,21 +42,10 @@ public ModelClassConstructor( 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); } /** 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 023a63ca..6fd3886e 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 @@ -71,6 +71,7 @@ public void setup() { Object.class, true, 0, + null, new StaticInjectAnnotationProcessorFactory[0], null); secondConstructorParameter = new ConstructorParameter( @@ -79,6 +80,7 @@ public void setup() { Object.class, true, 1, + null, new StaticInjectAnnotationProcessorFactory[0], null); }