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


Java CastExpression.setType方法代码示例

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


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

示例1: createUnobservedInitStmt

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private void createUnobservedInitStmt(final int logRecNo, final Block methodBlock, final AST ast)
{
	// NOTE: PLAIN INIT: has always one non-null param
	// TODO: use primitives
	final int    oid     = this.log.objectIds.get(logRecNo);
	final String type    = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
	final Object value   = this.log.params.get(logRecNo)[0];
	this.isXStreamNeeded = true;
	
	final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
	// handling because there must always be a new instantiation statement for pseudo inits
	this.oidToVarMapping.remove(oid);
	vd.setName(ast.newSimpleName(this.createNewVarName(oid, type)));
	
	final MethodInvocation methodInvocation = ast.newMethodInvocation();
	final Name name = ast.newSimpleName("XSTREAM");
	methodInvocation.setExpression(name);
	methodInvocation.setName(ast.newSimpleName("fromXML")); 
	
	final StringLiteral xmlParam = ast.newStringLiteral();
	xmlParam.setLiteralValue((String) value);
	methodInvocation.arguments().add(xmlParam);
	
	final CastExpression castExpr = ast.newCastExpression();
	castExpr.setType(this.createAstType(type, ast));
	castExpr.setExpression(methodInvocation);
	
	vd.setInitializer(castExpr);
	
	final VariableDeclarationStatement vs = ast.newVariableDeclarationStatement(vd);
	vs.setType(this.createAstType(type, ast));
			
	methodBlock.statements().add(vs);
}
 
开发者ID:EvoSuite,项目名称:evosuite,代码行数:36,代码来源:CodeGenerator.java

示例2: createShiftAssignment

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
private Expression createShiftAssignment(Expression shift1, Expression shift2) {
	// (int)(element ^ (element >>> 32));
	// see implementation in Arrays.hashCode(), Double.hashCode() and
	// Long.hashCode()
	CastExpression ce= fAst.newCastExpression();
	ce.setType(fAst.newPrimitiveType(PrimitiveType.INT));

	InfixExpression unsignedShiftRight= fAst.newInfixExpression();
	unsignedShiftRight.setLeftOperand(shift1);
	unsignedShiftRight.setRightOperand(fAst.newNumberLiteral("32")); //$NON-NLS-1$
	unsignedShiftRight.setOperator(Operator.RIGHT_SHIFT_UNSIGNED);

	InfixExpression xor= fAst.newInfixExpression();
	xor.setLeftOperand(shift2);
	xor.setRightOperand(parenthesize(unsignedShiftRight));
	xor.setOperator(InfixExpression.Operator.XOR);

	ce.setExpression(parenthesize(xor));
	return ce;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:21,代码来源:GenerateHashCodeEqualsOperation.java

示例3: createNarrowCastIfNessecary

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
/**
 * Checks if the assignment needs a downcast and inserts it if necessary
 *
 * @param expression the right hand-side
 * @param expressionType the type of the right hand-side. Can be null
 * @param ast the AST
 * @param variableType the Type of the variable the expression will be assigned to
 * @param is50OrHigher if <code>true</code> java 5.0 code will be assumed
 * @return the casted expression if necessary
 */
private static Expression createNarrowCastIfNessecary(
    Expression expression,
    ITypeBinding expressionType,
    AST ast,
    ITypeBinding variableType,
    boolean is50OrHigher) {
  PrimitiveType castTo = null;
  if (variableType.isEqualTo(expressionType)) return expression; // no cast for same type
  if (is50OrHigher) {
    if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType)) // $NON-NLS-1$
    castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
    if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType)) // $NON-NLS-1$
    castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
    if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType)) // $NON-NLS-1$
    castTo = ast.newPrimitiveType(PrimitiveType.SHORT);
  }
  if (ast.resolveWellKnownType("char").isEqualTo(variableType)) // $NON-NLS-1$
  castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
  if (ast.resolveWellKnownType("byte").isEqualTo(variableType)) // $NON-NLS-1$
  castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
  if (ast.resolveWellKnownType("short").isEqualTo(variableType)) // $NON-NLS-1$
  castTo = ast.newPrimitiveType(PrimitiveType.SHORT);
  if (castTo != null) {
    CastExpression cast = ast.newCastExpression();
    if (NecessaryParenthesesChecker.needsParentheses(
        expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
      ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
      parenthesized.setExpression(expression);
      cast.setExpression(parenthesized);
    } else cast.setExpression(expression);
    cast.setType(castTo);
    return cast;
  }
  return expression;
}
 
开发者ID:eclipse,项目名称:che,代码行数:46,代码来源:GetterSetterUtil.java

示例4: createNarrowCastIfNessecary

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
/**
 * Checks if the assignment needs a downcast and inserts it if necessary
 *
 * @param expression the right hand-side
 * @param expressionType the type of the right hand-side. Can be null
 * @param ast the AST
 * @param variableType the Type of the variable the expression will be assigned to
 * @param is50OrHigher if <code>true</code> java 5.0 code will be assumed
 * @return the casted expression if necessary
 */
private static Expression createNarrowCastIfNessecary(Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType, boolean is50OrHigher) {
	PrimitiveType castTo= null;
	if (variableType.isEqualTo(expressionType))
		return expression; //no cast for same type
	if (is50OrHigher) {
		if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType)) //$NON-NLS-1$
			castTo= ast.newPrimitiveType(PrimitiveType.CHAR);
		if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType)) //$NON-NLS-1$
			castTo= ast.newPrimitiveType(PrimitiveType.BYTE);
		if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType)) //$NON-NLS-1$
			castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
	}
	if (ast.resolveWellKnownType("char").isEqualTo(variableType)) //$NON-NLS-1$
		castTo= ast.newPrimitiveType(PrimitiveType.CHAR);
	if (ast.resolveWellKnownType("byte").isEqualTo(variableType)) //$NON-NLS-1$
		castTo= ast.newPrimitiveType(PrimitiveType.BYTE);
	if (ast.resolveWellKnownType("short").isEqualTo(variableType)) //$NON-NLS-1$
		castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
	if (castTo != null) {
		CastExpression cast= ast.newCastExpression();
		if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
			ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
			parenthesized.setExpression(expression);
			cast.setExpression(parenthesized);
		} else
			cast.setExpression(expression);
		cast.setType(castTo);
		return cast;
	}
	return expression;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:42,代码来源:GetterSetterUtil.java

示例5: useExistingParentCastProposal

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
private static boolean useExistingParentCastProposal(ICompilationUnit cu, CastExpression expression, Expression accessExpression, SimpleName accessSelector, ITypeBinding[] paramTypes, Collection<ICommandAccess> proposals) {
	ITypeBinding castType= expression.getType().resolveBinding();
	if (castType == null) {
		return false;
	}
	if (paramTypes != null) {
		if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes) == null) {
			return false;
		}
	} else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) {
		return false;
	}
	ITypeBinding bindingToCast= accessExpression.resolveTypeBinding();
	if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) {
		return false;
	}

	IMethodBinding res= Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes);
	if (res != null) {
		AST ast= expression.getAST();
		ASTRewrite rewrite= ASTRewrite.create(ast);
		CastExpression newCast= ast.newCastExpression();
		newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType()));
		newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression));
		ParenthesizedExpression parents= ast.newParenthesizedExpression();
		parents.setExpression(newCast);

		ASTNode node= rewrite.createCopyTarget(expression.getExpression());
		rewrite.replace(expression, node, null);
		rewrite.replace(accessExpression, parents, null);

		String label= CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
		ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_PARENTHESES_AROUND_CAST, image);
		proposals.add(proposal);
		return true;
	}
	return false;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:40,代码来源:UnresolvedElementsSubProcessor.java

