Java Code Examples for com.openpojo.reflection.PojoClass#getPojoConstructors()

The following examples show how to use com.openpojo.reflection.PojoClass#getPojoConstructors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InstanceFactory.java    From openpojo with Apache License 2.0 6 votes vote down vote up
private static Object doGetInstance(PojoClass pojoClass, Object[] parameters) {
  if (!pojoClass.isConcrete()) {
    throw ReflectionException.getInstance(String.format("[%s] is not a concrete class, can't create new instance", pojoClass));
  }

  Object instance;
  final List<PojoMethod> constructors = pojoClass.getPojoConstructors();
  for (final PojoMethod constructor : constructors) {
    if (areEquivalentParameters(upCast(constructor.getParameterTypes()), getTypes(parameters))) {
      instance = constructor.invoke(null, parameters);
      initializeBusinessKeys(pojoClass, instance);
      return instance;
    }
  }
  throw ReflectionException.getInstance(String.format("No matching constructor for [%s] found using parameters[%s]",
      pojoClass.getClazz(), Arrays.toString(getTypes(parameters))));
}
 
Example 2
Source File: PojoClassImplTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void testIsSyntheticOnSyntheticClass() {
  PojoClass syntheticPojoClass = getPojoClassImplForClass(AClassWithSythetics.class);
  Affirm.affirmEquals("Expected 2 constructors", 2, syntheticPojoClass.getPojoConstructors().size());

  PojoMethod constructor = null;

  for (PojoMethod constructorEntry : syntheticPojoClass.getPojoConstructors()) {
    if (constructorEntry.getParameterTypes().length > 0)
      constructor = constructorEntry;
  }

  Assert.assertNotNull(constructor);
  Affirm.affirmTrue("Failed to find synthetic constructor", constructor.isSynthetic());
  Affirm.affirmEquals("Synthetic Constructor should have just one parameter", 1, constructor.getParameterTypes().length);

  PojoClass aSyntheticClass = getPojoClassImplForClass(constructor.getParameterTypes()[0]);
  Affirm.affirmTrue("Parameter to synthetic constructor should be synthetic class", aSyntheticClass.isSynthetic());
}
 
Example 3
Source File: PojoParameterImplTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void isParameterized() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithParameterizedConstructors.class);

  Assert.assertTrue(pojoClass.isNestedClass());

  for (PojoMethod constructor : pojoClass.getPojoConstructors()) {
    if (!constructor.isSynthetic()) {
      List<PojoParameter> pojoParameters = constructor.getPojoParameters();
      Assert.assertThat(pojoParameters.size(), is(greaterThan(1)));
      for (int i = 1; i < pojoParameters.size(); i++) {
        PojoParameter parameter = pojoParameters.get(i);
        Assert.assertThat(parameter.isParameterized(), is(Matchers.equalTo(true)));
      }
    }
  }
}
 
Example 4
Source File: InstanceFactory.java    From openpojo with Apache License 2.0 5 votes vote down vote up
private static PojoMethod getConstructorByCriteria(final PojoClass pojoClass, final ArrayLengthBasedComparator comparator) {
  PojoMethod constructor = null;
  for (final PojoMethod pojoConstructor : pojoClass.getPojoConstructors()) {
    if (!pojoConstructor.isSynthetic() && !(pojoClass.isAbstract() && pojoConstructor.isPrivate()))
      if (constructor == null)
        constructor = pojoConstructor;
      else {
        if (comparator.compare(pojoConstructor.getParameterTypes(), constructor.getParameterTypes()))
          constructor = pojoConstructor;
      }
  }
  return constructor;
}
 
Example 5
Source File: JarFileReaderTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void onlyPrivateConstructors() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(JarFileReader.class);
  for (PojoMethod method : pojoClass.getPojoConstructors()) {
    Assert.assertTrue("Constructor must be private [" + method + "]", method.isPrivate());
  }
}
 
Example 6
Source File: PojoMethodImplGenericParametersTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetConstructorWithGenericParameter() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithGenericParameterConstructor.class);
  List<PojoMethod> constructors = pojoClass.getPojoConstructors();
  Affirm.affirmEquals(pojoClass.getName() + " should have only one generic parameterized constructor", 1, constructors.size());

  shouldHaveOneParameterizedParameter(constructors, String.class);
}
 
Example 7
Source File: PojoMethodImplGenericParametersTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHaveOneParameterForInstanceNestedClassWhenNoneDeclared() {
  PojoClass pojoclass = PojoClassFactory.getPojoClass(AClassWithNestedClass.NestedClass.class);
  List<PojoMethod> pojoConstructors = pojoclass.getPojoConstructors();
  Affirm.affirmEquals("Should have only one constructor", 1, pojoConstructors.size());
  PojoMethod constructor = pojoConstructors.get(0);
  List<PojoParameter> pojoParameters = constructor.getPojoParameters();
  Affirm.affirmEquals("Should have 1 parameter", 1, pojoParameters.size());
  Affirm.affirmFalse("Should be nonParameterized parameter", pojoParameters.get(0).isParameterized());
  Affirm.affirmEquals("Should be enclosing type", constructor.getParameterTypes()[0], pojoclass.getEnclosingClass().getClazz());
}
 
Example 8
Source File: PojoMethodImplGenericParametersTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHaveTwoParametersWithWhenOneParameterDeclared() {
  PojoClass pojoclass = PojoClassFactory.getPojoClass(AClassWithNestedClass.NestedClassWithOneParamConstructor.class);
  List<PojoMethod> pojoConstructors = pojoclass.getPojoConstructors();
  Affirm.affirmEquals("Should have only one constructor", 1, pojoConstructors.size());
  PojoMethod constructor = pojoConstructors.get(0);
  List<PojoParameter> pojoParameters = constructor.getPojoParameters();
  Affirm.affirmEquals("Should have 2 parameter", 2, pojoParameters.size());
  Affirm.affirmFalse("Should be nonParameterized parameter", pojoParameters.get(0).isParameterized());
  Affirm.affirmEquals("Should be enclosing type", constructor.getParameterTypes()[0], pojoclass.getEnclosingClass().getClazz());
  Affirm.affirmEquals("Should be int type", constructor.getParameterTypes()[1], int.class);
}
 
Example 9
Source File: PojoParameterImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructorWithAnnotatedParameter() {
  PojoClass aClassWithAnnotatedParameters = PojoClassFactory.getPojoClass(AClassWithAnnotatedParameters.class);
  List<PojoMethod> constructors = aClassWithAnnotatedParameters.getPojoConstructors();
  Assert.assertEquals(1, constructors.size());

  shouldHaveOneAnnotatedParameter(constructors);
}
 
Example 10
Source File: BusinessPojoHelperTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void shouldThrowExeptionIfConstructed() throws Throwable {
  PojoClass businessPojoHelper = PojoClassFactory.getPojoClass(BusinessPojoHelper.class);

  List<PojoMethod> pojoConstructors = businessPojoHelper.getPojoConstructors();
  Affirm.affirmEquals("Should have only one constructor", 1, pojoConstructors.size());
  Affirm.affirmTrue("Constructor must be private", pojoConstructors.get(0).isPrivate());

  try {
    businessPojoHelper.getPojoConstructors().get(0).invoke(null, (Object[]) null);
  } catch (ReflectionException re) {
    throw re.getCause().getCause();
  }
}
 
Example 11
Source File: CacheStorageFactoryTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void shouldThrowExeptionIfConstructed() throws Throwable {
  PojoClass cacheStorageFactoryPojo = PojoClassFactory.getPojoClass(CacheStorageFactory.class);

  List<PojoMethod> pojoConstructors = cacheStorageFactoryPojo.getPojoConstructors();
  Affirm.affirmEquals("Should have only one constructor", 1, pojoConstructors.size());
  Affirm.affirmTrue("Constructor must be private", pojoConstructors.get(0).isPrivate());

  try {
    pojoConstructors.get(0).invoke(null, (Object[]) null);
  } catch (ReflectionException re) {
    throw re.getCause().getCause();
  }

}
 
Example 12
Source File: URIRandomGeneratorTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void constructorIsPrivate() {
  PojoClass uriPojoClass = PojoClassFactory.getPojoClass(URIRandomGenerator.class);
  for (PojoMethod constructor : uriPojoClass.getPojoConstructors()) {
    if (!constructor.isSynthetic())
      assertTrue(constructor + " should be private", constructor.isPrivate());
  }
}
 
Example 13
Source File: URLRandomGeneratorTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void constructorIsPrivate() {
  PojoClass urlPojoClass = PojoClassFactory.getPojoClass(URLRandomGenerator.class);
  for (PojoMethod constructor : urlPojoClass.getPojoConstructors()) {
    if (!constructor.isSynthetic())
      assertTrue(constructor + " should be private", constructor.isPrivate());
  }
}
 
Example 14
Source File: UUIDRandomGeneratorTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void constructorIsPrivate() {
  PojoClass uuidPojoClass = PojoClassFactory.getPojoClass(UUIDRandomGenerator.class);
  for (PojoMethod constructor : uuidPojoClass.getPojoConstructors()) {
    if (!constructor.isSynthetic())
      assertTrue(constructor + " should be private", constructor.isPrivate());
  }
}
 
Example 15
Source File: ValidatorBuilderTest.java    From openpojo with Apache License 2.0 4 votes vote down vote up
@Test
public void constructorMustBePrivate() {
  PojoClass validatorBuilderPojoClass = PojoClassFactory.getPojoClass(ValidatorBuilder.class);
  for (PojoMethod constructor : validatorBuilderPojoClass.getPojoConstructors())
    Assert.assertTrue(constructor.isPrivate());
}
 
Example 16
Source File: JavaClassPathClassLoaderTest.java    From openpojo with Apache License 2.0 4 votes vote down vote up
@Test
public void onlyPrivateConstructors() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(JavaClassPathClassLoader.class);
  for (PojoMethod constructor : pojoClass.getPojoConstructors())
    Assert.assertThat(constructor.isPrivate(), is(true));
}