Example usage for java.lang Package getAnnotation

List of usage examples for java.lang Package getAnnotation

Introduction

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

Prototype

public <A extends Annotation> A getAnnotation(Class<A> annotationClass) 

Source Link

Usage

From source file:Main.java

public static void main(String args[]) {

    // create a package object for java.lang package
    Package pack = Package.getPackage("java.io");

    System.out.println(pack.getAnnotation(Deprecated.class));

}

From source file:Main.java

public static String getNamespace(Class<?> clazz) {
    XmlType xmlType = clazz.getAnnotation(XmlType.class);

    String namespace = null;//  ww w .ja va  2 s  .com

    if (xmlType != null) {
        namespace = xmlType.namespace();
        if ("##default".equals(namespace)) {
            namespace = null;
        }
    }

    if (namespace == null) {
        Package itemPackage = clazz.getPackage();
        XmlSchema xmlSchema = itemPackage.getAnnotation(XmlSchema.class);
        if (xmlSchema != null) {
            namespace = xmlSchema.namespace();
        }
    }

    return namespace;
}

From source file:Main.java

private static String getNamespace(final Package targetPackage) {
    String namespaceURI;/*from  w  ww. jav  a2 s .c  o m*/
    if (targetPackage == null) {
        namespaceURI = "";
    } else {
        final XmlSchema xmlSchemaAnnotation = targetPackage.getAnnotation(XmlSchema.class);
        if (xmlSchemaAnnotation == null) {
            namespaceURI = "";
        } else {
            final String packageNamespace = xmlSchemaAnnotation.namespace();
            if (packageNamespace == null || "".equals(packageNamespace)) {
                namespaceURI = "";
            } else {
                namespaceURI = packageNamespace;
            }
        }
    }
    return namespaceURI;
}

From source file:Main.java

private static String getPrefix(final Package targetPackage, String namespaceURI) {
    String prefix;/*  ww  w.  ja  v  a 2  s  .  com*/
    final Map<String, String> namespacePrefixes = new HashMap<String, String>();
    if (targetPackage != null) {
        final XmlSchema xmlSchemaAnnotation = targetPackage.getAnnotation(XmlSchema.class);
        if (xmlSchemaAnnotation != null) {
            for (XmlNs xmlns : xmlSchemaAnnotation.xmlns()) {
                namespacePrefixes.put(xmlns.namespaceURI(), xmlns.prefix());
            }
        }
    }

    prefix = namespacePrefixes.get(namespaceURI);
    return prefix;
}

From source file:org.mitre.stix.STIXSchema.java

/**
 * Return the namespace URI from the package for the class of the object.
 * //from   ww  w.  j a v a  2  s.  co m
 * @param obj
 *            Expects a JAXB model object.
 * @return Name of the XML namespace.
 */
public static String getNamespaceURI(Object obj) {

    Package pkg = obj.getClass().getPackage();

    XmlSchema xmlSchemaAnnotation = pkg.getAnnotation(XmlSchema.class);

    return xmlSchemaAnnotation.namespace();
}

From source file:edu.umich.flowfence.sandbox.ResolvedQM.java

protected static <TAnnot extends Annotation, TMember extends AnnotatedElement & Member> TAnnot getMatchingAnnotation(
        Class<TAnnot> annotClass, TMember member) {
    TAnnot annot = member.getAnnotation(annotClass);
    if (annot != null) {
        return annot;
    }/*from  ww  w  .  j a  v  a  2  s.com*/

    Class<?> declaringClass = member.getDeclaringClass();
    annot = declaringClass.getAnnotation(annotClass);
    if (annot != null) {
        return annot;
    }

    Package declaringPackage = declaringClass.getPackage();
    annot = declaringPackage.getAnnotation(annotClass);
    if (annot != null) {
        return annot;
    }

    return null;
}

From source file:org.alfresco.rest.framework.core.ResourceInspector.java

/**
 * Inspects the resource to determine what api it belongs to.
 * It does this by looking for the WebApi package annotation.
 * //  w w  w  . j  a  v a2  s.  c o m
 * @return Api
 */
