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

com.introproventures.graphql.jpa.query.introspection.Methods Maven / Gradle / Ivy

There is a newer version: 1.2.11
Show newest version
package com.introproventures.graphql.jpa.query.introspection;


import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Methods {

    protected final ClassDescriptor classDescriptor;
    protected final Map methodsMap;

    // cached
    private MethodDescriptor[] allMethods;

    public Methods(ClassDescriptor classDescriptor) {
        this.classDescriptor = classDescriptor;
        this.methodsMap = inspectMethods();
    }

    protected Map inspectMethods() {
        boolean scanAccessible = classDescriptor.isScanAccessible();
        boolean scanStatics = classDescriptor.isScanStatics();
        Class type = classDescriptor.getType();

        Method[] methods =
                scanAccessible ? ReflectionUtil.getAccessibleMethods(type) : ReflectionUtil.getAllMethodsOfClass(type);

        Map map = new LinkedHashMap<>(methods.length);

        for (Method method : methods) {
            
            if(!scanStatics && Modifier.isStatic(method.getModifiers())) {
                continue;
            }
                
            String methodName = method.getName();

            MethodDescriptor[] mds = map.get(methodName);

            if (mds == null) {
                mds = new MethodDescriptor[1];
            } else {
                mds = Arrays.copyOf(mds, mds.length + 1);
            }

            map.put(methodName, mds);

            mds[mds.length - 1] = createMethodDescriptor(method);
        }

        return map;
    }

    protected MethodDescriptor createMethodDescriptor(Method method) {
        return new MethodDescriptor(classDescriptor, method);
    }

    public MethodDescriptor getMethodDescriptor(String name, Class[] paramTypes) {
        MethodDescriptor[] methodDescriptors = methodsMap.get(name);
        if (methodDescriptors == null) {
            return null;
        }
        for (int i = 0; i < methodDescriptors.length; i++) {
            Method method = methodDescriptors[i].getMethod();
            if (ObjectUtil.isEquals(method.getParameterTypes(), paramTypes)) {
                return methodDescriptors[i];
            }
        }
        return null;
    }

    public MethodDescriptor getMethodDescriptor(String name) {
        MethodDescriptor[] methodDescriptors = methodsMap.get(name);
        if (methodDescriptors == null) {
            return null;
        }
        if (methodDescriptors.length != 1) {
            throw new IllegalArgumentException("Method name not unique: " + name);
        }
        return methodDescriptors[0];
    }

    public MethodDescriptor[] getAllMethodDescriptors(String name) {
        return methodsMap.get(name);
    }

    public MethodDescriptor[] getAllMethodDescriptors() {
        if (allMethods == null) {
            List allMethodsList = new ArrayList<>();

            for (MethodDescriptor[] methodDescriptors : methodsMap.values()) {
                Collections.addAll(allMethodsList, methodDescriptors);
            }

            MethodDescriptor[] allMethods = allMethodsList.toArray(new MethodDescriptor[allMethodsList.size()]);

            Arrays.sort(allMethods, new Comparator() {
                @Override
                public int compare(MethodDescriptor md1, MethodDescriptor md2) {
                    return md1.getMethod().getName().compareTo(md2.getMethod().getName());
                }
            });

            this.allMethods = allMethods;
        }
        return allMethods;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy