org.eclipse.jdt.ui.text.java.IInvocationContext Java Examples

The following examples show how to use org.eclipse.jdt.ui.text.java.IInvocationContext. 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: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean getRemoveExtraParenthesesProposals(IInvocationContext context, ASTNode covering, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
	ArrayList<ASTNode> nodes;
	if (context.getSelectionLength() == 0 && covering instanceof ParenthesizedExpression) {
		nodes= new ArrayList<ASTNode>();
		nodes.add(covering);
	} else {
		nodes= coveredNodes;
	}
	if (nodes.isEmpty())
		return false;

	IProposableFix fix= ExpressionsFix.createRemoveUnnecessaryParenthesisFix(context.getASTRoot(), nodes.toArray(new ASTNode[nodes.size()]));
	if (fix == null)
		return false;

	if (resultingCollections == null)
		return true;

	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_REMOVE);
	Map<String, String> options= new Hashtable<String, String>();
	options.put(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES, CleanUpOptions.TRUE);
	options.put(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER, CleanUpOptions.TRUE);
	FixCorrectionProposal proposal= new FixCorrectionProposal(fix, new ExpressionsCleanUp(options), IProposalRelevance.REMOVE_EXTRA_PARENTHESES, image, context);
	resultingCollections.add(proposal);
	return true;
}
 
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 addCasesOmittedProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) {
		AST ast= selectedNode.getAST();
		SwitchStatement parent= (SwitchStatement) selectedNode.getParent();
		
		for (Statement statement : (List<Statement>) parent.statements()) {
			if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) {
				
				// insert //$CASES-OMITTED$:
				ASTRewrite rewrite= ASTRewrite.create(ast);
				rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
				ListRewrite listRewrite= rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
				ASTNode casesOmittedComment= rewrite.createStringPlaceholder("//$CASES-OMITTED$", ASTNode.EMPTY_STATEMENT); //$NON-NLS-1$
				listRewrite.insertBefore(casesOmittedComment, statement, null);
				
				String label= CorrectionMessages.LocalCorrectionsSubProcessor_insert_cases_omitted;
				Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
				ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_CASES_OMITTED, image);
				proposals.add(proposal);
				break;
			}
		}
	}
}
 
Example #3
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static ASTRewriteCorrectionProposal createNoSideEffectProposal(IInvocationContext context, SimpleName nodeToQualify, IVariableBinding fieldBinding, String label, int relevance) {
	AST ast= nodeToQualify.getAST();

	Expression qualifier;
	if (Modifier.isStatic(fieldBinding.getModifiers())) {
		ITypeBinding declaringClass= fieldBinding.getDeclaringClass();
		qualifier= ast.newSimpleName(declaringClass.getTypeDeclaration().getName());
	} else {
		qualifier= ast.newThisExpression();
	}

	ASTRewrite rewrite= ASTRewrite.create(ast);
	FieldAccess access= ast.newFieldAccess();
	access.setName((SimpleName) rewrite.createCopyTarget(nodeToQualify));
	access.setExpression(qualifier);
	rewrite.replace(nodeToQualify, access, null);


	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	return new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, relevance, image);
}
 
Example #4
Source File: ModifierCorrectionSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void addNonFinalLocalProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> 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_TO_FINAL, image));
	}
}
 
Example #5
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 #6
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 #7
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 #8
Source File: QuickFixProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IProblemLocation[] locations) throws CoreException {
	if (locations == null || locations.length == 0) {
		return null;
	}

	HashSet<Integer> handledProblems= new HashSet<Integer>(locations.length);
	ArrayList<ICommandAccess> resultingCollections= new ArrayList<ICommandAccess>();
	for (int i= 0; i < locations.length; i++) {
		IProblemLocation curr= locations[i];
		Integer id= new Integer(curr.getProblemId());
		if (handledProblems.add(id)) {
			process(context, curr, resultingCollections);
		}
	}
	return resultingCollections.toArray(new IJavaCompletionProposal[resultingCollections.size()]);
}
 
