Java Code Examples for org.eclipse.jdt.ui.text.java.IProblemLocation#getCoveringNode()

The following examples show how to use org.eclipse.jdt.ui.text.java.IProblemLocation#getCoveringNode() . 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: ModifierCorrectionSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void addNeedToEmulateProposal(IInvocationContext context, IProblemLocation problem, Collection<ModifierChangeCorrectionProposal> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (!(selectedNode instanceof SimpleName)) {
		return;
	}

	IBinding binding= ((SimpleName) selectedNode).resolveBinding();
	if (binding instanceof IVariableBinding) {
		binding= ((IVariableBinding) binding).getVariableDeclaration();
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		String label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertofinal_description, BasicElementLabels.getJavaElementName(binding.getName()));
		proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, Modifier.FINAL, 0, IProposalRelevance.CHANGE_MODIFIER_OF_VARIABLE_TO_FINAL, image));
	}
}
 
Example 2
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void getMissingEnumConstantCaseProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	for (Iterator<ICommandAccess> iterator= proposals.iterator(); iterator.hasNext();) {
		ICommandAccess proposal= iterator.next();
		if (proposal instanceof ChangeCorrectionProposal) {
			if (CorrectionMessages.LocalCorrectionsSubProcessor_add_missing_cases_description.equals(((ChangeCorrectionProposal) proposal).getName())) {
				return;
			}
		}
	}
	
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) {
		SwitchStatement statement= (SwitchStatement) selectedNode.getParent();
		ITypeBinding binding= statement.getExpression().resolveTypeBinding();
		if (binding == null || !binding.isEnum()) {
			return;
		}

		ArrayList<String> missingEnumCases= new ArrayList<String>();
		boolean hasDefault= evaluateMissingSwitchCases(binding, statement.statements(), missingEnumCases);
		if (missingEnumCases.size() == 0 && hasDefault)
			return;

		createMissingCaseProposals(context, statement, missingEnumCases, proposals);
	}
}
 
Example 3
Source File: NullAnnotationsCorrectionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Fix for {@link IProblem#NullableFieldReference}
 * @param context context
 * @param problem problem to be fixed
 * @param proposals accumulator for computed proposals
 */
public static void addExtractCheckedLocalProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	CompilationUnit compilationUnit = context.getASTRoot();
	ICompilationUnit cu= (ICompilationUnit) compilationUnit.getJavaElement();

	ASTNode selectedNode= problem.getCoveringNode(compilationUnit);

	SimpleName name= findProblemFieldName(selectedNode, problem.getProblemId());
	if (name == null)
		return;

	ASTNode method= ASTNodes.getParent(selectedNode, MethodDeclaration.class);
	if (method == null)
		method= ASTNodes.getParent(selectedNode, Initializer.class);
	if (method == null)
		return;
	
	proposals.add(new ExtractToNullCheckedLocalProposal(cu, compilationUnit, name, method));
}
 
Example 4
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void addUnnecessaryThrownExceptionProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	selectedNode= ASTNodes.getNormalizedNode(selectedNode);
	if (selectedNode == null || selectedNode.getLocationInParent() != MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
		return;
	}
	MethodDeclaration decl= (MethodDeclaration) selectedNode.getParent();
	IMethodBinding binding= decl.resolveBinding();
	if (binding != null) {
		List<Type> thrownExceptions= decl.thrownExceptionTypes();
		int index= thrownExceptions.indexOf(selectedNode);
		if (index == -1) {
			return;
		}
		ChangeDescription[] desc= new ChangeDescription[thrownExceptions.size()];
		desc[index]= new RemoveDescription();

		ICompilationUnit cu= context.getCompilationUnit();
		String label= CorrectionMessages.LocalCorrectionsSubProcessor_unnecessarythrow_description;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_EXCEPTION);

		proposals.add(new ChangeMethodSignatureProposal(label, cu, selectedNode, binding, null, desc, IProposalRelevance.UNNECESSARY_THROW, image));
	}

	JavadocTagsSubProcessor.getUnusedAndUndocumentedParameterOrExceptionProposals(context, problem, proposals);
}
 
