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 voidaddAnnotation(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 voidaddAnnotation(IJavaElement javaElement, Annotation annotation) Adds the givenAnnotationto theIJavaElement.static voidaddEventMethod(IType type, String methodName, AnnotationType annotation, String refName) Refactor operation to add an event method with an annotation.static voidaddImport(IJavaElement javaElement, String qualifiedName) Adds an import to the compilation unit of the givenIJavaElement.static voidaddInterface(IType type, String interfaceFQN) Refactor operation that adds an interface to an element.static voidaddMemberValuePair(NormalAnnotation annotation, MemberValuePair memberValuePair) Adds theMemberValuePairto theNormalAnnotation.static booleancompareFieldNames(FieldDeclaration fieldDeclaration, IField field) Compares theFieldDeclarationandIField.static booleancompareMethods(MethodDeclaration methodOne, MethodDeclaration methodTwo) Compares the twoMethodDeclaration.static booleancompareMethods(MethodDeclaration methodDeclaration, IMethod method) Compares theMethodDeclarationandIMethod.static booleancompareTypeNames(AbstractTypeDeclaration abstractTypeDeclaration, IType type) Compares theAbstractTypeDeclarationandIType.static TextEditcreateAddAnnotationTextEdit(IJavaElement javaElement, Annotation annotation) Creates aTextEditobject representing the add annotation change to the source code of the Java elements compilation unit.static TextEditcreateAddImportTextEdit(IJavaElement javaElement, String qualifiedName) Creates aTextEditobject representing the add import change to the source code of the Java elements compilation unit.static TextEditcreateAddMemberValuePairTextEdit(NormalAnnotation annotation, MemberValuePair memberValuePair) Creates aTextEditobject representing the change of adding theMemberValuePairto theNormalAnnotation.static TextEditcreateRemoveAnnotationTextEdit(IJavaElement javaElement, Annotation annotation) Creates aTextEditobject representing the remove annotation change to the source code of the Java elements compilation unit.static TextEditcreateRemoveMemberValuePairTextEdit(NormalAnnotation annotation, MemberValuePair memberValuePair) Creates aTextEditobject representing the change of removing theMemberValuePairfrom theNormalAnnotation.static TextEditcreateUpdateMemberValuePairTextEdit(MemberValuePair memberValuePair, ASTNode value) Creates aTextEditobject representing the change of updating theMemberValuePairwith theASTNodevalue.static TextEditcreateUpdateSingleMemberAnnotationTextEdit(SingleMemberAnnotation annotation, ASTNode value) Creates aTextEditobject representing the change of updating theSingleMemberAnnotationwith theASTNodevalue.static IAnnotationgetAnnotation(Class<? extends Annotation> annotation, IAnnotatable annotatable) Returns the JDTIAnnotationthat corresponds to the givenAnnotationclass on theIAnnotatableelement.static AnnotationgetAnnotation(IJavaElement javaElement, Class<? extends Annotation> annotation) static StringgetAnnotationName(Annotation annotation) Returns the annotations type name.static List<Annotation> getAnnotations(IJavaElement javaElement) Returns a list of all theAnnotationthat are present on the givenIJavaElementstatic ExpressiongetAnnotationValue(NormalAnnotation normalAnnotation, String memberName) Returns theNormalAnnotationmember value pair value with the given member name.static ObjectgetAnnotationValue(IAnnotation annotation, String memberName) Returns the JDTIAnnotationmember value pair value with the given member name.static BooleangetBooleanValue(Annotation annotation, String memberName) Returns the member value with the given member name from theAnnotationas aBooleanvalue.static BooleangetBooleanValue(IAnnotation annotation, String memberName) Returns the member value with the given member name from theIAnnotationas aBooleanvalue.static ICompilationUnitgetCompilationUnitFromJavaElement(IJavaElement javaElement) Returns aICompilationUnitfor the givenIJavaElement.static StringgetEnumValue(Annotation annotation, String memberName) Returns the member value with the given member name from theAnnotationas aStringvalue.static StringgetEnumValue(IAnnotation annotation, String memberName) Returns the member value with the given member name from theIAnnotationas aStringvalue.static FieldDeclarationgetFieldDeclaration(IField field) Returns theFieldDeclarationthat corresponds to the givenIField.static ILocalVariablegetLocalVariable(IMethod method, int offset) Returns theILocalVariableat the given offset position in the source file.static ILocalVariablegetLocalVariable(IMethod method, String paramName) Returns theILocalVariablewith the given name within the declaredIMethod.static MemberValuePairgetMemberValuePair(NormalAnnotation normalAnnotation, String memberName) Returns theMemberValuePairwith the given member name from theNormalAnnotation.static MethodDeclarationgetMethodDeclaration(IMethod method) Returns theMethodDeclarationthat corresponds to the givenIMethod.static SingleVariableDeclarationgetSingleVariableDeclaration(ILocalVariable javaElement) Returns theSingleVariableDeclarationthat corresponds to the givenILocalVariable.static List<SingleVariableDeclaration> getSingleVariableDeclarations(IMethod method) Returns a list of all theSingleVariableDeclarationfor the givenIMethod.static StringgetStringValue(Annotation annotation, String memberName) Returns the member value with the given member name from theAnnotationas aStringvalue.static StringgetStringValue(IAnnotation annotation, String memberName) Returns the member value with the given member name from theIAnnotationas aStringvalue.static AbstractTypeDeclarationgetTypeDeclaration(IType type) Returns theAbstractTypeDeclarationthat corresponds to the givenIType.static booleanisAnnotationPresent(IJavaElement javaElement, String annotationName) Checks if the annotation with the given name is present on theIJavaElement.static booleanisAnnotationPresent(IJavaElement javaElement, Annotation annotation) Checks if the givenAnnotationis present on theIJavaElement.static voidremoveAnnotation(IJavaElement javaElement, Annotation annotation) Removes the givenAnnotationfrom theIJavaElement.static voidremoveMemberValuePair(NormalAnnotation annotation, MemberValuePair memberValuePair) Removes theMemberValuePairfrom theNormalAnnotation.static voidupdateMemberValuePair(MemberValuePair memberValuePair, ASTNode value) Updates theMemberValuePairvalue with the givenASTNode.static voidupdateSingleMemberAnnotation(SingleMemberAnnotation annotation, ASTNode value) Updates the value of theSingleMemberAnnotationwith 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 givenAnnotationto 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- AJavaModelExceptionis thrown when the underlying compilation units buffer could not be accessed.
removeAnnotation
public static void removeAnnotation(IJavaElement javaElement, Annotation annotation) throws JavaModelException Removes the givenAnnotationfrom 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- AJavaModelExceptionis thrown when the underlying compilation units buffer could not be accessed.
addMemberValuePair
public static void addMemberValuePair(NormalAnnotation annotation, MemberValuePair memberValuePair) throws JavaModelException Adds theMemberValuePairto theNormalAnnotation.- Parameters:
annotation- The normal annotation to add the member value pair to.memberValuePair- The member value pair to add.- Throws:
JavaModelException- AJavaModelExceptionis thrown when the underlying compilation units buffer could not be accessed.
removeMemberValuePair
public static void removeMemberValuePair(NormalAnnotation annotation, MemberValuePair memberValuePair) throws JavaModelException Removes theMemberValuePairfrom theNormalAnnotation.- Parameters:
annotation- The normal annotation from which to remove the member value pair.memberValuePair- The member value pair to remove.- Throws:
JavaModelException- AJavaModelExceptionis thrown when the underlying compilation units buffer could not be accessed.
updateMemberValuePair
public static void updateMemberValuePair(MemberValuePair memberValuePair, ASTNode value) throws JavaModelException Updates theMemberValuePairvalue with the givenASTNode.- Parameters:
memberValuePair- The member value pair to update.value- The value to set.- Throws:
JavaModelException- AJavaModelExceptionis 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 theSingleMemberAnnotationwith the givenASTNode.- Parameters:
annotation- The single member annotation to update.value- The value to set.- Throws:
JavaModelException- AJavaModelExceptionis thrown when the underlying compilation units buffer could not be accessed.
createAddImportTextEdit
public static TextEdit createAddImportTextEdit(IJavaElement javaElement, String qualifiedName) throws CoreException Creates aTextEditobject 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 aCompilationUnitwhich 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 aTextEditobject 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
MultiTextEditif the given Java element isn't supported. - Throws:
JavaModelException- AJavaModelExceptionis thrown when the underlying compilation units buffer could not be accessed.
createRemoveAnnotationTextEdit
public static TextEdit createRemoveAnnotationTextEdit(IJavaElement javaElement, Annotation annotation) throws JavaModelException Creates aTextEditobject 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
MultiTextEditif the given Java element isn't supported. - Throws:
JavaModelException- AJavaModelExceptionis thrown when the underlying compilation units buffer could not be accessed.
createAddMemberValuePairTextEdit
public static TextEdit createAddMemberValuePairTextEdit(NormalAnnotation annotation, MemberValuePair memberValuePair) throws JavaModelException Creates aTextEditobject representing the change of adding theMemberValuePairto 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- AJavaModelExceptionis thrown when the underlying compilation units buffer could not be accessed.
createRemoveMemberValuePairTextEdit
public static TextEdit createRemoveMemberValuePairTextEdit(NormalAnnotation annotation, MemberValuePair memberValuePair) throws JavaModelException Creates aTextEditobject representing the change of removing theMemberValuePairfrom 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- AJavaModelExceptionis thrown when the underlying compilation units buffer could not be accessed.
createUpdateMemberValuePairTextEdit
public static TextEdit createUpdateMemberValuePairTextEdit(MemberValuePair memberValuePair, ASTNode value) throws JavaModelException Creates aTextEditobject representing the change of updating theMemberValuePairwith theASTNodevalue. 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- AJavaModelExceptionis thrown when the underlying compilation units buffer could not be accessed.
createUpdateSingleMemberAnnotationTextEdit
public static TextEdit createUpdateSingleMemberAnnotationTextEdit(SingleMemberAnnotation annotation, ASTNode value) throws JavaModelException Creates aTextEditobject representing the change of updating theSingleMemberAnnotationwith theASTNodevalue. 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- AJavaModelExceptionis thrown when the underlying compilation units buffer could not be accessed.
getCompilationUnitFromJavaElement
Returns aICompilationUnitfor 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 theAbstractTypeDeclarationthat corresponds to the givenIType.- Parameters:
type- The type.- Returns:
- A type declaration or null if not found.
getMethodDeclaration
Returns theMethodDeclarationthat corresponds to the givenIMethod.- Parameters:
method- The method.- Returns:
- a method declaration or null if not found.
getFieldDeclaration
Returns theFieldDeclarationthat corresponds to the givenIField.- Parameters:
field- The field.- Returns:
- A field declaration or null if not found.
getSingleVariableDeclaration
Returns theSingleVariableDeclarationthat 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 theAbstractTypeDeclarationandIType.- Parameters:
abstractTypeDeclaration- The type declaration.type- The type.- Returns:
trueif the names match.
compareMethods
Compares theMethodDeclarationandIMethod.- Parameters:
methodDeclaration- The method declaration.method- The method.- Returns:
trueif the method names and parameter types match.
compareMethods
Compares the twoMethodDeclaration.- Parameters:
methodOne- The first method declaration.methodTwo- The second method declaration.- Returns:
trueif the method names and parameter types match.
compareFieldNames
Compares theFieldDeclarationandIField.- Parameters:
fieldDeclaration- The field declaration.field- The field.- Returns:
trueif the field names match.
isAnnotationPresent
Checks if the givenAnnotationis 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:
trueif 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:
trueif the annotation is present.
getAnnotations
Returns a list of all theAnnotationthat are present on the givenIJavaElementThe 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 theSingleVariableDeclarationfor the givenIMethod.- Parameters:
method- The method.- Returns:
- a list of single variable declarations.
getLocalVariable
Returns theILocalVariableat 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 theILocalVariablewith 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 ASTAnnotationthat corresponds to the givenAnnotationclass 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- TheAnnotationclass.- Returns:
- the AST annotation or null if not found.
getAnnotation
public static IAnnotation getAnnotation(Class<? extends Annotation> annotation, IAnnotatable annotatable) throws JavaModelException Returns the JDTIAnnotationthat corresponds to the givenAnnotationclass on theIAnnotatableelement.- Parameters:
annotation- TheAnnotationclass.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 theNormalAnnotationmember 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 JDTIAnnotationmember 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 theMemberValuePairwith 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 theAnnotationas aStringvalue.- 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 theIAnnotationas aStringvalue.- 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 theAnnotationas aBooleanvalue.- 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 theIAnnotationas aBooleanvalue.- 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 theAnnotationas aStringvalue. 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 theIAnnotationas aStringvalue. 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.