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:ch.entwine.weblounge.common.impl.util.TemplateUtils.java

/**
 * Loads the resource identified by concatenating the package name from
 * <code>clazz</code> and <code>path</code> from the classpath.
 * //  w  w  w.  j  ava  2s .c o m
 * @param path
 *          the path relative to the package name of <code>clazz</code>
 * @param clazz
 *          the class
 * @return the resource
 */
public static String load(String path, Class<?> clazz) {
    if (path == null)
        throw new IllegalArgumentException("path cannot be null");
    if (clazz == null)
        throw new IllegalArgumentException("clazz cannot be null");
    String pkg = "/" + clazz.getPackage().getName().replace('.', '/');
    InputStream is = clazz.getResourceAsStream(UrlUtils.concat(pkg, path));
    InputStreamReader isr = null;
    StringBuffer buf = new StringBuffer();
    if (is != null) {
        try {
            logger.debug("Loading " + path);
            isr = new InputStreamReader(is, Charset.forName("UTF-8"));
            char[] chars = new char[1024];
            int count = 0;
            while ((count = isr.read(chars)) > 0) {
                for (int i = 0; i < count; i++)
                    buf.append(chars[i]);
            }
            return buf.toString();
        } catch (Throwable t) {
            logger.warn("Error reading " + path + ": " + t.getMessage());
        } finally {
            IOUtils.closeQuietly(isr);
            IOUtils.closeQuietly(is);
        }
        logger.debug("Editor support (javascript) loaded");
    } else {
        logger.error("Repository item not found: " + path);
    }
    return null;
}

From source file:org.seedstack.seed.core.utils.BaseClassSpecifications.java

/**
 * Checks if the given class is annotated or meta annotated with the given annotation.
 *
 * @param aClass          the class to check
 * @param annotationClass the requested annotation
 * @return true if the class if annotated or meta annotated, false otherwise
 *//*  w  w  w  .  j  av a2  s  .co  m*/
public static boolean hasAnnotationDeep(Class<?> aClass, Class<? extends Annotation> annotationClass) {
    if (aClass.equals(annotationClass)) {
        return true;
    }

    for (Annotation anno : aClass.getAnnotations()) {
        Class<? extends Annotation> annoClass = anno.annotationType();
        if (!annoClass.getPackage().getName().startsWith("java.lang")
                && hasAnnotationDeep(annoClass, annotationClass)) {
            return true;
        }
    }

    return false;
}

From source file:org.cybercat.automation.annotations.AnnotationBuilder.java

@SuppressWarnings("unchecked")
private final static <T extends IVersionControl> Class<T> versionControlPreprocessor(Class<T> providerzz)
        throws AutomationFrameworkException {
    Reflections refSearch;/*  ww w. j ava  2  s. co m*/
    int version = 0;
    try {
        refSearch = getReflections(providerzz.getPackage().getName());
        version = (int) AutomationMain.getPropertyLong("app.version");
        if (refSearch == null || version < 0)
            return providerzz;
    } catch (Exception e) {
        return providerzz;
    }
    Set<Class<? extends T>> providers = refSearch.getSubTypesOf(providerzz);
    if (providers == null || providers.size() == 0)
        return providerzz;
    T candidate, caught = null;
    for (Class<? extends T> classProvider : providers) {
        try {
            Constructor<T> c = (Constructor<T>) classProvider.getConstructor();
            candidate = c.newInstance();
            if (candidate.getVersion() == version)
                return (Class<T>) candidate.getClass();
            if (caught == null
                    || (candidate.getVersion() < version && caught.getVersion() < candidate.getVersion())) {
                caught = candidate;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return (Class<T>) caught.getClass();
}

From source file:org.dasein.cloud.openstack.nova.os.NovaOpenStack.java

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

    if (pkg.equals("os")) {
        pkg = "";
    } else {/*from w  w  w.jav  a 2s. c om*/
        pkg = pkg + ".";
    }
    return Logger.getLogger("dasein.cloud.nova." + type + "." + pkg + getLastItem(cls.getName()));
}

From source file:org.beanlet.springframework.impl.SpringHelper.java

public static SpringContext getSpringContext(BeanletConfiguration<?> configuration, Element element,
        boolean memberOnly) {
    final SpringContext springContext;
    AnnotationDeclaration<SpringContext> declaration = configuration.getAnnotationDomain()
            .getDeclaration(SpringContext.class);
    SpringContext tmp = declaration.getDeclaredAnnotation(element);
    if (tmp != null) {
        springContext = tmp;/*from  w w  w .j a  v a  2s.com*/
    } else {
        if (element instanceof MethodParameterElement) {
            tmp = configuration.getAnnotationDomain().getDeclaration(SpringContext.class)
                    .getAnnotation(MethodElement.instance(((MethodParameterElement) element).getMethod()));
        } else if (element instanceof ConstructorParameterElement) {
            tmp = configuration.getAnnotationDomain().getDeclaration(SpringContext.class).getAnnotation(
                    ConstructorElement.instance(((ConstructorParameterElement) element).getConstructor()));
        }
        if (tmp != null) {
            springContext = tmp;
        } else if (!memberOnly) {
            Class<?> type = element.getMember().getDeclaringClass();
            tmp = declaration.getDeclaredAnnotation(TypeElement.instance(type));
            if (tmp != null) {
                springContext = tmp;
            } else {
                tmp = declaration.getDeclaredAnnotation(PackageElement.instance(type.getPackage()));
                if (tmp != null) {
                    springContext = tmp;
                } else {
                    springContext = null;
                }
            }
        } else {
            springContext = null;
        }
    }
    return springContext;
}

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

/**
 * Get the package name for a class./* www  .j  a v  a2  s.com*/
 * @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.ajax4jsf.renderkit.compiler.HtmlCompiler.java

/**
 * Compile XML from resource in classpath. Resource always readed
 * as UTF-8 text , to avoid xml declarations.
 * @param base - class of base object.//from w w  w.j av  a2 s .  c  o m
 * @param resource - path to resource ( if not start with / , relative to base object )
 * @return compiled XML, or Exception-generated stub, if parsing error occurs.
 */
public static PreparedTemplate compileResource(Class base, String resource) {
    String path;
    if (resource.startsWith("/")) {
        path = base.getPackage().getName().replace('.', '/') + resource;
    } else {
        path = base.getPackage().getName().replace('.', '/') + "/" + resource;
    }
    return compileResource(path);
}

From source file:org.opennms.core.xml.JaxbUtils.java

public static <T> String getNamespaceForClass(final Class<T> clazz) throws SAXException {
    final XmlSchema schema = clazz.getPackage().getAnnotation(XmlSchema.class);
    if (schema != null) {
        final String namespace = schema.namespace();
        if (namespace != null && !"".equals(namespace)) {
            return namespace;
        }// ww w  .j av  a2  s . c o m
    }
    return null;
}

From source file:org.jboss.confluence.plugin.docbook_tools.docbookimport.DocbookImporter.java

protected static final void printClassInfo(Class<?> clazz, String msg) {
    Package pack = clazz.getPackage();
    StringBuilder sb = new StringBuilder();
    sb.append(msg).append(": ").append(clazz.getName());
    sb.append(", Specification version: ").append(pack.getSpecificationVersion());
    sb.append(", Implementation version: ").append(pack.getImplementationVersion());
    log.info(sb.toString());//from w w w.j av a 2  s  .  com
}

From source file:org.toryt_II.testobject.DefaultTofPlFactory.java

/**
 * <p>The base package name list for <code>forClass</code>. These are the names of
 *   packages that are used as a starting point to look for
 *   <acronym title="Test Object Factory Priority List">TOF PL</acronym>-implementations
 *   for <code>forClass</code>.
 *   This is {@link #getBasePackageNamesList()}, prepended with the
 *   package name of <code>forClass</code>.</p>
 *
 * @pre forClass != null;/*from   ww w .  j  a va 2s .co m*/
 * @result result != null;
 * @result Collections.noNull(result);
 * @result result.get(0).equals(forClass.getPackage().getName());
 * @result (forall int i; (i >= 0) && (i < getBasePackageNamesList().size());
 *              result.get(i + 1).equals(getBasePackageNamesList().get(i)));
 */
public final static List<String> getBasePackageNamesList(Class<?> forClass) {
    assert forClass != null;
    List<String> result = new ArrayList<String>(getBasePackageNamesList().size() + 1);
    result.add(forClass.getPackage().getName());
    result.addAll(getBasePackageNamesList());
    if (_LOG.isDebugEnabled()) {
        _LOG.debug("base package name class for class " + forClass + " is " + result);
    }
    return java.util.Collections.unmodifiableList(result);
}