当前位置: 首页>>代码示例>>Java>>正文


Java ASTRewriteCorrectionProposal.createImportRewrite方法代码示例

本文整理汇总了Java中org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal.createImportRewrite方法的典型用法代码示例。如果您正苦于以下问题:Java ASTRewriteCorrectionProposal.createImportRewrite方法的具体用法?Java ASTRewriteCorrectionProposal.createImportRewrite怎么用?Java ASTRewriteCorrectionProposal.createImportRewrite使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal的用法示例。


在下文中一共展示了ASTRewriteCorrectionProposal.createImportRewrite方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createTypeRefChangeFullProposal

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
static CUCorrectionProposal createTypeRefChangeFullProposal(
    ICompilationUnit cu, ITypeBinding binding, ASTNode node, int relevance) {
  ASTRewrite rewrite = ASTRewrite.create(node.getAST());
  String label =
      Messages.format(
          CorrectionMessages.UnresolvedElementsSubProcessor_change_full_type_description,
          BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_DEFAULT));
  Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);

  ASTRewriteCorrectionProposal proposal =
      new ASTRewriteCorrectionProposal(label, cu, rewrite, relevance, image);

  ImportRewrite imports = proposal.createImportRewrite((CompilationUnit) node.getRoot());
  Type type = imports.addImport(binding, node.getAST());

  rewrite.replace(node, type, null);
  return proposal;
}
 
开发者ID:eclipse,项目名称:che,代码行数:19,代码来源:UnresolvedElementsSubProcessor.java

示例2: getAmbiguosTypeReferenceProposals

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
public static void getAmbiguosTypeReferenceProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
	final ICompilationUnit cu= context.getCompilationUnit();
	int offset= problem.getOffset();
	int len= problem.getLength();

	IJavaElement[] elements= cu.codeSelect(offset, len);
	for (int i= 0; i < elements.length; i++) {
		IJavaElement curr= elements[i];
		if (curr instanceof IType && !TypeFilter.isFiltered((IType) curr)) {
			String qualifiedTypeName= ((IType) curr).getFullyQualifiedName('.');

			CompilationUnit root= context.getASTRoot();

			String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importexplicit_description, BasicElementLabels.getJavaElementName(qualifiedTypeName));
			Image image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_IMPDECL);
			ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, ASTRewrite.create(root.getAST()), IProposalRelevance.IMPORT_EXPLICIT, image);

			ImportRewrite imports= proposal.createImportRewrite(root);
			imports.addImport(qualifiedTypeName);

			proposals.add(proposal);
		}
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:25,代码来源:UnresolvedElementsSubProcessor.java

示例3: getAmbiguosTypeReferenceProposals

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
public static void getAmbiguosTypeReferenceProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
	final ICompilationUnit cu= context.getCompilationUnit();
	int offset= problem.getOffset();
	int len= problem.getLength();

	IJavaElement[] elements= cu.codeSelect(offset, len);
	for (int i= 0; i < elements.length; i++) {
		IJavaElement curr= elements[i];
		if (curr instanceof IType && !TypeFilter.isFiltered((IType) curr)) {
			String qualifiedTypeName= ((IType) curr).getFullyQualifiedName('.');

			CompilationUnit root= context.getASTRoot();

			String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importexplicit_description, BasicElementLabels.getJavaElementName(qualifiedTypeName));
			Image image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_IMPDECL);
			ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, ASTRewrite.create(root.getAST()), 5, image);

			ImportRewrite imports= proposal.createImportRewrite(root);
			imports.addImport(qualifiedTypeName);

			proposals.add(proposal);
		}
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:25,代码来源:UnresolvedElementsSubProcessor.java

示例4: addQualifierToOuterProposal

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
private static void addQualifierToOuterProposal(
    IInvocationContext context,
    MethodInvocation invocationNode,
    IMethodBinding binding,
    Collection<ICommandAccess> proposals) {
  ITypeBinding declaringType = binding.getDeclaringClass();
  ITypeBinding parentType = Bindings.getBindingOfParentType(invocationNode);
  ITypeBinding currType = parentType;

  boolean isInstanceMethod = !Modifier.isStatic(binding.getModifiers());

  while (currType != null && !Bindings.isSuperType(declaringType, currType)) {
    if (isInstanceMethod && Modifier.isStatic(currType.getModifiers())) {
      return;
    }
    currType = currType.getDeclaringClass();
  }
  if (currType == null || currType == parentType) {
    return;
  }

  ASTRewrite rewrite = ASTRewrite.create(invocationNode.getAST());

  String label =
      Messages.format(
          CorrectionMessages.UnresolvedElementsSubProcessor_changetoouter_description,
          ASTResolving.getTypeSignature(currType));
  Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
  ASTRewriteCorrectionProposal proposal =
      new ASTRewriteCorrectionProposal(
          label,
          context.getCompilationUnit(),
          rewrite,
          IProposalRelevance.QUALIFY_WITH_ENCLOSING_TYPE,
          image);

  ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
  ImportRewriteContext importRewriteContext =
      new ContextSensitiveImportRewriteContext(invocationNode, imports);
  AST ast = invocationNode.getAST();

  String qualifier = imports.addImport(currType, importRewriteContext);
  Name name = ASTNodeFactory.newName(ast, qualifier);

  Expression newExpression;
  if (isInstanceMethod) {
    ThisExpression expr = ast.newThisExpression();
    expr.setQualifier(name);
    newExpression = expr;
  } else {
    newExpression = name;
  }

  rewrite.set(invocationNode, MethodInvocation.EXPRESSION_PROPERTY, newExpression, null);

  proposals.add(proposal);
}
 
开发者ID:eclipse,项目名称:che,代码行数:58,代码来源:UnresolvedElementsSubProcessor.java

示例5: getAmbiguosTypeReferenceProposals

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
public static void getAmbiguosTypeReferenceProposals(
    IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals)
    throws CoreException {
  final ICompilationUnit cu = context.getCompilationUnit();
  int offset = problem.getOffset();
  int len = problem.getLength();

  IJavaElement[] elements = cu.codeSelect(offset, len);
  for (int i = 0; i < elements.length; i++) {
    IJavaElement curr = elements[i];
    if (curr instanceof IType && !TypeFilter.isFiltered((IType) curr)) {
      String qualifiedTypeName = ((IType) curr).getFullyQualifiedName('.');

      CompilationUnit root = context.getASTRoot();

      String label =
          Messages.format(
              CorrectionMessages.UnresolvedElementsSubProcessor_importexplicit_description,
              BasicElementLabels.getJavaElementName(qualifiedTypeName));
      Image image = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_IMPDECL);
      ASTRewriteCorrectionProposal proposal =
          new ASTRewriteCorrectionProposal(
              label,
              cu,
              ASTRewrite.create(root.getAST()),
              IProposalRelevance.IMPORT_EXPLICIT,
              image);

      ImportRewrite imports = proposal.createImportRewrite(root);
      imports.addImport(qualifiedTypeName);

      proposals.add(proposal);
    }
  }
}
 
开发者ID:eclipse,项目名称:che,代码行数:36,代码来源:UnresolvedElementsSubProcessor.java

示例6: createTypeRefChangeFullProposal

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
static CUCorrectionProposal createTypeRefChangeFullProposal(ICompilationUnit cu, ITypeBinding binding, ASTNode node, int relevance) {
	ASTRewrite rewrite= ASTRewrite.create(node.getAST());
	String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_change_full_type_description, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_DEFAULT));
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);

	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, relevance, image);

	ImportRewrite imports= proposal.createImportRewrite((CompilationUnit) node.getRoot());
	Type type= imports.addImport(binding, node.getAST());

	rewrite.replace(node, type, null);
	return proposal;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:14,代码来源:UnresolvedElementsSubProcessor.java

示例7: addExplicitTypeArgumentsIfNecessary

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
private static void addExplicitTypeArgumentsIfNecessary(ASTRewrite rewrite, ASTRewriteCorrectionProposal proposal, Expression invocation) {
	if (Invocations.isResolvedTypeInferredFromExpectedType(invocation)) {
		ITypeBinding[] typeArguments= Invocations.getInferredTypeArguments(invocation);
		if (typeArguments == null)
			return;
		
		ImportRewrite importRewrite= proposal.getImportRewrite();
		if (importRewrite == null) {
			importRewrite= proposal.createImportRewrite((CompilationUnit) invocation.getRoot());
		}
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(invocation, importRewrite);
		
		AST ast= invocation.getAST();
		ListRewrite typeArgsRewrite= Invocations.getInferredTypeArgumentsRewrite(rewrite, invocation);
		
		for (int i= 0; i < typeArguments.length; i++) {
			Type typeArgumentNode= importRewrite.addImport(typeArguments[i], ast, importRewriteContext);
			typeArgsRewrite.insertLast(typeArgumentNode, null);
		}
		
		if (invocation instanceof MethodInvocation) {
			MethodInvocation methodInvocation= (MethodInvocation) invocation;
			Expression expression= methodInvocation.getExpression();
			if (expression == null) {
				IMethodBinding methodBinding= methodInvocation.resolveMethodBinding();
				if (methodBinding != null && Modifier.isStatic(methodBinding.getModifiers())) {
					expression= ast.newName(importRewrite.addImport(methodBinding.getDeclaringClass().getTypeDeclaration(), importRewriteContext));
				} else {
					expression= ast.newThisExpression();
				}
				rewrite.set(invocation, MethodInvocation.EXPRESSION_PROPERTY, expression, null);
			}
		}
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:36,代码来源:AdvancedQuickAssistProcessor.java

示例8: createTypeRefChangeFullProposal

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
static CUCorrectionProposal createTypeRefChangeFullProposal(ICompilationUnit cu, ITypeBinding binding, ASTNode node, int relevance) {
	ASTRewrite rewrite= ASTRewrite.create(node.getAST());
	String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_change_full_type_description, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_DEFAULT));
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);


	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, relevance + 3, image);

	ImportRewrite imports= proposal.createImportRewrite((CompilationUnit) node.getRoot());
	Type type= imports.addImport(binding, node.getAST());

	rewrite.replace(node, type, null);
	return proposal;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:15,代码来源:UnresolvedElementsSubProcessor.java

示例9: addRemoveIncludingConditionProposal

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
private static void addRemoveIncludingConditionProposal(
    IInvocationContext context,
    ASTNode toRemove,
    ASTNode replacement,
    Collection<ICommandAccess> proposals) {
  Image image =
      JavaPluginImages.get(
          JavaPluginImages
              .IMG_TOOL_DELETE); // JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
  String label =
      CorrectionMessages
          .LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
  AST ast = toRemove.getAST();
  ASTRewrite rewrite = ASTRewrite.create(ast);
  ASTRewriteCorrectionProposal proposal =
      new ASTRewriteCorrectionProposal(
          label,
          context.getCompilationUnit(),
          rewrite,
          IProposalRelevance.REMOVE_UNREACHABLE_CODE_INCLUDING_CONDITION,
          image);

  if (replacement == null
      || replacement instanceof EmptyStatement
      || replacement instanceof Block && ((Block) replacement).statements().size() == 0) {
    if (ASTNodes.isControlStatementBody(toRemove.getLocationInParent())) {
      rewrite.replace(toRemove, toRemove.getAST().newBlock(), null);
    } else {
      rewrite.remove(toRemove, null);
    }

  } else if (toRemove instanceof Expression && replacement instanceof Expression) {
    Expression moved = (Expression) rewrite.createMoveTarget(replacement);
    Expression toRemoveExpression = (Expression) toRemove;
    Expression replacementExpression = (Expression) replacement;
    ITypeBinding explicitCast =
        ASTNodes.getExplicitCast(replacementExpression, toRemoveExpression);
    if (explicitCast != null) {
      CastExpression cast = ast.newCastExpression();
      if (NecessaryParenthesesChecker.needsParentheses(
          replacementExpression, cast, CastExpression.EXPRESSION_PROPERTY)) {
        ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
        parenthesized.setExpression(moved);
        moved = parenthesized;
      }
      cast.setExpression(moved);
      ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
      ImportRewriteContext importRewriteContext =
          new ContextSensitiveImportRewriteContext(toRemove, imports);
      cast.setType(imports.addImport(explicitCast, ast, importRewriteContext));
      moved = cast;
    }
    rewrite.replace(toRemove, moved, null);

  } else {
    ASTNode parent = toRemove.getParent();
    ASTNode moveTarget;
    if ((parent instanceof Block || parent instanceof SwitchStatement)
        && replacement instanceof Block) {
      ListRewrite listRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
      List<Statement> list = ((Block) replacement).statements();
      int lastIndex = list.size() - 1;
      moveTarget = listRewrite.createMoveTarget(list.get(0), list.get(lastIndex));
    } else {
      moveTarget = rewrite.createMoveTarget(replacement);
    }

    rewrite.replace(toRemove, moveTarget, null);
  }

  proposals.add(proposal);
}
 
