@SuppressWarnings("deprecation") private boolean isParameterNamesAvailable() throws Exception { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setIgnoreMethodBodies(true); IJavaProject javaProject = projectProvider.getJavaProject(resourceSet); parser.setProject(javaProject); IType type = javaProject.findType("org.eclipse.xtext.common.types.testSetups.TestEnum"); IBinding[] bindings = parser.createBindings(new IJavaElement[] {type}, null); ITypeBinding typeBinding = (ITypeBinding) bindings[0]; IMethodBinding[] methods = typeBinding.getDeclaredMethods(); for (IMethodBinding method : methods) { if (method.isConstructor()) { IMethod element = (IMethod) method.getJavaElement(); if (element.exists()) { String[] parameterNames = element.getParameterNames(); if (parameterNames.length == 1 && parameterNames[0].equals("string")) { return true; } } else { return false; } } } return false; }
/** Returns all declared constructors for a specified type. */ public static Set<IMethodBinding> getDeclaredConstructors(ITypeBinding type) { Set<IMethodBinding> constructors = Sets.newHashSet(); for (IMethodBinding m : type.getDeclaredMethods()) { if (m.isConstructor()) { constructors.add(m); } } return constructors; }
/** * Returns an alphabetically sorted list of an annotation type's members. This is necessary since * an annotation's values can be specified in any order, but the annotation's constructor needs to * be invoked using its declaration order. */ public static IMethodBinding[] getSortedAnnotationMembers(ITypeBinding annotation) { IMethodBinding[] members = annotation.getDeclaredMethods(); Arrays.sort( members, new Comparator<IMethodBinding>() { @Override public int compare(IMethodBinding o1, IMethodBinding o2) { return o1.getName().compareTo(o2.getName()); } }); return members; }
/** Returns the method binding for a specific method of a specific type. */ public static IMethodBinding findDeclaredMethod( ITypeBinding type, String methodName, String... paramTypes) { outer: for (IMethodBinding method : type.getDeclaredMethods()) { if (method.getName().equals(methodName)) { ITypeBinding[] foundParamTypes = method.getParameterTypes(); if (paramTypes.length == foundParamTypes.length) { for (int i = 0; i < paramTypes.length; i++) { if (!paramTypes[i].equals(foundParamTypes[i].getQualifiedName())) { continue outer; } } return method; } } } return null; }
/** * Finds the declaration for a given method and receiver in the same way that the ObjC compiler * will search for a declaration. */ private IMethodBinding getFirstDeclaration(String methodSig, ITypeBinding type) { if (type == null) { return null; } type = type.getTypeDeclaration(); for (IMethodBinding declaredMethod : type.getDeclaredMethods()) { if (methodSig.equals(getObjCMethodSignature(declaredMethod))) { return declaredMethod; } } List<ITypeBinding> supertypes = Lists.newArrayList(); supertypes.addAll(Arrays.asList(type.getInterfaces())); supertypes.add(type.isTypeVariable() ? 0 : supertypes.size(), type.getSuperclass()); for (ITypeBinding supertype : supertypes) { IMethodBinding result = getFirstDeclaration(methodSig, supertype); if (result != null) { return result; } } return null; }
private void printAnnotationConstructor(ITypeBinding annotation) { print(annotationConstructorDeclaration(annotation)); println(" {"); println(" if ((self = [super init])) {"); for (IMethodBinding member : annotation.getDeclaredMethods()) { String name = member.getName(); printf(" %s = ", name); ITypeBinding type = member.getReturnType(); boolean needsRetain = !type.isPrimitive(); if (needsRetain) { print("RETAIN_("); } printf("%s_", name); if (needsRetain) { print(')'); } println(";"); } println(" }"); println(" return self;"); println("}\n"); }
private IJavaElement declarationMatched(IJavaElement javaElement, IBindingProvider mirror) { if (mirror != null) { parser.setProject(typeRoot.getJavaProject()); IBinding[] bindings = parser.createBindings(new IJavaElement[] {javaElement}, null); if (bindings.length > 0 && bindings[0] != null) { if (mirror instanceof JDTMethod && bindings[0] instanceof ITypeBinding) { // Case of a constructor : let's go to the constructor and not to the type. ITypeBinding typeBinding = (ITypeBinding) bindings[0]; for (IMethodBinding methodBinding : typeBinding.getDeclaredMethods()) { // if (methodBinding.isConstructor()) { if (CharOperation.equals( methodBinding.getKey().toCharArray(), mirror.getBindingKey())) { return methodBinding.getJavaElement(); } // } } } if (CharOperation.equals(bindings[0].getKey().toCharArray(), mirror.getBindingKey())) { return javaElement; } } } return null; }