本文整理汇总了Java中org.eclipse.jdt.internal.corext.dom.ASTNodes.getExplicitCast方法的典型用法代码示例。如果您正苦于以下问题:Java ASTNodes.getExplicitCast方法的具体用法?Java ASTNodes.getExplicitCast怎么用?Java ASTNodes.getExplicitCast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.internal.corext.dom.ASTNodes
的用法示例。
在下文中一共展示了ASTNodes.getExplicitCast方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: needsExplicitCast
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入方法依赖的package包/类
/**
* @param status the status
* @return <code>true</code> if explicit cast is needed otherwise <code>false</code>
*/
private boolean needsExplicitCast(RefactoringStatus status) {
// if the return type of the method is the same as the type of the
// returned expression then we don't need an explicit cast.
if (fSourceProvider.returnTypeMatchesReturnExpressions()) return false;
List<Expression> returnExprs = fSourceProvider.getReturnExpressions();
// it is inferred that only methods consisting of a single
// return statement can be inlined as parameters in other
// method invocations
if (returnExprs.size() != 1) return false;
if (fTargetNode.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
MethodInvocation methodInvocation = (MethodInvocation) fTargetNode.getParent();
if (methodInvocation.getExpression() == fTargetNode) return false;
IMethodBinding method = methodInvocation.resolveMethodBinding();
if (method == null) {
status.addError(
RefactoringCoreMessages.CallInliner_cast_analysis_error,
JavaStatusContext.create(fCUnit, methodInvocation));
return false;
}
ITypeBinding[] parameters = method.getParameterTypes();
int argumentIndex = methodInvocation.arguments().indexOf(fInvocation);
ITypeBinding parameterType = returnExprs.get(0).resolveTypeBinding();
if (method.isVarargs() && argumentIndex >= parameters.length - 1) {
argumentIndex = parameters.length - 1;
parameterType = parameterType.createArrayType(1);
}
parameters[argumentIndex] = parameterType;
ITypeBinding type = ASTNodes.getReceiverTypeBinding(methodInvocation);
TypeBindingVisitor visitor =
new AmbiguousMethodAnalyzer(
fTypeEnvironment, method, fTypeEnvironment.create(parameters));
if (!visitor.visit(type)) {
return true;
} else if (type.isInterface()) {
return !Bindings.visitInterfaces(type, visitor);
} else if (Modifier.isAbstract(type.getModifiers())) {
return !Bindings.visitHierarchy(type, visitor);
} else {
// it is not needed to visit interfaces if receiver is a concrete class
return !Bindings.visitSuperclasses(type, visitor);
}
} else {
ITypeBinding explicitCast =
ASTNodes.getExplicitCast(returnExprs.get(0), (Expression) fTargetNode);
return explicitCast != null;
}
}
示例2: replaceParameterWithExpression
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入方法依赖的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);
}
}
示例3: addRemoveIncludingConditionProposal
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入方法依赖的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);
}