Java Code Examples for org.eclipse.jdt.core.IMethod#getTypeParameters()

The following examples show how to use org.eclipse.jdt.core.IMethod#getTypeParameters() . 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: MethodOverrideTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean hasCompatibleTypeParameters(IMethod overriding, IMethod overridden) throws JavaModelException {
	ITypeParameter[] overriddenTypeParameters= overridden.getTypeParameters();
	ITypeParameter[] overridingTypeParameters= overriding.getTypeParameters();
	int nOverridingTypeParameters= overridingTypeParameters.length;
	if (overriddenTypeParameters.length != nOverridingTypeParameters) {
		return nOverridingTypeParameters == 0;
	}
	Substitutions overriddenSubst= getMethodSubstitions(overridden);
	Substitutions overridingSubst= getMethodSubstitions(overriding);
	for (int i= 0; i < nOverridingTypeParameters; i++) {
		String erasure1= overriddenSubst.getErasure(overriddenTypeParameters[i].getElementName());
		String erasure2= overridingSubst.getErasure(overridingTypeParameters[i].getElementName());
		if (erasure1 == null || !erasure1.equals(erasure2)) {
			return false;
		}
		// comparing only the erasure is not really correct: Need to compare all bounds, that can be in different order
		int nBounds= overriddenTypeParameters[i].getBounds().length;
		if (nBounds > 1 && nBounds != overridingTypeParameters[i].getBounds().length) {
			return false;
		}
	}
	return true;
}
 
Example 2
Source File: MethodOverrideTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Substitutions getMethodSubstitions(IMethod method) throws JavaModelException {
	if (fMethodSubstitutions == null) {
		fMethodSubstitutions= new LRUMap<IMethod, Substitutions>(3);
	}

	Substitutions s= fMethodSubstitutions.get(method);
	if (s == null) {
		ITypeParameter[] typeParameters= method.getTypeParameters();
		if (typeParameters.length == 0) {
			s= Substitutions.EMPTY_SUBST;
		} else {
			IType instantiatedType= method.getDeclaringType();
			s= new Substitutions();
			for (int i= 0; i < typeParameters.length; i++) {
				ITypeParameter curr= typeParameters[i];
				s.addSubstitution(curr.getElementName(), '+' + String.valueOf(i), getTypeParameterErasure(curr, instantiatedType));
			}
		}
		fMethodSubstitutions.put(method, s);
	}
	return s;
}
 
Example 3
Source File: SelectionRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean areTypeParametersCompatible(IMethod method, char[][] typeParameterNames, char[][][] typeParameterBoundNames) {
	try {
		ITypeParameter[] typeParameters = method.getTypeParameters();
		int length1 = typeParameters == null ? 0 : typeParameters.length;
		int length2 = typeParameterNames == null ? 0 : typeParameterNames.length;
		if (length1 != length2) {
			return false;
		} else {
			for (int j = 0; j < length1; j++) {
				ITypeParameter typeParameter = typeParameters[j];
				String typeParameterName = typeParameter.getElementName();
				if (!typeParameterName.equals(new String(typeParameterNames[j]))) {
					return false;
				}

				String[] bounds = typeParameter.getBounds();
				int boundCount = typeParameterBoundNames[j] == null ? 0 : typeParameterBoundNames[j].length;

				if (bounds.length != boundCount) {
					return false;
				} else {
					for (int k = 0; k < boundCount; k++) {
						String simpleName = Signature.getSimpleName(bounds[k]);
						int index = simpleName.indexOf('<');
						if (index != -1) {
							simpleName = simpleName.substring(0, index);
						}
						if (!simpleName.equals(new String(typeParameterBoundNames[j][k]))) {
							return false;
						}
					}
				}
			}
		}
	} catch (JavaModelException e) {
		return false;
	}
	return true;
}
 
