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:com.opensymphony.xwork2.util.AnnotationUtils.java

/**
 * Returns the annotation on the given class or the package of the class. This searchs up the
 * class hierarchy and the package hierarchy for the closest match.
 *
 * @param   <T> class type// www. j  a  va 2s . co m
 * @param   clazz The class to search for the annotation.
 * @param   annotationClass The Class of the annotation.
 * @return  The annotation or null.
 */
public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationClass) {
    T ann = clazz.getAnnotation(annotationClass);
    while (ann == null && clazz != null) {
        ann = clazz.getAnnotation(annotationClass);
        if (ann == null) {
            ann = clazz.getPackage().getAnnotation(annotationClass);
        }
        if (ann == null) {
            clazz = clazz.getSuperclass();
            if (clazz != null) {
                ann = clazz.getAnnotation(annotationClass);
            }
        }
    }

    return ann;
}

From source file:org.dasein.cloud.azure.Azure.java

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

    if (pkg.equals("azure")) {
        pkg = "";
    } else {/*  www .j a  v  a 2s  . c om*/
        pkg = pkg + ".";
    }
    return Logger.getLogger("dasein.cloud.azure.std." + pkg + getLastItem(cls.getName()));
}

From source file:org.springframework.util.ClassUtils.java

/**
 * Given an input class object, return a string which consists of the
 * class's package name as a pathname, i.e., all dots ('.') are replaced by
 * slashes ('/'). Neither a leading nor trailing slash is added. The result
 * could be concatenated with a slash and the name of a resource, and fed
 * directly to ClassLoader.getResource(). For it to be fed to Class.getResource,
 * a leading slash would also have to be prepended to the return value.
 * @param clazz the input class. A null value or the default (empty) package
 * will result in an empty string ("") being returned.
 * @return a path which represents the package name
 * @see ClassLoader#getResource/*from   w  w  w. j  a  v a 2 s.c o m*/
 * @see Class#getResource
 */
public static String classPackageAsResourcePath(Class clazz) {
    if (clazz == null || clazz.getPackage() == null) {
        return "";
    }
    return clazz.getPackage().getName().replace('.', '/');
}

From source file:org.gvnix.support.MessageBundleUtils.java

/**
 * Copy properties associated with the given class to the message bundle of
 * given language.//from  w w w .j  a  va  2 s.  c om
 * <p/>
 * Note that properties to add are taken from messages[_xx].properties files
 * and added to messages[_xx].properties in the destination project.
 * <p/>
 * <strong>This method doesn't check if messages[_xx].properties file exist
 * in the add-on invoking it</strong>
 * 
 * @param language Language locale as string (en, es, ca, ...)
 * @param invokingClass Class of the Add-on invoking this method. It's
 *        needed in order to load local resources
 * @param propFileOperations
 * @param projectOperations
 * @param fileManager
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void addPropertiesToMessageBundle(String language, Class invokingClass,
        PropFileOperations propFileOperations, ProjectOperations projectOperations, FileManager fileManager) {
    Properties properties = new Properties();
    LogicalPath webappPath = WebProjectUtils.getWebappPath(projectOperations);

    String sourcePropertyFile = "/".concat(invokingClass.getPackage().getName()).replace('.', '/');

    // Take "en" as default language
    String targetFilePath = "/WEB-INF/i18n/messages.properties";
    String targetFile = projectOperations.getPathResolver().getIdentifier(webappPath, targetFilePath);

    try {
        if (language.equals("en")) {
            sourcePropertyFile = sourcePropertyFile.concat("/messages.properties");
            properties.load(invokingClass.getResourceAsStream(sourcePropertyFile));
        } else {
            targetFilePath = "/WEB-INF/i18n/messages_".concat(language).concat(".properties");
            targetFile = projectOperations.getPathResolver().getIdentifier(webappPath, targetFilePath);

            sourcePropertyFile = sourcePropertyFile.concat("/messages_".concat(language).concat(".properties"));
            properties.load(invokingClass.getResourceAsStream(sourcePropertyFile));
        }

        if (fileManager.exists(targetFile)) {
            propFileOperations.addProperties(webappPath, targetFilePath,
                    new HashMap<String, String>((Map) properties), true, true);
        } else {
            logger.warning(targetFile.concat(" file doesn't exist in project."));
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE,
                "Message properties for language \"".concat(language).concat("\" can't be loaded"));
    }
}

From source file:tain.kr.test.vfs.v01.Shell.java

private static String getVersion(Class<?> cls) {

    try {//from w  w  w.j a  v  a  2 s  . com
        return cls.getPackage().getImplementationVersion();
    } catch (Exception ignored) {
        return "N/A";
    }
}

From source file:com.github.dozermapper.core.util.MappingUtils.java

public static String getClassNameWithoutPackage(Class<?> clazz) { // TODO Replace with Apache implementation
    Package pckage = clazz.getPackage();
    int pckageIndex = 0;
    if (pckage != null) {
        pckageIndex = pckage.getName().length() + 1;
    }/*from ww w .  j  av a 2 s .co  m*/
    return clazz.getName().substring(pckageIndex);
}

