Tests if two annotated methods are equivalent for enterprise annotation Parameter. - Java java.lang.annotation

Java examples for java.lang.annotation:Method Annotation

Description

Tests if two annotated methods are equivalent for enterprise annotation Parameter.

Demo Code


import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedParameter;
import javax.enterprise.inject.spi.AnnotatedType;

public class Main{
    public static void main(String[] argv) throws Exception{
        List listA = java.util.Arrays.asList("asdf","java2s.com");
        List listB = java.util.Arrays.asList("asdf","java2s.com");
        System.out.println(isMatch(listA,listB));
    }//from   w  w  w  .j av  a 2  s . c om
    private static boolean isMatch(List<AnnotatedParameter<?>> listA,
            List<AnnotatedParameter<?>> listB) {
        if (listA.size() != listB.size())
            return false;

        int len = listA.size();

        for (int i = 0; i < len; i++) {
            if (!listA.get(i).getBaseType()
                    .equals(listB.get(i).getBaseType()))
                return false;
        }

        return true;
    }
    /**
     * Tests if two annotated methods are equivalent.
     */
    public static boolean isMatch(AnnotatedMethod<?> methodA,
            AnnotatedMethod<?> methodB) {
        if (methodA == methodB)
            return true;
        else if (methodA == null || methodB == null)
            return false;

        Method javaMethodA = methodA.getJavaMember();
        Method javaMethodB = methodB.getJavaMember();

        if (!javaMethodA.getName().equals(javaMethodB.getName()))
            return false;

        List<AnnotatedParameter<?>> paramListA = (List) methodA
                .getParameters();
        List<AnnotatedParameter<?>> paramListB = (List) methodB
                .getParameters();

        if (isMatch(paramListA, paramListB)) {
            return true;
        }

        return false;
    }
    /**
     * Tests if an annotated method matches a name and parameter types.
     */
    public static boolean isMatch(AnnotatedMethod<?> method, String name,
            Class<?>[] param) {
        Method javaMethod = method.getJavaMember();

        if (!javaMethod.getName().equals(name))
            return false;

        Class<?>[] mparam = javaMethod.getParameterTypes();

        return isMatch(mparam, param);
    }
    /**
     * Tests if an annotated method matches a name and parameter types.
     */
    public static boolean isMatch(Method javaMethod, String name,
            Class<?>[] param) {
        if (!javaMethod.getName().equals(name))
            return false;

        Class<?>[] mparam = javaMethod.getParameterTypes();

        return isMatch(mparam, param);
    }
    /**
     * Tests if an annotated method matches a name and parameter types.
     */
    public static boolean isMatch(Method methodA, Method methodB) {
        if (!methodA.getName().equals(methodB.getName()))
            return false;

        return isMatch(methodA.getParameterTypes(),
                methodB.getParameterTypes());
    }
    /**
     * Tests if parameters match a method's parameter types.
     */
    public static boolean isMatch(Class<?>[] paramA, Class<?>[] paramB) {
        if (paramA.length != paramB.length)
            return false;

        for (int i = paramA.length - 1; i >= 0; i--) {
            if (!paramA[i].equals(paramB[i]))
                return false;
        }

        return true;
    }
    public static BaseType getBaseType(Annotated annotated) {
        if (annotated instanceof BaseTypeAnnotated)
            return ((BaseTypeAnnotated) annotated).getBaseTypeImpl();
        else
            return BaseTypeFactory.getCurrent().createForTarget(
                    annotated.getBaseType());
    }
}

Related Tutorials