Package org.eclipse.jdt.core.util
Class ExternalAnnotationUtil
java.lang.Object
org.eclipse.jdt.core.util.ExternalAnnotationUtil
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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Strategy for merging a new signature with an existing (possibly annotated) signature. -
Field Summary
Modifier and TypeFieldDescriptionstatic final char
Represents absence of a null annotation.static final char
Representation of a 'nonnull' annotation, independent of the concrete annotation name used in Java sources.static final char
Representation of a 'nullable' annotation, independent of the concrete annotation name used in Java sources. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
annotateMember
(String typeName, org.eclipse.core.resources.IFile file, String selector, String originalSignature, String annotatedSignature, ExternalAnnotationUtil.MergeStrategy mergeStrategy, org.eclipse.core.runtime.IProgressMonitor monitor) Update the given external annotation file with details regarding annotations of one specific method or field.static void
annotateMethodParameterType
(String typeName, org.eclipse.core.resources.IFile file, String selector, String originalSignature, String annotatedParameterType, int paramIdx, ExternalAnnotationUtil.MergeStrategy mergeStrategy, org.eclipse.core.runtime.IProgressMonitor monitor) Update the given external annotation file with details regarding annotations of a parameter type of a given method.static void
annotateMethodReturnType
(String typeName, org.eclipse.core.resources.IFile file, String selector, String originalSignature, String annotatedReturnType, ExternalAnnotationUtil.MergeStrategy mergeStrategy, org.eclipse.core.runtime.IProgressMonitor monitor) Update the given external annotation file with details regarding annotations of the return type of a given method.static void
annotateMethodTypeParameter
(String typeName, org.eclipse.core.resources.IFile file, String selector, String originalSignature, String annotatedTypeParameter, int rank, ExternalAnnotationUtil.MergeStrategy mergeStrategy, org.eclipse.core.runtime.IProgressMonitor monitor) Update the given external annotation file with details regarding annotations of a type parameter of a given method.static String[]
annotateParameterType
(String originalSignature, String annotatedType, int paramIdx, ExternalAnnotationUtil.MergeStrategy mergeStrategy) Apply the specified changes on a parameter within the given signature.static String[]
annotateReturnType
(String originalSignature, String annotatedType, ExternalAnnotationUtil.MergeStrategy mergeStrategy) Apply the specified changes on the return type of the given signature.static String[]
annotateType
(String originalSignature, String annotatedType, ExternalAnnotationUtil.MergeStrategy mergeStrategy) Apply the specified changes on the given type.static String[]
annotateTypeParameter
(String originalSignature, String annotatedType, int rank, ExternalAnnotationUtil.MergeStrategy mergeStrategy) Apply the specified changes on a type parameter within the given signature.static void
annotateTypeTypeParameter
(String typeName, org.eclipse.core.resources.IFile file, String originalSignature, String annotatedTypeParameter, int rank, ExternalAnnotationUtil.MergeStrategy mergeStrategy, org.eclipse.core.runtime.IProgressMonitor monitor) Update the given external annotation file with details regarding annotations of a type parameter of the type itself.static String
extractGenericSignature
(IMethodBinding methodBinding) Answer the give method's signature in class file format.static String
Answer the signature of all type parameters of this type in class file format.static String
Answer the given types's signature in class file format.static String
getAnnotatedSignature
(String typeName, org.eclipse.core.resources.IFile file, String selector, String originalSignature) Retrieve the annotated signature of a specified member as found in the given external annotation file, if any.static org.eclipse.core.resources.IFile
getAnnotationFile
(IJavaProject project, ITypeBinding type, org.eclipse.core.runtime.IProgressMonitor monitor) Answer the external annotation file corresponding to the given type as seen from the given project.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.static String
insertReturnAnnotation
(String methodSignature, char annotation, ExternalAnnotationUtil.MergeStrategy mergeStrategy) Insert an encoded annotation into the given methodSignature affecting its return type.
-
Field Details
-
NULLABLE
public static final char NULLABLERepresentation of a 'nullable' annotation, independent of the concrete annotation name used in Java sources.- See Also:
-
NONNULL
public static final char NONNULLRepresentation of a 'nonnull' annotation, independent of the concrete annotation name used in Java sources.- See Also:
-
NO_ANNOTATION
public static final char NO_ANNOTATIONRepresents 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:
-
-
Constructor Details
-
ExternalAnnotationUtil
public ExternalAnnotationUtil()
-
-
Method Details
-
extractGenericSignature
Answer the give method's signature in class file format.- Parameters:
methodBinding
- binding representing a method- Returns:
- a signature in class file format
-
extractGenericTypeSignature
Answer the given types's signature in class file format.- Parameters:
type
- binding representing a type- Returns:
- a signature in class file format
-
extractGenericTypeParametersSignature
Answer the signature of all type parameters of this type in class file format.- Parameters:
type
- binding representing a type, required to have type parameters- Returns:
- a signature in class file format
- Since:
- 3.19
-
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 formatannotation
- one ofNULLABLE
andNONNULL
.mergeStrategy
- when passingExternalAnnotationUtil.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 formatparamIdx
- 0-based index of the parameter to which the annotation should be attachedannotation
- one ofNULLABLE
andNONNULL
.mergeStrategy
- when passingExternalAnnotationUtil.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 org.eclipse.core.resources.IFile getAnnotationFile(IJavaProject project, ITypeBinding type, org.eclipse.core.runtime.IProgressMonitor monitor) throws org.eclipse.core.runtime.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 soughtmonitor
- 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:
org.eclipse.core.runtime.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, org.eclipse.core.resources.IFile file, String selector, String originalSignature, String annotatedSignature, ExternalAnnotationUtil.MergeStrategy mergeStrategy, org.eclipse.core.runtime.IProgressMonitor monitor) throws org.eclipse.core.runtime.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 annotatedfile
- a file assumed to be in .eea format, will be created if it doesn't exist.selector
- selector of the method or fieldoriginalSignature
- unannotated signature of the member, used for identificationannotatedSignature
- new signatures whose annotations should be superimposed on the membermergeStrategy
- controls how old and new signatures should be mergedmonitor
- progress monitor to be passed through into file operations, or null if no reporting is desired- Throws:
org.eclipse.core.runtime.CoreException
- if access to the file failsIOException
- if reading file content fails
-
annotateTypeTypeParameter
public static void annotateTypeTypeParameter(String typeName, org.eclipse.core.resources.IFile file, String originalSignature, String annotatedTypeParameter, int rank, ExternalAnnotationUtil.MergeStrategy mergeStrategy, org.eclipse.core.runtime.IProgressMonitor monitor) throws org.eclipse.core.runtime.CoreException, IOException, IllegalArgumentException Update the given external annotation file with details regarding annotations of a type parameter of the type itself. If the type already has external annotations, old and new annotations will be merged, with priorities controlled by the parameter 'mergeStrategy'.This method is suitable only for type use annotations.
- Parameters:
typeName
- binary name (slash separated) of the type being annotatedfile
- a file assumed to be in .eea format, will be created if it doesn't exist.originalSignature
- unannotated signature of all type parameters of the typeannotatedTypeParameter
- signature of the new type parameter whose annotations should be superimposed on the methodrank
- rank of the type parameter to be annotatedmergeStrategy
- controls how old and new signatures should be mergedmonitor
- progress monitor to be passed through into file operations, or null if no reporting is desired- Throws:
org.eclipse.core.runtime.CoreException
- if access to the file failsIOException
- if reading file content failsIllegalArgumentException
- if the annotatedTypeParameter does not structurally match to originalSignature- Since:
- 3.19
-
annotateMethodTypeParameter
public static void annotateMethodTypeParameter(String typeName, org.eclipse.core.resources.IFile file, String selector, String originalSignature, String annotatedTypeParameter, int rank, ExternalAnnotationUtil.MergeStrategy mergeStrategy, org.eclipse.core.runtime.IProgressMonitor monitor) throws org.eclipse.core.runtime.CoreException, IOException, IllegalArgumentException Update the given external annotation file with details regarding annotations of a type parameter 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 only for type use annotations.
- Parameters:
typeName
- binary name (slash separated) of the type being annotatedfile
- a file assumed to be in .eea format, will be created if it doesn't exist.selector
- selector of the methodoriginalSignature
- unannotated signature of the member, used for identificationannotatedTypeParameter
- signature of the new type parameter whose annotations should be superimposed on the methodrank
- rank of the type parameter to be annotatedmergeStrategy
- controls how old and new signatures should be mergedmonitor
- progress monitor to be passed through into file operations, or null if no reporting is desired- Throws:
org.eclipse.core.runtime.CoreException
- if access to the file failsIOException
- if reading file content failsIllegalArgumentException
- if the annotatedTypeParameter does not structurally match to originalSignature- Since:
- 3.19
-
annotateMethodReturnType
public static void annotateMethodReturnType(String typeName, org.eclipse.core.resources.IFile file, String selector, String originalSignature, String annotatedReturnType, ExternalAnnotationUtil.MergeStrategy mergeStrategy, org.eclipse.core.runtime.IProgressMonitor monitor) throws org.eclipse.core.runtime.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 annotatedfile
- a file assumed to be in .eea format, will be created if it doesn't exist.selector
- selector of the methodoriginalSignature
- unannotated signature of the member, used for identificationannotatedReturnType
- signature of the new return type whose annotations should be superimposed on the methodmergeStrategy
- controls how old and new signatures should be mergedmonitor
- progress monitor to be passed through into file operations, or null if no reporting is desired- Throws:
org.eclipse.core.runtime.CoreException
- if access to the file failsIOException
- if reading file content failsIllegalArgumentException
- if the annotatedReturnType does not structurally match to originalSignature
-
annotateMethodParameterType
public static void annotateMethodParameterType(String typeName, org.eclipse.core.resources.IFile file, String selector, String originalSignature, String annotatedParameterType, int paramIdx, ExternalAnnotationUtil.MergeStrategy mergeStrategy, org.eclipse.core.runtime.IProgressMonitor monitor) throws org.eclipse.core.runtime.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 annotatedfile
- a file assumed to be in .eea format, will be created if it doesn't exist.selector
- selector of the methodoriginalSignature
- unannotated signature of the member, used for identificationannotatedParameterType
- signature of the new parameter type whose annotations should be superimposed on the methodparamIdx
- 0-based index of the parameter to which the annotation should be attachedmergeStrategy
- controls how old and new signatures should be mergedmonitor
- progress monitor to be passed through into file operations, or null if no reporting is desired- Throws:
org.eclipse.core.runtime.CoreException
- if access to the file failsIOException
- if reading file content failsIllegalArgumentException
- if the annotatedParameterType does not structurally match to originalSignature
-
getAnnotatedSignature
public static String getAnnotatedSignature(String typeName, org.eclipse.core.resources.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 annotationsfile
- a file assumed to be in .eea format, must not be null, but may not existselector
- name of the member whose annotation we are looking for, or null when annotating the type's type parametersoriginalSignature
- 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 alreadyannotatedType
- 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 alreadyannotatedType
- 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 alreadyannotatedType
- a type signature with additional annotations (incl.NO_ANNOTATION
).paramIdx
- the index of a parameter to annotatemergeStrategy
- 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
-
annotateTypeParameter
public static String[] annotateTypeParameter(String originalSignature, String annotatedType, int rank, ExternalAnnotationUtil.MergeStrategy mergeStrategy) Apply the specified changes on a type 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 alreadyannotatedType
- a type signature with additional annotations (incl.NO_ANNOTATION
).rank
- the index of a type parameter to annotatemergeStrategy
- 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
- Since:
- 3.19
-