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

com.github.vladislavsevruk.generator.proxy.util.ClassMemberUtil Maven / Gradle / Ivy

/*
 * MIT License
 *
 * Copyright (c) 2020 Uladzislau Seuruk
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.github.vladislavsevruk.generator.proxy.util;

import java.lang.reflect.Executable;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Utility methods for getting characteristics of class members.
 */
public final class ClassMemberUtil {

    private static final List OBJECT_METHODS = Arrays.asList(Object.class.getMethods());

    private ClassMemberUtil() {
    }

    /**
     * Generates string with type variables declaration for received class member.
     *
     * @param genericDeclaration GenericDeclaration with type variables.
     * @return String with type variables declaration.
     */
    public static String generateBoundedTypeVariablesDeclaration(GenericDeclaration genericDeclaration) {
        String typeVariablesDeclaration = Arrays.stream(genericDeclaration.getTypeParameters())
                .map(ClassMemberUtil::generateTypeVariableDeclaration).collect(Collectors.joining(", "));
        return typeVariablesDeclaration.isEmpty() ? typeVariablesDeclaration
                : String.format("<%s>", typeVariablesDeclaration);
    }

    /**
     * Generates string with superclass type variables declaration for received class member.
     *
     * @param genericDeclaration GenericDeclaration with type variables.
     * @return String with type variables declaration.
     */
    public static String generateUnboundedTypeVariablesDeclaration(Class genericDeclaration) {
        String typeVariablesDeclaration = Arrays.stream(genericDeclaration.getTypeParameters())
                .map(TypeVariable::toString).collect(Collectors.joining(", "));
        return typeVariablesDeclaration.isEmpty() ? typeVariablesDeclaration
                : String.format("<%s>", typeVariablesDeclaration);
    }

    /**
     * Checks if received executable has no final modifier.
     *
     * @param executable Executable to check.
     * @return false if received executable has final modifier, true otherwise.
     */
    public static boolean isNonFinal(Executable executable) {
        return !Modifier.isFinal(executable.getModifiers());
    }

    /**
     * Checks if received method isn't one of Object class methods.
     *
     * @param method Method to check.
     * @return false if received method is one of Object class methods, true
     * otherwise.
     */
    public static boolean isNonObjectMethod(Method method) {
        return !OBJECT_METHODS.contains(method);
    }

    /**
     * Checks if received executable has no private modifier.
     *
     * @param executable Executable to check.
     * @return false if received executable has private modifier, true otherwise.
     */
    public static boolean isNonPrivate(Executable executable) {
        return !Modifier.isPrivate(executable.getModifiers());
    }

    /**
     * Checks if received executable has no static modifier.
     *
     * @param executable Executable to check.
     * @return false if received executable has static modifier, true otherwise.
     */
    public static boolean isNonStatic(Executable executable) {
        return !Modifier.isStatic(executable.getModifiers());
    }

    private static String generateTypeVariableDeclaration(TypeVariable typeVariable) {
        Type bound = typeVariable.getBounds()[0];
        if (Class.class.isAssignableFrom(bound.getClass()) && Object.class.equals(bound)) {
            return typeVariable.getName();
        }
        return String.format("%s extends %s", typeVariable.getName(), bound.getTypeName());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy