Skip to content
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 @@ -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;

Expand Down Expand Up @@ -1103,7 +1119,7 @@ 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());
value, (Class<?>) constructorParameter.getParameterType(), constructorParameter.getType());
if (result.wasSuccessful()) {
parameterValues.set(constructorParameter.getParameterIndex(), result.getValue());
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void setup() {
Object.class,
true,
0,
null,
new StaticInjectAnnotationProcessorFactory[0],
null);
secondConstructorParameter = new ConstructorParameter(
Expand All @@ -79,6 +80,7 @@ public void setup() {
Object.class,
true,
1,
null,
new StaticInjectAnnotationProcessorFactory[0],
null);
}
Expand Down