Java Code Examples for org.eclipse.jdt.core.dom.ITypeBinding#getDeclaredMethods()

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#getDeclaredMethods() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static IMethodBinding[] getDelegateCandidates(ITypeBinding binding, ITypeBinding hierarchy) {
	List<IMethodBinding> allMethods= new ArrayList<IMethodBinding>();
	boolean isInterface= binding.isInterface();
	IMethodBinding[] typeMethods= binding.getDeclaredMethods();
	for (int index= 0; index < typeMethods.length; index++) {
		final int modifiers= typeMethods[index].getModifiers();
		if (!typeMethods[index].isConstructor() && !Modifier.isStatic(modifiers) && (isInterface || Modifier.isPublic(modifiers))) {
			IMethodBinding result= Bindings.findOverriddenMethodInHierarchy(hierarchy, typeMethods[index]);
			if (result != null && Flags.isFinal(result.getModifiers()))
				continue;
			ITypeBinding[] parameterBindings= typeMethods[index].getParameterTypes();
			boolean upper= false;
			for (int offset= 0; offset < parameterBindings.length; offset++) {
				if (parameterBindings[offset].isWildcardType() && parameterBindings[offset].isUpperbound())
					upper= true;
			}
			if (!upper)
				allMethods.add(typeMethods[index]);
		}
	}
	return allMethods.toArray(new IMethodBinding[allMethods.size()]);
}
 
Example 2
Source File: AbstractToStringGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * adds a comment (if necessary) and an <code>@Override</code> annotation to the generated
 * method
 * 
 * @throws CoreException if creation failed
 */
protected void createMethodComment() throws CoreException {
	ITypeBinding object= fAst.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
	IMethodBinding[] objms= object.getDeclaredMethods();
	IMethodBinding objectMethod= null;
	for (int i= 0; i < objms.length; i++) {
		if (objms[i].getName().equals(METHODNAME_TO_STRING) && objms[i].getParameterTypes().length == 0)
			objectMethod= objms[i];
	}
	if (fContext.isCreateComments()) {
		String docString= CodeGeneration.getMethodComment(fContext.getCompilationUnit(), fContext.getTypeBinding().getQualifiedName(), toStringMethod, objectMethod, StubUtility
				.getLineDelimiterUsed(fContext.getCompilationUnit()));
		if (docString != null) {
			Javadoc javadoc= (Javadoc)fContext.getASTRewrite().createStringPlaceholder(docString, ASTNode.JAVADOC);
			toStringMethod.setJavadoc(javadoc);
		}
	}
	if (fContext.isOverrideAnnotation() && fContext.is50orHigher())
		StubUtility2.addOverrideAnnotation(fContext.getTypeBinding().getJavaElement().getJavaProject(), fContext.getASTRewrite(), toStringMethod, objectMethod);
}
 
Example 3
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Finds the method specified by <code>methodName<code> and </code>parameters</code> in
 * the given <code>type</code>. Returns <code>null</code> if no such method exits.
 * @param type The type to search the method in
 * @param methodName The name of the method to find
 * @param parameters The parameter types of the method to find. If <code>null</code> is passed, only the name is matched and parameters are ignored.
 * @return the method binding representing the method
 */
public static IMethodBinding findMethodInType(ITypeBinding type, String methodName, String[] parameters) {
	if (type.isPrimitive())
		return null;
	IMethodBinding[] methods= type.getDeclaredMethods();
	for (int i= 0; i < methods.length; i++) {
		if (parameters == null) {
			if (methodName.equals(methods[i].getName()))
				return methods[i];
		} else {
			if (isEqualMethod(methods[i], methodName, parameters))
				return methods[i];
		}
	}
	return null;
}
 
Example 4
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ITypeBinding getExpressionType(final MethodInvocation invocation, ITypeBinding typeBinding) {
	if (typeBinding == null)
		return null;
	for (IMethodBinding iMethodBinding : typeBinding.getDeclaredMethods()) {
		if (invocation.resolveMethodBinding() == iMethodBinding)
			return typeBinding.getTypeDeclaration();
	}
	ITypeBinding expressionType= getExpressionType(invocation, typeBinding.getSuperclass());
	if (expressionType != null) {
		return expressionType;
	}
	for (ITypeBinding interfaceBinding : typeBinding.getInterfaces()) {
		expressionType= getExpressionType(invocation, interfaceBinding);
		if (expressionType != null) {
			return expressionType;
		}
	}
	return null;
}
 
Example 5
Source File: HashCodeEqualsHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static String[] findExistingMethods(ITypeBinding typeBinding) {
	List<String> existingMethods = new ArrayList<>();
	IMethodBinding[] declaredMethods = typeBinding.getDeclaredMethods();
	for (IMethodBinding method : declaredMethods) {
		if (method.getName().equals(METHODNAME_EQUALS)) {
			ITypeBinding[] b = method.getParameterTypes();
			if ((b.length == 1) && (b[0].getQualifiedName().equals("java.lang.Object"))) {
				existingMethods.add(METHODNAME_EQUALS);
			}
		} else if (method.getName().equals(METHODNAME_HASH_CODE) && method.getParameterTypes().length == 0) {
			existingMethods.add(METHODNAME_HASH_CODE);
		}
		if (existingMethods.size() == 2) {
			break;
		}
	}
	return existingMethods.toArray(new String[0]);
}
 
Example 6
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 6 votes vote down vote up
private Set<IMethodBinding> getDeclaredMethods(ITypeBinding typeBinding) {
	Set<IMethodBinding> declaredMethods = new LinkedHashSet<IMethodBinding>();
	//first add the directly declared methods
	for(IMethodBinding methodBinding : typeBinding.getDeclaredMethods()) {
		declaredMethods.add(methodBinding);
	}
	ITypeBinding superclassTypeBinding = typeBinding.getSuperclass();
	if(superclassTypeBinding != null) {
		declaredMethods.addAll(getDeclaredMethods(superclassTypeBinding));
	}
	ITypeBinding[] interfaces = typeBinding.getInterfaces();
	for(ITypeBinding interfaceTypeBinding : interfaces) {
		declaredMethods.addAll(getDeclaredMethods(interfaceTypeBinding));
	}
	return declaredMethods;
}
 
Example 7
Source File: NewAsyncRemoteServiceInterfaceCreationWizardPage.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private static List<IMethodBinding> computeOverridableMethodsForInterface(
    ITypeBinding interfaceBinding) {
  assert (interfaceBinding.isInterface());

  List<ITypeBinding> superInterfaces = new ArrayList<ITypeBinding>();
  RemoteServiceUtilities.expandSuperInterfaces(interfaceBinding,
      superInterfaces);

  List<IMethodBinding> overridableMethods = new ArrayList<IMethodBinding>();
  for (ITypeBinding superInterface : superInterfaces) {
    for (IMethodBinding declaredMethod : superInterface.getDeclaredMethods()) {
      if (findOverridingMethod(declaredMethod, overridableMethods) == null) {
        overridableMethods.add(declaredMethod);
      }
    }
  }

  return overridableMethods;
}
 
Example 8
Source File: JdtUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private static Set<IMethodBinding> getOverriddenMethodsInType(
    IMethodBinding methodBinding, ITypeBinding typeBinding) {
  Set<IMethodBinding> overriddenMethods = new HashSet<>();
  for (IMethodBinding declaredMethod : typeBinding.getDeclaredMethods()) {
    if (methodBinding.overrides(declaredMethod) && !methodBinding.isConstructor()) {
      checkArgument(!Modifier.isStatic(methodBinding.getModifiers()));
      overriddenMethods.add(declaredMethod);
    }
  }
  // Recurse into immediate super class and interfaces for overridden method.
  if (typeBinding.getSuperclass() != null) {
    overriddenMethods.addAll(
        getOverriddenMethodsInType(methodBinding, typeBinding.getSuperclass()));
  }
  for (ITypeBinding interfaceBinding : typeBinding.getInterfaces()) {
    overriddenMethods.addAll(getOverriddenMethodsInType(methodBinding, interfaceBinding));
  }

  ITypeBinding javaLangObjectTypeBinding = JdtUtils.javaLangObjectTypeBinding.get();
  if (typeBinding != javaLangObjectTypeBinding) {
    for (IMethodBinding objectMethodBinding : javaLangObjectTypeBinding.getDeclaredMethods()) {
      if (!isPolymorphic(objectMethodBinding)) {
        continue;
      }
      checkState(!getVisibility(objectMethodBinding).isPackagePrivate());
      if (methodBinding.isSubsignature(objectMethodBinding)) {
        overriddenMethods.add(objectMethodBinding);
      }
    }
  }

  return overriddenMethods;
}
 
Example 9
Source File: ExtractMethodInputPage.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean methodDeclaredInTypeBinding(ITypeBinding typeBinding, String methodName) {
	if(!typeBinding.getQualifiedName().equals("java.lang.Object") &&
			ASTReader.getSystemObject().getClassObject(typeBinding.getQualifiedName()) != null) {
		IMethodBinding[] declaredMethods = typeBinding.getDeclaredMethods();
		for(IMethodBinding declaredMethod : declaredMethods) {
			if(declaredMethod.getName().equals(methodName)) {
				return true;
			}
		}
	}
	return false;
}
 
Example 10
Source File: CallInliner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean visit(ITypeBinding node) {
	IMethodBinding[] methods= node.getDeclaredMethods();
	for (int i= 0; i < methods.length; i++) {
		IMethodBinding candidate= methods[i];
		if (candidate == fOriginal) {
			continue;
		}
		if (fOriginal.getName().equals(candidate.getName())) {
			if (canImplicitlyCall(candidate)) {
				return false;
			}
		}
	}
	return true;
}
 
Example 11
Source File: SuperTypeConstraintsCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the original methods of the method hierarchy of the specified method.
 *
 * @param binding the method binding
 * @param type the current type
 * @param originals the original methods which have already been found (element type: <code>IMethodBinding</code>)
 * @param implementations <code>true</code> to favor implementation methods, <code>false</code> otherwise
 */
private static void getOriginalMethods(final IMethodBinding binding, final ITypeBinding type, final Collection<IMethodBinding> originals, final boolean implementations) {
	final ITypeBinding ancestor= type.getSuperclass();
	if (!implementations) {
		final ITypeBinding[] types= type.getInterfaces();
		for (int index= 0; index < types.length; index++)
			getOriginalMethods(binding, types[index], originals, implementations);
		if (ancestor != null)
			getOriginalMethods(binding, ancestor, originals, implementations);
	}
	if (implementations && ancestor != null)
		getOriginalMethods(binding, ancestor, originals, implementations);
	final IMethodBinding[] methods= type.getDeclaredMethods();
	IMethodBinding method= null;
	for (int index= 0; index < methods.length; index++) {
		method= methods[index];
		if (!binding.getKey().equals(method.getKey())) {
			boolean match= false;
			IMethodBinding current= null;
			for (final Iterator<IMethodBinding> iterator= originals.iterator(); iterator.hasNext();) {
				current= iterator.next();
				if (Bindings.isSubsignature(method, current))
					match= true;
			}
			if (!match && Bindings.isSubsignature(binding, method))
				originals.add(method);
		}
	}
}
 
