isAnnotationPresent(Class): This method is available in the java.lang.Package class of Java.
Syntax:
boolean java.lang.Package.isAnnotationPresent(Class<? extends Annotation> annotationClass)
This method takes one argument. This method returns true if an annotation for the specified type is present on this element, else false.
This method is designed primarily for convenient access to marker annotations.
Parameters: One parameter is required for this method.
annotationClass: the Class object corresponding to the annotation type.
Returns: true if an annotation for the specified annotation type is present on this element, else false.
Throws:
1. NullPointerException - if the given annotation class is null.
Approach 1: When no exception
Java
package com.Package;public class PackageisAnnotationPresent {@SuppressWarnings("unchecked")public static void main(String[] args)throws ClassNotFoundException {Class<?> class1 = Class.forName("java.lang.String");Package package1 = class1.getPackage();@SuppressWarnings("rawtypes")Class annotationClass = Class.forName("java.lang.annotation.Annotation");System.out.println(package1.isAnnotationPresent(annotationClass));}}
Output:
false
Approach 2: NullPointerException
Java
package com.Package;public class PackageisAnnotationPresent {@SuppressWarnings("unchecked")public static void main(String[] args)throws ClassNotFoundException {Class<?> class1 = Class.forName("java.lang.String");Package package1 = class1.getPackage();@SuppressWarnings("rawtypes")Class annotationClass = null;System.out.println(package1.isAnnotationPresent(annotationClass));}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.lang.Class.getAnnotation(Class.java:3865) at java.base/java.lang.Package.getAnnotation(Package.java:446) at java.base/java.lang.reflect.AnnotatedElement.isAnnotationPresent(AnnotatedElement.java:292) at java.base/java.lang.Package.isAnnotationPresent(Package.java:456) at com.Package.PackageisAnnotationPresent.main(PackageisAnnotationPresent.java:14)
Some other methods of Package
getAnnotation(Class): This method returns this element's annotation for the specified type if such an annotation is present, else null.
getAnnotations(): This method returns annotations that are present on this element.
getAnnotationsByType(Class): This method returns annotations that are associated with this element.
getDeclaredAnnotation(Class): This method returns this element's annotation for the specified type if such an annotation is directly present, else null. This method ignores inherited annotations.
getDeclaredAnnotations(): This method returns annotations that are directly present on this element. This method ignores inherited annotations.
getDeclaredAnnotationsByType(Class): This method returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.
getImplementationTitle(): This method returns the title of this package.
getImplementationVendor(): This method returns the vendor that implemented this package, null is returned if it is not known.
getImplementationVersion(): This method returns the version of this implementation.
getName(): This method returns the name of this package.
getPackages(): This method returns all of the Packages defined by the caller's class loader and its ancestors.
getSpecificationTitle(): This method returns the title of the specification that this package implements.
getSpecificationVendor(): This method returns the name of the organization, vendor, or company that owns and maintains the specification of the classes that implement this package.
getSpecificationVersion(): This method returns the version number of the specification that this package implements.
hashCode(): This method returns the hash code computed from the package name.
isAnnotationPresent(Class): This method returns true if an annotation for the specified type is present on this element, else false.
isCompatibleWith(String): This method compares this package's specification version with the desired version.
isSealed(): This method returns true if this package is sealed.
isSealed(URL): This method returns true if this package is sealed with respect to the specified code source URL.
toString(): This method returns the string representation of this Package.
No comments:
Post a Comment