Example 5
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void addRedundantSuperInterfaceProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (!(selectedNode instanceof Name)) {
		return;
	}
	ASTNode node= ASTNodes.getNormalizedNode(selectedNode);

	ASTRewrite rewrite= ASTRewrite.create(node.getAST());
	rewrite.remove(node, null);

	String label= CorrectionMessages.LocalCorrectionsSubProcessor_remove_redundant_superinterface;
	Image image= JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);

	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_REDUNDANT_SUPER_INTERFACE, image);
	proposals.add(proposal);

}
 
Example 6
Source File: UnusedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static UnusedCodeFix createRemoveUnusedCastFix(CompilationUnit compilationUnit, IProblemLocation problem) {
	if (problem.getProblemId() != IProblem.UnnecessaryCast)
		return null;

	ASTNode selectedNode= problem.getCoveringNode(compilationUnit);

	ASTNode curr= selectedNode;
	while (curr instanceof ParenthesizedExpression) {
		curr= ((ParenthesizedExpression) curr).getExpression();
	}

	if (!(curr instanceof CastExpression))
		return null;

	return new UnusedCodeFix(FixMessages.UnusedCodeFix_RemoveCast_description, compilationUnit, new CompilationUnitRewriteOperation[] {new RemoveCastOperation((CastExpression)curr)});
}
 
Example 7
Source File: ReturnTypeSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void addMethodWithConstrNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof MethodDeclaration) {
		MethodDeclaration declaration= (MethodDeclaration) selectedNode;

		ASTRewrite rewrite= ASTRewrite.create(declaration.getAST());
		rewrite.set(declaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null);

		String label= CorrectionMessages.ReturnTypeSubProcessor_constrnamemethod_description;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_TO_CONSTRUCTOR, image);
		proposals.add(proposal);
	}

}
 
Example 8
Source File: Java50Fix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static void createAddDeprecatedAnnotationOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> result) {
	for (int i= 0; i < locations.length; i++) {
		IProblemLocation problem= locations[i];

		if (isMissingDeprecationProblem(problem.getProblemId())) {
			ASTNode selectedNode= problem.getCoveringNode(compilationUnit);
			if (selectedNode != null) {

				ASTNode declaringNode= getDeclaringNode(selectedNode);
				if (declaringNode instanceof BodyDeclaration) {
					BodyDeclaration declaration= (BodyDeclaration) declaringNode;
					AnnotationRewriteOperation operation= new AnnotationRewriteOperation(declaration, DEPRECATED);
					result.add(operation);
				}
			}
		}
	}
}
 
Example 9
Source File: Java50Fix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static Java50Fix createFix(CompilationUnit compilationUnit, IProblemLocation problem, String annotation, String label) {
	ICompilationUnit cu= (ICompilationUnit)compilationUnit.getJavaElement();
	if (!JavaModelUtil.is50OrHigher(cu.getJavaProject()))
		return null;

	ASTNode selectedNode= problem.getCoveringNode(compilationUnit);
	if (selectedNode == null)
		return null;

	ASTNode declaringNode= getDeclaringNode(selectedNode);
	if (!(declaringNode instanceof BodyDeclaration))
		return null;

	BodyDeclaration declaration= (BodyDeclaration) declaringNode;

	AnnotationRewriteOperation operation= new AnnotationRewriteOperation(declaration, annotation);

	return new Java50Fix(label, compilationUnit, new CompilationUnitRewriteOperation[] {operation});
}
 
Example 10
Source File: ModifierCorrectionSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void removeOverrideAnnotationProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
	ICompilationUnit cu= context.getCompilationUnit();

	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (!(selectedNode instanceof MethodDeclaration)) {
		return;
	}
	MethodDeclaration methodDecl= (MethodDeclaration) selectedNode;
	Annotation annot= findAnnotation("java.lang.Override", methodDecl.modifiers()); //$NON-NLS-1$
	if (annot != null) {
		ASTRewrite rewrite= ASTRewrite.create(annot.getAST());
		rewrite.remove(annot, null);
		String label= CorrectionMessages.ModifierCorrectionSubProcessor_remove_override;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_OVERRIDE, image);
		proposals.add(proposal);

		QuickAssistProcessor.getCreateInSuperClassProposals(context, methodDecl.getName(), proposals);
	}
}
 