示例6: useExistingParentCastProposal

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
private static boolean useExistingParentCastProposal(ICompilationUnit cu, CastExpression expression, Expression accessExpression, SimpleName accessSelector, ITypeBinding[] paramTypes, Collection<ICommandAccess> proposals) {
	ITypeBinding castType= expression.getType().resolveBinding();
	if (castType == null) {
		return false;
	}
	if (paramTypes != null) {
		if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes) == null) {
			return false;
		}
	} else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) {
		return false;
	}
	ITypeBinding bindingToCast= accessExpression.resolveTypeBinding();
	if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) {
		return false;
	}

	IMethodBinding res= Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes);
	if (res != null) {
		AST ast= expression.getAST();
		ASTRewrite rewrite= ASTRewrite.create(ast);
		CastExpression newCast= ast.newCastExpression();
		newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType()));
		newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression));
		ParenthesizedExpression parents= ast.newParenthesizedExpression();
		parents.setExpression(newCast);

		ASTNode node= rewrite.createCopyTarget(expression.getExpression());
		rewrite.replace(expression, node, null);
		rewrite.replace(accessExpression, parents, null);

		String label= CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
		ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, 8, image);
		proposals.add(proposal);
		return true;
	}
	return false;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:40,代码来源:UnresolvedElementsSubProcessor.java

示例7: createNarrowCastIfNessecary

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
/**
 * Checks if the assignment needs a downcast and inserts it if necessary
 *
 * @param expression
 *            the right hand-side
 * @param expressionType
 *            the type of the right hand-side. Can be null
 * @param ast
 *            the AST
 * @param variableType
 *            the Type of the variable the expression will be assigned to
 * @param is50OrHigher
 *            if <code>true</code> java 5.0 code will be assumed
 * @return the casted expression if necessary
 */
private static Expression createNarrowCastIfNessecary(Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType, boolean is50OrHigher) {
	PrimitiveType castTo = null;
	if (variableType.isEqualTo(expressionType))
	 {
		return expression; //no cast for same type
	}
	if (is50OrHigher) {
		if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType)) {
			castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
		}
		if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType)) {
			castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
		}
		if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType)) {
			castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
		}
	}
	if (ast.resolveWellKnownType("char").isEqualTo(variableType)) {
		castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
	}
	if (ast.resolveWellKnownType("byte").isEqualTo(variableType)) {
		castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
	}
	if (ast.resolveWellKnownType("short").isEqualTo(variableType)) {
		castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
	}
	if (castTo != null) {
		CastExpression cast = ast.newCastExpression();
		if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
			ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
			parenthesized.setExpression(expression);
			cast.setExpression(parenthesized);
		} else {
			cast.setExpression(expression);
		}
		cast.setType(castTo);
		return cast;
	}
	return expression;
}
 
开发者ID:eclipse,项目名称:eclipse.jdt.ls,代码行数:56,代码来源:GetterSetterUtil.java

示例8: useExistingParentCastProposal

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
private static boolean useExistingParentCastProposal(ICompilationUnit cu, CastExpression expression,
		Expression accessExpression, SimpleName accessSelector, ITypeBinding[] paramTypes,
		Collection<CUCorrectionProposal> proposals) {
	ITypeBinding castType= expression.getType().resolveBinding();
	if (castType == null) {
		return false;
	}
	if (paramTypes != null) {
		if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes) == null) {
			return false;
		}
	} else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) {
		return false;
	}
	ITypeBinding bindingToCast= accessExpression.resolveTypeBinding();
	if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) {
		return false;
	}

	IMethodBinding res= Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes);
	if (res != null) {
		AST ast= expression.getAST();
		ASTRewrite rewrite= ASTRewrite.create(ast);
		CastExpression newCast= ast.newCastExpression();
		newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType()));
		newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression));
		ParenthesizedExpression parents= ast.newParenthesizedExpression();
		parents.setExpression(newCast);

		ASTNode node= rewrite.createCopyTarget(expression.getExpression());
		rewrite.replace(expression, node, null);
		rewrite.replace(accessExpression, parents, null);

		String label= CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description;
		ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite,
				IProposalRelevance.ADD_PARENTHESES_AROUND_CAST);
		proposals.add(proposal);
		return true;
	}
	return false;
}
 
开发者ID:eclipse,项目名称:eclipse.jdt.ls,代码行数:42,代码来源:UnresolvedElementsSubProcessor.java