Example 4
Source File: NewAsyncRemoteServiceInterfaceCreationWizardPage.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
public static String createMethodContents(IType newType,
    ImportManagerAdapter imports, IMethodBinding overridableSyncMethod,
    boolean addComments) throws CoreException, JavaModelException {
  StringBuilder sb = new StringBuilder();

  IMethod syncMethod = (IMethod) overridableSyncMethod.getJavaElement();

  if (addComments) {
    String lineDelimiter = "\n"; // OK, since content is formatted afterwards

    // Don't go through CodeGeneration type since it can't deal with delegates
    String comment = StubUtility.getMethodComment(
        newType.getCompilationUnit(), newType.getFullyQualifiedName(),
        syncMethod.getElementName(), NO_STRINGS, NO_STRINGS,
        Signature.SIG_VOID, NO_STRINGS, syncMethod, true, lineDelimiter);
    if (comment != null) {
      sb.append(comment);
      sb.append(lineDelimiter);
    }
  }

  // Expand the type parameters
  ITypeParameter[] typeParameters = syncMethod.getTypeParameters();
  ITypeBinding[] typeParameterBindings = overridableSyncMethod.getTypeParameters();
  if (typeParameters.length > 0) {
    sb.append("<");
    for (int i = 0; i < typeParameters.length; ++i) {
      sb.append(typeParameters[i].getElementName());
      ITypeBinding typeParameterBinding = typeParameterBindings[i];
      ITypeBinding[] typeBounds = typeParameterBinding.getTypeBounds();
      if (typeBounds.length > 0) {
        sb.append(" extends ");
        for (int j = 0; j < typeBounds.length; ++j) {
          if (j != 0) {
            sb.append(" & ");
          }
          expandTypeBinding(typeBounds[j], sb, imports);
        }
      }
    }
    sb.append(">");
  }

  // Default to a void return type for the async method
  sb.append("void ");

  // Expand the method name
  sb.append(overridableSyncMethod.getName());

  // Expand the arguments
  sb.append("(");
  String[] parameterNames = syncMethod.getParameterNames();
  ITypeBinding[] parameterTypes = overridableSyncMethod.getParameterTypes();
  for (int i = 0; i < parameterNames.length; ++i) {
    if (i != 0) {
      sb.append(", ");
    }

    expandTypeBinding(parameterTypes[i], sb, imports);
    sb.append(" ");
    sb.append(parameterNames[i]);
  }

  if (parameterNames.length > 0) {
    sb.append(", ");
  }

  sb.append(imports.addImport(RemoteServiceUtilities.ASYNCCALLBACK_QUALIFIED_NAME));
  sb.append("<");
  ITypeBinding syncReturnType = overridableSyncMethod.getReturnType();
  if (syncReturnType.isPrimitive()) {
    String wrapperTypeName = JavaASTUtils.getWrapperTypeName(syncReturnType.getName());
    sb.append(imports.addImport(wrapperTypeName));
  } else {
    expandTypeBinding(syncReturnType, sb, imports);
  }
  sb.append("> ");
  sb.append(StringUtilities.computeUniqueName(parameterNames, "callback"));
  sb.append(");");

  return sb.toString();
}
 
Example 5
Source File: NamedMember.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected String getKey(IMethod method, boolean forceOpen) throws JavaModelException {
	StringBuffer key = new StringBuffer();

	// declaring class
	String declaringKey = getKey((IType) method.getParent(), forceOpen);
	key.append(declaringKey);

	// selector
	key.append('.');
	String selector = method.getElementName();
	key.append(selector);

	// type parameters
	if (forceOpen) {
		ITypeParameter[] typeParameters = method.getTypeParameters();
		int length = typeParameters.length;
		if (length > 0) {
			key.append('<');
			for (int i = 0; i < length; i++) {
				ITypeParameter typeParameter = typeParameters[i];
				String[] bounds = typeParameter.getBounds();
				int boundsLength = bounds.length;
				char[][] boundSignatures = new char[boundsLength][];
				for (int j = 0; j < boundsLength; j++) {
					boundSignatures[j] = Signature.createCharArrayTypeSignature(bounds[j].toCharArray(), method.isBinary());
					CharOperation.replace(boundSignatures[j], '.', '/');
				}
				char[] sig = Signature.createTypeParameterSignature(typeParameter.getElementName().toCharArray(), boundSignatures);
				key.append(sig);
			}
			key.append('>');
		}
	}

	// parameters
	key.append('(');
	String[] parameters = method.getParameterTypes();
	for (int i = 0, length = parameters.length; i < length; i++)
		key.append(parameters[i].replace('.', '/'));
	key.append(')');

	// return type
	if (forceOpen)
		key.append(method.getReturnType().replace('.', '/'));
	else
		key.append('V');

	return key.toString();
}