001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.util.lang;
018
019
020
021
022/**
023 * Utilities for dealing with classes.
024 * 
025 * @author Jonathan Locke
026 */
027public final class Classes
028{
029
030        /**
031         * Gets the name of the given class or null if the class is null.
032         * 
033         * @param c
034         *            The class
035         * @return The class name
036         */
037        public static String name(Class<?> c)
038        {
039                String name = null;
040                if (c != null)
041                {
042                        while (c.isAnonymousClass())
043                        {
044                                c = c.getSuperclass();
045                        }
046                        name = c.getName();
047                }
048
049                return name;
050        }
051
052        /**
053         * Gets the simple name (without the package) of the given class or null if the class is null.
054         *
055         * @param c
056         *            The class
057         * @return The class simple name
058         */
059        public static String simpleName(Class<?> c)
060        {
061                String simpleName = null;
062                if (c != null)
063                {
064                        while (c.isAnonymousClass())
065                        {
066                                c = c.getSuperclass();
067                        }
068                        simpleName = c.getSimpleName();
069                }
070
071                return simpleName;
072        }
073
074        /**
075         * Takes a Class and a relative path to a class and returns any class at that relative path. For
076         * example, if the given Class was java.lang.System and the relative path was "../util/List",
077         * then the java.util.List class would be returned.
078         * 
079         * @param scope
080         *            The package to start at
081         * @param path
082         *            The relative path to the class
083         * @return The class
084         * @throws ClassNotFoundException
085         */
086        public static Class<?> relativeClass(final Class<?> scope, final String path)
087                throws ClassNotFoundException
088        {
089                return Class.forName(Packages.absolutePath(scope, path).replace('/', '.'));
090        }
091
092        /**
093         * Instantiation not allowed
094         */
095        private Classes()
096        {
097        }
098}