public synchronized static A findAnnotation()

in src/main/java/com/univocity/parsers/annotations/helpers/AnnotationHelper.java [901:969]


	public synchronized static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) {
		if (annotatedElement == null || annotationType == null) {
			return null;
		}

		if (annotatedElement.equals(lastProcessedElement) && annotationType == lastProcessedAnnotationType) {
			return (A) lastAnnotationFound;
		}

		lastProcessedElement = annotatedElement;
		lastProcessedAnnotationType = annotationType;

		Stack<Annotation> path = new Stack<Annotation>();

		A annotation = findAnnotation(annotatedElement, annotationType, new HashSet<Annotation>(), path);
		if (annotation == null || path.isEmpty()) {
			lastAnnotationFound = annotation;
			return annotation;
		}

		while (!path.isEmpty()) {
			Annotation parent = path.pop();
			Annotation target = path.isEmpty() ? annotation : path.peek();

			for (Method method : parent.annotationType().getDeclaredMethods()) {
				Copy copy = method.getAnnotation(Copy.class);
				if (copy != null) {
					Class targetClass = copy.to();

					String targetProperty = copy.property();
					if (targetProperty.trim().isEmpty()) {
						targetProperty = method.getName();
					}

					Object value;

					Object existingValue = AnnotationRegistry.getValue(annotatedElement, target, method.getName());
					if (existingValue != null) {
						value = existingValue;
					} else {
						value = invoke(parent, method);
					}

					Class sourceValueType = method.getReturnType();
					Class<?> targetPropertyType = findAnnotationMethodType(targetClass, targetProperty);
					if (targetPropertyType != null && targetPropertyType.isArray() && !value.getClass().isArray()) {
						Object array = Array.newInstance(sourceValueType, 1);
						Array.set(array, 0, value);
						value = array;
					}

					if (targetClass == target.annotationType()) {
						AnnotationRegistry.setValue(annotatedElement, annotation, targetProperty, value);
					} else {
						A ann = (A) findAnnotation(annotatedElement, targetClass, new HashSet<Annotation>(), new Stack<Annotation>());
						if (ann != null) {
							AnnotationRegistry.setValue(annotatedElement, ann, targetProperty, value);
						} else {
							throw new IllegalStateException("Can't process @Copy annotation on '" + method + "'. " +
									"Annotation '" + targetClass.getName() + "' not used in " + parent.annotationType().getName() + ". Unable to process field " + annotatedElement + "'");
						}
					}

				}
			}
		}
		lastAnnotationFound = annotation;
		return annotation;
	}