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

com.lambdaworks.redis.dynamic.parameter.Parameter Maven / Gradle / Ivy

Go to download

Advanced and thread-safe Java Redis client for synchronous, asynchronous, and reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs and much more.

The newest version!
/*
 * Copyright 2011-2016 the original author or authors.
 *
 * 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
 *
 *      http://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 com.lambdaworks.redis.dynamic.parameter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.lambdaworks.redis.dynamic.support.*;
import com.lambdaworks.redis.internal.LettuceAssert;
import com.lambdaworks.redis.internal.LettuceClassUtils;

/**
 * Abstracts a method parameter and exposes access to type and parameter information.
 * 
 * @author Mark Paluch
 * @since 5.0
 */
public class Parameter {

    private final ParameterNameDiscoverer discoverer = new CompositeParameterNameDiscoverer(
            new StandardReflectionParameterNameDiscoverer(), new AnnotationParameterNameDiscoverer());

    private final Method method;
    private final String name;
    private final int parameterIndex;
    private final TypeInformation typeInformation;
    private final MethodParameter methodParameter;

    public Parameter(Method method, int parameterIndex) {

        this.method = method;
        this.parameterIndex = parameterIndex;
        this.methodParameter = new MethodParameter(method, parameterIndex);
        this.methodParameter.initParameterNameDiscovery(discoverer);
        this.name = methodParameter.getParameterName();
        this.typeInformation = ClassTypeInformation.fromMethodParameter(method, parameterIndex);
    }

    /**
     * Return the parameter annotation of the given type, if available.
     * 
     * @param annotationType the annotation type to look for
     * @return the annotation object, or {@code null} if not found
     */
    public  A findAnnotation(Class annotationType) {
        return methodParameter.getParameterAnnotation(annotationType);
    }

    /**
     * Return all parameter annotations.
     * 
     * @return the {@link List} of annotation objects.
     */
    public List getAnnotations() {

        Annotation[] annotations = method.getParameterAnnotations()[parameterIndex];
        List result = new ArrayList<>(annotations.length);
        Collections.addAll(result, annotations);

        return result;
    }

    /**
     *
     * @return the parameter index.
     */
    public int getParameterIndex() {
        return parameterIndex;
    }

    /**
     *
     * @return the parameter type.
     */
    public Class getParameterType() {
        return method.getParameterTypes()[parameterIndex];
    }

    /**
     *
     * @return the parameter {@link TypeInformation}.
     */
    public TypeInformation getTypeInformation() {
        return typeInformation;
    }

    /**
     * Check whether the parameter is assignable to {@code target}.
     * 
     * @param target must not be {@literal null}.
     * @return
     */
    public boolean isAssignableTo(Class target) {

        LettuceAssert.notNull(target, "Target type must not be null");

        return LettuceClassUtils.isAssignable(target, getParameterType());
    }

    /**
     *
     * @return {@literal true} if the parameter is a special parameter.
     */
    public boolean isSpecialParameter() {
        return false;
    }

    /**
     * @return {@literal true} if the {@link Parameter} can be bound to a command.
     */
    boolean isBindable() {
        return !isSpecialParameter();
    }

    /**
     * @return the parameter name or {@literal null} if not available.
     */
    public String getName() {
        return name;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy