All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.portal.kernel.annotation.AnnotationLocator Maven / Gradle / Ivy

There is a newer version: 7.4.3.112-ga112
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.kernel.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author     Shuyang Zhou
 * @deprecated As of Judson (7.1.x), replaced by {@link
 *             com.liferay.petra.reflect.AnnotationLocator}
 */
@Deprecated
public class AnnotationLocator {

	public static List locate(Class targetClass) {
		Queue> queue = new LinkedList<>();

		queue.offer(targetClass);

		ArrayList annotationsList = new ArrayList<>();

		Class clazz = null;

		while ((clazz = queue.poll()) != null) {
			Annotation[] annotations = clazz.getAnnotations();

			_mergeAnnotations(annotations, annotationsList);

			_queueSuperTypes(queue, clazz);
		}

		annotationsList.trimToSize();

		return annotationsList;
	}

	public static  T locate(
		Class targetClass, Class annotationClass) {

		Queue> queue = new LinkedList<>();

		queue.offer(targetClass);

		Class clazz = null;

		while ((clazz = queue.poll()) != null) {
			T annotation = clazz.getAnnotation(annotationClass);

			if (annotation == null) {
				_queueSuperTypes(queue, clazz);
			}
			else {
				return annotation;
			}
		}

		return null;
	}

	public static List locate(Method method, Class targetClass) {
		Queue> queue = new LinkedList<>();

		if (targetClass == null) {
			queue.offer(method.getDeclaringClass());
		}
		else {
			queue.offer(targetClass);
		}

		ArrayList annotationsList = new ArrayList<>();

		Class clazz = null;

		while ((clazz = queue.poll()) != null) {
			try {
				Method specificMethod = clazz.getDeclaredMethod(
					method.getName(), method.getParameterTypes());

				Annotation[] annotations = specificMethod.getAnnotations();

				_mergeAnnotations(annotations, annotationsList);
			}
			catch (Exception e) {
			}

			try {

				// Ensure the class has a publicly inherited method

				clazz.getMethod(method.getName(), method.getParameterTypes());

				Annotation[] annotations = clazz.getAnnotations();

				_mergeAnnotations(annotations, annotationsList);
			}
			catch (Exception e) {
			}

			_queueSuperTypes(queue, clazz);
		}

		annotationsList.trimToSize();

		return annotationsList;
	}

	public static  T locate(
		Method method, Class targetClass, Class annotationClass) {

		Queue> queue = new LinkedList<>();

		if (targetClass == null) {
			queue.offer(method.getDeclaringClass());
		}
		else {
			queue.offer(targetClass);
		}

		Class clazz = null;

		while ((clazz = queue.poll()) != null) {
			T annotation = null;

			try {
				Method specificMethod = clazz.getDeclaredMethod(
					method.getName(), method.getParameterTypes());

				annotation = specificMethod.getAnnotation(annotationClass);

				if (annotation != null) {
					return annotation;
				}
			}
			catch (Exception e) {
			}

			try {

				// Ensure the class has a publicly inherited method

				clazz.getMethod(method.getName(), method.getParameterTypes());

				annotation = clazz.getAnnotation(annotationClass);
			}
			catch (Exception e) {
			}

			if (annotation == null) {
				_queueSuperTypes(queue, clazz);
			}
			else {
				return annotation;
			}
		}

		return null;
	}

	private static void _mergeAnnotations(
		Annotation[] sourceAnnotations, List targetAnnotationList) {

		merge:
		for (Annotation sourceAnnotation : sourceAnnotations) {
			for (Annotation targetAnnotation : targetAnnotationList) {
				if (sourceAnnotation.annotationType() ==
						targetAnnotation.annotationType()) {

					continue merge;
				}
			}

			targetAnnotationList.add(sourceAnnotation);
		}
	}

	private static void _queueSuperTypes(
		Queue> queue, Class clazz) {

		Class superClass = clazz.getSuperclass();

		if ((superClass != null) && (superClass != Object.class)) {
			queue.offer(superClass);
		}

		Class[] interfaceClasses = clazz.getInterfaces();

		for (Class interfaceClass : interfaceClasses) {
			if (!queue.contains(interfaceClass)) {
				queue.offer(interfaceClass);
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy