IncompleteAnnotationException in Java

java.lang.annotation.IncompleteAnnotationException

Thrown to indicate that a program has attempted to access an element of an annotation type that was added to the annotation type definition after the annotation was compiled (or serialized). This exception will not be thrown if the new element has a default value. This exception can be thrown by the API used to read annotations reflectively.

Declaration

public class IncompleteAnnotationException
extends RuntimeException {
    @java.io.Serial
    private static final long serialVersionUID =
8445097402741811912L;

    private Class<? extends Annotation> annotationType;
    private String elementName;
    public IncompleteAnnotationException(
            Class<? extends Annotation> annotationType,
            String elementName) {
        super(annotationType.getName() + " missing element " +
              elementName.toString());

        this.annotationType = annotationType;
        this.elementName = elementName;
    }

    public Class<? extends Annotation> annotationType() {
        return annotationType;
    }

    public String elementName() {
        return elementName;
    }
}


Methods

1. IncompleteAnnotationException(Class<? extends Annotation> annotationType, String elementName)

java.lang.annotation.IncompleteAnnotationException.IncompleteAnnotationException(Class<? extends Annotation> annotationType, String elementName)

This method takes two arguments. This method constructs an IncompleteAnnotationException to indicate that the named element was missing from the specified annotation type.

Parameters: Two parameters are required for this method.

annotationType: the Class object for the annotation type.

elementName: the name of the missing element.

Throws:

1. NullPointerException - if either parameter is null

Approach 1: When no exception

Java

package com.IncompleteAnnotationException;

import java.lang.annotation.Annotation;
import java.lang.annotation.IncompleteAnnotationException;

public class IncompleteAnnotationException1 {
    public static void main(String[] args)
throws ClassNotFoundException {

        String elementName = "Hello";
        Class<?> clazz =
Class.forName("java.lang.annotation.Annotation");
        @SuppressWarnings("unchecked")
        IncompleteAnnotationException exception =
new IncompleteAnnotationException((Class<?
extends Annotation>) clazz,
                elementName);

        System.out.println(exception);
    }
}

Output:

java.lang.annotation.IncompleteAnnotationException: java.lang.annotation.Annotation missing element Hello


Approach 2: NullPointerException 

Java

package com.IncompleteAnnotationException;

import java.lang.annotation.Annotation;
import java.lang.annotation.IncompleteAnnotationException;

public class IncompleteAnnotationException1 {
    public static void main(String[] args)
throws ClassNotFoundException {

        String elementName = null;
        Class<?> clazz =
Class.forName("java.lang.annotation.Annotation");
        @SuppressWarnings("unchecked")
        IncompleteAnnotationException exception =
new IncompleteAnnotationException((Class<?
extends Annotation>) clazz,
                elementName);

        System.out.println(exception);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toString()" because "elementName" is null at java.base/java.lang.annotation.IncompleteAnnotationException.<init>(IncompleteAnnotationException.java:60) at com.IncompleteAnnotationException.IncompleteAnnotationException1.main(IncompleteAnnotationException1.java:12)


2. annotationType()

Class<? extends Annotation> java.lang.annotation.IncompleteAnnotationException.annotationType()

This method returns the Class object for the annotation type with the missing element.

Parameters: NA

Returns: the Class object for the annotation type with the missing element.

Exceptions: NA

Approach

Java

package com.IncompleteAnnotationException;

import java.lang.annotation.Annotation;
import java.lang.annotation.IncompleteAnnotationException;

public class IncompleteAnnotationExceptionannotationType {
    public static void main(String[] args)
throws ClassNotFoundException {

        String elementName = "Hello";
        Class<?> clazz =
Class.forName("java.lang.annotation.Annotation");
        @SuppressWarnings("unchecked")
        IncompleteAnnotationException exception =
new IncompleteAnnotationException((Class<?
extends Annotation>) clazz,
                elementName);

        System.out.println(exception.annotationType());
    }
}

Output:

interface java.lang.annotation.Annotation


3. elementName()

String java.lang.annotation.IncompleteAnnotationException.elementName()

This method returns the name of the missing element.

Parameters: NA

Returns: the name of the missing element.

Exceptions: NA

Approach

Java

package com.IncompleteAnnotationException;

import java.lang.annotation.Annotation;
import java.lang.annotation.IncompleteAnnotationException;

public class IncompleteAnnotationExceptionelementName {
    public static void main(String[] args)
throws ClassNotFoundException {

        String elementName = "Hello";
        Class<?> clazz =
Class.forName("java.lang.annotation.Annotation");
        @SuppressWarnings("unchecked")
        IncompleteAnnotationException exception =
new IncompleteAnnotationException((Class<?
extends Annotation>) clazz,
                elementName);

        System.out.println(exception.elementName());
    }
}

Output:

Hello


Some other classes/interfaces of java.lang.annotation

Native: Indicates that a field defining a constant value may be referenced from native code.

InheritedIndicates that an annotation type is automatically inherited.

IncompleteAnnotationExceptionThrown to indicate that a program has attempted to access an element of an annotation type that was added to the annotation type definition after the annotation was compiled (or serialized). 

AnnotationFormatErrorThrown when the annotation parser attempts to read an annotation from a class file and determines that the annotation is malformed.

ElementTypeThe constants of this enumerated type provide a simple classification of the syntactic locations where annotations may appear in a Java program.

DocumentedIf the annotation @Documented is present on the declaration of an annotation type A, then any @A annotation on an element is considered part of the element's public contract.

AnnotationTypeMismatchExceptionThrown to indicate that a program has attempted to access an element of an annotation whose type has changed after the annotation was compiled.

AnnotationThe common interface is extended by all annotation types.

RepeatableThe annotation type is java.lang.annotation.Repeatable is used to indicate that the annotation type whose declaration it(meta-)annotates is repeatable.

RetentionIndicates how long annotations with the annotated type are to be retained. If no Retention annotation is present on an annotation-type declaration, the retention policy defaults to RetentionPolicy.CLASS.

RetentionPolicyAnnotation retention policy. The constants of this enumerated type describe the various policies for retaining annotations.

TargetIndicates the contexts in which an annotation type is applicable.

No comments:

Post a Comment