Retrieve the constructor of a Type.

@param genericClass the type of the object
@param constructorParameterTypes an array of parameters' type to find the right constructor

@return the right constructor that matchers parameters
515317
private static Constructor<?> getConstructor(final Class<?> genericClass, final Class<?>[] constructorParameterTypes) {
        Constructor<?> constructor = null;
        try {
            constructor = genericClass.getConstructor(constructorParameterTypes);
        } catch (final NoSuchMethodException e) {
            // Return the first constructor as a workaround
            constructor = genericClass.getConstructors()[0];
        } catch (final SecurityException e) {
            LOGGER.log(NO_CONSTRUCTOR, e);
            throw e; // Pop up the exception to let it managed by the caller method
        }
        return constructor;
    }
882138
public static <T> Supplier<Constructor<T>> constructor(Class<T> clazz, Class<?>... parameterTypes) {
        return new ConstructorSupplier<T>(clazz, parameterTypes);
    }
1150041
@SuppressWarnings("unchecked")
    public static <T> Constructor<T> findMatchingConstructor(Class<T> classToInspect, Object... params) {
        Constructor<T> checkedConstructors = null;
        for (Constructor<?> constructor : classToInspect.getDeclaredConstructors()) {
            Type[] parameterTypes = constructor.getParameterTypes();
            if (parameterTypes.length == params.length && checkParameterTypes(parameterTypes, params)) {
                if (checkedConstructors == null) {
                    checkedConstructors = (Constructor<T>) constructor;
                } else {
                    throw BusinessException.createNew(BusinessErrorCode.AMBIGUOUS_CONSTRUCTOR_FOUND)
                            .put("constructor1", constructor)
                            .put("constructor2", checkedConstructors)
                            .put("object", classToInspect.getSimpleName())
                            .put("parameters", params);
                }
            }
        }
        return checkedConstructors;
    }
893066
@SuppressWarnings( "unchecked" )
    public static <T> Class<T> returnedClass(Class<?> type)
    {
        ParameterizedType parameterizedType = (ParameterizedType) type.getGenericSuperclass();
        return (Class<T>) parameterizedType.getActualTypeArguments()[0];
    }
1345574
JaversType spawn(Type baseJavaType) {
        try {
            Constructor c = this.getClass().getConstructor(Type.class);
            return (JaversType)c.newInstance(new Object[]{baseJavaType});
        } catch (ReflectiveOperationException exception) {
            throw new RuntimeException("error calling Constructor for " + this.getClass().getName(), exception);
        }
    }
474153
public static <T> Constructor<T> getConstructor(Class<T> type, Class<?>... parameterTypes) {
        try {
            return type.getConstructor(parameterTypes);
        } catch (NoSuchMethodException e) {
            log.error(StringUtils.format("类[{0}]中不存在参数列表为[{1}]的构造器", type,
                parameterTypes == null ? "null" : Arrays.toString(parameterTypes)));
            throw new ReflectException(StringUtils.format("类[{0}]中不存在参数列表为[{1}]的构造器", type,
                parameterTypes == null ? "null" : Arrays.toString(parameterTypes)), e);
        }
    }
79703
@SafeVarargs
    public static <T> Constructor<T> getDeclaredConstructor(final Class<T> cls, final Class<?>... parameterTypes) {
        Map<Class<?>[], Constructor<?>> constructorPool = classDeclaredConstructorPool.get(cls);
        Constructor<T> constructor = null;

        if (constructorPool != null) {
            constructor = (Constructor<T>) constructorPool.get(parameterTypes);
        }

        if (constructor == null) {
            try {
                constructor = cls.getDeclaredConstructor(parameterTypes);

                // SHOULD NOT set it true here.
                // constructor.setAccessible(true);
            } catch (NoSuchMethodException e) {
                // ignore.
            }

            if (constructor != null) {
                if (constructorPool == null) {
                    constructorPool = new ArrayHashMap<>(ConcurrentHashMap.class);
                    classDeclaredConstructorPool.put(cls, constructorPool);
                }

                constructorPool.put(parameterTypes.clone(), constructor);
            }
        }

        return constructor;
    }
700813
public ParameterType<ConstructorType<T>> getOrCreateParameter()
   {
      List<Node> nodeList = childNode.get("parameter");
      if (nodeList != null &&  nodeList.size() > 0)
      {
         return new ParameterTypeImpl<ConstructorType<T>>(this, "parameter", childNode, nodeList.get(0));
      }
      return createParameter();
   }
493714
public static List<Parameter> collectConstructorParameters(Class<?> cls, Components components, javax.ws.rs.Consumes classConsumes) {
        if (cls.isLocalClass() || (cls.isMemberClass() && !Modifier.isStatic(cls.getModifiers()))) {
            return Collections.emptyList();
        }

        List<Parameter> selected = Collections.emptyList();
        int maxParamsCount = 0;

        for (Constructor<?> constructor : cls.getDeclaredConstructors()) {
            if (!ReflectionUtils.isConstructorCompatible(constructor)
                && !ReflectionUtils.isInject(Arrays.asList(constructor.getDeclaredAnnotations()))) {
                continue;
            }

            final Type[] genericParameterTypes = constructor.getGenericParameterTypes();
            final Annotation[][] annotations = constructor.getParameterAnnotations();

            int paramsCount = 0;
            final List<Parameter> parameters = new ArrayList<Parameter>();
            for (int i = 0; i < genericParameterTypes.length; i++) {
                final List<Annotation> tmpAnnotations = Arrays.asList(annotations[i]);
                if (isContext(tmpAnnotations)) {
                    paramsCount++;
                } else {
                    final Type genericParameterType = genericParameterTypes[i];
                    final List<Parameter> tmpParameters = collectParameters(genericParameterType, tmpAnnotations, components, classConsumes);
                    if (tmpParameters.size() >= 1) {
                        for (Parameter tmpParameter : tmpParameters) {
                            if (ParameterProcessor.applyAnnotations(
                                                                    tmpParameter,
                                                                    genericParameterType,
                                                                    tmpAnnotations,
                                                                    components,
                                                                    classConsumes == null ? new String[0] : classConsumes.value(),
                                                                    null) != null) {
                                parameters.add(tmpParameter);
                            }
                        }
                        paramsCount++;
                    }
                }
            }

            if (paramsCount >= maxParamsCount) {
                maxParamsCount = paramsCount;
                selected = parameters;
            }
        }

        return selected;
    }
