Java Class Find findClassBase(Class clazz)

Here you can find the source of findClassBase(Class clazz)

Description

Find the classpath for the particular class

License

BSD License

Parameter

Parameter Description
clazz a parameter

Declaration

public static URL findClassBase(Class<?> clazz) 

Method Source Code

//package com.java2s;
/* Chronos - Game Development Toolkit for Java game developers. The
 * original source remains://  w  w w  .j  a  v a2 s . c om
 * 
 * Copyright (c) 2013 Miguel Gonzalez http://my-reality.de
 * 
 * This source is provided under the terms of the BSD License.
 * 
 * Copyright (c) 2013, Chronos
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or 
 * without modification, are permitted provided that the following 
 * conditions are met:
 * 
 *  * Redistributions of source code must retain the above 
 *    copyright notice, this list of conditions and the 
 *    following disclaimer.
 *  * Redistributions in binary form must reproduce the above 
 *    copyright notice, this list of conditions and the following 
 *    disclaimer in the documentation and/or other materials provided 
 *    with the distribution.
 *  * Neither the name of the Chronos/my Reality Development nor the names of 
 *    its contributors may be used to endorse or promote products 
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE.
 */

import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**
     * Find the classpath for the particular class
     * 
     * @param clazz
     * @return
     */
    public static URL findClassBase(Class<?> clazz) {
        String resource = clazz.getName().replace('.', '/') + ".class";
        return findResourceBase(resource, clazz.getClassLoader());
    }

    private static URL findResourceBase(URL url, String baseResource) {
        String urlString = url.toString();
        int idx = urlString.lastIndexOf(baseResource);
        urlString = urlString.substring(0, idx);
        URL deployUrl = null;
        try {
            deployUrl = new URL(urlString);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        return deployUrl;
    }

    /**
     * Find the classpath URL for a specific classpath resource. The classpath
     * URL is extracted from
     * Thread.currentThread().getContextClassLoader().getResource() using the
     * baseResource.
     * 
     * @param baseResource
     * @return
     */
    public static URL findResourceBase(String baseResource) {
        return findResourceBase(baseResource, Thread.currentThread().getContextClassLoader());
    }

    /**
     * Find the classpath URL for a specific classpath resource. The classpath
     * URL is extracted from loader.getResource() using the baseResource.
     * 
     * @param baseResource
     * @param loader
     * @return
     */
    public static URL findResourceBase(String baseResource, ClassLoader loader) {
        URL url = loader.getResource(baseResource);
        return findResourceBase(url, baseResource);
    }
}

Related

  1. findAllClasses(String packageName)
  2. findClass(String packageName, Class[] superClass)
  3. findClasses(String directory, String packageName)
  4. findClasses(String path, String packageName)
  5. findClassesAnnotatedWith(String packageName, Class a)
  6. findClassesAssignableFrom(String packageName, Class assignableFrom)