Example #9
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 #10
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean getRenameLocalProposals(IInvocationContext context, ASTNode node, IProblemLocation[] locations, Collection<ICommandAccess> resultingCollections) {
	if (!(node instanceof SimpleName)) {
		return false;
	}
	SimpleName name= (SimpleName) node;
	IBinding binding= name.resolveBinding();
	if (binding != null && binding.getKind() == IBinding.PACKAGE) {
		return false;
	}

	if (containsQuickFixableRenameLocal(locations)) {
		return false;
	}

	if (resultingCollections == null) {
		return true;
	}

	LinkedNamesAssistProposal proposal= new LinkedNamesAssistProposal(context, name);
	if (locations.length != 0) {
		proposal.setRelevance(IProposalRelevance.LINKED_NAMES_ASSIST_ERROR);
	}

	resultingCollections.add(proposal);
	return true;
}
 
Example #11
Source File: UnresolvedElementsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static void addParameterMissmatchProposals(IInvocationContext context, IProblemLocation problem, List<IMethodBinding> similarElements, ASTNode invocationNode, List<Expression> arguments, Collection<ICommandAccess> proposals) throws CoreException {
	int nSimilarElements= similarElements.size();
	ITypeBinding[] argTypes= getArgumentTypes(arguments);
	if (argTypes == null || nSimilarElements == 0)  {
		return;
	}

	for (int i= 0; i < nSimilarElements; i++) {
		IMethodBinding elem = similarElements.get(i);
		int diff= elem.getParameterTypes().length - argTypes.length;
		if (diff == 0) {
			int nProposals= proposals.size();
			doEqualNumberOfParameters(context, invocationNode, problem, arguments, argTypes, elem, proposals);
			if (nProposals != proposals.size()) {
				return; // only suggest for one method (avoid duplicated proposals)
			}
		} else if (diff > 0) {
			doMoreParameters(context, invocationNode, argTypes, elem, proposals);
		} else {
			doMoreArguments(context, invocationNode, arguments, argTypes, elem, proposals);
		}
	}
}
 
Example #12
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean getAddParanoidalParenthesesProposals(IInvocationContext context, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {

		IProposableFix fix= ExpressionsFix.createAddParanoidalParenthesisFix(context.getASTRoot(), coveredNodes.toArray(new ASTNode[coveredNodes.size()]));
		if (fix == null)
			return false;

		if (resultingCollections == null)
			return true;

		// add correction proposal
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
		Map<String, String> options= new Hashtable<String, String>();
		options.put(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES, CleanUpOptions.TRUE);
		options.put(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS, CleanUpOptions.TRUE);
		FixCorrectionProposal proposal= new FixCorrectionProposal(fix, new ExpressionsCleanUp(options), IProposalRelevance.ADD_PARANOIDAL_PARENTHESES, image, context);
		resultingCollections.add(proposal);
		return true;
	}
 
Example #13
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean getConvertForLoopProposal(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
	ForStatement forStatement= getEnclosingForStatementHeader(node);
	if (forStatement == null)
		return false;

	if (resultingCollections == null)
		return true;

	IProposableFix fix= ConvertLoopFix.createConvertForLoopToEnhancedFix(context.getASTRoot(), forStatement);
	if (fix == null)
		return false;

	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	Map<String, String> options= new HashMap<String, String>();
	options.put(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED, CleanUpOptions.TRUE);
	ICleanUp cleanUp= new ConvertLoopCleanUp(options);
	FixCorrectionProposal proposal= new FixCorrectionProposal(fix, cleanUp, IProposalRelevance.CONVERT_FOR_LOOP_TO_ENHANCED, image, context);
	proposal.setCommandId(CONVERT_FOR_LOOP_ID);

	resultingCollections.add(proposal);
	return true;
}
 
Example #14
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void addUnusedMemberProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	int problemId = problem.getProblemId();
	UnusedCodeFix fix= UnusedCodeFix.createUnusedMemberFix(context.getASTRoot(), problem, false);
	if (fix != null) {
		addProposal(context, proposals, fix);
	}

	if (problemId==IProblem.LocalVariableIsNeverUsed){
		fix= UnusedCodeFix.createUnusedMemberFix(context.getASTRoot(), problem, true);
		addProposal(context, proposals, fix);
	}

	if (problemId == IProblem.ArgumentIsNeverUsed) {
		JavadocTagsSubProcessor.getUnusedAndUndocumentedParameterOrExceptionProposals(context, problem, proposals);
	}

	if (problemId == IProblem.UnusedPrivateField) {
		GetterSetterCorrectionSubProcessor.addGetterSetterProposal(context, problem, proposals, IProposalRelevance.GETTER_SETTER_UNUSED_PRIVATE_FIELD);
	}

}
 
Example #15
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean getConvertIterableLoopProposal(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
	ForStatement forStatement= getEnclosingForStatementHeader(node);
	if (forStatement == null)
		return false;

	if (resultingCollections == null)
		return true;

	IProposableFix fix= ConvertLoopFix.createConvertIterableLoopToEnhancedFix(context.getASTRoot(), forStatement);
	if (fix == null)
		return false;

	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	Map<String, String> options= new HashMap<String, String>();
	options.put(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED, CleanUpOptions.TRUE);
	ICleanUp cleanUp= new ConvertLoopCleanUp(options);
	FixCorrectionProposal proposal= new FixCorrectionProposal(fix, cleanUp, IProposalRelevance.CONVERT_ITERABLE_LOOP_TO_ENHANCED, image, context);
	proposal.setCommandId(CONVERT_FOR_LOOP_ID);

	resultingCollections.add(proposal);
	return true;
}
 
Example #16
Source File: GetterSetterCorrectionSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean addGetterSetterProposal(IInvocationContext context, ASTNode coveringNode, Collection<ICommandAccess> proposals, int relevance) {
	if (!(coveringNode instanceof SimpleName)) {
		return false;
	}
	SimpleName sn= (SimpleName) coveringNode;

	IBinding binding= sn.resolveBinding();
	if (!(binding instanceof IVariableBinding))
		return false;
	IVariableBinding variableBinding= (IVariableBinding) binding;
	if (!variableBinding.isField())
		return false;

	if (proposals == null)
		return true;

	ChangeCorrectionProposal proposal= getProposal(context.getCompilationUnit(), sn, variableBinding, relevance);
	if (proposal != null)
		proposals.add(proposal);
	return true;
}
 
Example #17
Source File: TypeArgumentMismatchSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void removeMismatchedArguments(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals){
	ICompilationUnit cu= context.getCompilationUnit();
	ASTNode selectedNode= problem.getCoveredNode(context.getASTRoot());
	if (!(selectedNode instanceof SimpleName)) {
		return;
	}

	ASTNode normalizedNode=ASTNodes.getNormalizedNode(selectedNode);
	if (normalizedNode instanceof ParameterizedType) {
		ASTRewrite rewrite = ASTRewrite.create(normalizedNode.getAST());
		ParameterizedType pt = (ParameterizedType) normalizedNode;
		ASTNode mt = rewrite.createMoveTarget(pt.getType());
		rewrite.replace(pt, mt, null);
		String label= CorrectionMessages.TypeArgumentMismatchSubProcessor_removeTypeArguments;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_TYPE_ARGUMENTS, image);
		proposals.add(proposal);
	}
}
 
Example #18
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean getAddFinallyProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
	TryStatement tryStatement= ASTResolving.findParentTryStatement(node);
	if (tryStatement == null || tryStatement.getFinally() != null) {
		return false;
	}
	Statement statement= ASTResolving.findParentStatement(node);
	if (tryStatement != statement && tryStatement.getBody() != statement) {
		return false; // an node inside a catch or finally block
	}

	if (resultingCollections == null) {
		return true;
	}

	AST ast= tryStatement.getAST();
	ASTRewrite rewrite= ASTRewrite.create(ast);
	Block finallyBody= ast.newBlock();

	rewrite.set(tryStatement, TryStatement.FINALLY_PROPERTY, finallyBody, null);

	String label= CorrectionMessages.QuickAssistProcessor_addfinallyblock_description;
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_FINALLY_BLOCK, image);
	resultingCollections.add(proposal);
	return true;
}
 