Example 12
Source File: AbstractPairedInterfaceValidator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public List<CategorizedProblem> validate(TypeDeclaration changedInterface,
    ITypeBinding dependentTypeBinding) {
  CompilationUnit compilationUnit = getCompilationUnit(changedInterface);
  if (JavaASTUtils.hasErrors(changedInterface, compilationUnit.getProblems())) {
    /*
     * Don't validate any type that already has errors (we are assuming that
     * it has JDT errors.
     */
    return Collections.emptyList();
  }

  if (dependentTypeBinding == null) {
    return doMissingDependentInterface(changedInterface);
  }

  // Check that for every method on the changed interface, there is a
  // corresponding method on the dependent interface
  List<CategorizedProblem> problems = new ArrayList<CategorizedProblem>();
  for (MethodDeclaration methodDeclaration : changedInterface.getMethods()) {
    List<CategorizedProblem> methodProblems = doValidateMethodOnChangedType(
        methodDeclaration, dependentTypeBinding);
    problems.addAll(methodProblems);
  }

  List<ITypeBinding> superTypeBindings = new ArrayList<ITypeBinding>();
  RemoteServiceUtilities.expandSuperInterfaces(dependentTypeBinding,
      superTypeBindings);

  for (ITypeBinding typeBinding : superTypeBindings) {
    for (IMethodBinding methodBinding : typeBinding.getDeclaredMethods()) {
      List<CategorizedProblem> dependentMethodProblems = doValidateMethodOnDependentInterface(
          methodBinding, changedInterface, typeBinding);
      problems.addAll(dependentMethodProblems);
    }
  }

  return problems;
}
 
Example 13
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean hasFieldWithName(ITypeBinding typeBinding, String name) {
	IMethodBinding[] methods= typeBinding.getDeclaredMethods();
	for (int i= 0; i < methods.length; i++) {
		if (methods[i].getName().equals(name)) {
			return true;
		}
	}
	ITypeBinding superclass= typeBinding.getSuperclass();
	if (superclass != null) {
		return hasMethodWithName(superclass, name);
	}
	return false;
}
 
Example 14
Source File: GenerateToStringAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public ToStringInfo(ITypeBinding typeBinding) {
	IMethodBinding[] declaredMethods= typeBinding.getDeclaredMethods();

	for (int i= 0; i < declaredMethods.length; i++) {
		if (declaredMethods[i].getName().equals(METHODNAME_TO_STRING) && declaredMethods[i].getParameterTypes().length == 0) {
			this.foundToString= true;
			if (Modifier.isFinal(declaredMethods[i].getModifiers()))
				this.foundFinalToString= true;
		}
	}
}
 
Example 15
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static DelegateEntry[] getDelegatableMethods(ITypeBinding binding) {
	final List<DelegateEntry> tuples= new ArrayList<DelegateEntry>();
	final List<IMethodBinding> declared= new ArrayList<IMethodBinding>();
	IMethodBinding[] typeMethods= binding.getDeclaredMethods();
	for (int index= 0; index < typeMethods.length; index++)
		declared.add(typeMethods[index]);
	IVariableBinding[] typeFields= binding.getDeclaredFields();
	for (int index= 0; index < typeFields.length; index++) {
		IVariableBinding fieldBinding= typeFields[index];
		if (fieldBinding.isField() && !fieldBinding.isEnumConstant() && !fieldBinding.isSynthetic())
			getDelegatableMethods(new ArrayList<IMethodBinding>(declared), fieldBinding, fieldBinding.getType(), binding, tuples);
	}
	// list of tuple<IVariableBinding, IMethodBinding>
	return tuples.toArray(new DelegateEntry[tuples.size()]);
}
 
Example 16
Source File: UnresolvedElementsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean hasFieldWithName(ITypeBinding typeBinding, String name) {
	IMethodBinding[] methods= typeBinding.getDeclaredMethods();
	for (int i= 0; i < methods.length; i++) {
		if (methods[i].getName().equals(name)) {
			return true;
		}
	}
	ITypeBinding superclass= typeBinding.getSuperclass();
	if (superclass != null) {
		return hasMethodWithName(superclass, name);
	}
	return false;
}
 