开发者ID:eclipse,项目名称:che,代码行数:73,代码来源:LocalCorrectionsSubProcessor.java

示例10: addExplicitTypeArgumentsIfNecessary

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
private static void addExplicitTypeArgumentsIfNecessary(
    ASTRewrite rewrite, ASTRewriteCorrectionProposal proposal, Expression invocation) {
  if (Invocations.isResolvedTypeInferredFromExpectedType(invocation)) {
    ITypeBinding[] typeArguments = Invocations.getInferredTypeArguments(invocation);
    if (typeArguments == null) return;

    ImportRewrite importRewrite = proposal.getImportRewrite();
    if (importRewrite == null) {
      importRewrite = proposal.createImportRewrite((CompilationUnit) invocation.getRoot());
    }
    ImportRewriteContext importRewriteContext =
        new ContextSensitiveImportRewriteContext(invocation, importRewrite);

    AST ast = invocation.getAST();
    ListRewrite typeArgsRewrite =
        Invocations.getInferredTypeArgumentsRewrite(rewrite, invocation);

    for (int i = 0; i < typeArguments.length; i++) {
      Type typeArgumentNode =
          importRewrite.addImport(typeArguments[i], ast, importRewriteContext);
      typeArgsRewrite.insertLast(typeArgumentNode, null);
    }

    if (invocation instanceof MethodInvocation) {
      MethodInvocation methodInvocation = (MethodInvocation) invocation;
      Expression expression = methodInvocation.getExpression();
      if (expression == null) {
        IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
        if (methodBinding != null && Modifier.isStatic(methodBinding.getModifiers())) {
          expression =
              ast.newName(
                  importRewrite.addImport(
                      methodBinding.getDeclaringClass().getTypeDeclaration(),
                      importRewriteContext));
        } else {
          expression = ast.newThisExpression();
        }
        rewrite.set(invocation, MethodInvocation.EXPRESSION_PROPERTY, expression, null);
      }
    }
  }
}
 
开发者ID:eclipse,项目名称:che,代码行数:43,代码来源:AdvancedQuickAssistProcessor.java

示例11: addRemoveIncludingConditionProposal

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
private static void addRemoveIncludingConditionProposal(IInvocationContext context, ASTNode toRemove, ASTNode replacement, Collection<ICommandAccess> proposals) {
	Image image= JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
	String label= CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
	AST ast= toRemove.getAST();
	ASTRewrite rewrite= ASTRewrite.create(ast);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_UNREACHABLE_CODE_INCLUDING_CONDITION, image);
	
	if (replacement == null
			|| replacement instanceof EmptyStatement
			|| replacement instanceof Block && ((Block)replacement).statements().size() == 0) {
		if (ASTNodes.isControlStatementBody(toRemove.getLocationInParent())) {
			rewrite.replace(toRemove, toRemove.getAST().newBlock(), null);
		} else {
			rewrite.remove(toRemove, null);
		}
		
	} else if (toRemove instanceof Expression && replacement instanceof Expression) {
		Expression moved= (Expression) rewrite.createMoveTarget(replacement);
		Expression toRemoveExpression= (Expression) toRemove;
		Expression replacementExpression= (Expression) replacement;
		ITypeBinding explicitCast= ASTNodes.getExplicitCast(replacementExpression, toRemoveExpression);
		if (explicitCast != null) {
			CastExpression cast= ast.newCastExpression();
			if (NecessaryParenthesesChecker.needsParentheses(replacementExpression, cast, CastExpression.EXPRESSION_PROPERTY)) {
				ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
				parenthesized.setExpression(moved);
				moved= parenthesized;
			}
			cast.setExpression(moved);
			ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot());
			ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(toRemove, imports);
			cast.setType(imports.addImport(explicitCast, ast, importRewriteContext));
			moved= cast;
		}
		rewrite.replace(toRemove, moved, null);
		
	} else {
		ASTNode parent= toRemove.getParent();
		ASTNode moveTarget;
		if ((parent instanceof Block || parent instanceof SwitchStatement) && replacement instanceof Block) {
			ListRewrite listRewrite= rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
			List<Statement> list= ((Block)replacement).statements();
			int lastIndex= list.size() - 1;
			moveTarget= listRewrite.createMoveTarget(list.get(0), list.get(lastIndex));
		} else {
			moveTarget= rewrite.createMoveTarget(replacement);
		}

		rewrite.replace(toRemove, moveTarget, null);
	}
	
	proposals.add(proposal);
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:54,代码来源:LocalCorrectionsSubProcessor.java