Example #19
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 #20
Source File: SurroundWithTemplateMenuAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static IAction[] getTemplateActions(JavaEditor editor) {
	ITextSelection textSelection= getTextSelection(editor);
	if (textSelection == null || textSelection.getLength() == 0)
		return null;

	ICompilationUnit cu= JavaUI.getWorkingCopyManager().getWorkingCopy(editor.getEditorInput());
	if (cu == null)
		return null;

	QuickTemplateProcessor quickTemplateProcessor= new QuickTemplateProcessor();
	IInvocationContext context= new AssistContext(cu, textSelection.getOffset(), textSelection.getLength());

	try {
		IJavaCompletionProposal[] proposals= quickTemplateProcessor.getAssists(context, null);
		if (proposals == null || proposals.length == 0)
			return null;

		return getActionsFromProposals(proposals, context.getSelectionOffset(), editor.getViewer());
	} catch (CoreException e) {
		JavaPlugin.log(e);
	}
	return null;
}
 
Example #21
Source File: ReturnTypeSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void addMethodRetunsVoidProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws JavaModelException {
	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (!(selectedNode instanceof ReturnStatement)) {
		return;
	}
	ReturnStatement returnStatement= (ReturnStatement) selectedNode;
	Expression expression= returnStatement.getExpression();
	if (expression == null) {
		return;
	}
	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methDecl= (MethodDeclaration) decl;
		Type retType= methDecl.getReturnType2();
		if (retType == null || retType.resolveBinding() == null) {
			return;
		}
		TypeMismatchSubProcessor.addChangeSenderTypeProposals(context, expression, retType.resolveBinding(), false, IProposalRelevance.METHOD_RETURNS_VOID, proposals);
	}
}
 
Example #22
Source File: JavadocTagsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void getInvalidQualificationProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode node= problem.getCoveringNode(context.getASTRoot());
	if (!(node instanceof Name)) {
		return;
	}
	Name name= (Name) node;
	IBinding binding= name.resolveBinding();
	if (!(binding instanceof ITypeBinding)) {
		return;
	}
	ITypeBinding typeBinding= (ITypeBinding)binding;

	AST ast= node.getAST();
	ASTRewrite rewrite= ASTRewrite.create(ast);
	rewrite.replace(name, ast.newName(typeBinding.getQualifiedName()), null);

	String label= CorrectionMessages.JavadocTagsSubProcessor_qualifylinktoinner_description;
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.QUALIFY_INNER_TYPE_NAME, image);

	proposals.add(proposal);
}
 
Example #23
Source File: GetterSetterCorrectionSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Used by quick assist
 * @param context the invocation context
 * @param coveringNode the covering node
 * @param locations the problems at the corrent location
 * @param resultingCollections the resulting proposals
 * @return <code>true</code> if the quick assist is applicable at this offset
 */
public static boolean addGetterSetterProposal(IInvocationContext context, ASTNode coveringNode, IProblemLocation[] locations, ArrayList<ICommandAccess> resultingCollections) {
	if (locations != null) {
		for (int i= 0; i < locations.length; i++) {
			int problemId= locations[i].getProblemId();
			if (problemId == IProblem.UnusedPrivateField)
				return false;
			if (problemId == IProblem.UnqualifiedFieldAccess)
				return false;
		}
	}
	return addGetterSetterProposal(context, coveringNode, resultingCollections, IProposalRelevance.GETTER_SETTER_QUICK_ASSIST);
}
 
Example #24
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 #25
Source File: QuickAssistProcessor1.java    From codeexamples-eclipse with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public IJavaCompletionProposal[] getAssists(IInvocationContext context, IProblemLocation[] locations)
		throws CoreException {
	return new IJavaCompletionProposal[] { new AbstractJavaCompletionProposal() {
		public org.eclipse.jface.viewers.StyledString getStyledDisplayString() {
			ICompilationUnit compilationUnit = context.getCompilationUnit();
			return new StyledString(
					"Generate Getter and setter for " + compilationUnit.findPrimaryType().getElementName());
		}
		
		protected int getPatternMatchRule(String pattern, String string) {
			// override the match rule since we do not work with a pattern, but just want to open the "Generate Getters and Setters..." dialog
			return -1;
		};
		
		public void apply(org.eclipse.jface.text.ITextViewer viewer, char trigger, int stateMask, int offset) {
			
			if(context instanceof AssistContext) {
				AssistContext assistContext = (AssistContext) context;
				AddGetterSetterAction addGetterSetterAction = new AddGetterSetterAction((CompilationUnitEditor)assistContext.getEditor());
				
				addGetterSetterAction.run();
			}
			
		}
	} };
}
 