From source file:gov.nih.nci.ncicb.tcga.dcc.common.jaxb.JAXBUtil.java

/**
 * Performs the same function as {@link JAXBUtil#unmarshal(File, String, boolean, boolean)}, but uses the
 * provided {@link Class} object to retrieve the package namespace of the JAXB classes.
 *
 * @param xmlFile                  - a {@link File} object representing the XML file to unmarshalled
 * @param jaxbClass                - the {@link Class} object that represents a JAXB generated class type
 * @param filterMetaDataNamespaces - boolean that specifies whether or not to filter meta-data
 *                                 namespaces using the {@link MetaDataXMLNamespaceFilter}
 * @param validate                 - boolean indicating weather or not the XML should be validated against a schema
 * @throws UnmarshalException if an error occurs during unmarshalling
 *//*  w  ww.  j a  v  a  2  s  . co m*/
public static UnmarshalResult unmarshal(final File xmlFile, final Class<?> jaxbClass,
        final boolean filterMetaDataNamespaces, final boolean validate) throws UnmarshalException {

    return unmarshal(xmlFile, jaxbClass.getPackage().getName(), filterMetaDataNamespaces, validate);
}

From source file:com.netxforge.oss2.core.xml.JaxbUtils.java

public static <T> XMLFilter getXMLFilterForClass(final Class<T> clazz) throws SAXException {
    final XMLFilter filter;
    final XmlSchema schema = clazz.getPackage().getAnnotation(XmlSchema.class);
    if (schema != null) {
        final String namespace = schema.namespace();
        if (namespace != null && !"".equals(namespace)) {
            LogUtils.tracef(clazz, "found namespace %s for class %s", namespace, clazz);
            filter = new SimpleNamespaceFilter(namespace, true);
        } else {// w  w  w.  ja  va 2  s.  c  o m
            filter = new SimpleNamespaceFilter("", false);
        }
    } else {
        filter = new SimpleNamespaceFilter("", false);
    }

    final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    filter.setParent(xmlReader);
    return filter;
}

From source file:org.dasein.cloud.azure.Azure.java

static public @Nonnull Logger getWireLogger(@Nonnull Class<?> cls) {
    return Logger.getLogger("dasein.cloud.azure.wire." + getLastItem(cls.getPackage().getName()) + "."
            + getLastItem(cls.getName()));
}

From source file:org.evosuite.utils.Utils.java

/**
 * Get package name from the class. Sometimes this maybe tricky, since
 * clazz.getPackage() could return null/*  www .ja  v a 2 s  .c o  m*/
 *
 * @param clazz
 *            - class which package should be determined
 * @return package name of the class
 */
public static String getPackageName(Class<?> clazz) {
    String packageName = "";
    if (clazz.getPackage() != null) {
        packageName = clazz.getPackage().getName();
    } else {
        String name = clazz.getName();
        if (name.contains("."))
            packageName = name.substring(0, name.lastIndexOf("."));
        else
            packageName = "";
    }
    return packageName;
}