Example 17
Source File: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private MethodDeclaration createHashCodeMethod() throws CoreException {

		MethodDeclaration hashCodeMethod= fAst.newMethodDeclaration();
		hashCodeMethod.modifiers().addAll(ASTNodeFactory.newModifiers(fAst, Modifier.PUBLIC));
		hashCodeMethod.setName(fAst.newSimpleName(METHODNAME_HASH_CODE));
		hashCodeMethod.setConstructor(false);
		hashCodeMethod.setReturnType2(fAst.newPrimitiveType(PrimitiveType.INT));

		Block body= fAst.newBlock();
		hashCodeMethod.setBody(body);

		// PRIME NUMBER
		VariableDeclarationFragment frag= fAst.newVariableDeclarationFragment();
		frag.setName(fAst.newSimpleName(VARIABLE_NAME_PRIME));
		frag.setInitializer(fAst.newNumberLiteral(PRIME_NUMBER));

		VariableDeclarationStatement primeNumberDeclaration= fAst.newVariableDeclarationStatement(frag);
		primeNumberDeclaration.modifiers().add(fAst.newModifier(ModifierKeyword.FINAL_KEYWORD));
		primeNumberDeclaration.setType(fAst.newPrimitiveType(PrimitiveType.INT));
		body.statements().add(primeNumberDeclaration);

		// RESULT
		VariableDeclarationFragment fragment= fAst.newVariableDeclarationFragment();
		fragment.setName(fAst.newSimpleName(VARIABLE_NAME_RESULT));

		VariableDeclarationStatement resultDeclaration= fAst.newVariableDeclarationStatement(fragment);
		resultDeclaration.setType(fAst.newPrimitiveType(PrimitiveType.INT));
		body.statements().add(resultDeclaration);

		if (needsNoSuperCall(fType, METHODNAME_HASH_CODE, new ITypeBinding[0])) {
			fragment.setInitializer(fAst.newNumberLiteral(INITIAL_HASHCODE_VALUE));
		} else {
			SuperMethodInvocation invoc= fAst.newSuperMethodInvocation();
			invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE));
			fragment.setInitializer(invoc);
		}

		if (isMemberType()) {
			body.statements().add(createAddOuterHashCode());
		}

		for (int i= 0; i < fFields.length; i++) {
			if (fFields[i].getType().isPrimitive()) {
				Statement[] sts= createAddSimpleHashCode(fFields[i].getType(), new IHashCodeAccessProvider() {

					public Expression getThisAccess(String name) {
						return getThisAccessForHashCode(name);
					}

				}, fFields[i].getName(), false);
				for (int j= 0; j < sts.length; j++) {
					body.statements().add(sts[j]);
				}
			} else if (fFields[i].getType().isArray())
				body.statements().add(createAddArrayHashCode(fFields[i]));
			else
				body.statements().add(createAddQualifiedHashCode(fFields[i]));
		}

		// the last return:
		ReturnStatement endReturn= fAst.newReturnStatement();
		endReturn.setExpression(fAst.newSimpleName(VARIABLE_NAME_RESULT));
		body.statements().add(endReturn);

		// method comment
		if (fSettings != null) {
			ITypeBinding object= fAst.resolveWellKnownType(JAVA_LANG_OBJECT);
			IMethodBinding[] objms= object.getDeclaredMethods();
			IMethodBinding objectMethod= null;
			for (int i= 0; i < objms.length; i++) {
				if (objms[i].getName().equals(METHODNAME_HASH_CODE) && objms[i].getParameterTypes().length == 0)
					objectMethod= objms[i];
			}
			createMethodComment(hashCodeMethod, objectMethod);
		}

		return hashCodeMethod;
	}
 
Example 18
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public boolean visit(ITypeBinding type) {
	IMethodBinding[] methods= type.getDeclaredMethods();
	for (int i= 0; i < methods.length; i++) {
		IMethodBinding candidate= methods[i];
		if (candidate.getMethodDeclaration() == fOriginalMethod.getMethodDeclaration()) {
			continue;
		}
		ITypeBinding candidateDeclaringType= candidate.getDeclaringClass();
		if (fDeclaringType != candidateDeclaringType) {
			int modifiers= candidate.getModifiers();
			if (candidateDeclaringType.isInterface() && Modifier.isStatic(modifiers)) {
				continue;
			}
			if (Modifier.isPrivate(modifiers)) {
				continue;
			}
		}
		if (fOriginalMethod.getName().equals(candidate.getName()) && !fOriginalMethod.overrides(candidate)) {
			ITypeBinding[] originalParameterTypes= fOriginalMethod.getParameterTypes();
			ITypeBinding[] candidateParameterTypes= candidate.getParameterTypes();
			
			boolean couldBeAmbiguous;
			if (originalParameterTypes.length == candidateParameterTypes.length) {
				couldBeAmbiguous= true;
			} else if (fOriginalMethod.isVarargs() || candidate.isVarargs() ) {
				int candidateMinArgumentCount= candidateParameterTypes.length;
				if (candidate.isVarargs())
					candidateMinArgumentCount--;
				couldBeAmbiguous= fArgumentCount >= candidateMinArgumentCount;
			} else {
				couldBeAmbiguous= false;
			}
			if (couldBeAmbiguous) {
				ITypeBinding parameterType= ASTResolving.getParameterTypeBinding(candidate, fArgIndex);
				if (parameterType != null && parameterType.getFunctionalInterfaceMethod() != null) {
					if (!fExpressionIsExplicitlyTyped) {
						/* According to JLS8 15.12.2.2, implicitly typed lambda expressions are not "pertinent to applicability"
						 * and hence potentially applicable methods are always "applicable by strict invocation",
						 * regardless of whether argument expressions are compatible with the method's parameter types or not.
						 * If there are multiple such methods, 15.12.2.5 results in an ambiguous method invocation.
						 */
						return false;
					}
					/* Explicitly typed lambda expressions are pertinent to applicability, and hence
					 * compatibility with the corresponding method parameter type is checked. And since this check
					 * separates functional interface methods by their void-compatibility state, functional interfaces
					 * with a different void compatibility are not applicable any more and hence can't cause
					 * an ambiguous method invocation.
					 */
					ITypeBinding origParamType= ASTResolving.getParameterTypeBinding(fOriginalMethod, fArgIndex);
					boolean originalIsVoidCompatible=  Bindings.isVoidType(origParamType.getFunctionalInterfaceMethod().getReturnType());
					boolean candidateIsVoidCompatible= Bindings.isVoidType(parameterType.getFunctionalInterfaceMethod().getReturnType());
					if (originalIsVoidCompatible == candidateIsVoidCompatible) {
						return false;
					}
				}
			}
		}
	}
	return true;
}
 
Example 19
Source File: JdtUtils.java    From j2cl with Apache License 2.0 4 votes vote down vote up
private static DeclaredTypeDescriptor createDeclaredType(final ITypeBinding typeBinding) {
  if (cachedDeclaredTypeDescriptorByTypeBinding.containsKey(typeBinding)) {
    return cachedDeclaredTypeDescriptorByTypeBinding.get(typeBinding);
  }

  checkArgument(!typeBinding.isArray());
  checkArgument(!typeBinding.isPrimitive());

  Supplier<ImmutableMap<String, MethodDescriptor>> declaredMethods =
      () -> {
        ImmutableMap.Builder<String, MethodDescriptor> mapBuilder = ImmutableMap.builder();
        for (IMethodBinding methodBinding : typeBinding.getDeclaredMethods()) {
          MethodDescriptor methodDescriptor = JdtUtils.createMethodDescriptor(methodBinding);
          mapBuilder.put(
              // TODO(b/33595109): Using the method declaration signature here is kind of iffy;
              // but needs to be done because parameterized types might make multiple
              // superinterface methods collide which are represented by JDT as different method
              // bindings but with the same signature, e.g.
              //   interface I<U, V extends Serializable> {
              //     void foo(U u);
              //     void foo(V v);
              //   }
              // When considering the type I<A,A>, there are two different method bindings
              // that describe the single method 'void foo(A a)' each with the respective
              // method declaration.
              methodDescriptor.getDeclarationDescriptor().getMethodSignature(), methodDescriptor);
        }
        return mapBuilder.build();
      };

  Supplier<ImmutableList<FieldDescriptor>> declaredFields =
      () ->
          Arrays.stream(typeBinding.getDeclaredFields())
              .map(JdtUtils::createFieldDescriptor)
              .collect(toImmutableList());

  TypeDeclaration typeDeclaration =
      JdtUtils.createDeclarationForType(typeBinding.getTypeDeclaration());

  // Compute these even later
  DeclaredTypeDescriptor typeDescriptor =
      DeclaredTypeDescriptor.newBuilder()
          .setTypeDeclaration(typeDeclaration)
          .setEnclosingTypeDescriptor(
              createDeclaredTypeDescriptor(typeBinding.getDeclaringClass()))
          .setInterfaceTypeDescriptorsFactory(
              () ->
                  createTypeDescriptors(
                      typeBinding.getInterfaces(), DeclaredTypeDescriptor.class))
          .setSingleAbstractMethodDescriptorFactory(
              () -> createMethodDescriptor(typeBinding.getFunctionalInterfaceMethod()))
          .setJsFunctionMethodDescriptorFactory(() -> getJsFunctionMethodDescriptor(typeBinding))
          .setSuperTypeDescriptorFactory(
              getSuperTypeFactory(typeDeclaration.isJsEnum(), typeBinding))
          .setTypeArgumentDescriptors(getTypeArgumentTypeDescriptors(typeBinding))
          .setDeclaredFieldDescriptorsFactory(declaredFields)
          .setDeclaredMethodDescriptorsFactory(declaredMethods)
          .build();
  cachedDeclaredTypeDescriptorByTypeBinding.put(typeBinding, typeDescriptor);
  return typeDescriptor;
}
 
Example 20
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static void findUnimplementedInterfaceMethods(ITypeBinding typeBinding, HashSet<ITypeBinding> visited,
		ArrayList<IMethodBinding> allMethods, IPackageBinding currPack, ArrayList<IMethodBinding> toImplement) {
	
	if (visited.add(typeBinding)) {
		IMethodBinding[] typeMethods= typeBinding.getDeclaredMethods();
		
		nextMethod: for (int i= 0; i < typeMethods.length; i++) {
			IMethodBinding curr= typeMethods[i];
			for (Iterator<IMethodBinding> allIter= allMethods.iterator(); allIter.hasNext();) {
				IMethodBinding oneMethod= allIter.next();
				if (Bindings.isSubsignature(oneMethod, curr)) {
					// We've already seen a method that is a subsignature of curr.
					if (!Bindings.isSubsignature(curr, oneMethod)) {
						// oneMethod is a true subsignature of curr; let's go with oneMethod
						continue nextMethod;
					}
					// Subsignatures are equivalent.
					// Check visibility and return types ('getErasure()' tries to achieve effect of "rename type variables")
					if (Bindings.isVisibleInHierarchy(oneMethod, currPack)
							&& oneMethod.getReturnType().getErasure().isSubTypeCompatible(curr.getReturnType().getErasure())) {
						// oneMethod is visible and curr doesn't have a stricter return type; let's go with oneMethod
						continue nextMethod;
					}
					// curr is stricter than oneMethod, so let's remove oneMethod
					allIter.remove();
					toImplement.remove(oneMethod);
				} else if (Bindings.isSubsignature(curr, oneMethod)) {
					// curr is a true subsignature of oneMethod. Let's remove oneMethod.
					allIter.remove();
					toImplement.remove(oneMethod);
				}
			}
			if (Modifier.isAbstract(curr.getModifiers())) {
				toImplement.add(curr);
				allMethods.add(curr);
			}
		}
		ITypeBinding[] superInterfaces= typeBinding.getInterfaces();
		for (int i= 0; i < superInterfaces.length; i++)
			findUnimplementedInterfaceMethods(superInterfaces[i], visited, allMethods, currPack, toImplement);
	}
}