com.google.inject.internal.DefaultConstructionProxyFactory Maven / Gradle / Ivy
package com.google.inject.internal;
import com.google.inject.spi.InjectionPoint;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
/**
* Produces construction proxies that invoke the class constructor.
*/
final class DefaultConstructionProxyFactory implements ConstructionProxyFactory {
private final InjectionPoint injectionPoint;
/**
* @param injectionPoint an injection point whose member is a constructor of {@code T}.
*/
DefaultConstructionProxyFactory(InjectionPoint injectionPoint) {
this.injectionPoint = injectionPoint;
}
public ConstructionProxy create() {
@SuppressWarnings("unchecked") // the injection point is for a constructor of T
final Constructor constructor = (Constructor) injectionPoint.getMember();
if (!Modifier.isPublic(constructor.getDeclaringClass().getModifiers()) ||
!Modifier.isPublic(constructor.getModifiers())) {
constructor.setAccessible(true);
}
return new ConstructionProxy() {
public T newInstance(Object... arguments) throws InvocationTargetException {
try {
return constructor.newInstance(arguments);
} catch (InstantiationException | IllegalAccessException e) {
throw new AssertionError(e); // shouldn't happen, we know this is a concrete type
}
}
@Override
public InjectionPoint getInjectionPoint() {
return injectionPoint;
}
@Override
public Constructor getConstructor() {
return constructor;
}
};
}
}