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

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#isWildcardType() . 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: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static ITypeBinding guessBindingForTypeReference(ASTNode node) {
  	StructuralPropertyDescriptor locationInParent= node.getLocationInParent();
  	if (locationInParent == QualifiedName.QUALIFIER_PROPERTY) {
  		return null; // can't guess type for X.A
  	}
if (locationInParent == SimpleType.NAME_PROPERTY ||
		locationInParent == NameQualifiedType.NAME_PROPERTY) {
  		node= node.getParent();
  	}
  	ITypeBinding binding= Bindings.normalizeTypeBinding(getPossibleTypeBinding(node));
  	if (binding != null) {
  		if (binding.isWildcardType()) {
  			return normalizeWildcardType(binding, true, node.getAST());
  		}
  	}
  	return binding;
  }
 
Example 2
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private String computeTypeProposal(ITypeBinding binding, ITypeParameter parameter) throws JavaModelException {
	final String name = TypeProposalUtils.getTypeQualifiedName(binding);
	if (binding.isWildcardType()) {

		if (binding.isUpperbound()) {
			// replace the wildcard ? with the type parameter name to get "E extends Bound" instead of "? extends Bound"
			//				String contextName= name.replaceFirst("\\?", parameter.getElementName()); //$NON-NLS-1$
			// upper bound - the upper bound is the bound itself
			return binding.getBound().getName();
		}

		// no or upper bound - use the type parameter of the inserted type, as it may be more
		// restrictive (eg. List<?> list= new SerializableList<Serializable>())
		return computeTypeProposal(parameter);
	}

	// not a wildcard but a type or type variable - this is unambigously the right thing to insert
	return name;
}
 
Example 3
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Normalizes the binding so that it can be used as a type inside a declaration (e.g. variable
 * declaration, method return type, parameter type, ...).
 * For null bindings, java.lang.Object is returned.
 * For void bindings, <code>null</code> is returned.
 * 
 * @param binding binding to normalize
 * @param ast current AST
 * @return the normalized type to be used in declarations, or <code>null</code>
 */
public static ITypeBinding normalizeForDeclarationUse(ITypeBinding binding, AST ast) {
	if (binding.isNullType())
		return ast.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
	if (binding.isPrimitive())
		return binding;
	binding= normalizeTypeBinding(binding);
	if (binding == null || !binding.isWildcardType())
		return binding;
	ITypeBinding bound= binding.getBound();
	if (bound == null || !binding.isUpperbound()) {
		ITypeBinding[] typeBounds= binding.getTypeBounds();
		if (typeBounds.length > 0) {
			return typeBounds[0];
		} else {
			return binding.getErasure();
		}
	} else {
		return bound;
	}
}
 
Example 4
Source File: AssignToVariableAssistProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public AssignToVariableAssistProposal(ICompilationUnit cu, int variableKind, ExpressionStatement node, ITypeBinding typeBinding, int relevance) {
	super("", cu, null, relevance, null); //$NON-NLS-1$

	fVariableKind= variableKind;
	fNodeToAssign= node;
	if (typeBinding.isWildcardType()) {
		typeBinding= ASTResolving.normalizeWildcardType(typeBinding, true, node.getAST());
	}

	fTypeBinding= typeBinding;
	if (variableKind == LOCAL) {
		setDisplayName(CorrectionMessages.AssignToVariableAssistProposal_assigntolocal_description);
		setImage(JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL));
	} else {
		setDisplayName(CorrectionMessages.AssignToVariableAssistProposal_assigntofield_description);
		setImage(JavaPluginImages.get(JavaPluginImages.IMG_FIELD_PRIVATE));
	}
	createImportRewrite((CompilationUnit) node.getRoot());
}
 
Example 5
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void collectTypeVariables(ITypeBinding typeBinding, Set<ITypeBinding> typeVariablesCollector) {
	if (typeBinding.isTypeVariable()) {
		typeVariablesCollector.add(typeBinding);
		ITypeBinding[] typeBounds= typeBinding.getTypeBounds();
		for (int i= 0; i < typeBounds.length; i++)
			collectTypeVariables(typeBounds[i], typeVariablesCollector);

	} else if (typeBinding.isArray()) {
		collectTypeVariables(typeBinding.getElementType(), typeVariablesCollector);

	} else if (typeBinding.isParameterizedType()) {
		ITypeBinding[] typeArguments= typeBinding.getTypeArguments();
		for (int i= 0; i < typeArguments.length; i++)
			collectTypeVariables(typeArguments[i], typeVariablesCollector);

	} else if (typeBinding.isWildcardType()) {
		ITypeBinding bound= typeBinding.getBound();
		if (bound != null) {
			collectTypeVariables(bound, typeVariablesCollector);
		}
	}
}
 
Example 6
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static ICompilationUnit findCompilationUnitForBinding(ICompilationUnit cu, CompilationUnit astRoot, ITypeBinding binding) throws JavaModelException {
	if (binding == null || !binding.isFromSource() || binding.isTypeVariable() || binding.isWildcardType()) {
		return null;
	}
	ASTNode node= astRoot.findDeclaringNode(binding.getTypeDeclaration());
	if (node == null) {
		ICompilationUnit targetCU= Bindings.findCompilationUnit(binding, cu.getJavaProject());
		if (targetCU != null) {
			return targetCU;
		}
		return null;
	} else if (node instanceof AbstractTypeDeclaration || node instanceof AnonymousClassDeclaration) {
		return cu;
	}
	return null;
}
 
Example 7
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static ChangeDescription[] createSignatureChangeDescription(int[] indexOfDiff, int nDiffs, ITypeBinding[] paramTypes, List<Expression> arguments, ITypeBinding[] argTypes) {
	ChangeDescription[] changeDesc= new ChangeDescription[paramTypes.length];
	for (int i= 0; i < nDiffs; i++) {
		int diffIndex= indexOfDiff[i];
		Expression arg= arguments.get(diffIndex);
		String name= getExpressionBaseName(arg);
		ITypeBinding argType= argTypes[diffIndex];
		if (argType.isWildcardType()) {
			argType= ASTResolving.normalizeWildcardType(argType, true, arg.getAST());
			if (argType== null) {
				return null;
			}
		}
		changeDesc[diffIndex]= new EditDescription(argType, name);
	}
	return changeDesc;
}
 
Example 8
Source File: JdtUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private static boolean isIntersectionType(ITypeBinding binding) {
  return binding.isIntersectionType()
      // JDT returns true for isIntersectionType() for type variables, wildcards and captures
      // with intersection type bounds.
      && !binding.isCapture()
      && !binding.isTypeVariable()
      && !binding.isWildcardType();
}
 
Example 9
Source File: ConvertIterableLoopOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the type of elements returned by the iterator.
 *
 * @param iterator
 *            the iterator type binding, or <code>null</code>
 * @return the element type
 */
private ITypeBinding getElementType(final ITypeBinding iterator) {
	if (iterator != null) {
		final ITypeBinding[] bindings= iterator.getTypeArguments();
		if (bindings.length > 0) {
			ITypeBinding arg= bindings[0];
			if (arg.isWildcardType()) {
				arg= ASTResolving.normalizeWildcardType(arg, true, getRoot().getAST());
			}
			return arg;
		}
	}
	return getRoot().getAST().resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
}
 
Example 10
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
static boolean containsTypeVariables(ITypeBinding type) {
	if (type.isTypeVariable())
		return true;
	if (type.isArray())
		return containsTypeVariables(type.getElementType());
	if (type.isCapture())
		return containsTypeVariables(type.getWildcard());
	if (type.isParameterizedType())
		return containsTypeVariables(type.getTypeArguments());
	if (type.isWildcardType() && type.getBound() != null)
		return containsTypeVariables(type.getBound());
	return false;
}
 
Example 11
Source File: AbstractPairedInterfaceValidator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if the given type binding references a type
 * variable, at any depth.
 */
protected static boolean containsTypeVariableReferences(
    ITypeBinding typeBinding) {
  if (typeBinding.isTypeVariable()) {
    return true;
  }

  if (typeBinding.isArray()) {
    return containsTypeVariableReferences(typeBinding.getComponentType());
  }

  if (typeBinding.isParameterizedType()) {
    ITypeBinding[] typeArguments = typeBinding.getTypeArguments();
    for (int i = 0; i < typeArguments.length; ++i) {
      if (containsTypeVariableReferences(typeArguments[i])) {
        return true;
      }
    }
  }

  if (typeBinding.isWildcardType()) {
    ITypeBinding bound = typeBinding.getBound();
    if (bound != null) {
      return containsTypeVariableReferences(bound);
    }
  }

  return false;
}
 
