All Downloads are FREE. Search and download functionalities are using the official Maven repository.

autofixture.generators.objects.implementationdetails.ConstructorCall Maven / Gradle / Ivy

Go to download

An attempt to reimplement core features of a popular .NET anonymous value generator - AutoFixture - in Java

There is a newer version: 2.1.10
Show newest version
package autofixture.generators.objects.implementationdetails;

import com.google.common.reflect.Invokable;
import com.google.common.reflect.TypeToken;

import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.List;

import static java.util.stream.Collectors.toList;

public class ConstructorCall {
  private final Invokable rawValue;

  public ConstructorCall(Invokable constructor1) {
    this.rawValue = constructor1;
  }

  public static  List>
  getConstructorsSortedFromLongestToShortestParametersCount(
      TypeToken typeToken) {
    final Constructor[] constructors = typeToken.getRawType().getDeclaredConstructors();
    Arrays.stream(constructors).forEach(c -> c.setAccessible(true));

    List> constructorCalls =
        Arrays.stream(constructors)
            .map(typeToken::constructor)
            .map(ConstructorCall::new)
            .collect(toList());

    constructorCalls.sort((arg0, arg1) -> arg0.compareConstructors(arg1));
    return constructorCalls;
  }

  boolean isParameterless() {
    return getParametersCount() == 0;
  }

  boolean hasLessParametersThan(int currentArgumentCount) {
    return getParametersCount() < currentArgumentCount;
  }

  int getParametersCount() {
    return getRawValue().getParameters().size();
  }

  public boolean isPackagePrivate() {
    return getRawValue().isPackagePrivate();
  }

  public boolean isPublic() {
    return getRawValue().isPublic();
  }

  public int compareConstructors(ConstructorCall constructorCall2) {
    return Integer.compare(
        constructorCall2.getParametersCount(), getParametersCount());
  }

  public Invokable getRawValue() {
    return rawValue;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy