KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > structure > MemberCheckUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.structure;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
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         //static only
37
}
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 JavaDoc message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_signature_exists,
58                     new String JavaDoc[]{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 JavaDoc message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_same_param_count,
64                          new String JavaDoc[]{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 JavaDoc message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_field_exists,
76                 new String JavaDoc[]{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 JavaDoc typeName= type.getElementName();
83         IType destinationTypeType= destinationType.getType(typeName);
84         if (destinationTypeType.exists()){
85             String JavaDoc message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_type_name_conflict0,
86                     new String JavaDoc[]{typeName, JavaModelUtil.getFullyQualifiedName(destinationType)});
87             RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), destinationTypeType.getNameRange());
88             result.addError(message, context);
89         } else {
90             //need to check the hierarchy of enclosing and enclosed types
91
if (destinationType.getElementName().equals(typeName)){
92                 String JavaDoc message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_type_name_conflict1,
93                         new String JavaDoc[]{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 JavaDoc message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_type_name_conflict2,
99                         new String JavaDoc[]{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 JavaDoc message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_type_name_conflict3,
113                         new String JavaDoc[]{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 JavaDoc message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_type_name_conflict4,
119                         new String JavaDoc[]{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 JavaDoc result= new ArrayList JavaDoc(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 JavaDoc 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     /**
148      * Finds a method in a list of methods. Compares methods by signature
149      * (only SimpleNames of types), and not by the declaring type.
150      * @return The found method or <code>null</code>, if nothing found
151      */

152     public static IMethod findMethod(IMethod method, IMethod[] allMethods) throws JavaModelException {
153         String JavaDoc name= method.getElementName();
154         String JavaDoc[] 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