Example 12
Source File: NewMethodCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private Type evaluateParameterType(AST ast, Expression elem, String key, ImportRewriteContext context) {
	ITypeBinding binding= Bindings.normalizeTypeBinding(elem.resolveTypeBinding());
	if (binding != null && binding.isWildcardType()) {
		binding= ASTResolving.normalizeWildcardType(binding, true, ast);
	}
	if (binding != null) {
		return getImportRewrite().addImport(binding, ast, context, TypeLocation.PARAMETER);
	}
	return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
}
 
Example 13
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static ITypeBinding[] getParameterTypes(List<Expression> args) {
	ITypeBinding[] params= new ITypeBinding[args.size()];
	for (int i= 0; i < args.size(); i++) {
		Expression expr= args.get(i);
		ITypeBinding curr= Bindings.normalizeTypeBinding(expr.resolveTypeBinding());
		if (curr != null && curr.isWildcardType()) {
			curr= ASTResolving.normalizeWildcardType(curr, true, expr.getAST());
		}
		if (curr == null) {
			curr= expr.getAST().resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
		}
		params[i]= curr;
	}
	return params;
}
 
Example 14
Source File: NewVariableCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private Type evaluateVariableType(AST ast, ImportRewrite imports, ImportRewriteContext importRewriteContext, IBinding targetContext, TypeLocation location) {
	if (fOriginalNode.getParent() instanceof MethodInvocation) {
		MethodInvocation parent= (MethodInvocation) fOriginalNode.getParent();
		if (parent.getExpression() == fOriginalNode) {
			// _x_.foo() -> guess qualifier type by looking for a type with method 'foo'
			ITypeBinding[] bindings= ASTResolving.getQualifierGuess(fOriginalNode.getRoot(), parent.getName().getIdentifier(), parent.arguments(), targetContext);
			if (bindings.length > 0) {
				return imports.addImport(bindings[0], ast, importRewriteContext, location);
			}
		}
	}

	ITypeBinding binding= ASTResolving.guessBindingForReference(fOriginalNode);
	if (binding != null) {
		if (binding.isWildcardType()) {
			binding= ASTResolving.normalizeWildcardType(binding, isVariableAssigned(), ast);
			if (binding == null) {
				// only null binding applies
				binding= ast.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
			}
		}

		return imports.addImport(binding, ast, importRewriteContext, location);
	}
	// no binding, find type AST node instead -> ABC a= x-> use 'ABC' as is
	Type type = org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.guessTypeForReference(ast, fOriginalNode);
	if (type != null) {
		return type;
	}
	if (fVariableKind == CONST_FIELD) {
		return ast.newSimpleType(ast.newSimpleName("String")); //$NON-NLS-1$
	}
	return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
}
 
Example 15
Source File: RedundantNullnessTypeAnnotationsFilter.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public IAnnotationBinding[] removeUnwantedTypeAnnotations(IAnnotationBinding[] annotations, TypeLocation location, ITypeBinding type) {
	if (location == TypeLocation.OTHER) {
		return NO_ANNOTATIONS;
	}
	if(type.isTypeVariable() || type.isWildcardType()) {
		return annotations;
	}
	boolean excludeAllNullAnnotations = NEVER_NULLNESS_LOCATIONS.contains(location);
	if (excludeAllNullAnnotations || fNonNullByDefaultLocations.contains(location)) {
		ArrayList<IAnnotationBinding> list= new ArrayList<>(annotations.length);
		for (IAnnotationBinding annotation : annotations) {
			ITypeBinding annotationType= annotation.getAnnotationType();
			if (annotationType != null) {
				if (annotationType.getQualifiedName().equals(fNonNullAnnotationName)) {
					// ignore @NonNull
				} else if (excludeAllNullAnnotations && annotationType.getQualifiedName().equals(fNullableAnnotationName)) {
					// also ignore @Nullable
				} else {
					list.add(annotation);
				}
			} else {
				list.add(annotation);
			}
		}
		return list.size() == annotations.length ? annotations : list.toArray(new IAnnotationBinding[list.size()]);
	} else {
		return annotations;
	}
}
 
Example 16
Source File: JdtUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a type descriptor for the given type binding, taking into account nullability.
 *
 * @param typeBinding the type binding, used to create the type descriptor.
 * @param elementAnnotations the annotations on the element
 */