示例9: replaceParameterWithExpression

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
private void replaceParameterWithExpression(
    ASTRewrite rewriter, CallContext context, ImportRewrite importRewrite) throws CoreException {
  Expression[] arguments = context.arguments;
  try {
    ITextFileBuffer buffer = RefactoringFileBuffers.acquire(context.compilationUnit);
    for (int i = 0; i < arguments.length; i++) {
      Expression expression = arguments[i];
      String expressionString = null;
      if (expression instanceof SimpleName) {
        expressionString = ((SimpleName) expression).getIdentifier();
      } else {
        try {
          expressionString =
              buffer.getDocument().get(expression.getStartPosition(), expression.getLength());
        } catch (BadLocationException exception) {
          JavaPlugin.log(exception);
          continue;
        }
      }
      ParameterData parameter = getParameterData(i);
      List<SimpleName> references = parameter.references();
      for (Iterator<SimpleName> iter = references.iterator(); iter.hasNext(); ) {
        ASTNode element = iter.next();
        Expression newExpression =
            (Expression)
                rewriter.createStringPlaceholder(expressionString, expression.getNodeType());
        AST ast = rewriter.getAST();
        ITypeBinding explicitCast = ASTNodes.getExplicitCast(expression, (Expression) element);
        if (explicitCast != null) {
          CastExpression cast = ast.newCastExpression();
          if (NecessaryParenthesesChecker.needsParentheses(
              expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
            newExpression = createParenthesizedExpression(newExpression, ast);
          }
          cast.setExpression(newExpression);
          ImportRewriteContext importRewriteContext =
              new ContextSensitiveImportRewriteContext(expression, importRewrite);
          cast.setType(importRewrite.addImport(explicitCast, ast, importRewriteContext));
          expression = newExpression = cast;
        }
        if (NecessaryParenthesesChecker.needsParentheses(
            expression, element.getParent(), element.getLocationInParent())) {
          newExpression = createParenthesizedExpression(newExpression, ast);
        }
        rewriter.replace(element, newExpression, null);
      }
    }
  } finally {
    RefactoringFileBuffers.release(context.compilationUnit);
  }
}
 
开发者ID:eclipse,项目名称:che,代码行数:52,代码来源:SourceProvider.java

示例10: useExistingParentCastProposal

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
private static boolean useExistingParentCastProposal(
    ICompilationUnit cu,
    CastExpression expression,
    Expression accessExpression,
    SimpleName accessSelector,
    ITypeBinding[] paramTypes,
    Collection<ICommandAccess> proposals) {
  ITypeBinding castType = expression.getType().resolveBinding();
  if (castType == null) {
    return false;
  }
  if (paramTypes != null) {
    if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes)
        == null) {
      return false;
    }
  } else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) {
    return false;
  }
  ITypeBinding bindingToCast = accessExpression.resolveTypeBinding();
  if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) {
    return false;
  }

  IMethodBinding res =
      Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes);
  if (res != null) {
    AST ast = expression.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    CastExpression newCast = ast.newCastExpression();
    newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType()));
    newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression));
    ParenthesizedExpression parents = ast.newParenthesizedExpression();
    parents.setExpression(newCast);

    ASTNode node = rewrite.createCopyTarget(expression.getExpression());
    rewrite.replace(expression, node, null);
    rewrite.replace(accessExpression, parents, null);

    String label =
        CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
    ASTRewriteCorrectionProposal proposal =
        new ASTRewriteCorrectionProposal(
            label, cu, rewrite, IProposalRelevance.ADD_PARENTHESES_AROUND_CAST, image);
    proposals.add(proposal);
    return true;
  }
  return false;
}
 
开发者ID:eclipse,项目名称:che,代码行数:51,代码来源:UnresolvedElementsSubProcessor.java