Example 11
Source File: NullAnnotationsCorrectionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void addReturnAndArgumentTypeProposal(IInvocationContext context, IProblemLocation problem, ChangeKind changeKind, 
		Collection<ICommandAccess> proposals) {
	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);

	boolean isArgumentProblem= NullAnnotationsFix.isComplainingAboutArgument(selectedNode);
	if (isArgumentProblem || NullAnnotationsFix.isComplainingAboutReturn(selectedNode))
		addNullAnnotationInSignatureProposal(context, problem, proposals, changeKind, isArgumentProblem);
}
 
Example 12
Source File: UnusedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static SimpleName getUnusedName(CompilationUnit compilationUnit, IProblemLocation problem) {
	ASTNode selectedNode= problem.getCoveringNode(compilationUnit);

	if (selectedNode instanceof MethodDeclaration) {
		return ((MethodDeclaration) selectedNode).getName();
	} else if (selectedNode instanceof SimpleName) {
		return (SimpleName) selectedNode;
	}

	return null;
}
 
Example 13
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void addUninitializedLocalVariableProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (!(selectedNode instanceof Name)) {
		return;
	}
	Name name= (Name) selectedNode;
	IBinding binding= name.resolveBinding();
	if (!(binding instanceof IVariableBinding)) {
		return;
	}
	IVariableBinding varBinding= (IVariableBinding) binding;

	CompilationUnit astRoot= context.getASTRoot();
	ASTNode node= astRoot.findDeclaringNode(binding);
	if (node instanceof VariableDeclarationFragment) {
		ASTRewrite rewrite= ASTRewrite.create(node.getAST());

		VariableDeclarationFragment fragment= (VariableDeclarationFragment) node;
		if (fragment.getInitializer() != null) {
			return;
		}
		Expression expression= ASTNodeFactory.newDefaultExpression(astRoot.getAST(), varBinding.getType());
		if (expression == null) {
			return;
		}
		rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, expression, null);

		String label= CorrectionMessages.LocalCorrectionsSubProcessor_uninitializedvariable_description;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);

		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.INITIALIZE_VARIABLE, image);
		proposal.addLinkedPosition(rewrite.track(expression), false, "initializer"); //$NON-NLS-1$
		proposals.add(proposal);
	}
}
 
Example 14
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void addUnnecessaryInstanceofProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());

	ASTNode curr= selectedNode;
	while (curr instanceof ParenthesizedExpression) {
		curr= ((ParenthesizedExpression) curr).getExpression();
	}

	if (curr instanceof InstanceofExpression) {
		AST ast= curr.getAST();

		ASTRewrite rewrite= ASTRewrite.create(ast);

		InstanceofExpression inst= (InstanceofExpression) curr;

		InfixExpression expression= ast.newInfixExpression();
		expression.setLeftOperand((Expression) rewrite.createCopyTarget(inst.getLeftOperand()));
		expression.setOperator(InfixExpression.Operator.NOT_EQUALS);
		expression.setRightOperand(ast.newNullLiteral());

		rewrite.replace(inst, expression, null);

		String label= CorrectionMessages.LocalCorrectionsSubProcessor_unnecessaryinstanceof_description;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.UNNECESSARY_INSTANCEOF, image);
		proposals.add(proposal);
	}

}
 
Example 15
Source File: JavadocTagsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void getRemoveJavadocTagProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode node= problem.getCoveringNode(context.getASTRoot());
	while (node != null && !(node instanceof TagElement)) {
		node= node.getParent();
	}
	if (node == null) {
		return;
	}
	ASTRewrite rewrite= ASTRewrite.create(node.getAST());
	rewrite.remove(node, null);

	String label= CorrectionMessages.JavadocTagsSubProcessor_removetag_description;
	Image image= JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
	proposals.add(new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_TAG, image));
}
 
Example 16
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void addValueForAnnotationProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof Annotation) {
		Annotation annotation= (Annotation) selectedNode;
		if (annotation.resolveTypeBinding() == null) {
			return;
		}
		MissingAnnotationAttributesProposal proposal= new MissingAnnotationAttributesProposal(cu, annotation, 10);
		proposals.add(proposal);
	}
}
 
Example 17
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static void addDeprecatedFieldsToMethodsProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof Name) {
		IBinding binding= ((Name) selectedNode).resolveBinding();
		if (binding instanceof IVariableBinding) {
			IVariableBinding variableBinding= (IVariableBinding) binding;
			if (variableBinding.isField()) {
				String qualifiedName= variableBinding.getDeclaringClass().getTypeDeclaration().getQualifiedName();
				String fieldName= variableBinding.getName();
				String[] methodName= getMethod(JavaModelUtil.concatenateName(qualifiedName, fieldName));
				if (methodName != null) {
					AST ast= selectedNode.getAST();
					ASTRewrite astRewrite= ASTRewrite.create(ast);
					ImportRewrite importRewrite= StubUtility.createImportRewrite(context.getASTRoot(), true);

					MethodInvocation method= ast.newMethodInvocation();
					String qfn= importRewrite.addImport(methodName[0]);
					method.setExpression(ast.newName(qfn));
					method.setName(ast.newSimpleName(methodName[1]));
					ASTNode parent= selectedNode.getParent();
					ICompilationUnit cu= context.getCompilationUnit();
					// add explicit type arguments if necessary (for 1.8 and later, we're optimistic that inference just works):
					if (Invocations.isInvocationWithArguments(parent) && !JavaModelUtil.is18OrHigher(cu.getJavaProject())) {
						IMethodBinding methodBinding= Invocations.resolveBinding(parent);
						if (methodBinding != null) {
							ITypeBinding[] parameterTypes= methodBinding.getParameterTypes();
							int i= Invocations.getArguments(parent).indexOf(selectedNode);
							if (parameterTypes.length >= i && parameterTypes[i].isParameterizedType()) {
								ITypeBinding[] typeArguments= parameterTypes[i].getTypeArguments();
								for (int j= 0; j < typeArguments.length; j++) {
									ITypeBinding typeArgument= typeArguments[j];
									typeArgument= Bindings.normalizeForDeclarationUse(typeArgument, ast);
									if (! TypeRules.isJavaLangObject(typeArgument)) {
										// add all type arguments if at least one is found to be necessary:
										List<Type> typeArgumentsList= method.typeArguments();
										for (int k= 0; k < typeArguments.length; k++) {
											typeArgument= typeArguments[k];
											typeArgument= Bindings.normalizeForDeclarationUse(typeArgument, ast);
											typeArgumentsList.add(importRewrite.addImport(typeArgument, ast));
										}
										break;
									}
								}
							}
						}
					}
					
					astRewrite.replace(selectedNode, method, null);

					String label= Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_replacefieldaccesswithmethod_description, BasicElementLabels.getJavaElementName(ASTNodes.asString(method)));
					Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
					ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, astRewrite, IProposalRelevance.REPLACE_FIELD_ACCESS_WITH_METHOD, image);
					proposal.setImportRewrite(importRewrite);
					proposals.add(proposal);
				}
			}
		}
	}
}
 
