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

com.buschmais.xo.api.metadata.reflection.SetPropertyMethod Maven / Gradle / Ivy

The newest version!
package com.buschmais.xo.api.metadata.reflection;

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

import com.buschmais.xo.api.XOException;

/**
 * Represents a set method.
 */
public class SetPropertyMethod extends AbstractPropertyMethod {

    private final GetPropertyMethod getter;

    /**
     * Constructor.
     *
     * @param setter
     *     The set method.
     * @param getter
     *     The corresponding {@link GetPropertyMethod}.
     * @param name
     *     The name of the property.
     * @param type
     *     The type of the property.
     */
    public SetPropertyMethod(Method setter, GetPropertyMethod getter, String name, Class type, Type genericType) {
        super(setter, name, type, genericType);
        if (getter == null) {
            throw new XOException("No getter defined for property '" + name + "' of type '" + type.getName() + "' in type '" + setter.getDeclaringClass()
                .getName() + "'.");
        }
        this.getter = getter;
    }

    @Override
    public  T getAnnotation(Class type) {
        if (super.getAnnotation(type) != null) {
            reportInvalidAnnotationLocation();
        }
        return getter.getAnnotation(type);
    }

    @Override
    public  T getByMetaAnnotation(Class type) {
        if (super.getByMetaAnnotation(type) != null) {
            return reportInvalidAnnotationLocation();
        }
        return getter.getByMetaAnnotation(type);
    }

    @Override
    public Annotation[] getAnnotations() {
        return getter.getAnnotations();
    }

    private  T reportInvalidAnnotationLocation() {
        throw new XOException("A setter method must not be annotated, use getter instead: " + getAnnotatedElement().toString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy