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

The following examples show how to use com.openpojo.reflection.PojoClass#getPojoMethods() . 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: PojoMethodImplTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotIncludeAbstractGetterMethod() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithAbstractGetter.class);
  boolean hasAbstractGetterMethod = false;
  PojoField pojoField = pojoClass.getPojoFields().get(0);

  String expectedGetterName = "get" + pojoField.getName().substring(0, 1).toUpperCase() + pojoField.getName().substring(1,
      pojoField.getName().length());
  for (PojoMethod pojoMethod : pojoClass.getPojoMethods()) {

    if (pojoMethod.getName().equals(expectedGetterName))
      hasAbstractGetterMethod = true;
  }
  Assert.assertTrue(hasAbstractGetterMethod);
  Assert.assertEquals(1, pojoClass.getPojoFields().size());
  Assert.assertFalse(pojoField.hasGetter());
}
 
Example 2
Source File: PojoParameterImplTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void testMethodWithAnnotatedParameter() {
  PojoClass aClassWithMethodWithAnnotatedParemeters =
      PojoClassFactory.getPojoClass(AClassWithMethodWithAnnotatedParemeters.class);
  List<PojoMethod> allMethods = aClassWithMethodWithAnnotatedParemeters.getPojoMethods();
  List<PojoMethod> methods = new ArrayList<PojoMethod>();

  for (PojoMethod method : allMethods) {
    if (!method.isConstructor())
      methods.add(method);
  }

  Assert.assertEquals(1, methods.size());

  shouldHaveOneAnnotatedParameter(methods);
}
 
Example 3
Source File: PojoMethodImplTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotIncludeAbstractSetterMethod() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithAbstractSetter.class);
  boolean hasAbstractSetterMethod = false;
  PojoField pojoField = pojoClass.getPojoFields().get(0);

  String expectedSetterName = "set" + pojoField.getName().substring(0, 1).toUpperCase() + pojoField.getName().substring(1,
      pojoField.getName().length());
  for (PojoMethod pojoMethod : pojoClass.getPojoMethods()) {

    if (pojoMethod.getName().equals(expectedSetterName))
      hasAbstractSetterMethod = true;
  }
  Assert.assertTrue(hasAbstractSetterMethod);
  Assert.assertEquals(1, pojoClass.getPojoFields().size());
  Assert.assertFalse(pojoField.hasGetter());
}
 
Example 4
Source File: PojoMethodImplGenericParametersTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldGetMethodWithGenericParameter() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithGenericParameterMethod.class);
  List<PojoMethod> allMethodsAndConstructors = pojoClass.getPojoMethods();

  List<PojoMethod> methods = new LinkedList<PojoMethod>();
  for (PojoMethod method : allMethodsAndConstructors) {
    if (method.isConstructor())
      continue;
    methods.add(method);
  }

  Affirm.affirmEquals(pojoClass.getName() + " should have only one generic parameterized method", 1, methods.size());

  shouldHaveOneParameterizedParameter(methods, Integer.class);

}
 
Example 5
Source File: ASMServiceEnd2EndTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void tryOutAbstractMethods() {
  AbstractClassWithVariousAbstractMethods instance = getRandomValue(AbstractClassWithVariousAbstractMethods.class);
  PojoClass pojoClass = PojoClassFactory.getPojoClass(AbstractClassWithVariousAbstractMethods.class);
  for (PojoMethod method : pojoClass.getPojoMethods())
    if (!method.isConstructor()) {

      assertThat("Method" + method, method.isAbstract(), is(true));

      final List<PojoParameter> pojoParameters = method.getPojoParameters();
      Object[] params = new Object[pojoParameters.size()];
      for (int idx = 0; idx < params.length; idx++) {
        params[idx] = RandomFactory.getRandomValue(pojoParameters.get(idx));
      }

      final Class<?> returnType = method.getReturnType();
      if (!returnType.equals(Void.class) && !returnType.equals(void.class))
        assertThat("Method " + method, method.invoke(instance, params), notNullValue());
      else
        assertThat("Method " + method, method.invoke(instance, params), nullValue());
    }
}
 
Example 6
Source File: StructuralTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
public boolean include(PojoClass pojoClass) {

      if (pojoClass.getSourcePath().contains("test-classes")
          || RuntimeException.class.isAssignableFrom(pojoClass.getClazz())
          || excluded.contains(pojoClass.getName())
          || pojoClass.isSynthetic()
          || (pojoClass.isNestedClass() && pojoClass.isPrivate()))
        return false;

      boolean includeReturn = true;
      for (PojoMethod method : pojoClass.getPojoMethods())
      if (!method.isSynthetic() && !method.isConstructor())
        includeReturn &= method.isStatic();

      return includeReturn;
    }
 
Example 7
Source File: ASMServiceEnd2EndTest.java    From openpojo with Apache License 2.0 6 votes vote down vote up
@Test
public void implementAbstractToString() {
  final Class<AbstractClassWithAbstractToString> type = AbstractClassWithAbstractToString.class;
  PojoClass pojoClass = PojoClassFactory.getPojoClass(type);
  for (PojoMethod method : pojoClass.getPojoMethods())
    if (method.getName().equals("toString"))
      assertThat(method.isAbstract(), is(true));

  AbstractClassWithAbstractToString abstractToString = getRandomValue(type);
  assertThat(abstractToString, notNullValue());

  final String actual = abstractToString.toString();
  final String expected = type.getName() + SubClassDefinition.GENERATED_CLASS_POSTFIX + " [@";

  assertThat(actual, notNullValue());
  assertThat(actual, startsWith(expected));
}
 
Example 8
Source File: EqualsAndHashCodeMatchRuleTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldPassWhenEqualsAndHashCodeAreImplemented() {
  PojoClass aClassImplementingEqualsAndHashcode = PojoClassFactory.getPojoClass(AClassImplementingEqualsAndHashCode.class);
  List<PojoMethod> methods = aClassImplementingEqualsAndHashcode.getPojoMethods();

  Assert.assertEquals(3, methods.size());

  boolean constructorFound = false;
  boolean equalsFound = false;
  boolean hashCodeFound = false;

  for (PojoMethod method : methods) {
    if (method.isConstructor())
      constructorFound = true;
    if (method.getName().equals("hashCode")
        && method.getPojoParameters().size() == 0
        && method.getReturnType().equals(int.class))
      hashCodeFound = true;
    if (method.getName().equals("equals")
        && method.getPojoParameters().size() == 1
        && method.getReturnType().equals(boolean.class))
      equalsFound = true;
  }

  Assert.assertTrue("Constructor not found", constructorFound);
  Assert.assertTrue("Equals not found", equalsFound);
  Assert.assertTrue("hashCode not found", hashCodeFound);

  rule.evaluate(aClassImplementingEqualsAndHashcode);
}
 
Example 9
Source File: PojoMethodImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void classWithPrivateConstructorAndBuilder_hasSyntheticConstrutor() {
  Assert.assertNotNull(ClassWithSyntheticConstructor.Builder.getInstance());
  PojoClass pojoClass = PojoClassFactory.getPojoClass(ClassWithSyntheticConstructor.class);
  Assert.assertEquals(2, pojoClass.getPojoConstructors().size());
  for (PojoMethod constructor : pojoClass.getPojoMethods()) {
    if (constructor.getParameterTypes().length == 0)
      Assert.assertFalse("Synthatic constructor found!! [" + constructor + "]", constructor.isSynthetic());
    else
      Assert.assertTrue("None synthatic constructor found!! [" + constructor + "]", constructor.isSynthetic());
  }
}
 
Example 10
Source File: PojoMethodImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsSynthetic() {
  PojoClass syntheticPojoClass = PojoClassFactory.getPojoClass(AClassWithSyntheticMethod.class);
  for (PojoMethod pojoMethod : syntheticPojoClass.getPojoMethods()) {
    if (!pojoMethod.getName().equals("doSomethingSneaky") && !pojoMethod.isConstructor()) {
      Affirm.affirmFalse("Failed to check synthetic method [" + pojoMethod + "]", !pojoMethod.isSynthetic());
      return;
    }
  }
  Affirm.fail("failed to find a synthetic method in class");
}
 
Example 11
Source File: PojoAbstractMethodImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldfindOneAbstractMethod() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(AnAbstractClassWithOneAbstractMethod.class);
  for (PojoMethod pojoMethod : pojoClass.getPojoMethods()) {
    if (!pojoMethod.isConstructor())
      Assert.assertTrue(pojoMethod.isAbstract());
  }

  Assert.assertEquals(1 + 1 /* constructor */, pojoClass.getPojoMethods().size());
}
 
Example 12
Source File: PojoAbstractMethodImplTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFindNoAbstractMethods() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(AnAbstractClassEmpty.class);
  for (PojoMethod pojoMethod : pojoClass.getPojoMethods()) {
    Assert.assertTrue(pojoMethod.isConstructor());
    Assert.assertFalse(pojoMethod.isAbstract());
  }

  Assert.assertEquals(1, pojoClass.getPojoMethods().size());
}
 
Example 13
Source File: IssueTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void ensureEnumStaysIntact() {
  PojoClass pojoClass = PojoClassFactory.getPojoClass(SomeEnumWithValuesMethod.class);
  boolean valuesMethodExists = false;
  for (PojoMethod pojoMethod : pojoClass.getPojoMethods()) {
    if (pojoMethod.getName().equals("values") && pojoMethod.getPojoParameters().size() > 0 && pojoMethod.isStatic())
      valuesMethodExists = true;
  }
  Affirm.affirmTrue("values method must exist, Enum Class Changed?!", valuesMethodExists);
}
 
Example 14
Source File: EqualsAndHashCodeMatchRuleTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFailOnlyEqualsIsImplemented() {
  PojoClass aClassImplementingEqualsOnly = PojoClassFactory.getPojoClass(AClassImplementingEqualsOnly.class);
  List<PojoMethod> methods = aClassImplementingEqualsOnly.getPojoMethods();

  Assert.assertEquals(2, methods.size());
  boolean constructorFound = false;
  boolean equalsFound = false;

  for (PojoMethod method : methods) {
    if (method.isConstructor())
      constructorFound = true;
    if (method.getName().equals("equals")
        && method.getPojoParameters().size() == 1
        && method.getReturnType().equals(boolean.class))
      equalsFound = true;
  }

  Assert.assertTrue("Constructor not found", constructorFound);
  Assert.assertTrue("Equals not found", equalsFound);

  try {
    rule.evaluate(aClassImplementingEqualsOnly);
    Assert.fail("Should have failed validation but did not");
  } catch (AssertionError ae) {
    Assert.assertEquals("equals implemented but hashcode isn't in Pojo [" + aClassImplementingEqualsOnly + "]", ae.getMessage
        ());
  }
}
 
Example 15
Source File: EqualsAndHashCodeMatchRuleTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldPassIfNoEqualsOrHashcodeImplemented() {
  PojoClass aClassNotImplementingHashCodeOrEquals = PojoClassFactory.getPojoClass(AClassNotImplementingHashcodeOrEquals.class);
  List<PojoMethod> methods = aClassNotImplementingHashCodeOrEquals.getPojoMethods();


  Assert.assertEquals(1, methods.size());
  Assert.assertTrue(methods.get(0).isConstructor());

  rule.evaluate(aClassNotImplementingHashCodeOrEquals);
}
 
Example 16
Source File: JacocoPojoClassAdapter.java    From openpojo with Apache License 2.0 5 votes vote down vote up
public PojoClass adapt(final PojoClass pojoClass) {
  final List<PojoField> cleansedPojoFields = new LinkedList<PojoField>();
  for (final PojoField pojoField : pojoClass.getPojoFields()) {
    if (!pojoField.getName().equals(JACOCO_FIELD_NAME)) {
      cleansedPojoFields.add(pojoField);
    }
  }
  final List<PojoMethod> cleansedPojoMethods = new LinkedList<PojoMethod>();
  for (final PojoMethod pojoMethod : pojoClass.getPojoMethods()) {
    if (!pojoMethod.getName().equals(JACOCO_METHOD_NAME)) {
      cleansedPojoMethods.add(pojoMethod);
    }
  }
  return new PojoClassImpl(pojoClass.getClazz(), cleansedPojoFields, cleansedPojoMethods);
}
 
Example 17
Source File: IssueTest.java    From openpojo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHideJacocoFieldAndMethod() throws NoSuchFieldException, NoSuchMethodException {
  Field field = this.getClass().getDeclaredField(JACOCO_FIELD_NAME);
  Assert.assertNotNull("Should not be null", field);

  Method method = this.getClass().getDeclaredMethod(JACOCO_METHOD_NAME);
  Assert.assertNotNull("Should not be null", method);

  PojoClassAdapter jacocoPojoClassAdapter = JacocoPojoClassAdapter.getInstance();
  PojoClass cleansedPojoClass = jacocoPojoClassAdapter.adapt(PojoClassFactory.getPojoClass(this.getClass()));

  for (PojoField pojoField : cleansedPojoClass.getPojoFields()) {
    if (pojoField.getName().equals(JACOCO_FIELD_NAME)) {
      Affirm.fail(JACOCO_FIELD_NAME + " field is still visible!!");
    }
  }

  for (PojoMethod pojoMethod : cleansedPojoClass.getPojoMethods()) {
    if (pojoMethod.getName().equals(JACOCO_METHOD_NAME)) {
      Affirm.fail(JACOCO_METHOD_NAME + " method is still visible!!");
    }
  }

  Assert.assertNotNull(this.getClass().getDeclaredField("JACOCO_FIELD_NAME"));
  Assert.assertNotNull(this.getClass().getDeclaredField("JACOCO_METHOD_NAME"));
  Assert.assertNotNull(this.getClass().getDeclaredMethod("shouldHideJacocoFieldAndMethod"));

}
 
Example 18
Source File: CloverPojoClassAdapter.java    From openpojo with Apache License 2.0 5 votes vote down vote up
public PojoClass adapt(PojoClass pojoClass) {
  final List<PojoField> cleansedPojoFields = new ArrayList<PojoField>();
  for (final PojoField pojoField : pojoClass.getPojoFields()) {
    if (!pojoField.getName().startsWith(CLOVER_INJECTED)) {
      cleansedPojoFields.add(pojoField);
    }
  }
  return new PojoClassImpl(pojoClass.getClazz(), cleansedPojoFields, pojoClass.getPojoMethods());
}
 
Example 19
Source File: AbstractUnitTest.java    From cia with Apache License 2.0 5 votes vote down vote up
private static PojoMethod findMethod(final PojoClass pojoClass, final String name) {
	final List<PojoMethod> methods = pojoClass.getPojoMethods();

	for (final PojoMethod pojoMethod : methods) {
		if (name.equalsIgnoreCase(pojoMethod.getName())) {
			return pojoMethod;
		}
	}
	return null;
}
 
Example 20
Source File: EqualsAndHashCodeMatchRule.java    From openpojo with Apache License 2.0 4 votes vote down vote up
private boolean hasHashCode(PojoClass pojoClass) {
  for (PojoMethod method : pojoClass.getPojoMethods())
    if (method.getName().equals("hashCode") && method.getPojoParameters().size() == 0)
      return true;
  return false;
}