/** * Tries to find the given {@link IApiMethod} in the given {@link IType}. If a matching method is * not found <code>null</code> is returned * * @param type the type top look in for the given {@link IApiMethod} * @param method the {@link IApiMethod} to look for * @return the {@link IMethod} from the given {@link IType} that matches the given {@link * IApiMethod} or <code>null</code> if no matching method is found * @throws JavaModelException * @throws CoreException */ protected IMethod findMethodInType(IType type, IApiMethod method) throws JavaModelException, CoreException { String[] parameterTypes = Signature.getParameterTypes(method.getSignature()); for (int i = 0; i < parameterTypes.length; i++) { parameterTypes[i] = parameterTypes[i].replace('/', '.'); } String methodname = method.getName(); if (method.isConstructor()) { IApiType enclosingType = method.getEnclosingType(); if (enclosingType.isMemberType() && !Flags.isStatic(enclosingType.getModifiers())) { // remove the synthetic argument that corresponds to the enclosing type int length = parameterTypes.length - 1; System.arraycopy(parameterTypes, 1, (parameterTypes = new String[length]), 0, length); } methodname = enclosingType.getSimpleName(); } IMethod Qmethod = type.getMethod(methodname, parameterTypes); IMethod[] methods = type.getMethods(); IMethod match = null; for (int i = 0; i < methods.length; i++) { IMethod m = methods[i]; if (m.isSimilar(Qmethod)) { match = m; break; } } return match; }
public Object convertToObject(String parameterValue) throws ParameterValueConversionException { assertWellFormed(parameterValue != null); final int projectEndPosition = parameterValue.indexOf(PROJECT_END_CHAR); assertWellFormed(projectEndPosition != -1); String projectName = parameterValue.substring(0, projectEndPosition); String javaElementRef = parameterValue.substring(projectEndPosition + 1); IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); assertExists(javaModel); IJavaProject javaProject = javaModel.getJavaProject(projectName); assertExists(javaProject); final int typeEndPosition = javaElementRef.indexOf(TYPE_END_CHAR); String typeName; if (typeEndPosition == -1) { typeName = javaElementRef; } else { typeName = javaElementRef.substring(0, typeEndPosition); } IType type = null; try { type = javaProject.findType(typeName); } catch (JavaModelException ex) { // type == null } assertExists(type); if (typeEndPosition == -1) { return type; } String memberRef = javaElementRef.substring(typeEndPosition + 1); final int paramStartPosition = memberRef.indexOf(PARAM_START_CHAR); if (paramStartPosition == -1) { IField field = type.getField(memberRef); assertExists(field); return field; } String methodName = memberRef.substring(0, paramStartPosition); String signature = memberRef.substring(paramStartPosition); String[] parameterTypes = null; try { parameterTypes = Signature.getParameterTypes(signature); } catch (IllegalArgumentException ex) { // parameterTypes == null } assertWellFormed(parameterTypes != null); IMethod method = type.getMethod(methodName, parameterTypes); assertExists(method); return method; }
public static String[] getParameterTypesString(IMethod method) { try { String[] parameterQualifiedTypes = Signature.getParameterTypes(method.getSignature()); int length = parameterQualifiedTypes == null ? 0 : parameterQualifiedTypes.length; String[] parameterPackages = new String[length]; for (int i = 0; i < length; i++) { parameterQualifiedTypes[i] = parameterQualifiedTypes[i].replace('/', '.'); parameterPackages[i] = Signature.getSignatureSimpleName(parameterQualifiedTypes[i]); } return parameterPackages; } catch (IllegalArgumentException e) { } catch (JavaModelException e) { } return null; }
private boolean checkParameters( char[] methodDescriptor, char[][] parameterSimpleNames, char[][] parameterQualifications, boolean isCaseSensitive, boolean isCamelCase) { char[][] arguments = Signature.getParameterTypes(methodDescriptor); int parameterCount = parameterSimpleNames.length; if (parameterCount != arguments.length) return false; for (int i = 0; i < parameterCount; i++) if (!checkTypeName( parameterSimpleNames[i], parameterQualifications[i], Signature.toCharArray(arguments[i]), isCaseSensitive, isCamelCase)) return false; return true; }
/** * Takes a method signature <code> * [< typeVariableName : formalTypeDecl >] ( paramTypeSig1* ) retTypeSig</code> and returns * it with any parameter signatures filtered through <code>getLowerBound</code> and the return * type filtered through <code>getUpperBound</code>. Any preceding formal type variable * declarations are removed. * * <p>TODO this is a temporary workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=83600 * * @param signature the method signature to convert * @return the signature with no bounded types */ public static char[] unboundedSignature(char[] signature) { if (signature == null || signature.length < 2) return signature; final boolean BUG_83600 = true; // XXX the signatures from CompletionRequestor contain a superfluous '+' // before type parameters to parameter types if (BUG_83600) { signature = fix83600(signature); } StringBuffer res = new StringBuffer("("); // $NON-NLS-1$ char[][] parameters = Signature.getParameterTypes(signature); for (int i = 0; i < parameters.length; i++) { char[] param = parameters[i]; res.append(getLowerBound(param)); } res.append(')'); res.append(getUpperBound(Signature.getReturnType(signature))); return res.toString().toCharArray(); }
/** * Determines is the java element contains a specific method. * * <p>The syntax for the property tester is of the form: methodname, signature, modifiers. * * <ol> * <li>methodname - case sensitive method name, required. For example, <code>toString</code>. * <li>signature - JLS style method signature, required. For example, <code>(QString;)V</code>. * <li>modifiers - optional space separated list of modifiers, for example, <code>public static * </code>. * </ol> * * @param element the element to check for the method * @param args first arg is method name, secondary args are parameter types signatures * @return true if the method is found in the element, false otherwise */ private boolean hasMethod(IJavaElement element, Object[] args) { try { if (args.length > 1) { IType type = getType(element); if (type != null && type.exists()) { String name = (String) args[0]; String signature = (String) args[1]; String[] parms = Signature.getParameterTypes(signature); String returnType = Signature.getReturnType(signature); IMethod candidate = type.getMethod(name, parms); if (candidate.exists()) { // check return type if (candidate.getReturnType().equals(returnType)) { // check modifiers if (args.length > 2) { String modifierText = (String) args[2]; String[] modifiers = modifierText.split(" "); // $NON-NLS-1$ int flags = 0; for (int j = 0; j < modifiers.length; j++) { String modifier = modifiers[j]; Integer flag = (Integer) fgModifiers.get(modifier); if (flag != null) { flags = flags | flag.intValue(); } } if (candidate.getFlags() == flags) { return true; } } } } } } } catch (JavaModelException e) { } return false; }
/* * Look for annotations references */ private void matchAnnotations( SearchPattern pattern, MatchLocator locator, ClassFile classFile, IBinaryType binaryType) throws CoreException { // Only process TypeReference patterns switch (pattern.kind) { case TYPE_REF_PATTERN: break; case OR_PATTERN: SearchPattern[] patterns = ((OrPattern) pattern).patterns; for (int i = 0, length = patterns.length; i < length; i++) { matchAnnotations(patterns[i], locator, classFile, binaryType); } // $FALL-THROUGH$ - fall through default to return default: return; } TypeReferencePattern typeReferencePattern = (TypeReferencePattern) pattern; // Look for references in class annotations IBinaryAnnotation[] annotations = binaryType.getAnnotations(); BinaryType classFileBinaryType = (BinaryType) classFile.getType(); BinaryTypeBinding binaryTypeBinding = null; if (checkAnnotations(typeReferencePattern, annotations, binaryType.getTagBits())) { classFileBinaryType = new ResolvedBinaryType( (JavaElement) classFileBinaryType.getParent(), classFileBinaryType.getElementName(), classFileBinaryType.getKey()); TypeReferenceMatch match = new TypeReferenceMatch( classFileBinaryType, SearchMatch.A_ACCURATE, -1, 0, false, locator.getParticipant(), locator.currentPossibleMatch.resource); // TODO 3.4 M7 (frederic) - bug 209996: see how create the annotation handle from the binary // and put it in the local element match.setLocalElement(null); locator.report(match); } // Look for references in methods annotations MethodInfo[] methods = (MethodInfo[]) binaryType.getMethods(); if (methods != null) { for (int i = 0, max = methods.length; i < max; i++) { MethodInfo method = methods[i]; if (checkAnnotations(typeReferencePattern, method.getAnnotations(), method.getTagBits())) { binaryTypeBinding = locator.cacheBinaryType(classFileBinaryType, binaryType); IMethod methodHandle = classFileBinaryType.getMethod( new String( method.isConstructor() ? binaryTypeBinding .compoundName[binaryTypeBinding.compoundName.length - 1] : method.getSelector()), CharOperation.toStrings( Signature.getParameterTypes( convertClassFileFormat(method.getMethodDescriptor())))); TypeReferenceMatch match = new TypeReferenceMatch( methodHandle, SearchMatch.A_ACCURATE, -1, 0, false, locator.getParticipant(), locator.currentPossibleMatch.resource); // TODO 3.4 M7 (frederic) - bug 209996: see how create the annotation handle from the // binary and put it in the local element match.setLocalElement(null); locator.report(match); } } } // Look for references in fields annotations FieldInfo[] fields = (FieldInfo[]) binaryType.getFields(); if (fields != null) { for (int i = 0, max = fields.length; i < max; i++) { FieldInfo field = fields[i]; if (checkAnnotations(typeReferencePattern, field.getAnnotations(), field.getTagBits())) { IField fieldHandle = classFileBinaryType.getField(new String(field.getName())); TypeReferenceMatch match = new TypeReferenceMatch( fieldHandle, SearchMatch.A_ACCURATE, -1, 0, false, locator.getParticipant(), locator.currentPossibleMatch.resource); // TODO 3.4 M7 (frederic) - bug 209996: see how create the annotation handle from the // binary and put it in the local element match.setLocalElement(null); locator.report(match); } } } }
/** Locate declaration in the current class file. This class file is always in a jar. */ public void locateMatches(MatchLocator locator, ClassFile classFile, IBinaryType info) throws CoreException { SearchPattern pattern = locator.pattern; // check annotations references matchAnnotations(pattern, locator, classFile, info); // check class definition BinaryType binaryType = (BinaryType) classFile.getType(); if (matchBinary(pattern, info, null)) { binaryType = new ResolvedBinaryType( (JavaElement) binaryType.getParent(), binaryType.getElementName(), binaryType.getKey()); locator.reportBinaryMemberDeclaration(null, binaryType, null, info, SearchMatch.A_ACCURATE); return; } // Define arrays to store methods/fields from binary type if necessary IBinaryMethod[] binaryMethods = info.getMethods(); int bMethodsLength = binaryMethods == null ? 0 : binaryMethods.length; IBinaryMethod[] unresolvedMethods = null; char[][] binaryMethodSignatures = null; boolean hasUnresolvedMethods = false; // Get fields from binary type info IBinaryField[] binaryFields = info.getFields(); int bFieldsLength = binaryFields == null ? 0 : binaryFields.length; IBinaryField[] unresolvedFields = null; boolean hasUnresolvedFields = false; // Report as many accurate matches as possible int accuracy = SearchMatch.A_ACCURATE; boolean mustResolve = pattern.mustResolve; if (mustResolve) { BinaryTypeBinding binding = locator.cacheBinaryType(binaryType, info); if (binding != null) { // filter out element not in hierarchy scope if (!locator.typeInHierarchy(binding)) return; // Search matches on resolved methods MethodBinding[] availableMethods = binding.availableMethods(); int aMethodsLength = availableMethods == null ? 0 : availableMethods.length; hasUnresolvedMethods = bMethodsLength != aMethodsLength; for (int i = 0; i < aMethodsLength; i++) { MethodBinding method = availableMethods[i]; char[] methodSignature = method.genericSignature(); if (methodSignature == null) methodSignature = method.signature(); // Report the match if possible int level = locator.patternLocator.resolveLevel(method); if (level != PatternLocator.IMPOSSIBLE_MATCH) { IMethod methodHandle = binaryType.getMethod( new String( method.isConstructor() ? binding.compoundName[binding.compoundName.length - 1] : method.selector), CharOperation.toStrings( Signature.getParameterTypes(convertClassFileFormat(methodSignature)))); accuracy = level == PatternLocator.ACCURATE_MATCH ? SearchMatch.A_ACCURATE : SearchMatch.A_INACCURATE; locator.reportBinaryMemberDeclaration(null, methodHandle, method, info, accuracy); } // Remove method from unresolved list if (hasUnresolvedMethods) { if (binaryMethodSignatures == null) { // Store binary method signatures to avoid multiple computation binaryMethodSignatures = new char[bMethodsLength][]; for (int j = 0; j < bMethodsLength; j++) { IBinaryMethod binaryMethod = binaryMethods[j]; char[] signature = binaryMethod.getGenericSignature(); if (signature == null) signature = binaryMethod.getMethodDescriptor(); binaryMethodSignatures[j] = signature; } } for (int j = 0; j < bMethodsLength; j++) { if (CharOperation.equals(binaryMethods[j].getSelector(), method.selector) && CharOperation.equals(binaryMethodSignatures[j], methodSignature)) { if (unresolvedMethods == null) { System.arraycopy( binaryMethods, 0, unresolvedMethods = new IBinaryMethod[bMethodsLength], 0, bMethodsLength); } unresolvedMethods[j] = null; break; } } } } // Search matches on resolved fields FieldBinding[] availableFields = binding.availableFields(); int aFieldsLength = availableFields == null ? 0 : availableFields.length; hasUnresolvedFields = bFieldsLength != aFieldsLength; for (int i = 0; i < aFieldsLength; i++) { FieldBinding field = availableFields[i]; // Report the match if possible int level = locator.patternLocator.resolveLevel(field); if (level != PatternLocator.IMPOSSIBLE_MATCH) { IField fieldHandle = binaryType.getField(new String(field.name)); accuracy = level == PatternLocator.ACCURATE_MATCH ? SearchMatch.A_ACCURATE : SearchMatch.A_INACCURATE; locator.reportBinaryMemberDeclaration(null, fieldHandle, field, info, accuracy); } // Remove the field from unresolved list if (hasUnresolvedFields) { for (int j = 0; j < bFieldsLength; j++) { if (CharOperation.equals(binaryFields[j].getName(), field.name)) { if (unresolvedFields == null) { System.arraycopy( binaryFields, 0, unresolvedFields = new IBinaryField[bFieldsLength], 0, bFieldsLength); } unresolvedFields[j] = null; break; } } } } // If all methods/fields were accurate then returns now if (!hasUnresolvedMethods && !hasUnresolvedFields) { return; } } accuracy = SearchMatch.A_INACCURATE; } // Report inaccurate methods if (mustResolve) binaryMethods = unresolvedMethods; bMethodsLength = binaryMethods == null ? 0 : binaryMethods.length; for (int i = 0; i < bMethodsLength; i++) { IBinaryMethod method = binaryMethods[i]; if (method == null) continue; // impossible match or already reported as accurate if (matchBinary(pattern, method, info)) { char[] name; if (method.isConstructor()) { name = info.getName(); int lastSlash = CharOperation.lastIndexOf('/', name); if (lastSlash != -1) { name = CharOperation.subarray(name, lastSlash + 1, name.length); } } else { name = method.getSelector(); } String selector = new String(name); char[] methodSignature = binaryMethodSignatures == null ? null : binaryMethodSignatures[i]; if (methodSignature == null) { methodSignature = method.getGenericSignature(); if (methodSignature == null) methodSignature = method.getMethodDescriptor(); } String[] parameterTypes = CharOperation.toStrings( Signature.getParameterTypes(convertClassFileFormat(methodSignature))); IMethod methodHandle = binaryType.getMethod(selector, parameterTypes); methodHandle = new ResolvedBinaryMethod(binaryType, selector, parameterTypes, methodHandle.getKey()); locator.reportBinaryMemberDeclaration(null, methodHandle, null, info, accuracy); } } // Report inaccurate fields if (mustResolve) binaryFields = unresolvedFields; bFieldsLength = binaryFields == null ? 0 : binaryFields.length; for (int i = 0; i < bFieldsLength; i++) { IBinaryField field = binaryFields[i]; if (field == null) continue; // impossible match or already reported as accurate if (matchBinary(pattern, field, info)) { String fieldName = new String(field.getName()); IField fieldHandle = binaryType.getField(fieldName); fieldHandle = new ResolvedBinaryField(binaryType, fieldName, fieldHandle.getKey()); locator.reportBinaryMemberDeclaration(null, fieldHandle, null, info, accuracy); } } }