Example usage for java.lang Package getName

List of usage examples for java.lang Package getName

Introduction

In this page you can find the example usage for java.lang Package getName.

Prototype

public String getName() 

Source Link

Document

Return the name of this package.

Usage

From source file:com.eryansky.common.utils.io.FileUtil.java

@SuppressWarnings("rawtypes")
public static String getAppPath(Class cls) {
    // ??// ww w .  ja va  2  s  .  c  o m
    if (cls == null)
        throw new java.lang.IllegalArgumentException("???");
    ClassLoader loader = cls.getClassLoader();
    // ????
    String clsName = cls.getName() + ".class";
    // ?
    Package pack = cls.getPackage();
    String path = "";
    // ?????
    if (pack != null) {
        String packName = pack.getName();
        // ??JavaJDK
        if (packName.startsWith("java.") || packName.startsWith("javax."))
            throw new java.lang.IllegalArgumentException("????");
        // ??????
        clsName = clsName.substring(packName.length() + 1);
        // ?????????
        if (packName.indexOf(".") < 0)
            path = packName + "/";
        else {// ???????
            int start = 0, end = 0;
            end = packName.indexOf(".");
            while (end != -1) {
                path = path + packName.substring(start, end) + "/";
                start = end + 1;
                end = packName.indexOf(".", start);
            }
            path = path + packName.substring(start) + "/";
        }
    }
    // ClassLoadergetResource????
    java.net.URL url = loader.getResource(path + clsName);
    // URL??
    String realPath = url.getPath();
    // ?????"file:"
    int pos = realPath.indexOf("file:");
    if (pos > -1)
        realPath = realPath.substring(pos + 5);
    // ????
    pos = realPath.indexOf(path + clsName);
    realPath = realPath.substring(0, pos - 1);
    // JARJAR??
    if (realPath.endsWith("!"))
        realPath = realPath.substring(0, realPath.lastIndexOf("/"));
    /*------------------------------------------------------------ 
     ClassLoadergetResourceutf-8?? 
      ??? 
      URLDecoderdecode? 
      ? 
    -------------------------------------------------------------*/
    try {
        realPath = java.net.URLDecoder.decode(realPath, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    System.out.println("realPath----->" + realPath);
    return realPath;
}

From source file:org.dhatim.util.ClassUtil.java

public static String toFilePath(Package aPackage) {
    return "/" + aPackage.getName().replace('.', '/');
}

From source file:com.eryansky.common.utils.io.FileUtils.java

@SuppressWarnings("rawtypes")
public static String getAppPath(Class cls) {
    // ??/*  ww w.j a v a2 s . co  m*/
    if (cls == null)
        throw new IllegalArgumentException("???");
    ClassLoader loader = cls.getClassLoader();
    // ????
    String clsName = cls.getName() + ".class";
    // ?
    Package pack = cls.getPackage();
    String path = "";
    // ?????
    if (pack != null) {
        String packName = pack.getName();
        // ??JavaJDK
        if (packName.startsWith("java.") || packName.startsWith("javax."))
            throw new IllegalArgumentException("????");
        // ??????
        clsName = clsName.substring(packName.length() + 1);
        // ?????????
        if (packName.indexOf(".") < 0)
            path = packName + "/";
        else {// ???????
            int start = 0, end = 0;
            end = packName.indexOf(".");
            while (end != -1) {
                path = path + packName.substring(start, end) + "/";
                start = end + 1;
                end = packName.indexOf(".", start);
            }
            path = path + packName.substring(start) + "/";
        }
    }
    // ClassLoadergetResource????
    java.net.URL url = loader.getResource(path + clsName);
    // URL??
    String realPath = url.getPath();
    // ?????"file:"
    int pos = realPath.indexOf("file:");
    if (pos > -1)
        realPath = realPath.substring(pos + 5);
    // ????
    pos = realPath.indexOf(path + clsName);
    realPath = realPath.substring(0, pos - 1);
    // JARJAR??
    if (realPath.endsWith("!"))
        realPath = realPath.substring(0, realPath.lastIndexOf("/"));
    /*------------------------------------------------------------ 
     ClassLoadergetResourceutf-8?? 
      ??? 
      URLDecoderdecode? 
      ? 
    -------------------------------------------------------------*/
    try {
        realPath = java.net.URLDecoder.decode(realPath, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    System.out.println("realPath----->" + realPath);
    return realPath;
}

From source file:org.apache.tapestry5.cdi.test.InjectTest.java

/**
 * Generate a web archive for arquillian
 * @return a WebArchive object//from w  w w  .  j  av a2  s .c om
 */
@Deployment(testable = false)
public static WebArchive war() {

    File indexPage = new File(toPath(Index.class.getName()));
    Package rootPackage = toPackage(indexPage.getParentFile().getParent());
    WebArchive war = ShrinkWrap.create(WebArchive.class, "inject.war")
            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
            .addAsWebInfResource(new StringAsset(createWebXml()), "web.xml")
            // our test classes (src/test) = the webapp
            .addPackages(true, rootPackage)
            // tapestry dependencies, for real project put it in a helper
            // class: new TapestryArchive(name)...
            .addAsLibraries(JarLocation.jarLocation(HttpClient.class))
            .addAsLibraries(JarLocation.jarLocation(Lexer.class))
            .addAsLibraries(JarLocation.jarLocation(StringEncoder.class))
            .addAsLibraries(JarLocation.jarLocation(IOCConstants.class))
            .addAsLibraries(JarLocation.jarLocation(PlasticClass.class))
            .addAsLibraries(JarLocation.jarLocation(JSONArray.class))
            .addAsLibraries(JarLocation.jarLocation(InjectService.class))
            .addAsLibraries(JarLocation.jarLocation(Mapper.class))
            .addAsLibraries(JarLocation.jarLocation(TapestryModule.class))
            // for jbossAS7 server
            .addAsLibraries(JarLocation.jarLocation(org.jboss.shrinkwrap.api.asset.Asset.class))
            // for Glassfish container
            .addAsLibraries(JarLocation.jarLocation(org.slf4j.Logger.class));

    // our test resources (src/test) = the webapp
    // add template resources from package "pages"
    Package pagePackage = toPackage(indexPage.getParent());
    File pageDirectory = new File(TEST_RESOURCES_ROOT_PATH + toPath(pagePackage.getName()));
    for (String template : pageDirectory.list()) {
        war.addAsResource(pagePackage, template);
    }

    // add template resources from package "components"
    Package componentPackage = DumbComponent.class.getPackage();
    File componentDirectory = new File(TEST_RESOURCES_ROOT_PATH + toPath(componentPackage.getName()));
    for (String template : componentDirectory.list()) {
        war.addAsResource(componentPackage, template);
    }

    // add tapestry-cdi module to the archive
    war.addAsLibraries(createJarArchive("tapestry-cdi.jar"));
    war.addAsWebInfResource(new File(TEST_RESOURCES_ROOT_PATH + "log4j.xml"));
    return war;
}

From source file:net.darkmist.alib.res.PkgRes.java

/**
 * Get the package name for a class./*from   ww  w .java2 s.c o  m*/
 * @param cls The class to get the package name for.
 * @return the class's pacakge name.
 * @throws NullPointerException if cls is null.
 */
public static String getPackageName(Class<?> cls) {
    Package pkg;
    String str;
    int pos;

    if (cls == null)
        throw new NullPointerException("cls is null");
    if ((pkg = cls.getPackage()) != null)
        return pkg.getName();
    str = cls.getName();
    if ((pos = str.lastIndexOf('.')) >= 0)
        return str.substring(0, pos);
    return ""; // default package
}

From source file:org.dcm4che.tool.common.CLIUtils.java

public static CommandLine parseComandLine(String[] args, Options opts, ResourceBundle rb2, Class<?> clazz)
        throws ParseException {
    CommandLineParser parser = new PosixParser();
    CommandLine cl = parser.parse(opts, args);
    if (cl.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(rb2.getString("usage"), rb2.getString("description"), opts,
                rb2.getString("example"));
        System.exit(0);//from  w  w  w  .  j  a v a 2  s  .  co  m
    }
    if (cl.hasOption("V")) {
        Package p = clazz.getPackage();
        String s = p.getName();
        System.out.println(s.substring(s.lastIndexOf('.') + 1) + ": " + p.getImplementationVersion());
        System.exit(0);
    }
    return cl;
}

From source file:com.yahoo.elide.core.EntityDictionary.java

private static Package getParentPackage(Package pkg) {
    String name = pkg.getName();
    int idx = name.lastIndexOf('.');
    return idx == -1 ? null : Package.getPackage(name.substring(0, idx));
}

From source file:org.dhatim.util.ClassUtil.java

/**
 * Get the specified resource as a stream.
 *
 * @param resourceName/*w w  w. j av a 2  s.  c om*/
 *            The name of the class to load.
 * @param caller
 *            The class of the caller.
 * @return The input stream for the resource or null if not found.
 */
public static InputStream getResourceAsStream(final String resourceName, final Class caller) {
    final String resource;

    if (!resourceName.startsWith("/")) {
        final Package callerPackage = caller.getPackage();
        if (callerPackage != null) {
            resource = callerPackage.getName().replace('.', '/') + '/' + resourceName;
        } else {
            resource = resourceName;
        }

        return getResourceAsStream(resource, caller.getClassLoader());
    } else {
        return getResourceAsStream(resourceName, caller.getClassLoader());
    }
}

From source file:com.virtualparadigm.packman.processor.JPackageManagerBU.java

private static void addInstalledPackage(Package installPackage) {
    installPackageMap.put(installPackage.getName(), installPackage);
}

From source file:org.apdplat.qa.util.Tools.java

public static String getAppPath(Class cls) {
    // ??//ww w  .j a  v  a2 s.  c  om
    if (cls == null) {
        throw new IllegalArgumentException("???");
    }
    ClassLoader loader = cls.getClassLoader();
    // ????
    String clsName = cls.getName() + ".class";
    // ?
    Package pack = cls.getPackage();
    String path = "";
    // ?????
    if (pack != null) {
        String packName = pack.getName();
        // ??JavaJDK
        if (packName.startsWith("java.") || packName.startsWith("javax.")) {
            throw new IllegalArgumentException("????");
        }
        // ??????
        clsName = clsName.substring(packName.length() + 1);
        // ?????????
        if (packName.indexOf(".") < 0) {
            path = packName + "/";
        } else {
            // ???????
            int start = 0, end = 0;
            end = packName.indexOf(".");
            while (end != -1) {
                path = path + packName.substring(start, end) + "/";
                start = end + 1;
                end = packName.indexOf(".", start);
            }
            path = path + packName.substring(start) + "/";
        }
    }
    // ClassLoadergetResource????
    URL url = loader.getResource(path + clsName);
    // URL??
    String realPath = url.getPath();
    // ?????"file:"
    int pos = realPath.indexOf("file:");
    if (pos > -1) {
        realPath = realPath.substring(pos + 5);
    }
    // ????
    pos = realPath.indexOf(path + clsName);
    realPath = realPath.substring(0, pos - 1);
    // JARJAR??
    if (realPath.endsWith("!")) {
        realPath = realPath.substring(0, realPath.lastIndexOf("/"));
    }
    /*------------------------------------------------------------  
     ClassLoadergetResourceutf-8??  
     ???  
     URLDecoderdecode?  
     ?  
     -------------------------------------------------------------*/
    try {
        realPath = URLDecoder.decode(realPath, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return realPath;
}