示例12: addQualifierToOuterProposal

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
private static void addQualifierToOuterProposal(IInvocationContext context, MethodInvocation invocationNode, IMethodBinding binding, Collection<ICommandAccess> proposals) {
	ITypeBinding declaringType= binding.getDeclaringClass();
	ITypeBinding parentType= Bindings.getBindingOfParentType(invocationNode);
	ITypeBinding currType= parentType;

	boolean isInstanceMethod= !Modifier.isStatic(binding.getModifiers());

	while (currType != null && !Bindings.isSuperType(declaringType, currType)) {
		if (isInstanceMethod && Modifier.isStatic(currType.getModifiers())) {
			return;
		}
		currType= currType.getDeclaringClass();
	}
	if (currType == null || currType == parentType) {
		return;
	}

	ASTRewrite rewrite= ASTRewrite.create(invocationNode.getAST());

	String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetoouter_description, ASTResolving.getTypeSignature(currType));
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.QUALIFY_WITH_ENCLOSING_TYPE, image);

	ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot());
	ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(invocationNode, imports);
	AST ast= invocationNode.getAST();

	String qualifier= imports.addImport(currType, importRewriteContext);
	Name name= ASTNodeFactory.newName(ast, qualifier);

	Expression newExpression;
	if (isInstanceMethod) {
		ThisExpression expr= ast.newThisExpression();
		expr.setQualifier(name);
		newExpression= expr;
	} else {
		newExpression= name;
	}

	rewrite.set(invocationNode, MethodInvocation.EXPRESSION_PROPERTY, newExpression, null);

	proposals.add(proposal);
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:44,代码来源:UnresolvedElementsSubProcessor.java

示例13: addRemoveIncludingConditionProposal

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
private static void addRemoveIncludingConditionProposal(IInvocationContext context, ASTNode toRemove, ASTNode replacement, Collection<ICommandAccess> proposals) {
	Image image= JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
	String label= CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
	AST ast= toRemove.getAST();
	ASTRewrite rewrite= ASTRewrite.create(ast);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, 10, image);
	
	if (replacement == null
			|| replacement instanceof EmptyStatement
			|| replacement instanceof Block && ((Block)replacement).statements().size() == 0) {
		if (ASTNodes.isControlStatementBody(toRemove.getLocationInParent())) {
			rewrite.replace(toRemove, toRemove.getAST().newBlock(), null);
		} else {
			rewrite.remove(toRemove, null);
		}
		
	} else if (toRemove instanceof Expression && replacement instanceof Expression) {
		Expression moved= (Expression) rewrite.createMoveTarget(replacement);
		Expression toRemoveExpression= (Expression) toRemove;
		Expression replacementExpression= (Expression) replacement;
		ITypeBinding explicitCast= ASTNodes.getExplicitCast(replacementExpression, toRemoveExpression);
		if (explicitCast != null) {
			CastExpression cast= ast.newCastExpression();
			if (NecessaryParenthesesChecker.needsParentheses(replacementExpression, cast, CastExpression.EXPRESSION_PROPERTY)) {
				ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
				parenthesized.setExpression(moved);
				moved= parenthesized;
			}
			cast.setExpression(moved);
			ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot());
			ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(toRemove, imports);
			cast.setType(imports.addImport(explicitCast, ast, importRewriteContext));
			moved= cast;
		}
		rewrite.replace(toRemove, moved, null);
		
	} else {
		ASTNode parent= toRemove.getParent();
		ASTNode moveTarget;
		if ((parent instanceof Block || parent instanceof SwitchStatement) && replacement instanceof Block) {
			ListRewrite listRewrite= rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
			List<Statement> list= ((Block)replacement).statements();
			int lastIndex= list.size() - 1;
			moveTarget= listRewrite.createMoveTarget(list.get(0), list.get(lastIndex));
		} else {
			moveTarget= rewrite.createMoveTarget(replacement);
		}

		rewrite.replace(toRemove, moveTarget, null);
	}
	
	proposals.add(proposal);
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:54,代码来源:LocalCorrectionsSubProcessor.java