public static Api inspectApi(Class<?> resource) {
    Package myPackage = resource.getPackage();
    Annotation annot = myPackage.getAnnotation(WebApi.class);

    if (annot != null) {
        Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(annot);
        String apiName = String.valueOf(annotAttribs.get("name"));
        String apiScope = String.valueOf(annotAttribs.get("scope"));
        String apiVersion = String.valueOf(annotAttribs.get("version"));
        return Api.valueOf(apiName, apiScope, apiVersion);
    }
    return null;
}

From source file:com.mockey.plugin.PluginStore.java

/**
 * Looks to the default plug-in directory location to initialize this store
 *///from  ww  w  .  j  a  v a2 s .  co  m
public void initializeOrUpdateStore() {

    try {
        List<PackageInfo> list = PackageInfoPeerClassFinder.findPackageInfo();
        for (PackageInfo pi : list) {
            for (String className : pi.getClassList()) {

                try {
                    // If we don't have the class
                    Class<?> o = Class.forName(className);
                    if (o == null) {
                        throw new Exception("Class not available");
                    }
                } catch (NoClassDefFoundError ncdfe) {
                    // By Design: gobbling up this error to reduce the
                    // non-needed noise upon startup. If there is a real
                    // issue, then it will bubble up somewhere else.
                } catch (Exception e) {
                    // Explicitly load classes from packages that have
                    // package-info
                    try {
                        ClassLoader.getSystemClassLoader().loadClass(className);
                    } catch (java.lang.NoClassDefFoundError ncdfe) {
                        // By Design: gobbling up this error to reduce the
                        // non-needed noise upon startup. If there is a real
                        // issue, then it will bubble up somewhere else.
                    }
                }

                Package packageItem = Package.getPackage(pi.getName());
                if (null != packageItem.getAnnotation(MockeyRequestInspector.class)) {
                    Class<?> x = doesThisImplementIRequestInspector(className);
                    if (x != null && !this.reqInspectorClassNameList.contains(x)) {
                        this.reqInspectorClassNameList.add(x);
                        logger.debug("Plugin added: " + className);
                    }
                }
            }
        }

    } catch (Exception e) {
        logger.error("Found a Mockey.jar, but unable read mockey jar", e);
    }
}

From source file:org.omnaest.utils.reflection.ReflectionUtils.java

/**
 * Returns the {@link Annotation} instance for any {@link Annotation} declared on a {@link Package} with the given type of
 * {@link Annotation}/*from  w w  w  .  j  a v a2 s  .  c om*/
 * 
 * @param package_
 * @param annotationType
 * @return
 */
public static <A extends Annotation> A annotation(Package package_, Class<? extends A> annotationType) {
    //
    A retval = null;

    //
    if (package_ != null) {
        retval = package_.getAnnotation(annotationType);
    }

    //
    return retval;
}

From source file:org.omnaest.utils.reflection.ReflectionUtils.java

/**
 * Returns a {@link Map} of all {@link Package}s out of the given {@link Package}s {@link Set} annotated with at least one of
 * the given package level {@link Annotation}s including the {@link Annotation} instances related to each {@link Package}. <br>
 * <br>/* w  w  w .  j a  v  a2 s  .  c  o  m*/
 * If no {@link Annotation} or {@link Package} is specified an empty {@link Map} is returned.
 * 
 * @see #annotatedPackageSet(Class...)
 * @param scannedPackageSet
 * @param packageAnnotationTypes
 * @return
 */
@SuppressWarnings("unchecked")
public static <A extends Annotation> Map<Package, Set<A>> annotatedPackageToAnnotationSetMap(
        Set<Package> scannedPackageSet, Class<? extends A>... packageAnnotationTypes) {
    //
    final Map<Package, Set<A>> retmap = MapUtils.initializedMap(new LinkedHashSetFactory<A>());

    //
    if (scannedPackageSet != null && packageAnnotationTypes.length > 0) {
        //
        for (Package package_ : scannedPackageSet) {
            for (Class<? extends Annotation> packageAnnotationType : packageAnnotationTypes) {
                //
                final Annotation annotation = package_.getAnnotation(packageAnnotationType);
                if (annotation != null) {
                    retmap.get(package_).add((A) annotation);
                }
            }
        }
    }

    //
    return retmap;
}