private static TypeDescriptor createTypeDescriptorWithNullability(
    ITypeBinding typeBinding, IAnnotationBinding[] elementAnnotations) {
  if (typeBinding == null) {
    return null;
  }

  if (typeBinding.isPrimitive()) {
    return PrimitiveTypes.get(typeBinding.getName());
  }

  if (isIntersectionType(typeBinding)) {
    return createIntersectionType(typeBinding);
  }

  if (typeBinding.isNullType()) {
    return TypeDescriptors.get().javaLangObject;
  }

  if (typeBinding.isTypeVariable() || typeBinding.isCapture() || typeBinding.isWildcardType()) {
    return createTypeVariable(typeBinding);
  }

  boolean isNullable = isNullable(typeBinding, elementAnnotations);
  if (typeBinding.isArray()) {
    TypeDescriptor componentTypeDescriptor = createTypeDescriptor(typeBinding.getComponentType());
    return ArrayTypeDescriptor.newBuilder()
        .setComponentTypeDescriptor(componentTypeDescriptor)
        .setNullable(isNullable)
        .build();
  }

  return withNullability(createDeclaredType(typeBinding), isNullable);
}
 
Example 17
Source File: ReturnTypeSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (selectedNode == null) {
		return;
	}
	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methodDeclaration= (MethodDeclaration) decl;

		ReturnStatementCollector eval= new ReturnStatementCollector();
		decl.accept(eval);

		AST ast= astRoot.getAST();

		ITypeBinding typeBinding= eval.getTypeBinding(decl.getAST());
		typeBinding= Bindings.normalizeTypeBinding(typeBinding);
		if (typeBinding == null) {
			typeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
		}
		if (typeBinding.isWildcardType()) {
			typeBinding= ASTResolving.normalizeWildcardType(typeBinding, true, ast);
		}

		ASTRewrite rewrite= ASTRewrite.create(ast);

		String label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProviderCore.getBindingLabel(typeBinding, BindingLabelProviderCore.DEFAULT_TEXTFLAGS));
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE);

		ImportRewrite imports= proposal.createImportRewrite(astRoot);
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports);
		Type type= imports.addImport(typeBinding, ast, importRewriteContext, TypeLocation.RETURN_TYPE);

		rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
		rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);

		Javadoc javadoc= methodDeclaration.getJavadoc();
		if (javadoc != null && typeBinding != null) {
			TagElement newTag= ast.newTagElement();
			newTag.setTagName(TagElement.TAG_RETURN);
			TextElement commentStart= ast.newTextElement();
			newTag.fragments().add(commentStart);

			JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
			proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start"); //$NON-NLS-1$
		}

		String key= "return_type"; //$NON-NLS-1$
		proposal.addLinkedPosition(rewrite.track(type), true, key);
		if (typeBinding != null) {
			ITypeBinding[] bindings= ASTResolving.getRelaxingTypes(ast, typeBinding);
			for (int i= 0; i < bindings.length; i++) {
				proposal.addLinkedPositionProposal(key, bindings[i]);
			}
		}

		proposals.add(proposal);

		// change to constructor
		ASTNode parentType= ASTResolving.findParentType(decl);
		if (parentType instanceof AbstractTypeDeclaration) {
			boolean isInterface= parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
			if (!isInterface) {
				String constructorName= ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
				ASTNode nameNode= methodDeclaration.getName();
				label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
				proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
			}
		}
	}
}
 
Example 18
Source File: RefactorProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean getConvertVarTypeToResolvedTypeProposal(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> proposals) {
	CompilationUnit astRoot = context.getASTRoot();
	IJavaElement root = astRoot.getJavaElement();
	if (root == null) {
		return false;
	}
	IJavaProject javaProject = root.getJavaProject();
	if (javaProject == null) {
		return false;
	}
	if (!JavaModelUtil.is10OrHigher(javaProject)) {
		return false;
	}

	if (!(node instanceof SimpleName)) {
		return false;
	}
	SimpleName name = (SimpleName) node;
	IBinding binding = name.resolveBinding();
	if (!(binding instanceof IVariableBinding)) {
		return false;
	}
	IVariableBinding varBinding = (IVariableBinding) binding;
	if (varBinding.isField() || varBinding.isParameter()) {
		return false;
	}

	ASTNode varDeclaration = astRoot.findDeclaringNode(varBinding);
	if (varDeclaration == null) {
		return false;
	}

	ITypeBinding typeBinding = varBinding.getType();
	if (typeBinding == null || typeBinding.isAnonymous() || typeBinding.isIntersectionType() || typeBinding.isWildcardType()) {
		return false;
	}

	Type type = null;
	if (varDeclaration instanceof SingleVariableDeclaration) {
		type = ((SingleVariableDeclaration) varDeclaration).getType();
	} else if (varDeclaration instanceof VariableDeclarationFragment) {
		ASTNode parent = varDeclaration.getParent();
		if (parent instanceof VariableDeclarationStatement) {
			type = ((VariableDeclarationStatement) parent).getType();
		} else if (parent instanceof VariableDeclarationExpression) {
			type = ((VariableDeclarationExpression) parent).getType();
		}
	}
	if (type == null || !type.isVar()) {
		return false;
	}

	TypeChangeCorrectionProposal proposal = new TypeChangeCorrectionProposal(context.getCompilationUnit(), varBinding, astRoot, typeBinding, false, IProposalRelevance.CHANGE_VARIABLE);
	proposal.setKind(CodeActionKind.Refactor);
	proposals.add(proposal);
	return true;
}
 
