|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 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 |
| 55 |
AbstractParameterValueConverter { |
| 56 |
|
| 57 |
private static final char PROJECT_END_CHAR = '/'; |
| 58 |
|
| 59 |
private static final char TYPE_END_CHAR = '#'; |
| 60 |
|
| 61 |
private static final char PARAM_START_CHAR = Signature.C_PARAM_START; |
| 62 |
|
| 63 |
private static final char PARAM_END_CHAR = Signature.C_PARAM_END; |
| 64 |
|
| 65 |
public Object convertToObject(String parameterValue) |
| 66 |
throws ParameterValueConversionException { |
| 67 |
|
| 68 |
assertWellFormed(parameterValue != null); |
| 69 |
|
| 70 |
final int projectEndPosition = parameterValue.indexOf(PROJECT_END_CHAR); |
| 71 |
assertWellFormed(projectEndPosition != -1); |
| 72 |
|
| 73 |
String projectName = parameterValue.substring(0, projectEndPosition); |
| 74 |
String javaElementRef = parameterValue |
| 75 |
.substring(projectEndPosition + 1); |
| 76 |
|
| 77 |
IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace() |
| 78 |
.getRoot()); |
| 79 |
assertExists(javaModel); |
| 80 |
|
| 81 |
IJavaProject javaProject = javaModel.getJavaProject(projectName); |
| 82 |
assertExists(javaProject); |
| 83 |
|
| 84 |
final int typeEndPosition = javaElementRef.indexOf(TYPE_END_CHAR); |
| 85 |
String typeName; |
| 86 |
if (typeEndPosition == -1) { |
| 87 |
typeName = javaElementRef; |
| 88 |
} else { |
| 89 |
typeName = javaElementRef.substring(0, typeEndPosition); |
| 90 |
} |
| 91 |
|
| 92 |
IType type = null; |
| 93 |
try { |
| 94 |
type = javaProject.findType(typeName); |
| 95 |
} catch (JavaModelException ex) { |
| 96 |
// type == null |
| 97 |
} |
| 98 |
assertExists(type); |
| 99 |
|
| 100 |
if (typeEndPosition == -1) { |
| 101 |
return type; |
| 102 |
} |
| 103 |
|
| 104 |
String memberRef = javaElementRef.substring(typeEndPosition + 1); |
| 105 |
|
| 106 |
final int paramStartPosition = memberRef.indexOf(PARAM_START_CHAR); |
| 107 |
if (paramStartPosition == -1) { |
| 108 |
IField field = type.getField(memberRef); |
| 109 |
assertExists(field); |
| 110 |
return field; |
| 111 |
} |
| 112 |
String methodName = memberRef.substring(0, paramStartPosition); |
| 113 |
String signature = memberRef.substring(paramStartPosition); |
| 114 |
String[] parameterTypes = null; |
| 115 |
try { |
| 116 |
parameterTypes = Signature.getParameterTypes(signature); |
| 117 |
} catch (IllegalArgumentException ex) { |
| 118 |
// parameterTypes == null |
| 119 |
} |
| 120 |
assertWellFormed(parameterTypes != null); |
| 121 |
IMethod method = type.getMethod(methodName, parameterTypes); |
| 122 |
assertExists(method); |
| 123 |
return method; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Throws a <code>ParameterValueConversionException</code> if the java |
| 128 |
* element reference string does not meet some well-formedness condition. |
| 129 |
* |
| 130 |
* @param assertion |
| 131 |
* a boolean check for well-formedness |
| 132 |
* @throws ParameterValueConversionException |
| 133 |
*/ |
| 134 |
private void assertWellFormed(boolean assertion) |
| 135 |
throws ParameterValueConversionException { |
| 136 |
if (!assertion) { |
| 137 |
throw new ParameterValueConversionException( |
| 138 |
"Malformed parameterValue"); //$NON-NLS-1$ |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Throws a <code>ParameterValueConversionException</code> if the java |
| 144 |
* element reference string identifes an element that does not exist. |
| 145 |
* |
| 146 |
* @param javaElement |
| 147 |
* an element to check for existence |
| 148 |
* @throws ParameterValueConversionException |
| 149 |
*/ |
| 150 |
private void assertExists(IJavaElement javaElement) |
| 151 |
throws ParameterValueConversionException { |
| 152 |
if ((javaElement == null) || (!javaElement.exists())) { |
| 153 |
throw new ParameterValueConversionException( |
| 154 |
"parameterValue must reference an existing IJavaElement"); //$NON-NLS-1$ |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
public String convertToString(Object parameterValue) |
| 159 |
throws ParameterValueConversionException { |
| 160 |
|
| 161 |
if (!(parameterValue instanceof IJavaElement)) { |
| 162 |
throw new ParameterValueConversionException( |
| 163 |
"parameterValue must be an IJavaElement"); //$NON-NLS-1$ |
| 164 |
} |
| 165 |
|
| 166 |
IJavaElement javaElement = (IJavaElement) parameterValue; |
| 167 |
|
| 168 |
IJavaProject javaProject = javaElement.getJavaProject(); |
| 169 |
if (javaProject == null) { |
| 170 |
throw new ParameterValueConversionException( |
| 171 |
"Could not get IJavaProject for element"); //$NON-NLS-1$ |
| 172 |
} |
| 173 |
|
| 174 |
StringBuffer buffer; |
| 175 |
|
| 176 |
if (javaElement instanceof IType) { |
| 177 |
IType type = (IType) javaElement; |
| 178 |
buffer = composeTypeReference(type); |
| 179 |
} else if (javaElement instanceof IMethod) { |
| 180 |
IMethod method = (IMethod) javaElement; |
| 181 |
buffer = composeTypeReference(method.getDeclaringType()); |
| 182 |
buffer.append(TYPE_END_CHAR); |
| 183 |
buffer.append(method.getElementName()); |
| 184 |
String[] parameterTypes = method.getParameterTypes(); |
| 185 |
buffer.append(PARAM_START_CHAR); |
| 186 |
for (int i = 0; i < parameterTypes.length; i++) { |
| 187 |
buffer.append(parameterTypes[i]); |
| 188 |
} |
| 189 |
buffer.append(PARAM_END_CHAR); |
| 190 |
} else if (javaElement instanceof IField) { |
| 191 |
IField field = (IField) javaElement; |
| 192 |
buffer = composeTypeReference(field.getDeclaringType()); |
| 193 |
buffer.append(TYPE_END_CHAR); |
| 194 |
buffer.append(field.getElementName()); |
| 195 |
} else { |
| 196 |
throw new ParameterValueConversionException( |
| 197 |
"Unsupported IJavaElement type"); //$NON-NLS-1$ |
| 198 |
} |
| 199 |
|
| 200 |
return buffer.toString(); |
| 201 |
} |
| 202 |
|
| 203 |
private StringBuffer composeTypeReference(IType type) { |
| 204 |
StringBuffer buffer = new StringBuffer(); |
| 205 |
buffer.append(type.getJavaProject().getElementName()); |
| 206 |
buffer.append(PROJECT_END_CHAR); |
| 207 |
buffer.append(type.getFullyQualifiedName()); |
| 208 |
return buffer; |
| 209 |
} |
| 210 |
|
| 211 |
} |