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

The following examples show how to use org.eclipse.jdt.core.IMethod#getReturnType() . 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: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isGeneralizeTypeAvailable(final IStructuredSelection selection) throws JavaModelException {
	if (selection.size() == 1) {
		final Object element= selection.getFirstElement();
		if (element instanceof IMethod) {
			final IMethod method= (IMethod) element;
			if (!method.exists())
				return false;
			final String type= method.getReturnType();
			if (PrimitiveType.toCode(Signature.toString(type)) == null)
				return Checks.isAvailable(method);
		} else if (element instanceof IField) {
			final IField field= (IField) element;
			if (!field.exists())
				return false;
			if (!JdtFlags.isEnum(field))
				return Checks.isAvailable(field);
		}
	}
	return false;
}
 
Example 2
Source File: ChangeTypeAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static IMember getMember(IStructuredSelection selection) throws JavaModelException {
	if (selection.size() != 1)
		return null;

	Object element= selection.getFirstElement();
	if (!(element instanceof IMember))
		return null;

	if (element instanceof IMethod) {
		IMethod method= (IMethod)element;
		String returnType= method.getReturnType();
		if (PrimitiveType.toCode(Signature.toString(returnType)) != null)
			return null;
		return method;
	} else if (element instanceof IField && !JdtFlags.isEnum((IMember) element)) {
		return (IField)element;
	}
	return null;
}
 
Example 3
Source File: FindBrokenNLSKeysAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isValueAccessor(IMethod method) throws JavaModelException {
	if (!"getString".equals(method.getElementName())) //$NON-NLS-1$
		return false;

	int flags= method.getFlags();
	if (!Modifier.isStatic(flags) || !Modifier.isPublic(flags))
		return false;

	String returnType= method.getReturnType();
	if (!JAVA_LANG_STRING.equals(returnType))
		return false;

	String[] parameters= method.getParameterTypes();
	if (parameters.length != 1 || !JAVA_LANG_STRING.equals(parameters[0]))
		return false;

	return true;
}
 
Example 4
Source File: ParameterGuesser.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public Variable createVariable(IJavaElement element, IType enclosingType, String expectedType, int positionScore) throws JavaModelException {
	int variableType;
	int elementType= element.getElementType();
	String elementName= element.getElementName();

	String typeSignature;
	switch (elementType) {
		case IJavaElement.FIELD: {
			IField field= (IField) element;
			if (field.getDeclaringType().equals(enclosingType)) {
				variableType= Variable.FIELD;
			} else {
				variableType= Variable.INHERITED_FIELD;
			}
			if (field.isResolved()) {
				typeSignature= new BindingKey(field.getKey()).toSignature();
			} else {
				typeSignature= field.getTypeSignature();
			}
			break;
		}
		case IJavaElement.LOCAL_VARIABLE: {
			ILocalVariable locVar= (ILocalVariable) element;
			variableType= Variable.LOCAL;
			typeSignature= locVar.getTypeSignature();
			break;
		}
		case IJavaElement.METHOD: {
			IMethod method= (IMethod) element;
			if (isMethodToSuggest(method)) {
				if (method.getDeclaringType().equals(enclosingType)) {
					variableType= Variable.METHOD;
				} else {
					variableType= Variable.INHERITED_METHOD;
				}
				if (method.isResolved()) {
					typeSignature= Signature.getReturnType(new BindingKey(method.getKey()).toSignature());
				} else {
					typeSignature= method.getReturnType();
				}
				elementName= elementName + "()";  //$NON-NLS-1$
			} else {
				return null;
			}
			break;
		}
		default:
			return null;
	}
	String type= Signature.toString(typeSignature);

	boolean isAutoboxMatch= isPrimitiveType(expectedType) != isPrimitiveType(type);
	return new Variable(type, elementName, variableType, isAutoboxMatch, positionScore);
}
 
Example 5
Source File: UiBinderUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Resolves a snippet (assumed to be trailing a reference to the given
 * <code>baseType</code>) to the right-most possible type.
 * <p>
 * For example, a base type of String and a snippet of
 * "toString.getClass.getDeclaredField" will return the Field type.
 *
 * @param contextType the type which will be used as the starting point for
 *          the snippet
 * @param snippet the string to trail onto a reference to the base type
 * @param visitor optional visitor that receives the corresponding
 *          {@link IMethod} for each fragment. This will be called even for
 *          methods whose return types do not resolve.
 * @return the resolved type, or null if type is a primitive or the type could
 *         not be resolved
 */
