Package com.iizigo.java.refactor
Class RefactorHelper
java.lang.Object
com.iizigo.java.refactor.RefactorHelper
Utility class for refactoring: adding, removing and updating annotations and member value pairs.
- Author:
- Christopher Mindus
Method Summary
Modifier and TypeMethodDescriptionstatic void
addAnnotation
(IJavaElement javaElement, String annotationFQN, String param, String value, boolean allowMultiple) Refactor operation that adds an annotation to an element with a value/pair initialized with a StringLiteral value.static void
addAnnotation
(IJavaElement javaElement, Annotation annotation) Adds the givenAnnotation
to theIJavaElement
.static void
addEventMethod
(IType type, String methodName, AnnotationType annotation, String refName) Refactor operation to add an event method with an annotation.static void
addImport
(IJavaElement javaElement, String qualifiedName) Adds an import to the compilation unit of the givenIJavaElement
.static void
addInterface
(IType type, String interfaceFQN) Refactor operation that adds an interface to an element.static void
addMemberValuePair
(NormalAnnotation annotation, MemberValuePair memberValuePair) Adds theMemberValuePair
to theNormalAnnotation
.static boolean
compareFieldNames
(FieldDeclaration fieldDeclaration, IField field) Compares theFieldDeclaration
andIField
.static boolean
compareMethods
(MethodDeclaration methodOne, MethodDeclaration methodTwo) Compares the twoMethodDeclaration
.static boolean
compareMethods
(MethodDeclaration methodDeclaration, IMethod method) Compares theMethodDeclaration
andIMethod
.static boolean
compareTypeNames
(AbstractTypeDeclaration abstractTypeDeclaration, IType type) Compares theAbstractTypeDeclaration
andIType
.static TextEdit
createAddAnnotationTextEdit
(IJavaElement javaElement, Annotation annotation) Creates aTextEdit
object representing the add annotation change to the source code of the Java elements compilation unit.static TextEdit
createAddImportTextEdit
(IJavaElement javaElement, String qualifiedName) Creates aTextEdit
object representing the add import change to the source code of the Java elements compilation unit.static TextEdit
createAddMemberValuePairTextEdit
(NormalAnnotation annotation, MemberValuePair memberValuePair) Creates aTextEdit
object representing the change of adding theMemberValuePair
to theNormalAnnotation
.static TextEdit
createRemoveAnnotationTextEdit
(IJavaElement javaElement, Annotation annotation) Creates aTextEdit
object representing the remove annotation change to the source code of the Java elements compilation unit.static TextEdit
createRemoveMemberValuePairTextEdit
(NormalAnnotation annotation, MemberValuePair memberValuePair) Creates aTextEdit
object representing the change of removing theMemberValuePair
from theNormalAnnotation
.static TextEdit
createUpdateMemberValuePairTextEdit
(MemberValuePair memberValuePair, ASTNode value) Creates aTextEdit
object representing the change of updating theMemberValuePair
with theASTNode
value.static TextEdit
createUpdateSingleMemberAnnotationTextEdit
(SingleMemberAnnotation annotation, ASTNode value) Creates aTextEdit
object representing the change of updating theSingleMemberAnnotation
with theASTNode
value.static IAnnotation
getAnnotation
(Class<? extends Annotation> annotation, IAnnotatable annotatable) Returns the JDTIAnnotation
that corresponds to the givenAnnotation
class on theIAnnotatable
element.static Annotation
getAnnotation
(IJavaElement javaElement, Class<? extends Annotation> annotation) static String
getAnnotationName
(Annotation annotation) Returns the annotations type name.static List<Annotation>
getAnnotations
(IJavaElement javaElement) Returns a list of all theAnnotation
that are present on the givenIJavaElement
static Expression
getAnnotationValue
(NormalAnnotation normalAnnotation, String memberName) Returns theNormalAnnotation
member value pair value with the given member name.static Object
getAnnotationValue
(IAnnotation annotation, String memberName) Returns the JDTIAnnotation
member value pair value with the given member name.static Boolean
getBooleanValue
(Annotation annotation, String memberName) Returns the member value with the given member name from theAnnotation
as aBoolean
value.static Boolean
getBooleanValue
(IAnnotation annotation, String memberName) Returns the member value with the given member name from theIAnnotation
as aBoolean
value.static ICompilationUnit
getCompilationUnitFromJavaElement
(IJavaElement javaElement) Returns aICompilationUnit
for the givenIJavaElement
.static String
getEnumValue
(Annotation annotation, String memberName) Returns the member value with the given member name from theAnnotation
as aString
value.static String
getEnumValue
(IAnnotation annotation, String memberName) Returns the member value with the given member name from theIAnnotation
as aString
value.static FieldDeclaration
getFieldDeclaration
(IField field) Returns theFieldDeclaration
that corresponds to the givenIField
.static ILocalVariable
getLocalVariable
(IMethod method, int offset) Returns theILocalVariable
at the given offset position in the source file.static ILocalVariable
getLocalVariable
(IMethod method, String paramName) Returns theILocalVariable
with the given name within the declaredIMethod
.static MemberValuePair
getMemberValuePair
(NormalAnnotation normalAnnotation, String memberName) Returns theMemberValuePair
with the given member name from theNormalAnnotation
.static MethodDeclaration
getMethodDeclaration
(IMethod method) Returns theMethodDeclaration
that corresponds to the givenIMethod
.static SingleVariableDeclaration
getSingleVariableDeclaration
(ILocalVariable javaElement) Returns theSingleVariableDeclaration
that corresponds to the givenILocalVariable
.static List<SingleVariableDeclaration>
getSingleVariableDeclarations
(IMethod method) Returns a list of all theSingleVariableDeclaration
for the givenIMethod
.static String
getStringValue
(Annotation annotation, String memberName) Returns the member value with the given member name from theAnnotation
as aString
value.static String
getStringValue
(IAnnotation annotation, String memberName) Returns the member value with the given member name from theIAnnotation
as aString
value.static AbstractTypeDeclaration
getTypeDeclaration
(IType type) Returns theAbstractTypeDeclaration
that corresponds to the givenIType
.static boolean
isAnnotationPresent
(IJavaElement javaElement, String annotationName) Checks if the annotation with the given name is present on theIJavaElement
.static boolean
isAnnotationPresent
(IJavaElement javaElement, Annotation annotation) Checks if the givenAnnotation
is present on theIJavaElement
.static void
removeAnnotation
(IJavaElement javaElement, Annotation annotation) Removes the givenAnnotation
from theIJavaElement
.static void
removeMemberValuePair
(NormalAnnotation annotation, MemberValuePair memberValuePair) Removes theMemberValuePair
from theNormalAnnotation
.static void
updateMemberValuePair
(MemberValuePair memberValuePair, ASTNode value) Updates theMemberValuePair
value with the givenASTNode
.static void
updateSingleMemberAnnotation
(SingleMemberAnnotation annotation, ASTNode value) Updates the value of theSingleMemberAnnotation
with the givenASTNode
.
Method Details
addAnnotation
public static void addAnnotation(IJavaElement javaElement, String annotationFQN, String param, String value, boolean allowMultiple) throws CoreException Refactor operation that adds an annotation to an element with a value/pair initialized with a StringLiteral value. The import is added too if required. No checking if the annotation already exists is done as repeatable annotations is supported.The following types of Java elements are supported:
- IJavaElement.PACKAGE_DECLARATION
- IJavaElement.TYPE
- IJavaElement.FIELD
- IJavaElement.METHOD
- IJavaElement.LOCAL_VARIABLE
- Parameters:
javaElement
- The Java element which is used to get the compilation unit to add annotation to.annotationFQN
- Fully qualified name of annotation. It is assumed to have a non-empty package name.param
- A parameter as specified by the annotation.value
- The string value that will become a StringLiteral.allowMultiple
- Flag to enable multiple annotations or not.- Throws:
CoreException
- The exception is thrown if the import rewrite fails.
addInterface
Refactor operation that adds an interface to an element.- Parameters:
type
- The type.interfaceFQN
- Fully qualified name of the interface. It is assumed to have a non-empty package name.- Throws:
CoreException
- The exception is thrown if the import rewrite fails.
addEventMethod
public static void addEventMethod(IType type, String methodName, AnnotationType annotation, String refName) throws CoreException Refactor operation to add an event method with an annotation.- Parameters:
type
- The class.methodName
- The method name.annotation
- The annotation.refName
- Reference name.- Throws:
CoreException
addImport
Adds an import to the compilation unit of the givenIJavaElement
.The following types of Java elements are supported:
- IJavaElement.COMPILATION_UNIT
- IJavaElement.PACKAGE_DECLARATION
- IJavaElement.TYPE
- IJavaElement.FIELD
- IJavaElement.METHOD
- IJavaElement.LOCAL_VARIABLE
- Parameters:
javaElement
- The Java element which is used to get the compilation unit to add the import to.qualifiedName
- The import to add.- Throws:
CoreException
- The exception is thrown if the import rewrite fails.
addAnnotation
public static void addAnnotation(IJavaElement javaElement, Annotation annotation) throws JavaModelException Adds the givenAnnotation
to theIJavaElement
.The following types of Java elements are supported:
- IJavaElement.PACKAGE_DECLARATION
- IJavaElement.TYPE
- IJavaElement.FIELD
- IJavaElement.METHOD
- IJavaElement.LOCAL_VARIABLE
- Parameters:
javaElement
- The Java element which is used to get the compilation unit to add annotation to.annotation
- The annotation to add.- Throws:
JavaModelException
- AJavaModelException
is thrown when the underlying compilation units buffer could not be accessed.
removeAnnotation
public static void removeAnnotation(IJavaElement javaElement, Annotation annotation) throws JavaModelException Removes the givenAnnotation
from theIJavaElement
.The following types of Java elements are supported:
- IJavaElement.PACKAGE_DECLARATION
- IJavaElement.TYPE
- IJavaElement.FIELD
- IJavaElement.METHOD
- IJavaElement.LOCAL_VARIABLE
- Parameters:
javaElement
- Java element to operate on.annotation
- The annotation to remove.- Throws:
JavaModelException
- AJavaModelException
is thrown when the underlying compilation units buffer could not be accessed.
addMemberValuePair
public static void addMemberValuePair(NormalAnnotation annotation, MemberValuePair memberValuePair) throws JavaModelException Adds theMemberValuePair
to theNormalAnnotation
.- Parameters:
annotation
- The normal annotation to add the member value pair to.memberValuePair
- The member value pair to add.- Throws:
JavaModelException
- AJavaModelException
is thrown when the underlying compilation units buffer could not be accessed.
removeMemberValuePair
public static void removeMemberValuePair(NormalAnnotation annotation, MemberValuePair memberValuePair) throws JavaModelException Removes theMemberValuePair
from theNormalAnnotation
.- Parameters:
annotation
- The normal annotation from which to remove the member value pair.memberValuePair
- The member value pair to remove.- Throws:
JavaModelException
- AJavaModelException
is thrown when the underlying compilation units buffer could not be accessed.
updateMemberValuePair
public static void updateMemberValuePair(MemberValuePair memberValuePair, ASTNode value) throws JavaModelException Updates theMemberValuePair
value with the givenASTNode
.- Parameters:
memberValuePair
- The member value pair to update.value
- The value to set.- Throws:
JavaModelException
- AJavaModelException
is thrown when the underlying compilation units buffer could not be accessed.
updateSingleMemberAnnotation
public static void updateSingleMemberAnnotation(SingleMemberAnnotation annotation, ASTNode value) throws JavaModelException Updates the value of theSingleMemberAnnotation
with the givenASTNode
.- Parameters:
annotation
- The single member annotation to update.value
- The value to set.- Throws:
JavaModelException
- AJavaModelException
is thrown when the underlying compilation units buffer could not be accessed.
createAddImportTextEdit
public static TextEdit createAddImportTextEdit(IJavaElement javaElement, String qualifiedName) throws CoreException Creates aTextEdit
object representing the add import change to the source code of the Java elements compilation unit. The compilation unit itself is not modified.The following types of Java elements are supported:
- IJavaElement.PACKAGE_DECLARATION
- IJavaElement.TYPE
- IJavaElement.FIELD
- IJavaElement.METHOD
- IJavaElement.LOCAL_VARIABLE
- Parameters:
javaElement
- The Java element will be used to create aCompilationUnit
which will in turn be used to create anImportRewrite
.qualifiedName
- The import to add.- Returns:
- Text edit object describing the add import changes.
- Throws:
CoreException
- The exception is thrown if the import rewrite fails.
createAddAnnotationTextEdit
public static TextEdit createAddAnnotationTextEdit(IJavaElement javaElement, Annotation annotation) throws JavaModelException Creates aTextEdit
object representing the add annotation change to the source code of the Java elements compilation unit. The compilation unit itself is not modified.The following types of Java elements are supported:
- IJavaElement.PACKAGE_DECLARATION
- IJavaElement.TYPE
- IJavaElement.FIELD
- IJavaElement.METHOD
- IJavaElement.LOCAL_VARIABLE
- Parameters:
javaElement
- Element to operate on.annotation
- The annotation to add.- Returns:
- Text edit object describing the add annotation changes. Returns a
MultiTextEdit
if the given Java element isn't supported. - Throws:
JavaModelException
- AJavaModelException
is thrown when the underlying compilation units buffer could not be accessed.
createRemoveAnnotationTextEdit
public static TextEdit createRemoveAnnotationTextEdit(IJavaElement javaElement, Annotation annotation) throws JavaModelException Creates aTextEdit
object representing the remove annotation change to the source code of the Java elements compilation unit. The compilation unit itself is not modified.The following types of Java elements are supported:
- IJavaElement.PACKAGE_DECLARATION
- IJavaElement.TYPE
- IJavaElement.FIELD
- IJavaElement.METHOD
- IJavaElement.LOCAL_VARIABLE
- Parameters:
javaElement
- Element to operate on.annotation
- The annotation to remove.- Returns:
- Text edit object describing the add annotation changes. Returns a
MultiTextEdit
if the given Java element isn't supported. - Throws:
JavaModelException
- AJavaModelException
is thrown when the underlying compilation units buffer could not be accessed.
createAddMemberValuePairTextEdit
public static TextEdit createAddMemberValuePairTextEdit(NormalAnnotation annotation, MemberValuePair memberValuePair) throws JavaModelException Creates aTextEdit
object representing the change of adding theMemberValuePair
to theNormalAnnotation
. The underlying compilation unit itself is not modified.- Parameters:
annotation
- The normal annotation to add the member value pair to.memberValuePair
- The member value pair to add.- Returns:
- Text edit object describing the add member value pair change.
- Throws:
JavaModelException
- AJavaModelException
is thrown when the underlying compilation units buffer could not be accessed.
createRemoveMemberValuePairTextEdit
public static TextEdit createRemoveMemberValuePairTextEdit(NormalAnnotation annotation, MemberValuePair memberValuePair) throws JavaModelException Creates aTextEdit
object representing the change of removing theMemberValuePair
from theNormalAnnotation
. The underlying compilation unit itself is not modified.- Parameters:
annotation
- The normal annotation to remove the member value pair from.memberValuePair
- The member value pair to remove.- Returns:
- Text edit object describing the remove member value pair change.
- Throws:
JavaModelException
- AJavaModelException
is thrown when the underlying compilation units buffer could not be accessed.
createUpdateMemberValuePairTextEdit
public static TextEdit createUpdateMemberValuePairTextEdit(MemberValuePair memberValuePair, ASTNode value) throws JavaModelException Creates aTextEdit
object representing the change of updating theMemberValuePair
with theASTNode
value. The underlying compilation unit itself is not modified.- Parameters:
memberValuePair
- The member value pair to update.value
- The value to set.- Returns:
- Text edit object describing the update member value pair change.
- Throws:
JavaModelException
- AJavaModelException
is thrown when the underlying compilation units buffer could not be accessed.
createUpdateSingleMemberAnnotationTextEdit
public static TextEdit createUpdateSingleMemberAnnotationTextEdit(SingleMemberAnnotation annotation, ASTNode value) throws JavaModelException Creates aTextEdit
object representing the change of updating theSingleMemberAnnotation
with theASTNode
value. The underlying compilation unit itself is not modified.- Parameters:
annotation
- The single member annotation to update.value
- The value to set.- Returns:
- Text edit object describing the update single member annotation change.
- Throws:
JavaModelException
- AJavaModelException
is thrown when the underlying compilation units buffer could not be accessed.
getCompilationUnitFromJavaElement
Returns aICompilationUnit
for the givenIJavaElement
.The following types of Java elements are supported:
- IJavaElement.PACKAGE_DECLARATION
- IJavaElement.TYPE
- IJavaElement.FIELD
- IJavaElement.METHOD
- IJavaElement.LOCAL_VARIABLE
- Parameters:
javaElement
- The Java element.- Returns:
- A compilation unit.
getTypeDeclaration
Returns theAbstractTypeDeclaration
that corresponds to the givenIType
.- Parameters:
type
- The type.- Returns:
- A type declaration or null if not found.
getMethodDeclaration
Returns theMethodDeclaration
that corresponds to the givenIMethod
.- Parameters:
method
- The method.- Returns:
- a method declaration or null if not found.
getFieldDeclaration
Returns theFieldDeclaration
that corresponds to the givenIField
.- Parameters:
field
- The field.- Returns:
- A field declaration or null if not found.
getSingleVariableDeclaration
Returns theSingleVariableDeclaration
that corresponds to the givenILocalVariable
.- Parameters:
javaElement
- The local variable.- Returns:
- A single variable declaration or null if not found.
getAnnotationName
Returns the annotations type name. If the annotation name is a simple name, the result is the name's identifier. If the name is a qualified name, the result is the name of the qualifier followed by "." followed by the name's identifier.- Parameters:
annotation
- The annotation.- Returns:
- The annotation name. The simple name or the fully qualified name.
compareTypeNames
Compares theAbstractTypeDeclaration
andIType
.- Parameters:
abstractTypeDeclaration
- The type declaration.type
- The type.- Returns:
true
if the names match.
compareMethods
Compares theMethodDeclaration
andIMethod
.- Parameters:
methodDeclaration
- The method declaration.method
- The method.- Returns:
true
if the method names and parameter types match.
compareMethods
Compares the twoMethodDeclaration
.- Parameters:
methodOne
- The first method declaration.methodTwo
- The second method declaration.- Returns:
true
if the method names and parameter types match.
compareFieldNames
Compares theFieldDeclaration
andIField
.- Parameters:
fieldDeclaration
- The field declaration.field
- The field.- Returns:
true
if the field names match.
isAnnotationPresent
Checks if the givenAnnotation
is present on theIJavaElement
.The following types of Java elements are supported:
- IJavaElement.PACKAGE_DECLARATION
- IJavaElement.TYPE
- IJavaElement.FIELD
- IJavaElement.METHOD
- IJavaElement.LOCAL_VARIABLE
- Parameters:
javaElement
- The checked element.annotation
- The annotation.- Returns:
true
if the annotation is present.
isAnnotationPresent
Checks if the annotation with the given name is present on theIJavaElement
.The following types of Java elements are supported:
- IJavaElement.PACKAGE_DECLARATION
- IJavaElement.TYPE
- IJavaElement.FIELD
- IJavaElement.METHOD
- IJavaElement.LOCAL_VARIABLE
- Parameters:
javaElement
- The checked element.annotationName
- The annotation name.- Returns:
true
if the annotation is present.
getAnnotations
Returns a list of all theAnnotation
that are present on the givenIJavaElement
The following types of Java elements are supported:
- IJavaElement.PACKAGE_DECLARATION
- IJavaElement.TYPE
- IJavaElement.FIELD
- IJavaElement.METHOD
- IJavaElement.LOCAL_VARIABLE
- Parameters:
javaElement
- The element.- Returns:
- a list of annotations.
getSingleVariableDeclarations
Returns a list of all theSingleVariableDeclaration
for the givenIMethod
.- Parameters:
method
- The method.- Returns:
- a list of single variable declarations.
getLocalVariable
Returns theILocalVariable
at the given offset position in the source file.- Parameters:
method
- The method in which the local variable is declared.offset
- The character index of the local variable in the source file. The offset must be >= to the start position of the node representing the local variable and <= the nodes start position plus length.- Returns:
- The local variable or null if not found.
getLocalVariable
Returns theILocalVariable
with the given name within the declaredIMethod
.- Parameters:
method
- The method in which the local variable is declared.paramName
- The local variable name.- Returns:
- the local variable or null if not found.
getAnnotation
public static Annotation getAnnotation(IJavaElement javaElement, Class<? extends Annotation> annotation) Returns the ASTAnnotation
that corresponds to the givenAnnotation
class on theIJavaElement
.The following types of Java elements are supported:
- IJavaElement.PACKAGE_DECLARATION
- IJavaElement.TYPE
- IJavaElement.FIELD
- IJavaElement.METHOD
- IJavaElement.LOCAL_VARIABLE
- Parameters:
javaElement
- The Java element to query.annotation
- TheAnnotation
class.- Returns:
- the AST annotation or null if not found.
getAnnotation
public static IAnnotation getAnnotation(Class<? extends Annotation> annotation, IAnnotatable annotatable) throws JavaModelException Returns the JDTIAnnotation
that corresponds to the givenAnnotation
class on theIAnnotatable
element.- Parameters:
annotation
- TheAnnotation
class.annotatable
- A package declaration, a type, a method, a field or a local variable in a compilation unit.- Returns:
- The annotation or null if not found.
- Throws:
JavaModelException
- If the annotatable element does not exist or if an exception occurs while accessing its corresponding resource.
getAnnotationValue
Returns theNormalAnnotation
member value pair value with the given member name.- Parameters:
normalAnnotation
- The normal annotation.memberName
- The member value pair member name.- Returns:
- the value expression or null if not found.
getAnnotationValue
public static Object getAnnotationValue(IAnnotation annotation, String memberName) throws JavaModelException Returns the JDTIAnnotation
member value pair value with the given member name.- Parameters:
annotation
- The annotation.memberName
- The member name.- Returns:
- An object representing the member value pairs value.
- Throws:
JavaModelException
- If the annotation does not exist or if an exception occurs while accessing its corresponding resource.
getMemberValuePair
public static MemberValuePair getMemberValuePair(NormalAnnotation normalAnnotation, String memberName) Returns theMemberValuePair
with the given member name from theNormalAnnotation
.- Parameters:
normalAnnotation
- The normal annotation.memberName
- The member name of the member value pair to return.- Returns:
- A member value pair or null if no member value pair with the given member name can be found.
getStringValue
Returns the member value with the given member name from theAnnotation
as aString
value.- Parameters:
annotation
- The AST annotation.memberName
- The member name.- Returns:
- The member value as a String or null if no member with the member name can be found.
getStringValue
public static String getStringValue(IAnnotation annotation, String memberName) throws JavaModelException Returns the member value with the given member name from theIAnnotation
as aString
value.- Parameters:
annotation
- The JDT annotation.memberName
- The member name.- Returns:
- The member value as a String or null if no member with the member name can be found.
- Throws:
JavaModelException
- If the annotation does not exist or if an exception occurs while accessing its corresponding resource.
getBooleanValue
Returns the member value with the given member name from theAnnotation
as aBoolean
value.- Parameters:
annotation
- The AST annotation.memberName
- The member name.- Returns:
- The member value as a Boolean or null if no member with the member name can be found.
getBooleanValue
public static Boolean getBooleanValue(IAnnotation annotation, String memberName) throws JavaModelException Returns the member value with the given member name from theIAnnotation
as aBoolean
value.- Parameters:
annotation
- The JDT annotation.memberName
- The member name.- Returns:
- The member value as a Boolean or null if no member with the member name can be found.
- Throws:
JavaModelException
- If the annotation does not exist or if an exception occurs while accessing its corresponding resource.
getEnumValue
Returns the member value with the given member name from theAnnotation
as aString
value. The returned String value is the name of the enum constant.- Parameters:
annotation
- The AST annotation.memberName
- The member name.- Returns:
- The member value as a String or null if no member with the member name can be found.
getEnumValue
public static String getEnumValue(IAnnotation annotation, String memberName) throws JavaModelException Returns the member value with the given member name from theIAnnotation
as aString
value. The returned String value is the name of the enum constant.- Parameters:
annotation
- The JDT annotation.memberName
- The member name.- Returns:
- the member value as a String or null if no member with the member name can be found.
- Throws:
JavaModelException
- If the annotation does not exist or if an exception occurs while accessing its corresponding resource.