package org.jboss.resteasy.util;
import org.jboss.resteasy.resteasy_jaxrs.i18n.LogMessages;
import javax.ws.rs.core.Context;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
public class PickConstructor
{
public static Constructor pickSingletonConstructor(Class clazz)
{
Constructor<?>[] constructors = clazz.getConstructors();
Constructor<?> constructor = null;
int numParameters = 0;
Constructor pick = null;
boolean potentialConflict = false;
for (Constructor con : constructors)
{
if (Modifier.isPublic(con.getModifiers()) == false)
{
continue;
}
if (con.getParameterTypes().length >= numParameters)
{
if (con.getParameterTypes().length > numParameters) {
potentialConflict = false;
}
boolean noContextAnnotation = false;
if (con.getParameterAnnotations() != null)
{
for (Annotation[] ann : con.getParameterAnnotations())
{
if (FindAnnotation.findAnnotation(ann, Context.class) == null)
{
noContextAnnotation = true;
}
}
}
if (noContextAnnotation) continue;
if (con.getParameterTypes().length == numParameters && numParameters != 0) {
potentialConflict = true;
}
numParameters = con.getParameterTypes().length;
pick = con;
}
}
if (potentialConflict) {
LogMessages.LOGGER.ambiguousConstructorsFound(clazz);
}
return pick;
}
public static Constructor pickPerRequestConstructor(Class clazz)
{
Constructor<?>[] constructors = clazz.getConstructors();
Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
Constructor<?> constructor = null;
int numParameters = 0;
Constructor pick = null;
boolean potentialConflict = false;
for (Constructor con : constructors)
{
if (Modifier.isPublic(con.getModifiers()) == false)
{
continue;
}
if (con.getParameterTypes().length >= numParameters)
{
if (con.getParameterTypes().length > numParameters) {
potentialConflict = false;
}
boolean noContextAnnotation = false;
if (con.getParameterAnnotations() != null)
{
for (Annotation[] ann : con.getParameterAnnotations())
{
if (FindAnnotation.findJaxRSAnnotations(ann).length == 0)
{
noContextAnnotation = true;
}
}
}
if (noContextAnnotation) continue;
if (con.getParameterTypes().length == numParameters && numParameters != 0) {
potentialConflict = true;
}
numParameters = con.getParameterTypes().length;
pick = con;
}
}
if (potentialConflict) {
LogMessages.LOGGER.ambiguousConstructorsFound(clazz);
}
return pick;
}
}