23622
private static String getGenericTypeData(Type genericType) {
    List<String> types = Lists.newArrayList();
    if (genericType != null && genericType instanceof ParameterizedType) {
      for (Type t : ((ParameterizedType) genericType).getActualTypeArguments()) {
        if (t instanceof ParameterizedType) {
          String nestedGeneric = ((Class<?>) ((ParameterizedType) t).getRawType()).getSimpleName();
          nestedGeneric += getGenericTypeData(t);
          types.add(nestedGeneric);
        } else {
          types.add(((Class<?>) t).getSimpleName());
        }
      }
    }
    return (types.size() > 0) ? "<" + Joiner.on(", ").join(types) + ">" : "";
  }
816691
public ASTConstructor getConstructor(Constructor constructor, boolean isEnum, boolean isInnerClass) {
        ASTAccessModifier modifier = ASTAccessModifier.getModifier(constructor.getModifiers());

        Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();

        if(constructor.getDeclaringClass().getEnclosingClass() != null && !Modifier.isStatic(constructor.getDeclaringClass().getModifiers())){
            // An inner class constructor contains a hidden non-annotated prameter
            Annotation[][] paddedParameterAnnotations = new Annotation[parameterAnnotations.length + 1][];

            paddedParameterAnnotations[0] = new Annotation[0];
            System.arraycopy(parameterAnnotations, 0, paddedParameterAnnotations, 1, parameterAnnotations.length);

            parameterAnnotations = paddedParameterAnnotations;
        }

        ImmutableList<ASTParameter> constructorParameters = getParameters(constructor.getParameterTypes(), constructor.getGenericParameterTypes(), parameterAnnotations);
        if(isEnum) {
            constructorParameters = constructorParameters.subList(2, constructorParameters.size());
        }
        if(isInnerClass){
            constructorParameters = constructorParameters.subList(1, constructorParameters.size());
        }
        ImmutableSet<ASTType> throwsTypes = getTypes(constructor.getExceptionTypes());

        return new ASTClassConstructor(getAnnotations(constructor), constructor, constructorParameters, modifier, throwsTypes);
    }
265496
@SuppressWarnings("unchecked")
  @NonNull
  public final GenericType<? extends T> getSubtype(@NonNull Class<?> subclass) {
    return new GenericType(token.getSubtype(subclass));
  }
1015613
@SuppressWarnings("unchecked")
	public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
		for (Annotation annotation : this.annotations) {
			if (annotation.annotationType().equals(annotationType)) {
				return (T) annotation;
			}
		}
		for (Annotation metaAnn : this.annotations) {
			T ann = metaAnn.annotationType().getAnnotation(annotationType);
			if (ann != null) {
				return ann;
			}
		}
		return null;
	}
485756
@SuppressWarnings("unchecked")
    public <T> TypeHandler<T> getInstance(Class<?> javaTypeClass, Class<?> typeHandlerClass) {
        if (javaTypeClass != null) {
          try {
            Constructor<?> c = typeHandlerClass.getConstructor(Class.class);
            return (TypeHandler<T>) c.newInstance(javaTypeClass);
          } catch (NoSuchMethodException ignored) {
            // ignored
          } catch (Exception e) {
            throw new TypeException("Failed invoking constructor for handler " + typeHandlerClass, e);
          }
        }
        try {
          Constructor<?> c = typeHandlerClass.getConstructor();
          return (TypeHandler<T>) c.newInstance();
        } catch (Exception e) {
          throw new TypeException("Unable to find a usable constructor for " + typeHandlerClass, e);
        }
      }
304068
protected SerializerFactory getSerializerFactory(boolean multipleClassLoader, boolean generic) {
        if (generic) {
            return multipleClassLoader ? new GenericMultipleClassLoaderSofaSerializerFactory() :
                new GenericSingleClassLoaderSofaSerializerFactory();
        } else {
            return multipleClassLoader ? new MultipleClassLoaderSofaSerializerFactory() :
                new SingleClassLoaderSofaSerializerFactory();
        }
    }
682531
public static ParameterizedType createParameterizedType(Class<?> rawClass, Type... genericTypes) {
        return new ParameterizedType() {
            @Override
            public Type[] getActualTypeArguments() {
                return genericTypes;
            }

            @Override
            public Type getRawType() {
                return rawClass;
            }

            @Override
            public Type getOwnerType() {
                return null;
            }
        };
    }
629266
public Class<?> getParameterType() {
        if (this.parameterType == null) {
            if (this.parameterIndex < 0) {
                this.parameterType = (this.method != null ? this.method.getReturnType() : null);
            } else {
                this.parameterType = (this.method != null ? this.method.getParameterTypes()[this.parameterIndex]
                        : this.constructor.getParameterTypes()[this.parameterIndex]);
            }
        }
        return this.parameterType;
    }