get the package name of the specified class to the specified name and replaces all '.' - Java Reflection

Java examples for Reflection:package

Description

get the package name of the specified class to the specified name and replaces all '.'

Demo Code

/*//from w w  w. j a  v  a  2  s .  co  m
 * Copyright (c) 2015-2016 QuartzDesk.com.
 * Licensed under the MIT license (https://opensource.org/licenses/MIT).
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String name = "java2s.com";
        Class clazz = String.class;
        System.out.println(getAbsoluteResourceName(name, clazz));
    }

    /**
     * Simply prepends the package name of the specified class to the
     * specified name and replaces all '.' with '/'.
     *
     * @param name a resource name.
     * @param clazz a class to be used to resolve the name if it
     * is a relative name.
     * @return the absolute resource name.
     */
    public static String getAbsoluteResourceName(String name, Class<?> clazz) {
        StringBuilder absName = new StringBuilder(clazz.getPackage()
                .getName().replace('.', '/'));
        absName.append('/');
        absName.append(name);
        return absName.toString();
    }
}

Related Tutorials