com.github.jeluard.guayaba.lang.Classes.java Source code

Java tutorial

Introduction

Here is the source code for com.github.jeluard.guayaba.lang.Classes.java

Source

/**
 * Copyright 2012 Julien Eluard
 * This project includes software developed by Julien Eluard: https://github.com/jeluard/
 *
 * 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.github.jeluard.guayaba.lang;

import com.google.common.base.Preconditions;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Helper methods for {@link Class}.
 */
public final class Classes {

    private Classes() {
    }

    /**
     * @param type
     * @return all superClasses of specified {@link Class}
     */
    public static List<Class<?>> allSuperClasses(final Class<?> type) {
        Preconditions.checkNotNull(type, "null type");

        final List<Class<?>> superClasses = new LinkedList<Class<?>>();
        Class<?> superClass = type.getSuperclass();
        while (superClass != null) {
            superClasses.add(superClass);
            superClass = superClass.getSuperclass();
        }
        return superClasses;
    }

    /**
     * @param type
     * @return all {@link Field} (irregardless of the visibility) from this class, including those defined in super classes
     */
    public static List<Field> allFields(final Class<?> type) {
        Preconditions.checkNotNull(type, "null type");

        final List<Field> allFields = new LinkedList<Field>();
        allFields.addAll(Arrays.asList(type.getDeclaredFields()));
        for (final Class<?> superClass : Classes.allSuperClasses(type)) {
            allFields.addAll(Arrays.asList(superClass.getDeclaredFields()));
        }
        return allFields;
    }

    /**
     * @param type
     * @param methodName 
     * @param parameterTypes 
     * @return true if specified method is defined in `type`. Ignore {@link Object} default implementation
     */
    public static boolean isMethodDefined(final Class<?> type, final String methodName,
            final Class<?>... parameterTypes) {
        Preconditions.checkNotNull(type, "null type");
        Preconditions.checkNotNull(methodName, "null methodName");
        Preconditions.checkNotNull(parameterTypes, "null parameterTypes");

        Class<?> clazz = type;
        while (!clazz.equals(Object.class)) {
            try {
                clazz.getDeclaredMethod(methodName, parameterTypes);
                return true;
            } catch (Exception e) {
            }
            clazz = clazz.getSuperclass();
        }
        return false;
    }

    /**
     * @param type
     * @return true if specified `type` overrides {@link Object#toString()}
     */
    public static boolean isToStringDefined(final Class<?> type) {
        return Classes.isMethodDefined(type, "toString");
    }

}