示例11: replaceParameterWithExpression

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
private void replaceParameterWithExpression(ASTRewrite rewriter, CallContext context, ImportRewrite importRewrite) throws CoreException {
	Expression[] arguments= context.arguments;
	try {
		ITextFileBuffer buffer= RefactoringFileBuffers.acquire(context.compilationUnit);
		for (int i= 0; i < arguments.length; i++) {
			Expression expression= arguments[i];
			String expressionString= null;
			if (expression instanceof SimpleName) {
				expressionString= ((SimpleName)expression).getIdentifier();
			} else {
				try {
					expressionString= buffer.getDocument().get(expression.getStartPosition(), expression.getLength());
				} catch (BadLocationException exception) {
					JavaPlugin.log(exception);
					continue;
				}
			}
			ParameterData parameter= getParameterData(i);
			List<SimpleName> references= parameter.references();
			for (Iterator<SimpleName> iter= references.iterator(); iter.hasNext();) {
				ASTNode element= iter.next();
				Expression newExpression= (Expression)rewriter.createStringPlaceholder(expressionString, expression.getNodeType());
				AST ast= rewriter.getAST();
				ITypeBinding explicitCast= ASTNodes.getExplicitCast(expression, (Expression)element);
				if (explicitCast != null) {
					CastExpression cast= ast.newCastExpression();
					if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
						newExpression= createParenthesizedExpression(newExpression, ast);
					}
					cast.setExpression(newExpression);
					ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(expression, importRewrite);
					cast.setType(importRewrite.addImport(explicitCast, ast, importRewriteContext));
					expression= newExpression= cast;
				}
				if (NecessaryParenthesesChecker.needsParentheses(expression, element.getParent(), element.getLocationInParent())) {
					newExpression= createParenthesizedExpression(newExpression, ast);
				}
				rewriter.replace(element, newExpression, null);
			}
		}
	} finally {
		RefactoringFileBuffers.release(context.compilationUnit);
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:45,代码来源:SourceProvider.java

示例12: rewriteAST

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
/**
		 * {@inheritDoc}
		 */
		@Override
		public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {

			ASTRewrite rewrite= cuRewrite.getASTRewrite();
			ImportRemover importRemover= cuRewrite.getImportRemover();
			AST ast= rewrite.getAST();

			HashMap<ClassInstanceCreation, HashSet<String>> cicToNewNames= new HashMap<ClassInstanceCreation, HashSet<String>>();
			for (int i= 0; i < fExpressions.size(); i++) {
				ClassInstanceCreation classInstanceCreation= fExpressions.get(i);
				TextEditGroup group= createTextEditGroup(FixMessages.LambdaExpressionsFix_convert_to_lambda_expression, cuRewrite);

				AnonymousClassDeclaration anonymTypeDecl= classInstanceCreation.getAnonymousClassDeclaration();
				List<BodyDeclaration> bodyDeclarations= anonymTypeDecl.bodyDeclarations();

				Object object= bodyDeclarations.get(0);
				if (!(object instanceof MethodDeclaration))
					continue;
				MethodDeclaration methodDeclaration= (MethodDeclaration) object;
				HashSet<String> excludedNames= new HashSet<String>();
				if (i != 0) {
					for (ClassInstanceCreation convertedCic : fExpressions.subList(0, i)) {
						if (ASTNodes.isParent(classInstanceCreation, convertedCic)) {
							excludedNames.addAll(cicToNewNames.get(convertedCic));
						}
					}
				}
				HashSet<String> newNames= makeNamesUnique(excludedNames, methodDeclaration, rewrite, group);
				cicToNewNames.put(classInstanceCreation, new HashSet<String>(newNames));
				List<SingleVariableDeclaration> methodParameters= methodDeclaration.parameters();

				// use short form with inferred parameter types and without parentheses if possible
				LambdaExpression lambdaExpression= ast.newLambdaExpression();
				List<VariableDeclaration> lambdaParameters= lambdaExpression.parameters();
				lambdaExpression.setParentheses(methodParameters.size() != 1);
				for (SingleVariableDeclaration methodParameter : methodParameters) {
					VariableDeclarationFragment lambdaParameter= ast.newVariableDeclarationFragment();
					lambdaParameter.setName((SimpleName) rewrite.createCopyTarget(methodParameter.getName()));
					lambdaParameters.add(lambdaParameter);
				}
				
				Block body= methodDeclaration.getBody();
				List<Statement> statements= body.statements();
				ASTNode lambdaBody= body;
				if (statements.size() == 1) {
					// use short form with just an expression body if possible
					Statement statement= statements.get(0);
					if (statement instanceof ExpressionStatement) {
						lambdaBody= ((ExpressionStatement) statement).getExpression();
					} else if (statement instanceof ReturnStatement) {
						Expression returnExpression= ((ReturnStatement) statement).getExpression();
						if (returnExpression != null) {
							lambdaBody= returnExpression;
						}
					}
				}
				//TODO: Bug 421479: [1.8][clean up][quick assist] convert anonymous to lambda must consider lost scope of interface
//				lambdaBody.accept(new InterfaceAccessQualifier(rewrite, classInstanceCreation.getType().resolveBinding())); //TODO: maybe need a separate ASTRewrite and string placeholder
				
				lambdaExpression.setBody(rewrite.createCopyTarget(lambdaBody));
				Expression replacement= lambdaExpression;
				if (ASTNodes.isTargetAmbiguous(classInstanceCreation, lambdaParameters.isEmpty())) {
					CastExpression cast= ast.newCastExpression();
					cast.setExpression(lambdaExpression);
					ImportRewrite importRewrite= cuRewrite.getImportRewrite();
					ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(classInstanceCreation, importRewrite);
					Type castType= importRewrite.addImport(classInstanceCreation.getType().resolveBinding(), ast, importRewriteContext);
					cast.setType(castType);
					importRemover.registerAddedImports(castType);
					replacement= cast;
				}
				rewrite.replace(classInstanceCreation, replacement, group);

				importRemover.registerRemovedNode(classInstanceCreation);
				importRemover.registerRetainedNode(lambdaBody);
			}
		}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:81,代码来源:LambdaExpressionsFix.java

示例13: addRemoveIncludingConditionProposal

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的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

示例14: getModifiedInitializerSource

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的package包/类
private Expression getModifiedInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException {
	VariableDeclaration varDecl= getVariableDeclaration();
	Expression initializer= varDecl.getInitializer();

	ASTNode referenceContext= reference.getParent();
	if (Invocations.isResolvedTypeInferredFromExpectedType(initializer)) {
		if (! (referenceContext instanceof VariableDeclarationFragment
				|| referenceContext instanceof SingleVariableDeclaration
				|| referenceContext instanceof Assignment)) {
			ITypeBinding[] typeArguments= Invocations.getInferredTypeArguments(initializer);
			if (typeArguments != null) {
				String newSource= createParameterizedInvocation(initializer, typeArguments, rewrite);
				return (Expression) rewrite.getASTRewrite().createStringPlaceholder(newSource, initializer.getNodeType());
			}
		}
	}
	
	Expression copy= (Expression) rewrite.getASTRewrite().createCopyTarget(initializer);
	
	AST ast= rewrite.getAST();
	ITypeBinding explicitCast= ASTNodes.getExplicitCast(initializer, reference);
	if (explicitCast != null) {
		CastExpression cast= ast.newCastExpression();
		if (NecessaryParenthesesChecker.needsParentheses(initializer, cast, CastExpression.EXPRESSION_PROPERTY)) {
			ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
			parenthesized.setExpression(copy);
			copy= parenthesized;
		}
		cast.setExpression(copy);
		cast.setType(rewrite.getImportRewrite().addImport(explicitCast, ast));
		copy= cast;
		
	} else if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(varDecl) > 0) {
		ArrayType newType= (ArrayType) ASTNodeFactory.newType(ast, varDecl);

		ArrayCreation newArrayCreation= ast.newArrayCreation();
		newArrayCreation.setType(newType);
		newArrayCreation.setInitializer((ArrayInitializer) copy);
		return newArrayCreation;
	}
	return copy;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:43,代码来源:InlineTempRefactoring.java

示例15: addRemoveIncludingConditionProposal

import org.eclipse.jdt.core.dom.CastExpression; //导入方法依赖的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


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