Example 19
Source File: NewVariableCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private Type evaluateVariableType(AST ast, ImportRewrite imports, ImportRewriteContext importRewriteContext, IBinding targetContext) {
	if (fOriginalNode.getParent() instanceof MethodInvocation) {
		MethodInvocation parent= (MethodInvocation) fOriginalNode.getParent();
		if (parent.getExpression() == fOriginalNode) {
			// _x_.foo() -> guess qualifier type by looking for a type with method 'foo'
			ITypeBinding[] bindings= ASTResolving.getQualifierGuess(fOriginalNode.getRoot(), parent.getName().getIdentifier(), parent.arguments(), targetContext);
			if (bindings.length > 0) {
				for (int i= 0; i < bindings.length; i++) {
					addLinkedPositionProposal(KEY_TYPE, bindings[i]);
				}
				return imports.addImport(bindings[0], ast, importRewriteContext);
			}
		}
	}

	ITypeBinding binding= ASTResolving.guessBindingForReference(fOriginalNode);
	if (binding != null) {
		if (binding.isWildcardType()) {
			binding= ASTResolving.normalizeWildcardType(binding, isVariableAssigned(), ast);
			if (binding == null) {
				// only null binding applies
				binding= ast.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
			}
		}

		if (isVariableAssigned()) {
			ITypeBinding[] typeProposals= ASTResolving.getRelaxingTypes(ast, binding);
			for (int i= 0; i < typeProposals.length; i++) {
				addLinkedPositionProposal(KEY_TYPE, typeProposals[i]);
			}
		}
		return imports.addImport(binding, ast, importRewriteContext);
	}
	// no binding, find type AST node instead -> ABC a= x-> use 'ABC' as is
	Type type= ASTResolving.guessTypeForReference(ast, fOriginalNode);
	if (type != null) {
		return type;
	}
	if (fVariableKind == CONST_FIELD) {
		return ast.newSimpleType(ast.newSimpleName("String")); //$NON-NLS-1$
	}
	return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
}
 
Example 20
Source File: LazyGenericTypeProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns a type argument proposal for a given type binding. The proposal is:
 * <ul>
 * <li>the simple type name for normal types or type variables (unambigous proposal)</li>
 * <li>for wildcard types (ambigous proposals):
 * <ul>
 * <li>the upper bound for wildcards with an upper bound</li>
 * <li>the {@linkplain #computeTypeProposal(ITypeParameter) parameter proposal} for unbounded
 * wildcards or wildcards with a lower bound</li>
 * </ul>
 * </li>
 * </ul>
 * 
 * @param binding the type argument binding in the expected type
 * @param parameter the type parameter of the inserted type
 * @return a type argument proposal for <code>binding</code>
 * @throws JavaModelException if this element does not exist or if an exception occurs while
 *             accessing its corresponding resource
 * @see #computeTypeProposal(ITypeParameter)
 */
private TypeArgumentProposal computeTypeProposal(ITypeBinding binding, ITypeParameter parameter) throws JavaModelException {
	final String name= Bindings.getTypeQualifiedName(binding);
	if (binding.isWildcardType()) {

		if (binding.isUpperbound()) {
			// replace the wildcard ? with the type parameter name to get "E extends Bound" instead of "? extends Bound"
			String contextName= name.replaceFirst("\\?", parameter.getElementName()); //$NON-NLS-1$
			// upper bound - the upper bound is the bound itself
			return new TypeArgumentProposal(binding.getBound().getName(), true, contextName);
		}

		// no or upper bound - use the type parameter of the inserted type, as it may be more
		// restrictive (eg. List<?> list= new SerializableList<Serializable>())
		return computeTypeProposal(parameter);
	}

	// not a wildcard but a type or type variable - this is unambigously the right thing to insert
	return new TypeArgumentProposal(name, false, name);
}