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:net.sradonia.i18n.StringBundle.java

public StringBundle(Class<?> clazz, String baseName) {
    this(clazz.getPackage(), baseName);
    setPrefix(clazz);
}

From source file:org.jasig.portlet.courses.dao.xml.Jaxb2CourseSummaryHttpMessageConverter.java

@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
    return (clazz.isAnnotationPresent(XmlRootElement.class) || clazz.isAnnotationPresent(XmlType.class)
            || clazz.getPackage().equals(CourseSummaryWrapper.class.getPackage())) && isSupported(mediaType);
}

From source file:com.urbanmania.spring.beans.factory.config.annotations.PropertyAnnotationAndPlaceholderConfigurer.java

private void processAnnotatedProperties(Properties properties, String name, MutablePropertyValues mpv,
        Class<?> clazz) {
    // TODO support proxies
    if (clazz != null && clazz.getPackage() != null) {
        if (basePackage != null && !clazz.getPackage().getName().startsWith(basePackage)) {
            return;
        }//from   w w w  . j  a  v a2s . c  o m

        log.info("Configuring properties for bean=" + name + "[" + clazz + "]");

        for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(clazz)) {
            if (log.isLoggable(Level.FINE))
                log.fine("examining property=[" + clazz.getName() + "." + property.getName() + "]");
            Method setter = property.getWriteMethod();
            Method getter = property.getReadMethod();
            Property annotation = null;
            if (setter != null && setter.isAnnotationPresent(Property.class)) {
                annotation = setter.getAnnotation(Property.class);
            } else if (setter != null && getter != null && getter.isAnnotationPresent(Property.class)) {
                annotation = getter.getAnnotation(Property.class);
            } else if (setter == null && getter != null && getter.isAnnotationPresent(Property.class)) {
                throwBeanConfigurationException(clazz, property.getName());
            }
            if (annotation != null) {
                setProperty(properties, name, mpv, clazz, property, annotation);
            }
        }

        for (Field field : clazz.getDeclaredFields()) {
            if (log.isLoggable(Level.FINE))
                log.fine("examining field=[" + clazz.getName() + "." + field.getName() + "]");
            if (field.isAnnotationPresent(Property.class)) {
                Property annotation = field.getAnnotation(Property.class);
                PropertyDescriptor property = BeanUtils.getPropertyDescriptor(clazz, field.getName());

                if (property == null || property.getWriteMethod() == null) {
                    throwBeanConfigurationException(clazz, field.getName());
                }

                setProperty(properties, name, mpv, clazz, property, annotation);
            }
        }
    }
}

From source file:org.trpr.platform.integration.impl.xml.XMLTranscoderImpl.java

/**
 * Replacement method that uses JAXB directly instead of via Spring OXM. Provided just as an option
 *///from ww  w.j a v a2s  . c  om
@SuppressWarnings("unchecked")
private <T> T unmarshalUsingJAXB(String xml, Class<T> clazz) throws XMLDataException {
    try {
        javax.xml.bind.JAXBContext context = javax.xml.bind.JAXBContext
                .newInstance(clazz.getPackage().getName());
        javax.xml.bind.Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (javax.xml.bind.JAXBException e) {
        throw new XMLDataException(
                "Error unmarshalling XML. XML:packageName is " + xml + ":" + clazz.getPackage().getName(), e);
    }
}

From source file:org.arsenal.framework.core.loader.ShadowingClassLoader.java

private Class doLoadClass(String name) throws ClassNotFoundException {
    String internalName = StringUtils.replace(name, ".", "/") + ".class";
    InputStream is = this.parentClassLoader.getResourceAsStream(internalName);
    if (is == null) {
        throw new ClassNotFoundException(name);
    }// www.  java  2  s.  c  o  m
    try {

        byte[] bytes = XIOUtils.toByteArray(is);
        bytes = applyTransformers(name, bytes);
        Class cls = defineClass(name, bytes, 0, bytes.length);
        // Additional check for defining the package, if not defined yet.
        if (cls.getPackage() == null) {
            int packageSeparator = name.lastIndexOf('.');
            if (packageSeparator != -1) {
                String packageName = name.substring(0, packageSeparator);
                definePackage(packageName, null, null, null, null, null, null, null);
            }
        }
        this.classCache.put(name, cls);
        return cls;
    } catch (IOException ex) {
        throw new ClassNotFoundException("Cannot load resource for class [" + name + "]", ex);
    }
}

From source file:org.springframework.boot.StartupInfoLogger.java

private String getVersion(final Class<?> source) {
    return getValue(" v", new Callable<Object>() {
        @Override/*  w  ww  .  j a  v  a  2 s.  c om*/
        public Object call() throws Exception {
            return source.getPackage().getImplementationVersion();
        }
    }, "");
}

From source file:net.sradonia.i18n.StringBundle.java

public StringBundle(Class<?> clazz, String baseName, Locale locale) {
    this(clazz.getPackage(), baseName, locale);
    setPrefix(clazz);
}

From source file:net.sradonia.i18n.StringBundle.java

public StringBundle(Class<?> clazz, Locale locale, ClassLoader loader) {
    this(clazz.getPackage(), locale, loader);
    setPrefix(clazz);
}

From source file:alluxio.cli.fs.FileSystemShellUtilsTest.java

@Test
public void loadCommands() {
    Map<String, Command> map = FileSystemShellUtils.loadCommands(mFileSystem);

    String pkgName = Command.class.getPackage().getName();
    Reflections reflections = new Reflections(pkgName);
    Set<Class<? extends Command>> cmdSet = reflections.getSubTypesOf(Command.class);
    for (Map.Entry<String, Command> entry : map.entrySet()) {
        Assert.assertEquals(entry.getValue().getCommandName(), entry.getKey());
        Assert.assertEquals(cmdSet.contains(entry.getValue().getClass()), true);
    }/*from w  w  w . j  a v a2s.c  o m*/

    int expectSize = 0;
    for (Class<? extends Command> cls : cmdSet) {
        if (cls.getPackage().getName().equals(FileSystemShell.class.getPackage().getName() + ".command")
                && !Modifier.isAbstract(cls.getModifiers())) {
            expectSize++;
        }
    }
    Assert.assertEquals(expectSize, map.size());
}

From source file:jatoo.resources.ResourcesTexts.java

/**
 * Creates a {@link ResourcesTexts} object using as base name the package of
 * the specified class. If a resource is not found, the fallback will be used
 * before returning./* w w w. j  a  v  a 2s. c  o m*/
 * 
 * @param clazz
 *          the class that will provide the base name
 * @param fallback
 *          the {@link ResourcesTexts} to be used in case a resource is not
 *          found in this one
 */
public ResourcesTexts(final Class<?> clazz, final ResourcesTexts fallback) {

    logger = LogFactory.getLog(clazz);

    ResourceBundle resourceBundleTmp;

    try {
        resourceBundleTmp = ResourceBundle.getBundle(clazz.getPackage().getName() + ".texts");
    }

    catch (Exception e) {
        resourceBundleTmp = null;
        logger.error("no texts for class: " + clazz.getName() + ", getText(key) will return the key", e);
    }

    this.clazz = clazz;
    this.fallback = fallback;

    this.resourceBundle = resourceBundleTmp;
}