use of org.eclipse.jdt.core.dom.LambdaExpression in project flux by eclipse.
the class ASTNodes method isExplicitlyTypedLambda.
private static boolean isExplicitlyTypedLambda(Expression expression) {
if (!(expression instanceof LambdaExpression))
return false;
LambdaExpression lambda = (LambdaExpression) expression;
List<VariableDeclaration> parameters = lambda.parameters();
if (parameters.isEmpty())
return true;
return parameters.get(0) instanceof SingleVariableDeclaration;
}
use of org.eclipse.jdt.core.dom.LambdaExpression in project flux by eclipse.
the class ASTNodes method getTargetType.
/**
* Derives the target type defined at the location of the given expression if the target context
* supports poly expressions.
*
* @param expression the expression at whose location the target type is required
* @return the type binding of the target type defined at the location of the given expression
* if the target context supports poly expressions, or <code>null</code> if the target
* type could not be derived
*
* @since 3.10
*/
public static ITypeBinding getTargetType(Expression expression) {
ASTNode parent = expression.getParent();
StructuralPropertyDescriptor locationInParent = expression.getLocationInParent();
if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY) {
return ((VariableDeclaration) parent).getName().resolveTypeBinding();
} else if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
return ((Assignment) parent).getLeftHandSide().resolveTypeBinding();
} else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
return getTargetTypeForReturnStmt((ReturnStatement) parent);
} else if (locationInParent == ArrayInitializer.EXPRESSIONS_PROPERTY) {
return getTargetTypeForArrayInitializer((ArrayInitializer) parent);
} else if (locationInParent == MethodInvocation.ARGUMENTS_PROPERTY) {
MethodInvocation methodInvocation = (MethodInvocation) parent;
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null) {
return getParameterTypeBinding(expression, methodInvocation.arguments(), methodBinding);
}
} else if (locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY) {
SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
IMethodBinding superMethodBinding = superMethodInvocation.resolveMethodBinding();
if (superMethodBinding != null) {
return getParameterTypeBinding(expression, superMethodInvocation.arguments(), superMethodBinding);
}
} else if (locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY) {
ConstructorInvocation constructorInvocation = (ConstructorInvocation) parent;
IMethodBinding constructorBinding = constructorInvocation.resolveConstructorBinding();
if (constructorBinding != null) {
return getParameterTypeBinding(expression, constructorInvocation.arguments(), constructorBinding);
}
} else if (locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY) {
SuperConstructorInvocation superConstructorInvocation = (SuperConstructorInvocation) parent;
IMethodBinding superConstructorBinding = superConstructorInvocation.resolveConstructorBinding();
if (superConstructorBinding != null) {
return getParameterTypeBinding(expression, superConstructorInvocation.arguments(), superConstructorBinding);
}
} else if (locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY) {
ClassInstanceCreation creation = (ClassInstanceCreation) parent;
IMethodBinding creationBinding = creation.resolveConstructorBinding();
if (creationBinding != null) {
return getParameterTypeBinding(expression, creation.arguments(), creationBinding);
}
} else if (locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY) {
EnumConstantDeclaration enumConstantDecl = (EnumConstantDeclaration) parent;
IMethodBinding enumConstructorBinding = enumConstantDecl.resolveConstructorBinding();
if (enumConstructorBinding != null) {
return getParameterTypeBinding(expression, enumConstantDecl.arguments(), enumConstructorBinding);
}
} else if (locationInParent == LambdaExpression.BODY_PROPERTY) {
IMethodBinding methodBinding = ((LambdaExpression) parent).resolveMethodBinding();
if (methodBinding != null) {
return methodBinding.getReturnType();
}
} else if (locationInParent == ConditionalExpression.THEN_EXPRESSION_PROPERTY || locationInParent == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
return getTargetType((ConditionalExpression) parent);
} else if (locationInParent == CastExpression.EXPRESSION_PROPERTY) {
return ((CastExpression) parent).getType().resolveBinding();
} else if (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
return getTargetType((ParenthesizedExpression) parent);
}
return null;
}
use of org.eclipse.jdt.core.dom.LambdaExpression in project che by eclipse.
the class ExtractMethodAnalyzer method visit.
@Override
public boolean visit(LambdaExpression node) {
Selection selection = getSelection();
int selectionStart = selection.getOffset();
int selectionExclusiveEnd = selection.getExclusiveEnd();
int lambdaStart = node.getStartPosition();
int lambdaExclusiveEnd = lambdaStart + node.getLength();
ASTNode body = node.getBody();
int bodyStart = body.getStartPosition();
int bodyExclusiveEnd = bodyStart + body.getLength();
boolean isValidSelection = false;
if ((body instanceof Block) && (bodyStart < selectionStart && selectionExclusiveEnd <= bodyExclusiveEnd)) {
// if selection is inside lambda body's block
isValidSelection = true;
} else if (body instanceof Expression) {
try {
TokenScanner scanner = new TokenScanner(fCUnit);
int arrowExclusiveEnd = scanner.getTokenEndOffset(ITerminalSymbols.TokenNameARROW, lambdaStart);
if (selectionStart >= arrowExclusiveEnd) {
isValidSelection = true;
}
} catch (CoreException e) {
// ignore
}
}
if (selectionStart <= lambdaStart && selectionExclusiveEnd >= lambdaExclusiveEnd) {
// if selection covers the lambda node
isValidSelection = true;
}
if (!isValidSelection) {
return false;
}
return super.visit(node);
}
use of org.eclipse.jdt.core.dom.LambdaExpression in project che by eclipse.
the class ExtractMethodAnalyzer method computeLastStatementSelected.
private void computeLastStatementSelected() {
ASTNode[] nodes = getSelectedNodes();
if (nodes.length == 0) {
fIsLastStatementSelected = false;
} else {
Block body = null;
LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
if (enclosingLambdaExpr != null) {
ASTNode lambdaBody = enclosingLambdaExpr.getBody();
if (lambdaBody instanceof Block) {
body = (Block) lambdaBody;
} else {
fIsLastStatementSelected = true;
return;
}
} else {
if (fEnclosingBodyDeclaration instanceof MethodDeclaration) {
body = ((MethodDeclaration) fEnclosingBodyDeclaration).getBody();
} else if (fEnclosingBodyDeclaration instanceof Initializer) {
body = ((Initializer) fEnclosingBodyDeclaration).getBody();
}
}
if (body != null) {
List<Statement> statements = body.statements();
if (statements.size() > 0) {
fIsLastStatementSelected = nodes[nodes.length - 1] == statements.get(statements.size() - 1);
} else {
fIsLastStatementSelected = true;
}
}
}
}
use of org.eclipse.jdt.core.dom.LambdaExpression in project che by eclipse.
the class ExtractMethodAnalyzer method initReturnType.
private void initReturnType(ImportRewrite rewriter) {
AST ast = fEnclosingBodyDeclaration.getAST();
fReturnType = null;
fReturnTypeBinding = null;
switch(fReturnKind) {
case ACCESS_TO_LOCAL:
VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
fReturnType = ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
if (declaration.resolveBinding() != null) {
fReturnTypeBinding = declaration.resolveBinding().getType();
}
break;
case EXPRESSION:
Expression expression = (Expression) getFirstSelectedNode();
if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding();
} else {
fExpressionBinding = expression.resolveTypeBinding();
}
if (fExpressionBinding != null) {
if (fExpressionBinding.isNullType()) {
getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
} else {
ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
if (normalizedBinding != null) {
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
fReturnType = rewriter.addImport(normalizedBinding, ast, context);
fReturnTypeBinding = normalizedBinding;
}
}
} else {
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
}
break;
case RETURN_STATEMENT_VALUE:
LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
if (enclosingLambdaExpr != null) {
fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null;
} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null;
}
break;
default:
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
}
if (fReturnType == null) {
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
}
}
Aggregations