Example 18
Source File: CodeStyleFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static ToStaticAccessOperation[] createToStaticAccessOperations(CompilationUnit astRoot, HashMap<ASTNode, Block> createdBlocks, IProblemLocation problem, boolean conservative) {
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (selectedNode == null) {
		return null;
	}

	Expression qualifier= null;
	IBinding accessBinding= null;

	if (selectedNode instanceof SimpleName) {
		selectedNode= selectedNode.getParent();
	}
	if (selectedNode instanceof QualifiedName) {
		QualifiedName name= (QualifiedName) selectedNode;
		qualifier= name.getQualifier();
		accessBinding= name.resolveBinding();
	} else if (selectedNode instanceof MethodInvocation) {
		MethodInvocation methodInvocation= (MethodInvocation) selectedNode;
		qualifier= methodInvocation.getExpression();
		accessBinding= methodInvocation.getName().resolveBinding();
	} else if (selectedNode instanceof FieldAccess) {
		FieldAccess fieldAccess= (FieldAccess) selectedNode;
		qualifier= fieldAccess.getExpression();
		accessBinding= fieldAccess.getName().resolveBinding();
	}

	if (accessBinding != null && qualifier != null) {
		if (conservative && ASTResolving.findParentStatement(qualifier) == null)
			return null;
		
		ToStaticAccessOperation declaring= null;
		ITypeBinding declaringTypeBinding= getDeclaringTypeBinding(accessBinding);
		if (declaringTypeBinding != null) {
			declaringTypeBinding= declaringTypeBinding.getTypeDeclaration(); // use generic to avoid any type arguments

			declaring= new ToStaticAccessOperation(declaringTypeBinding, qualifier, createdBlocks);
		}
		ToStaticAccessOperation instance= null;
		ITypeBinding instanceTypeBinding= Bindings.normalizeTypeBinding(qualifier.resolveTypeBinding());
		if (instanceTypeBinding != null) {
			instanceTypeBinding= instanceTypeBinding.getTypeDeclaration();  // use generic to avoid any type arguments
			if (instanceTypeBinding.getTypeDeclaration() != declaringTypeBinding) {
				instance= new ToStaticAccessOperation(instanceTypeBinding, qualifier, createdBlocks);
			}
		}
		if (declaring != null && instance != null) {
			return new ToStaticAccessOperation[] {declaring, instance};
		} else {
			return new ToStaticAccessOperation[] {declaring};
		}
	}
	return null;
}
 
Example 19
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static void getGenerateForLoopProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode coveringNode= problem.getCoveringNode(context.getASTRoot());
	if (coveringNode != null) {
		QuickAssistProcessor.getGenerateForLoopProposals(context, coveringNode, null, proposals);
	}
}
 
Example 20
Source File: TypeMismatchSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static void addIncompatibleReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws JavaModelException {
	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (selectedNode == null) {
		return;
	}
	MethodDeclaration decl= ASTResolving.findParentMethodDeclaration(selectedNode);
	if (decl == null) {
		return;
	}
	IMethodBinding methodDeclBinding= decl.resolveBinding();
	if (methodDeclBinding == null) {
		return;
	}

	ITypeBinding returnType= methodDeclBinding.getReturnType();
	IMethodBinding overridden= Bindings.findOverriddenMethod(methodDeclBinding, false);
	if (overridden == null || overridden.getReturnType() == returnType) {
		return;
	}


	ICompilationUnit cu= context.getCompilationUnit();
	IMethodBinding methodDecl= methodDeclBinding.getMethodDeclaration();
	ITypeBinding overriddenReturnType= overridden.getReturnType();
	if (! JavaModelUtil.is50OrHigher(context.getCompilationUnit().getJavaProject())) {
		overriddenReturnType= overriddenReturnType.getErasure();
	}
	proposals.add(new TypeChangeCorrectionProposal(cu, methodDecl, astRoot, overriddenReturnType, false, IProposalRelevance.CHANGE_RETURN_TYPE));

	ICompilationUnit targetCu= cu;

	IMethodBinding overriddenDecl= overridden.getMethodDeclaration();
	ITypeBinding overridenDeclType= overriddenDecl.getDeclaringClass();

	if (overridenDeclType.isFromSource()) {
		targetCu= ASTResolving.findCompilationUnitForBinding(cu, astRoot, overridenDeclType);
		if (targetCu != null && ASTResolving.isUseableTypeInContext(returnType, overriddenDecl, false)) {
			TypeChangeCorrectionProposal proposal= new TypeChangeCorrectionProposal(targetCu, overriddenDecl, astRoot, returnType, false, IProposalRelevance.CHANGE_RETURN_TYPE_OF_OVERRIDDEN);
			if (overridenDeclType.isInterface()) {
				proposal.setDisplayName(Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changereturnofimplemented_description, BasicElementLabels.getJavaElementName(overriddenDecl.getName())));
			} else {
				proposal.setDisplayName(Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changereturnofoverridden_description, BasicElementLabels.getJavaElementName(overriddenDecl.getName())));
			}
			proposals.add(proposal);
		}
	}
}