Java Code Examples for java.lang.reflect.Constructor#getDeclaredAnnotations()

The following examples show how to use java.lang.reflect.Constructor#getDeclaredAnnotations() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
public void testConstructor2()
{
	try
	{
		Constructor<TestClass1> ctor = TestClass1.class.getConstructor
			(new Class[] {Integer.class});
					
		// No annotations should be present on the constructor itself
		Annotation[] ctor_annotations = ctor.getDeclaredAnnotations();
		assertTrue( ctor_annotations.length == 0);			
		ctor_annotations = ctor.getAnnotations();
		assertTrue( ctor_annotations.length == 0);			

		// 3 parameter annotation should be present on the constructor
		Annotation[][] params = ctor.getParameterAnnotations();
		assertTrue(params.length != 0);  // there are parameters
		// first parameter has 3 annotations
		assertTrue(params[0].length  == 3);
	}
	catch (NoSuchMethodException e)
	{
		fail("Failed to get TestClass1 constructor (Integer)");
	}
}
 
Example 2
public void testConstructor3()
{
	try
	{
		Constructor<TestClass1> ctor = TestClass1.class.getConstructor();
					
		// No annotations should be present on the constructor itself
		Annotation[] ctor_annotations = ctor.getDeclaredAnnotations();
		assertTrue( ctor_annotations.length == 0);			
		ctor_annotations = ctor.getAnnotations();
		assertTrue( ctor_annotations.length == 0);			

		// No parameters (and therefore no parameter annotations) should be
		// present on the constructor
		Annotation[][] params = ctor.getParameterAnnotations();
		assertTrue(params.length == 0); // no parameters

	}
	catch (NoSuchMethodException e)
	{
		fail("Failed to get TestClass1 constructor()");
	}
}
 
Example 3
Source Project: caja   File: ParseTreeNodes.java    License: Apache License 2.0 5 votes vote down vote up
private static final boolean isReflectiveCtorAnnotated(Constructor<?> ctor) {
  for (int i = 0; i < ctor.getDeclaredAnnotations().length; ++i) {
    if (ctor.getDeclaredAnnotations()[i]
        instanceof ParseTreeNode.ReflectiveCtor) {
      return true;
    }
  }
  return false;
}
 
Example 4
Source Project: j2objc   File: ConstructorTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_getDeclaredAnnotations() throws Exception {
    Constructor<ConstructorTestHelper> ctor1 = null;
    ctor1 = ConstructorTestHelper.class.getConstructor(new Class[0]);
    Annotation[] annotations = ctor1.getDeclaredAnnotations();
    assertEquals("Wrong number of annotations returned", 2,
            annotations.length);
    Set<Class<?>> ignoreOrder = new HashSet<Class<?>>();
    ignoreOrder.add(annotations[0].annotationType());
    ignoreOrder.add(annotations[1].annotationType());

    assertTrue("Missing ConstructorTestAnnotationRuntime0", ignoreOrder
            .contains(ConstructorTestAnnotationRuntime0.class));
    assertTrue("Missing ConstructorTestAnnotationRuntime1", ignoreOrder
            .contains(ConstructorTestAnnotationRuntime1.class));
}
 
Example 5
public void testConstructor1()
{
	try
	{
		Constructor<TestClass1> ctor = TestClass1.class.getConstructor
			(new Class[] {Integer.class, String.class});
				
		Annotation[] ctor_annotations = ctor.getDeclaredAnnotations();
		assertTrue( ctor_annotations.length == 2);

		boolean[] annot_count = new boolean[] {false, false};
		for (int i=0; i<2; i++) 
		{
			assertTrue( ctor_annotations[i].annotationType().equals(A0.class)
					|| ctor_annotations[i].annotationType().equals(A5.class));
			
			if (ctor_annotations[i] instanceof A0)
			{
				annot_count[0] = true;
			}
			else if (ctor_annotations[i] instanceof A5)
			{
				annot_count[1] = true;			
			}								
		}
		
		A0 a0 = (A0) ctor.getAnnotation(A0.class);
		assertTrue (ctor.getAnnotation(A0.class).equals(a0));
		A5 a5 = (A5) ctor.getAnnotation(A5.class);
		assertTrue (ctor.getAnnotation(A5.class).equals(a5));
		
		// Make sure both @A0 and @A5 were found
		assertTrue (annot_count[0] && annot_count[1]);
				
		// Verify the annotations associated with the constructor parameters			
		Annotation[][] params = ctor.getParameterAnnotations();
		assertTrue(params.length != 0);	
		// first parameter has 1 annotation
		assertTrue(params[0].length  == 1);
		// second parameter has 4annotations			
		assertTrue(params[1].length  == 4);
		assertTrue(params[0][0] instanceof A0);
		
		annot_count = new boolean[] {false, false, false, false};
		for (int i=0; i<4; i++)
		{		
			if (params[1][i] instanceof A0)
			{
				annot_count[0] = true;
			}
			else if (params[1][i] instanceof A1)
			{
				annot_count[1] = true;
			}
			else if (params[1][i] instanceof A2)
			{
				annot_count[2] = true;
			}
			else if (params[1][i] instanceof A3)
			{
				annot_count[3] = true;
			}
		}				
		assertTrue (annot_count[0] && annot_count[1] &&
				annot_count[2] && annot_count[3]);		                                                            
	}
	catch (NoSuchMethodException e)
	{
		fail("Failed to get TestClass1 constructor (Integer, String): " + e.getMessage());
	}		
}