Returns the given class (val) as itself or nulls if it it is equal to the default Class. - Java Reflection

Java examples for Reflection:Class

Description

Returns the given class (val) as itself or nulls if it it is equal to the default Class.

Demo Code

/**//from ww  w .  ja  v a2  s  .com
 * Copyright (C) 2011 Tom Spencer <thegaffer@tpspencer.com>
 *
 * This file is part of TAL.
 *
 * TAL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TAL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TAL. If not, see <http://www.gnu.org/licenses/>.
 *
 * Note on dates: Year above is the year this code was built. This
 * project first created in 2008. Code was created between these two
 * years inclusive.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class val = String.class;
        Class defaultClass = String.class;
        System.out.println(getAnnotationClass(val, defaultClass));
    }

    /**
     * Returns the given class (val) as itself or nulls if it
     * it is equal to the defaultClass. This is because you
     * cannot have null as default, so this method helps deal
     * with that.
     * 
     * @param val The class
     * @param defaultClass The class that if above is equal to should null the result
     * @return The val, or null if it is equal to default
     */
    public static Class<?> getAnnotationClass(Class<?> val,
            Class<?> defaultClass) {
        if (val.equals(defaultClass))
            return null;
        return val;
    }
}

Related Tutorials