Example usage for java.lang Class getPackage

List of usage examples for java.lang Class getPackage

Introduction

In this page you can find the example usage for java.lang Class getPackage.

Prototype

public Package getPackage() 

Source Link

Document

Gets the package of this class.

Usage

From source file:org.jboss.bqt.core.BundleUtil.java

/**
 * Return the {@link BundleUtil} for the class. The bundle must be in the
 * same package or a parent package of the class.
 * //from  w ww. jav a 2 s  . co  m
 * @param clazz
 * @return BundleUtil
 */
public static BundleUtil getBundleUtil(Class<?> clazz) {
    String packageName = clazz.getPackage().getName();

    while (true) {
        // scan up packages until found
        String bundleName = packageName + ".i18n"; //$NON-NLS-1$
        try {
            ResourceBundle bundle = ResourceBundle.getBundle(bundleName, Locale.getDefault(),
                    clazz.getClassLoader());
            return new BundleUtil(packageName, bundleName, bundle);
        } catch (MissingResourceException e) {
            int index = packageName.lastIndexOf('.');
            if (index < 0) {
                throw e;
            }
            packageName = packageName.substring(0, index);
        }
    }
}

From source file:org.dasein.cloud.digitalocean.DigitalOcean.java

static public @Nonnull Logger getLogger(@Nonnull Class<?> cls) {
    String pkg = getLastItem(cls.getPackage().getName());

    if (pkg.equals("digitalocean")) {
        pkg = "";
    } else {//from w w w. j ava 2 s . c  o  m
        pkg = pkg + ".";
    }
    return Logger.getLogger("dasein.cloud.digitalocean.std." + pkg + getLastItem(cls.getName()));
}

From source file:test.org.tradex.camel.TestCaseAppContextBuilder.java

/**
 * Creates a new application context using the file found at <code>ROOT_RESOURCE_DIR + clazz.getPackageName + fileName</code>
 * @param fileName The filename in the calculated directory
 * @param clazz The clazz we're launching the app context for
 * @return the app context//from w w w.  j  a  v a 2s  .c  o  m
 */
public static GenericXmlApplicationContext buildFor(String fileName, Class<?> clazz) {
    if (clazz == null)
        throw new IllegalArgumentException("Passed class was null", new Throwable());
    File springXml = new File(
            ROOT_RESOURCE_DIR + clazz.getPackage().getName().replace('.', '/') + "/" + fileName);
    if (!springXml.canRead()) {
        throw new RuntimeException("Failed to read Spring XML file at [" + springXml + "]", new Throwable());
    }
    return service(new GenericXmlApplicationContext(
            new FileSystemResource[] { new FileSystemResource(springXml.getAbsoluteFile()) }));
}

From source file:org.dasein.cloud.joyent.SmartDataCenter.java

static public @Nonnull Logger getLogger(@Nonnull Class<?> cls, @Nonnull String type) {
    String pkg = getLastItem(cls.getPackage().getName());

    if (pkg.equals("joyent")) {
        pkg = "";
    } else {//from  w  w w .  j  ava  2  s  .c o m
        pkg = pkg + ".";
    }
    return Logger.getLogger("dasein.cloud.joyent." + type + "." + pkg + getLastItem(cls.getName()));
}

From source file:com.us.util.FileHelper.java

/**
 * ???<br/>//w  w w .  j  a  v a 2s  . com
 * <ol>
 * <li>~ [?]</li>
 * <li>root: [Web]</li>
 * </ol>
 * 
 * @param path
 * @return
 */
public static File getFile(String path) {
    if (path.startsWith("~")) {
        if (OSHelper.isMac()) {
            return new File(System.getenv().get("HOME") + path.substring(1));
        }
        return new File(System.getenv().get("USERPROFILE") + path.substring(1));
    }
    if (path.startsWith("classpath:")) {
        Class<?> clazz = new FileHelper().getClass();
        String uripath = clazz.getResource(".").getPath();
        String packepath = clazz.getPackage().getName().replace(".", "/").toString();
        return new File(uripath.replace("/" + packepath, "").substring(1) + path.substring(10));
    }
    return new File(path);
}

From source file:Main.java

