Class ExternalAnnotationUtil


  • public final class ExternalAnnotationUtil
    extends Object
    Utilities for accessing and manipulating text files that externally define annotations for a given Java type. Files are assumed to be in ".eea format", a textual representation of annotated signatures of members of a given type.
    Since:
    3.11
    Restriction:
    This class is not intended to be instantiated by clients.
    • Field Detail

      • NULLABLE

        public static final char NULLABLE
        Representation of a 'nullable' annotation, independent of the concrete annotation name used in Java sources.
        See Also:
        Constant Field Values
      • NONNULL

        public static final char NONNULL
        Representation of a 'nonnull' annotation, independent of the concrete annotation name used in Java sources.
        See Also:
        Constant Field Values
      • NO_ANNOTATION

        public static final char NO_ANNOTATION
        Represents absence of a null annotation. Useful for removing an existing null annotation. This character is used only internally, it is not part of the Eclipse External Annotation file format.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ExternalAnnotationUtil

        public ExternalAnnotationUtil()
    • Method Detail

      • extractGenericSignature

        public static String extractGenericSignature​(IMethodBinding methodBinding)
        Answer the give method's signature in class file format.
        Parameters:
        methodBinding - binding representing a method
        Returns:
        a signature in class file format
      • extractGenericTypeSignature

        public static String extractGenericTypeSignature​(ITypeBinding type)
        Answer the given types's signature in class file format.
        Parameters:
        type - binding representing a type
        Returns:
        a signature in class file format
      • insertReturnAnnotation

        public static String insertReturnAnnotation​(String methodSignature,
                                                    char annotation,
                                                    ExternalAnnotationUtil.MergeStrategy mergeStrategy)
        Insert an encoded annotation into the given methodSignature affecting its return type.

        This method is suitable for declaration annotations.

        Parameters:
        methodSignature - a method signature in class file format
        annotation - one of NULLABLE and NONNULL.
        mergeStrategy - when passing ExternalAnnotationUtil.MergeStrategy.ADD_ANNOTATIONS this method will refuse to overwrite any existing annotation in the specified location
        Returns:
        the modified method signature, or the original signature if modification would conflict with the given merge strategy.
        Throws:
        IllegalArgumentException - if the method signature is malformed or its return type is not a reference type.
      • insertParameterAnnotation

        public static String insertParameterAnnotation​(String methodSignature,
                                                       int paramIdx,
                                                       char annotation,
                                                       ExternalAnnotationUtil.MergeStrategy mergeStrategy)
        Insert an encoded annotation into the given methodSignature affecting one of its parameters.

        This method is suitable for declaration annotations.

        Parameters:
        methodSignature - a method signature in class file format
        paramIdx - 0-based index of the parameter to which the annotation should be attached
        annotation - one of NULLABLE and NONNULL.
        mergeStrategy - when passing ExternalAnnotationUtil.MergeStrategy.ADD_ANNOTATIONS this method will refuse to overwrite any existing annotation in the specified location
        Returns:
        the modified method signature, or the original signature if modification would conflict with the given merge strategy.
        Throws:
        IllegalArgumentException - if the method signature is malformed or its specified parameter type is not a reference type.
      • getAnnotationFile

        public static IFile getAnnotationFile​(IJavaProject project,
                                              ITypeBinding type,
                                              IProgressMonitor monitor)
                                       throws CoreException
        Answer the external annotation file corresponding to the given type as seen from the given project. Note that manipulation of external annotations is only supported for annotation files in the workspace, and only in directory layout, not from zip files.
        Parameters:
        project - current project that references the given type from a jar file.
        type - the type for which external annotations are sought
        monitor - progress monitor to be passed through into file operations
        Returns:
        a file assumed (but not checked) to be in .eea format. The file may not "exist". Can be null if the given type is not contained in a jar file for which an external annotation path has been defined in the context of the given project.
        Throws:
        CoreException - Signals a problem in accessing any of the relevant elements: the project, the type, the containing jar file and finally the sought annotation file.
      • annotateMember

        public static void annotateMember​(String typeName,
                                          IFile file,
                                          String selector,
                                          String originalSignature,
                                          String annotatedSignature,
                                          ExternalAnnotationUtil.MergeStrategy mergeStrategy,
                                          IProgressMonitor monitor)
                                   throws CoreException,
                                          IOException
        Update the given external annotation file with details regarding annotations of one specific method or field. If the specified member already has external annotations, old and new annotations will be merged, with priorities controlled by the parameter 'mergeStrategy'.

        This method is suitable for declaration annotations and type use annotations.

        Parameters:
        typeName - binary name (slash separated) of the type being annotated
        file - a file assumed to be in .eea format, will be created if it doesn't exist.
        selector - selector of the method or field
        originalSignature - unannotated signature of the member, used for identification
        annotatedSignature - new signatures whose annotations should be superimposed on the member
        mergeStrategy - controls how old and new signatures should be merged
        monitor - progress monitor to be passed through into file operations, or null if no reporting is desired
        Throws:
        CoreException - if access to the file fails
        IOException - if reading file content fails
      • annotateMethodReturnType

        public static void annotateMethodReturnType​(String typeName,
                                                    IFile file,
                                                    String selector,
                                                    String originalSignature,
                                                    String annotatedReturnType,
                                                    ExternalAnnotationUtil.MergeStrategy mergeStrategy,
                                                    IProgressMonitor monitor)
                                             throws CoreException,
                                                    IOException,
                                                    IllegalArgumentException
        Update the given external annotation file with details regarding annotations of the return type of a given method. If the specified method already has external annotations, old and new annotations will be merged, with priorities controlled by the parameter 'mergeStrategy'.

        This method is suitable for declaration annotations and type use annotations.

        Parameters:
        typeName - binary name (slash separated) of the type being annotated
        file - a file assumed to be in .eea format, will be created if it doesn't exist.
        selector - selector of the method
        originalSignature - unannotated signature of the member, used for identification
        annotatedReturnType - signature of the new return type whose annotations should be superimposed on the method
        mergeStrategy - controls how old and new signatures should be merged
        monitor - progress monitor to be passed through into file operations, or null if no reporting is desired
        Throws:
        CoreException - if access to the file fails
        IOException - if reading file content fails
        IllegalArgumentException - if the annotatedReturnType does not structurally match to originalSignature
      • annotateMethodParameterType

        public static void annotateMethodParameterType​(String typeName,
                                                       IFile file,
                                                       String selector,
                                                       String originalSignature,
                                                       String annotatedParameterType,
                                                       int paramIdx,
                                                       ExternalAnnotationUtil.MergeStrategy mergeStrategy,
                                                       IProgressMonitor monitor)
                                                throws CoreException,
                                                       IOException,
                                                       IllegalArgumentException
        Update the given external annotation file with details regarding annotations of a parameter type of a given method. If the specified method already has external annotations, old and new annotations will be merged, with priorities controlled by the parameter 'mergeStrategy'.

        This method is suitable for declaration annotations and type use annotations.

        Parameters:
        typeName - binary name (slash separated) of the type being annotated
        file - a file assumed to be in .eea format, will be created if it doesn't exist.
        selector - selector of the method
        originalSignature - unannotated signature of the member, used for identification
        annotatedParameterType - signature of the new parameter type whose annotations should be superimposed on the method
        paramIdx - 0-based index of the parameter to which the annotation should be attached
        mergeStrategy - controls how old and new signatures should be merged
        monitor - progress monitor to be passed through into file operations, or null if no reporting is desired
        Throws:
        CoreException - if access to the file fails
        IOException - if reading file content fails
        IllegalArgumentException - if the annotatedParameterType does not structurally match to originalSignature
      • getAnnotatedSignature

        public static String getAnnotatedSignature​(String typeName,
                                                   IFile file,
                                                   String selector,
                                                   String originalSignature)
        Retrieve the annotated signature of a specified member as found in the given external annotation file, if any.
        Parameters:
        typeName - fully qualified slash-separated name of the type for which the file defines external annotations
        file - a file assumed to be in .eea format, must not be null, but may not exist
        selector - name of the member whose annotation we are looking for
        originalSignature - the unannotated signature by which the member is identified
        Returns:
        the annotated signature as found in the file, or null.
      • annotateType

        public static String[] annotateType​(String originalSignature,
                                            String annotatedType,
                                            ExternalAnnotationUtil.MergeStrategy mergeStrategy)
        Apply the specified changes on the given type. This method can be used as a dry run without modifying an annotation file.
        Parameters:
        originalSignature - the original type signature, may be annotated already
        annotatedType - a type signature with additional annotations (incl. NO_ANNOTATION).
        mergeStrategy - controls how old and new signatures should be merged
        Returns:
        an array of length four:
        • prefix up-to the changed type
        • original type
        • changed type
        • postfix after the changed type (here: empty string)
      • annotateReturnType

        public static String[] annotateReturnType​(String originalSignature,
                                                  String annotatedType,
                                                  ExternalAnnotationUtil.MergeStrategy mergeStrategy)
        Apply the specified changes on the return type of the given signature. This method can be used as a dry run without modifying an annotation file.
        Parameters:
        originalSignature - the original full signature, may be annotated already
        annotatedType - a type signature with additional annotations (incl. NO_ANNOTATION).
        mergeStrategy - controls how old and new signatures should be merged
        Returns:
        an array of length four:
        • prefix up-to the changed type
        • original type
        • changed type
        • postfix after the changed type (here: empty string)
      • annotateParameterType

        public static String[] annotateParameterType​(String originalSignature,
                                                     String annotatedType,
                                                     int paramIdx,
                                                     ExternalAnnotationUtil.MergeStrategy mergeStrategy)
        Apply the specified changes on a parameter within the given signature. This method can be used as a dry run without modifying an annotation file.
        Parameters:
        originalSignature - the original full signature, may be annotated already
        annotatedType - a type signature with additional annotations (incl. NO_ANNOTATION).
        paramIdx - the index of a parameter to annotated
        mergeStrategy - controls how old and new signatures should be merged
        Returns:
        an array of length four:
        • prefix up-to the changed type
        • original type
        • changed type
        • postfix after the changed type