public static IType resolveJavaElExpression(IType contextType,
    String snippet, ElExpressionFragmentVisitor visitor) {
  int offset = 0;
  String[] snippetFragments = snippet.split("[.]");
  IType currentType = contextType;

  for (int i = 0; i < snippetFragments.length; i++) {
    if (i > 0) {
      // Count the previous fragment
      offset += snippetFragments[i - 1].length();

      // Count the period
      offset++;
    }

    String fragment = snippetFragments[i];

    try {
      ITypeHierarchy hierarchy = currentType.newSupertypeHierarchy(NULL_PROGRESS_MONITOR);
      IMethod method = JavaModelSearch.findMethodInHierarchy(hierarchy,
          currentType, fragment, new String[0]);
      if (method != null) {
        if (visitor != null) {
          visitor.visitResolvedFragmentMethod(method, offset,
              fragment.length());
        }

        String returnTypeSignature = method.getReturnType();
        if (SignatureUtilities.isPrimitiveType(returnTypeSignature)) {
          if (i != snippetFragments.length - 1) {
            // It returns a primitive but isn't the last fragment
            if (visitor != null) {
              visitor.visitNonterminalPrimitiveFragment(fragment, offset,
                  snippet.length() - offset);
            }
          }

          return null;
        }

        IType fragmentType = JavaModelSearch.resolveType(
            method.getDeclaringType(),
            returnTypeSignature);
        if (JavaModelSearch.isValidElement(fragmentType)) {
          currentType = fragmentType;
          continue;
        }

        // Final attempt to resolve the type, this resolves the binding
        // in the case of generics or parameterized types. Fixes issue 373
        ITypeBinding binding = JavaASTUtils.findTypeBinding(currentType);
        IMethodBinding methodBinding = Bindings.findMethodInHierarchy(binding,
            fragment, new ITypeBinding[] {});
        if(methodBinding.getReturnType() != null) {
          currentType = fragmentType;
          continue;
        }
      }
    } catch (JavaModelException e) {
      // Ignore, and continue the search
    }

    if (visitor != null) {
      visitor.visitUnresolvedFragment(fragment, offset, fragment.length(),
          currentType);
    }
    return null;
  }

  return currentType;
}
 
Example 6
Source File: ParameterGuesser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public Variable createVariable(IJavaElement element, IType enclosingType, String expectedType, int positionScore) throws JavaModelException {
	int variableType;
	int elementType= element.getElementType();
	String elementName= element.getElementName();

	String typeSignature;
	switch (elementType) {
		case IJavaElement.FIELD: {
			IField field= (IField) element;
			if (field.getDeclaringType().equals(enclosingType)) {
				variableType= Variable.FIELD;
			} else {
				variableType= Variable.INHERITED_FIELD;
			}
			if (field.isResolved()) {
				typeSignature= new BindingKey(field.getKey()).toSignature();
			} else {
				typeSignature= field.getTypeSignature();
			}
			break;
		}
		case IJavaElement.LOCAL_VARIABLE: {
			ILocalVariable locVar= (ILocalVariable) element;
			variableType= Variable.LOCAL;
			typeSignature= locVar.getTypeSignature();
			break;
		}
		case IJavaElement.METHOD: {
			IMethod method= (IMethod) element;
			if (isMethodToSuggest(method)) {
				if (method.getDeclaringType().equals(enclosingType)) {
					variableType= Variable.METHOD;
				} else {
					variableType= Variable.INHERITED_METHOD;
				}
				if (method.isResolved()) {
					typeSignature= Signature.getReturnType(new BindingKey(method.getKey()).toSignature());
				} else {
					typeSignature= method.getReturnType();
				}
				elementName= elementName + "()";  //$NON-NLS-1$
			} else {
				return null;
			}
			break;
		}
		default:
			return null;
	}
	String type= Signature.toString(typeSignature);

	boolean isAutoboxMatch= isPrimitiveType(expectedType) != isPrimitiveType(type);
	return new Variable(type, elementName, variableType, isAutoboxMatch, positionScore, NO_TRIGGERS, getImageDescriptor(element));
}
 
Example 7
Source File: CodeGeneration.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Returns the comment for a method or constructor using the comment code templates (constructor / method / overriding method).
 * <code>null</code> is returned if the template is empty.
 * <p>The returned string is unformatted and not indented.
 *
 * @param method The method to be documented. The method must exist.
 * @param overridden The method that will be overridden by the created method or
 * <code>null</code> for non-overriding methods. If not <code>null</code>, the method must exist.
 * @param lineDelimiter The line delimiter to be used.
 * @return Returns the constructed comment or <code>null</code> if
 * the comment code template is empty. The returned string is unformatted and and has no indent (formatting required).
 * @throws CoreException Thrown when the evaluation of the code template fails.
 */
public static String getMethodComment(IMethod method, IMethod overridden, String lineDelimiter) throws CoreException {
	String retType= method.isConstructor() ? null : method.getReturnType();
	String[] paramNames= method.getParameterNames();
	String[] typeParameterNames= StubUtility.shouldGenerateMethodTypeParameterTags(method.getJavaProject()) ? StubUtility.getTypeParameterNames(method.getTypeParameters()) : new String[0];

	return StubUtility.getMethodComment(method.getCompilationUnit(), method.getDeclaringType().getElementName(),
		method.getElementName(), paramNames, method.getExceptionTypes(), retType, typeParameterNames, overridden, false, lineDelimiter);
}