1 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 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 convertToObject(String 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 projectName= parameterValue.substring(0, projectEndPosition); 72 String 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 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 } 94 assertExists(type); 95 96 if (typeEndPosition == -1) { 97 return type; 98 } 99 100 String 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 methodName= memberRef.substring(0, paramStartPosition); 109 String signature= memberRef.substring(paramStartPosition); 110 String [] parameterTypes= null; 111 try { 112 parameterTypes= Signature.getParameterTypes(signature); 113 } catch (IllegalArgumentException ex) { 114 } 116 assertWellFormed(parameterTypes != null); 117 IMethod method= type.getMethod(methodName, parameterTypes); 118 assertExists(method); 119 return method; 120 } 121 122 130 private void assertWellFormed(boolean assertion) throws ParameterValueConversionException { 131 if (!assertion) { 132 throw new ParameterValueConversionException("Malformed parameterValue"); } 134 } 135 136 144 private void assertExists(IJavaElement javaElement) throws ParameterValueConversionException { 145 if ((javaElement == null) || (!javaElement.exists())) { 146 throw new ParameterValueConversionException("parameterValue must reference an existing IJavaElement"); } 148 } 149 150 public String convertToString(Object parameterValue) throws ParameterValueConversionException { 151 152 if (!(parameterValue instanceof IJavaElement)) { 153 throw new ParameterValueConversionException("parameterValue must be an IJavaElement"); } 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"); } 162 163 StringBuffer 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 [] 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"); } 189 190 return buffer.toString(); 191 } 192 193 private StringBuffer composeTypeReference(IType type) { 194 StringBuffer buffer= new StringBuffer (); 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 |