public static String getApplicationPath(Class cls) {
    if (cls == null)
        throw new java.lang.IllegalArgumentException("parameter is not null !");
    ClassLoader loader = cls.getClassLoader();
    String clsName = cls.getName() + ".class";
    Package pack = cls.getPackage();
    System.out.println("package name is : " + (pack == null));
    String path = "";
    if (pack != null) {
        String packName = pack.getName();
        if (packName.startsWith("java.") || packName.startsWith("javax."))
            throw new java.lang.IllegalArgumentException("This is system class");
        clsName = clsName.substring(packName.length() + 1);
        if (packName.indexOf(".") < 0)
            path = packName + "/";
        else {//w w  w. j a v a 2  s. c o  m
            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) + "/";
        }
    }
    java.net.URL url = loader.getResource(path + clsName);
    String realPath = url.getPath();
    int pos = realPath.indexOf("file:");
    if (pos > -1)
        realPath = realPath.substring(pos + 5);
    pos = realPath.indexOf(path + clsName);
    realPath = realPath.substring(0, pos - 1);
    if (realPath.endsWith("!"))
        realPath = realPath.substring(0, realPath.lastIndexOf("/"));
    try {
        realPath = java.net.URLDecoder.decode(realPath, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return realPath;
}

From source file:org.baswell.routes.ResponseTypeMapper.java

static Pair<ResponseStringWriteStrategy, String> mapResponseStringWriteStrategy(Class returnType,
        Set<MediaType> respondToMedia, String contentType, AvailableLibraries availableLibraries) {
    String returnClassName = returnType.getCanonicalName();
    String returnTypePackage = returnType.getPackage().getName();

    MediaType mediaType = contentType == null ? null : MediaType.findFromMimeType(contentType);
    if ((mediaType == null) && (respondToMedia != null) && (respondToMedia.size() == 1)) {
        mediaType = respondToMedia.iterator().next();
    }//ww w .  j a  v  a2s . com

    if (returnTypePackage.startsWith("org.json")) {
        return pair(ResponseStringWriteStrategy.TO_STRING, MIMETypes.JSON);
    } else if (returnTypePackage.startsWith("org.w3c.dom")) {
        return pair(ResponseStringWriteStrategy.W3C_NODE, MIMETypes.XML);
    } else if (returnClassName.equals("org.jdom2.Document")) {
        return pair(ResponseStringWriteStrategy.JDOM2_DOCUMENT, MIMETypes.XML);
    } else if (returnClassName.equals("org.jdom2.Element")) {
        return pair(ResponseStringWriteStrategy.JDOM2_ELEMENT, MIMETypes.XML);
    } else if (returnType.getAnnotation(XmlRootElement.class) != null) {
        return pair(ResponseStringWriteStrategy.JAXB, MIMETypes.XML);
    } else if (classImplementsInterface(returnType, CharSequence.class)) {
        return pair(ResponseStringWriteStrategy.TO_STRING, contentType);
    } else if ((((mediaType != null) && (mediaType == MediaType.JSON))) && availableLibraries.gsonAvailable()) {
        return pair(ResponseStringWriteStrategy.GSON, MIMETypes.JSON);
    } else if ((((mediaType != null) && (mediaType == MediaType.JSON)))
            && availableLibraries.jacksonAvailable()) {
        return pair(ResponseStringWriteStrategy.JACKSON, MIMETypes.JSON);
    } else {
        return null;
    }
}

From source file:org.springframework.web.method.ControllerAdviceBean.java

private static List<Package> initBasePackagesFromBeanType(Class<?> beanType, ControllerAdvice annotation) {
    List<Package> basePackages = new ArrayList<Package>();
    List<String> basePackageNames = new ArrayList<String>();
    basePackageNames.addAll(Arrays.asList(annotation.value()));
    basePackageNames.addAll(Arrays.asList(annotation.basePackages()));
    for (String pkgName : basePackageNames) {
        if (StringUtils.hasText(pkgName)) {
            Package pkg = Package.getPackage(pkgName);
            if (pkg != null) {
                basePackages.add(pkg);//  w w  w .  j ava2 s .c om
            } else {
                logger.warn("Package [" + pkgName + "] was not found, see [" + beanType.getName() + "]");
            }
        }
    }
    for (Class<?> markerClass : annotation.basePackageClasses()) {
        Package pack = markerClass.getPackage();
        if (pack != null) {
            basePackages.add(pack);
        } else {
            logger.warn("Package was not found for class [" + markerClass.getName() + "], see ["
                    + beanType.getName() + "]");
        }
    }
    return basePackages;
}

From source file:org.apache.commons.vfs2.example.Shell.java

private static String getVersion(Class<?> cls) {
    try {/*from   www .  j av a  2s.  com*/
        return cls.getPackage().getImplementationVersion();
    } catch (Exception ignored) {
        return "N/A";
    }
}

From source file:org.dasein.cloud.tier3.Tier3.java

static public @Nonnull Logger getLogger(@Nonnull Class<?> cls) {
    String pkg = getLastItem(cls.getPackage().getName());

    if (pkg.equals("centurylink")) {
        pkg = "";
    } else {//from  w ww . java2 s .co m
        pkg = pkg + ".";
    }
    return Logger.getLogger("dasein.cloud.centurylink.std." + pkg + getLastItem(cls.getName()));
}