1 11 package org.eclipse.jdt.internal.corext.refactoring.structure; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.List ; 16 17 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 18 import org.eclipse.ltk.core.refactoring.RefactoringStatusContext; 19 20 import org.eclipse.jdt.core.IField; 21 import org.eclipse.jdt.core.IJavaElement; 22 import org.eclipse.jdt.core.IMember; 23 import org.eclipse.jdt.core.IMethod; 24 import org.eclipse.jdt.core.IType; 25 import org.eclipse.jdt.core.JavaModelException; 26 27 import org.eclipse.jdt.internal.corext.refactoring.Checks; 28 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 29 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext; 30 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 31 import org.eclipse.jdt.internal.corext.util.Messages; 32 33 class MemberCheckUtil { 34 35 private MemberCheckUtil(){ 36 } 38 39 public static RefactoringStatus checkMembersInDestinationType(IMember[] members, IType destinationType) throws JavaModelException { 40 RefactoringStatus result= new RefactoringStatus(); 41 for (int i= 0; i < members.length; i++) { 42 if (members[i].getElementType() == IJavaElement.METHOD) 43 checkMethodInType(destinationType, result, (IMethod)members[i]); 44 else if (members[i].getElementType() == IJavaElement.FIELD) 45 checkFieldInType(destinationType, result, (IField)members[i]); 46 else if (members[i].getElementType() == IJavaElement.TYPE) 47 checkTypeInType(destinationType, result, (IType)members[i]); 48 } 49 return result; 50 } 51 52 private static void checkMethodInType(IType destinationType, RefactoringStatus result, IMethod method) throws JavaModelException { 53 IMethod[] destinationTypeMethods= destinationType.getMethods(); 54 IMethod found= findMethod(method, destinationTypeMethods); 55 if (found != null){ 56 RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), found.getSourceRange()); 57 String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_signature_exists, 58 new String []{method.getElementName(), JavaModelUtil.getFullyQualifiedName(destinationType)}); 59 result.addError(message, context); 60 } else { 61 IMethod similar= Checks.findMethod(method, destinationType); 62 if (similar != null){ 63 String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_same_param_count, 64 new String []{method.getElementName(), JavaModelUtil.getFullyQualifiedName(destinationType)}); 65 RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), similar.getSourceRange()); 66 result.addWarning(message, context); 67 } 68 } 69 } 70 71 private static void checkFieldInType(IType destinationType, RefactoringStatus result, IField field) throws JavaModelException { 72 IField destinationTypeField= destinationType.getField(field.getElementName()); 73 if (! destinationTypeField.exists()) 74 return; 75 String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_field_exists, 76 new String []{field.getElementName(), JavaModelUtil.getFullyQualifiedName(destinationType)}); 77 RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), destinationTypeField.getSourceRange()); 78 result.addError(message, context); 79 } 80 81 private static void checkTypeInType(IType destinationType, RefactoringStatus result, IType type) throws JavaModelException { 82 String typeName= type.getElementName(); 83 IType destinationTypeType= destinationType.getType(typeName); 84 if (destinationTypeType.exists()){ 85 String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_type_name_conflict0, 86 new String []{typeName, JavaModelUtil.getFullyQualifiedName(destinationType)}); 87 RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), destinationTypeType.getNameRange()); 88 result.addError(message, context); 89 } else { 90 if (destinationType.getElementName().equals(typeName)){ 92 String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_type_name_conflict1, 93 new String []{JavaModelUtil.getFullyQualifiedName(type)}); 94 RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), destinationType.getNameRange()); 95 result.addError(message, context); 96 } 97 if (typeNameExistsInEnclosingTypeChain(destinationType, typeName)){ 98 String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_type_name_conflict2, 99 new String []{JavaModelUtil.getFullyQualifiedName(type)}); 100 RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), destinationType.getNameRange()); 101 result.addError(message, context); 102 } 103 checkHierarchyOfEnclosedTypes(destinationType, result, type); 104 } 105 } 106 107 private static void checkHierarchyOfEnclosedTypes(IType destinationType, RefactoringStatus result, IType type) throws JavaModelException { 108 IType[] enclosedTypes= getAllEnclosedTypes(type); 109 for (int i= 0; i < enclosedTypes.length; i++) { 110 IType enclosedType= enclosedTypes[i]; 111 if (destinationType.getElementName().equals(enclosedType.getElementName())){ 112 String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_type_name_conflict3, 113 new String []{JavaModelUtil.getFullyQualifiedName(enclosedType), JavaModelUtil.getFullyQualifiedName(type)}); 114 RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), destinationType.getNameRange()); 115 result.addError(message, context); 116 } 117 if (typeNameExistsInEnclosingTypeChain(destinationType, enclosedType.getElementName())){ 118 String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_type_name_conflict4, 119 new String []{JavaModelUtil.getFullyQualifiedName(enclosedType), JavaModelUtil.getFullyQualifiedName(type)}); 120 RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), destinationType.getNameRange()); 121 result.addError(message, context); 122 } 123 } 124 } 125 126 private static IType[] getAllEnclosedTypes(IType type) throws JavaModelException { 127 List result= new ArrayList (2); 128 IType[] directlyEnclosed= type.getTypes(); 129 result.addAll(Arrays.asList(directlyEnclosed)); 130 for (int i= 0; i < directlyEnclosed.length; i++) { 131 IType enclosedType= directlyEnclosed[i]; 132 result.addAll(Arrays.asList(getAllEnclosedTypes(enclosedType))); 133 } 134 return (IType[]) result.toArray(new IType[result.size()]); 135 } 136 137 private static boolean typeNameExistsInEnclosingTypeChain(IType type, String typeName){ 138 IType enclosing= type.getDeclaringType(); 139 while (enclosing != null){ 140 if (enclosing.getElementName().equals(typeName)) 141 return true; 142 enclosing= enclosing.getDeclaringType(); 143 } 144 return false; 145 } 146 147 152 public static IMethod findMethod(IMethod method, IMethod[] allMethods) throws JavaModelException { 153 String name= method.getElementName(); 154 String [] paramTypes= method.getParameterTypes(); 155 boolean isConstructor= method.isConstructor(); 156 157 for (int i= 0; i < allMethods.length; i++) { 158 if (JavaModelUtil.isSameMethodSignature(name, paramTypes, isConstructor, allMethods[i])) 159 return allMethods[i]; 160 } 161 return null; 162 } 163 } 164 | Popular Tags |