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

org.omnifaces.services.util.AnnotatedMethodWrapper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021 OmniFaces
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.omnifaces.services.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jakarta.enterprise.inject.spi.AnnotatedMethod;
import jakarta.enterprise.inject.spi.AnnotatedParameter;
import jakarta.enterprise.inject.spi.AnnotatedType;

public class AnnotatedMethodWrapper implements AnnotatedMethod {

	private AnnotatedMethod wrappedAnnotatedMethod;

	private Set annotations;

	public AnnotatedMethodWrapper(AnnotatedMethod wrappedAnnotatedMethod) {
		this.wrappedAnnotatedMethod = wrappedAnnotatedMethod;

		annotations = new HashSet<>(wrappedAnnotatedMethod.getAnnotations());
	}

	@Override
	public List> getParameters() {
		return wrappedAnnotatedMethod.getParameters();
	}

	@Override
	public AnnotatedType getDeclaringType() {
		return wrappedAnnotatedMethod.getDeclaringType();
	}

	@Override
	public boolean isStatic() {
		return wrappedAnnotatedMethod.isStatic();
	}

	@Override
	public  T getAnnotation(Class annotationType) {
		for (Annotation annotation : annotations) {
			if (annotationType.isInstance(annotation)) {
				return annotationType.cast(annotation);
			}
		}

		return null;
	}

	@Override
	public Set getAnnotations() {
		return Collections.unmodifiableSet(annotations);
	}

	@Override
	public Type getBaseType() {
		return wrappedAnnotatedMethod.getBaseType();
	}

	@Override
	public Set getTypeClosure() {
		return wrappedAnnotatedMethod.getTypeClosure();
	}

	@Override
	public boolean isAnnotationPresent(Class annotationType) {
		for (Annotation annotation : annotations) {
			if (annotationType.isInstance(annotation)) {
				return true;
			}
		}

		return false;
	}

	@Override
	public Method getJavaMember() {
		return wrappedAnnotatedMethod.getJavaMember();
	}

	public void addAnnotation(Annotation annotation) {
		annotations.add(annotation);
	}

	public void removeAnnotation(Annotation annotation) {
		annotations.remove(annotation);
	}

	public void removeAnnotation(Class annotationType) {
		Annotation annotation = getAnnotation(annotationType);
		if (annotation != null ) {
			removeAnnotation(annotation);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy