KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > commands > JavaElementReferenceConverter


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.ui.commands;
12
13 import org.eclipse.core.commands.AbstractParameterValueConverter;
14 import org.eclipse.core.commands.ParameterValueConversionException;
15
16 import org.eclipse.core.resources.ResourcesPlugin;
17
18 import org.eclipse.jdt.core.IField;
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.IJavaModel;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.IMethod;
23 import org.eclipse.jdt.core.IType;
24 import org.eclipse.jdt.core.JavaCore;
25 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.jdt.core.Signature;
27
28 /**
29  * A command parameter value converter to convert between Java elements and
30  * String references that identify them.
31  * <p>
32  * References can be made to Java types, methods and fields. The reference
33  * identifies the project to use as a search scope as well as the java element
34  * information. Note that non-source elements may be referenced (such as
35  * java.lang.Object), but they must be resolved within the scope of some
36  * project.
37  * </p>
38  * <p>
39  * References take the form:
40  *
41  * <pre>
42  * elementRef := typeRef | fieldRef | methodRef
43  * typeRef := projectName '/' fullyQualifiedTypeName
44  * fieldRef := typeRef '#' fieldName
45  * methodRef := typeRef '#' methodName '(' parameterSignatures ')'
46  * </pre>
47  *
48  * where <code>parameterSignatures</code> uses the signature format documented
49  * in the {@link org.eclipse.jdt.core.Signature Signature} class.
50  * </p>
51  *
52  * @since 3.2
53  */

54 public class JavaElementReferenceConverter extends AbstractParameterValueConverter {
55
56     private static final char PROJECT_END_CHAR= '/';
57
58     private static final char TYPE_END_CHAR= '#';
59
60     private static final char PARAM_START_CHAR= Signature.C_PARAM_START;
61
62     private static final char PARAM_END_CHAR= Signature.C_PARAM_END;
63
64     public Object JavaDoc convertToObject(String JavaDoc parameterValue) throws ParameterValueConversionException {
65
66         assertWellFormed(parameterValue != null);
67
68         final int projectEndPosition= parameterValue.indexOf(PROJECT_END_CHAR);
69         assertWellFormed(projectEndPosition != -1);
70
71         String JavaDoc projectName= parameterValue.substring(0, projectEndPosition);
72         String JavaDoc javaElementRef= parameterValue.substring(projectEndPosition + 1);
73
74         IJavaModel javaModel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
75         assertExists(javaModel);
76
77         IJavaProject javaProject= javaModel.getJavaProject(projectName);
78         assertExists(javaProject);
79
80         final int typeEndPosition= javaElementRef.indexOf(TYPE_END_CHAR);
81         String JavaDoc typeName;
82         if (typeEndPosition == -1) {
83             typeName= javaElementRef;
84         } else {
85             typeName= javaElementRef.substring(0, typeEndPosition);
86         }
87
88         IType type= null;
89         try {
90             type= javaProject.findType(typeName);
91         } catch (JavaModelException ex) {
92             // type == null
93
}
94         assertExists(type);
95
96         if (typeEndPosition == -1) {
97             return type;
98         }
99
100         String JavaDoc memberRef= javaElementRef.substring(typeEndPosition + 1);
101
102         final int paramStartPosition= memberRef.indexOf(PARAM_START_CHAR);
103         if (paramStartPosition == -1) {
104             IField field= type.getField(memberRef);
105             assertExists(field);
106             return field;
107         }
108         String JavaDoc methodName= memberRef.substring(0, paramStartPosition);
109         String JavaDoc signature= memberRef.substring(paramStartPosition);
110         String JavaDoc[] parameterTypes= null;
111         try {
112             parameterTypes= Signature.getParameterTypes(signature);
113         } catch (IllegalArgumentException JavaDoc ex) {
114             // parameterTypes == null
115
}
116         assertWellFormed(parameterTypes != null);
117         IMethod method= type.getMethod(methodName, parameterTypes);
118         assertExists(method);
119         return method;
120     }
121
122     /**
123      * Throws a <code>ParameterValueConversionException</code> if the java
124      * element reference string does not meet some well-formedness condition.
125      *
126      * @param assertion
127      * a boolean check for well-formedness
128      * @throws ParameterValueConversionException
129      */

130     private void assertWellFormed(boolean assertion) throws ParameterValueConversionException {
131         if (!assertion) {
132             throw new ParameterValueConversionException("Malformed parameterValue"); //$NON-NLS-1$
133
}
134     }
135
136     /**
137      * Throws a <code>ParameterValueConversionException</code> if the java
138      * element reference string identifies an element that does not exist.
139      *
140      * @param javaElement
141      * an element to check for existence
142      * @throws ParameterValueConversionException
143      */

144     private void assertExists(IJavaElement javaElement) throws ParameterValueConversionException {
145         if ((javaElement == null) || (!javaElement.exists())) {
146             throw new ParameterValueConversionException("parameterValue must reference an existing IJavaElement"); //$NON-NLS-1$
147
}
148     }
149
150     public String JavaDoc convertToString(Object JavaDoc parameterValue) throws ParameterValueConversionException {
151
152         if (!(parameterValue instanceof IJavaElement)) {
153             throw new ParameterValueConversionException("parameterValue must be an IJavaElement"); //$NON-NLS-1$
154
}
155
156         IJavaElement javaElement= (IJavaElement) parameterValue;
157
158         IJavaProject javaProject= javaElement.getJavaProject();
159         if (javaProject == null) {
160             throw new ParameterValueConversionException("Could not get IJavaProject for element"); //$NON-NLS-1$
161
}
162
163         StringBuffer JavaDoc buffer;
164
165         if (javaElement instanceof IType) {
166             IType type= (IType) javaElement;
167             buffer= composeTypeReference(type);
168         } else
169             if (javaElement instanceof IMethod) {
170                 IMethod method= (IMethod) javaElement;
171                 buffer= composeTypeReference(method.getDeclaringType());
172                 buffer.append(TYPE_END_CHAR);
173                 buffer.append(method.getElementName());
174                 String JavaDoc[] parameterTypes= method.getParameterTypes();
175                 buffer.append(PARAM_START_CHAR);
176                 for (int i= 0; i < parameterTypes.length; i++) {
177                     buffer.append(parameterTypes[i]);
178                 }
179                 buffer.append(PARAM_END_CHAR);
180             } else
181                 if (javaElement instanceof IField) {
182                     IField field= (IField) javaElement;
183                     buffer= composeTypeReference(field.getDeclaringType());
184                     buffer.append(TYPE_END_CHAR);
185                     buffer.append(field.getElementName());
186                 } else {
187                     throw new ParameterValueConversionException("Unsupported IJavaElement type"); //$NON-NLS-1$
188
}
189
190         return buffer.toString();
191     }
192
193     private StringBuffer JavaDoc composeTypeReference(IType type) {
194         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
195         buffer.append(type.getJavaProject().getElementName());
196         buffer.append(PROJECT_END_CHAR);
197         buffer.append(type.getFullyQualifiedName());
198         return buffer;
199     }
200
201 }
202
Popular Tags