示例14: addTypeMismatchInForEachProposals

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
public static void addTypeMismatchInForEachProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (selectedNode == null || selectedNode.getLocationInParent() != EnhancedForStatement.EXPRESSION_PROPERTY) {
		return;
	}
	EnhancedForStatement forStatement= (EnhancedForStatement) selectedNode.getParent();


	ITypeBinding expressionBinding= forStatement.getExpression().resolveTypeBinding();
	if (expressionBinding == null) {
		return;
	}

	ITypeBinding expectedBinding;
	if (expressionBinding.isArray()) {
		expectedBinding= expressionBinding.getComponentType();
	} else {
		IMethodBinding iteratorMethod= Bindings.findMethodInHierarchy(expressionBinding, "iterator", new String[0]); //$NON-NLS-1$
		if (iteratorMethod == null) {
			return;
		}
		ITypeBinding[] typeArguments= iteratorMethod.getReturnType().getTypeArguments();
		if (typeArguments.length != 1) {
			return;
		}
		expectedBinding= typeArguments[0];
	}
	AST ast= astRoot.getAST();
	expectedBinding= Bindings.normalizeForDeclarationUse(expectedBinding, ast);

	SingleVariableDeclaration parameter= forStatement.getParameter();

	String label= Messages.format(CorrectionMessages.TypeMismatchSubProcessor_incompatible_for_each_type_description, new String[] { BasicElementLabels.getJavaElementName(parameter.getName().getIdentifier()), BindingLabelProvider.getBindingLabel(expectedBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS) });
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	ASTRewrite rewrite= ASTRewrite.create(ast);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, 5, image);

	ImportRewrite importRewrite= proposal.createImportRewrite(astRoot);
	ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(selectedNode), importRewrite);
	Type newType= importRewrite.addImport(expectedBinding, ast, importRewriteContext);
	rewrite.replace(parameter.getType(), newType, null);

	proposals.add(proposal);
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:46,代码来源:TypeMismatchSubProcessor.java

示例15: addQualifierToOuterProposal

import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; //导入方法依赖的package包/类
private static void addQualifierToOuterProposal(IInvocationContext context, MethodInvocation invocationNode, IMethodBinding binding, Collection<ICommandAccess> proposals) {
	ITypeBinding declaringType= binding.getDeclaringClass();
	ITypeBinding parentType= Bindings.getBindingOfParentType(invocationNode);
	ITypeBinding currType= parentType;

	boolean isInstanceMethod= !Modifier.isStatic(binding.getModifiers());

	while (currType != null && !Bindings.isSuperType(declaringType, currType)) {
		if (isInstanceMethod && Modifier.isStatic(currType.getModifiers())) {
			return;
		}
		currType= currType.getDeclaringClass();
	}
	if (currType == null || currType == parentType) {
		return;
	}

	ASTRewrite rewrite= ASTRewrite.create(invocationNode.getAST());

	String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetoouter_description, ASTResolving.getTypeSignature(currType));
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, 8, image);

	ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot());
	ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(invocationNode, imports);
	AST ast= invocationNode.getAST();

	String qualifier= imports.addImport(currType, importRewriteContext);
	Name name= ASTNodeFactory.newName(ast, qualifier);

	Expression newExpression;
	if (isInstanceMethod) {
		ThisExpression expr= ast.newThisExpression();
		expr.setQualifier(name);
		newExpression= expr;
	} else {
		newExpression= name;
	}

	rewrite.set(invocationNode, MethodInvocation.EXPRESSION_PROPERTY, newExpression, null);

	proposals.add(proposal);
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:44,代码来源:UnresolvedElementsSubProcessor.java


注:本文中的org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal.createImportRewrite方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。