Example #26
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 #27
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean getConvertSwitchToIfProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
	if (!(covering instanceof SwitchStatement)) {
		return false;
	}
	//  we could produce quick assist (if all 'case' statements end with 'break')
	if (resultingCollections == null) {
		return true;
	}
	if (!getConvertSwitchToIfProposals(context, covering, resultingCollections, false))
		return false;
	return getConvertSwitchToIfProposals(context, covering, resultingCollections, true);
}
 
Example #28
Source File: ReorgCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void getAccessRulesProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	IBinding referencedElement= null;
	ASTNode node= problem.getCoveredNode(context.getASTRoot());
	if (node instanceof Type) {
		referencedElement= ((Type) node).resolveBinding();
	} else if (node instanceof Name) {
		referencedElement= ((Name) node).resolveBinding();
	}
	if (referencedElement != null && canModifyAccessRules(referencedElement)) {
		IProject project= context.getCompilationUnit().getJavaProject().getProject();
		String label= CorrectionMessages.ReorgCorrectionsSubProcessor_accessrules_description;
		OpenBuildPathCorrectionProposal proposal= new OpenBuildPathCorrectionProposal(project, label, IProposalRelevance.CONFIGURE_ACCESS_RULES, referencedElement);
		proposals.add(proposal);
	}
}
 
Example #29
Source File: NullAnnotationsCorrectionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void addRemoveRedundantAnnotationProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	NullAnnotationsFix fix= NullAnnotationsFix.createRemoveRedundantNullAnnotationsFix(context.getASTRoot(), problem);
	if (fix == null)
		return;

	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	Map<String, String> options= new Hashtable<String, String>();
	FixCorrectionProposal proposal= new FixCorrectionProposal(fix, new NullAnnotationsCleanUp(options, problem.getProblemId()), IProposalRelevance.REMOVE_REDUNDANT_NULLNESS_ANNOTATION, image, context);
	proposals.add(proposal);
}
 
Example #30
Source File: JavaMarkerResolutionGenerator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public IMarkerResolution[] getResolutions(IMarker marker) {
  if (!hasResolutions(marker)) {
    return NO_RESOLUTIONS;
  }

  ICompilationUnit cu = getCompilationUnit(marker);
  if (cu != null) {
    IEditorInput input = new FileEditorInput(
        (IFile) cu.getPrimary().getResource());
    if (input != null) {
      int offset = marker.getAttribute(IMarker.CHAR_START, -1);
      int length = marker.getAttribute(IMarker.CHAR_END, -1) - offset;
      int problemId = marker.getAttribute(IJavaModelMarker.ID, -1);
      boolean isError = (marker.getAttribute(IMarker.SEVERITY, -1) == IMarker.SEVERITY_ERROR);
      String[] arguments = CorrectionEngine.getProblemArguments(marker);

      IProblemLocation location = new ProblemLocation(offset, length,
          problemId, arguments, isError, null);
      IInvocationContext context = new AssistContext(cu, offset, length);

      IJavaCompletionProposal[] proposals = new IJavaCompletionProposal[0];

      try {
        proposals = getCorrections(context, new IProblemLocation[] {location});
      } catch (CoreException e) {
        CorePluginLog.logError(e);
      }

      int nProposals = proposals.length;
      IMarkerResolution[] resolutions = new IMarkerResolution[nProposals];
      for (int i = 0; i < nProposals; i++) {
        resolutions[i] = new QuickFixCompletionProposalWrapper(cu, offset,
            length, proposals[i]);
      }
      return resolutions;
    }
  }

  return NO_RESOLUTIONS;
}