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

The following examples show how to use java.lang.reflect.Constructor#getAnnotations() . 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: AnnotationsTest.java    From yGuard with MIT License 6 votes vote down vote up
/** Creates a new instance of AnnotationsTest */
@TestAnnotation(id=23, test1="blah", test3=2, classType=AnnotationsTest.class, enumTest = TestEnum.V3, recursive = @YATAnnotation(blah = "gaga"), classArray= {AnnotationsTest.class, String.class}, intArray = {2,3,4})
public AnnotationsTest()
{
  try {
    Constructor c = getClass().getConstructor();
    Annotation[] a = c.getAnnotations();
    for (int i = 0; i < a.length; i++){
      System.out.println("annotation " + a[i]);
      if (a[i] instanceof TestAnnotation){
        TestAnnotation ta = (TestAnnotation) a[i];
        System.out.println("id = 23 ? " + ta.id());
        System.out.println("test1 = blah ? " + ta.test1());
        System.out.println("test2 = test ? " + ta.test2());
        System.out.println("test3 = 2 ? " + ta.test3());
        System.out.println("classType " + ta.classType());
        System.out.println("recursive " + ta.recursive());
        System.out.println("enumTest " + ta.enumTest());
        System.out.println("classArray " + ta.classArray()[0]);
      }
    }
  } catch (Exception ex){
    ex.printStackTrace();
  }    
}
 
Example 2
Source File: AnnotationConstructorTests.java    From openjdk-systemtest with Apache License 2.0 6 votes vote down vote up
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 3
Source File: AnnotationConstructorTests.java    From openjdk-systemtest with Apache License 2.0 6 votes vote down vote up
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 4
Source File: AnnotationUtils.java    From Paginize with MIT License 6 votes vote down vote up
public static void handleAnnotatedConstructors(Class clazz,
                                               Object object,
                                               ViewFinder viewFinder,
                                               boolean initForLazy) throws
    InvocationTargetException , IllegalAccessException,
    NoSuchMethodException, InstantiationException {

  Constructor[] constructors = clazz.getDeclaredConstructors();
  for (int i = 0; i < constructors.length; ++i) {
    Constructor constructor = constructors[i];
    Annotation[] annotations = constructor.getAnnotations();
    for (int j = 0; j < annotations.length; ++j) {

      Annotation anno = annotations[j];
      if (anno instanceof ListenerDefs) {
        handleListenerDefs(clazz, object, viewFinder, initForLazy, (ListenerDefs) anno);

      } else if (anno instanceof ListenerDefs2) {
        handleListenerDefs2(clazz, object, viewFinder, initForLazy, (ListenerDefs2) anno);
      }

    }
  }
}
 
Example 5
Source File: InjectionPoint.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
static <T> InjectionPoint<T> of(Constructor<?> c)
{
  return new InjectionPointImpl<>(Key.of(c),
      c.getDeclaringClass(),
      c.getDeclaringClass().getSimpleName(),
      c.getAnnotations(),
      c.getDeclaringClass());
}
 
Example 6
Source File: DTOContractTest.java    From phoenicis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Optional<Constructor<?>> getConstructorAnnotatedWithJsonCreator(Class<?> clazz) {
    for (Constructor<?> constructor : clazz.getConstructors()) {
        for (Annotation annotation : constructor.getAnnotations()) {
            if (annotation.annotationType() == JsonCreator.class) {
                return Optional.of(constructor);
            }
        }
    }
    return Optional.empty();
}
 
Example 7
Source File: TranslatableContractTest.java    From phoenicis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Optional<Constructor<?>> getTranslationConstructor(Class<?> clazz) {
    for (Constructor<?> constructor : clazz.getConstructors()) {
        for (Annotation annotation : constructor.getAnnotations()) {
            if (annotation.annotationType() == TranslatableCreator.class) {
                return Optional.of(constructor);
            }
        }
    }
    return Optional.empty();
}
 
Example 8
Source File: Localisation.java    From phoenicis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Optional<Constructor<?>> findTranslateCreator(Class<?> clazz) {
    for (Constructor<?> constructor : clazz.getConstructors()) {
        for (Annotation annotation : constructor.getAnnotations()) {
            if (annotation.annotationType() == TranslatableCreator.class) {
                return Optional.of(constructor);
            }
        }
    }

    return Optional.empty();
}
 
Example 9
Source File: ClassConstructorFactory.java    From astrix with Apache License 2.0 5 votes vote down vote up
private boolean isAnnotationPresent(Constructor<?> constructor, Class<? extends Annotation> annotation) {
	for (Annotation a : constructor.getAnnotations()) {
		if (annotation.isAssignableFrom(a.getClass())) {
			return true;
		}
	}
	return false;
}