org.eclipse.jdt.core.dom.LambdaExpression Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.LambdaExpression. 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: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 6 votes vote down vote up
private Expression convert(LambdaExpression expression) {
  MethodDescriptor functionalMethodDescriptor =
      JdtUtils.createMethodDescriptor(expression.resolveMethodBinding());

  return FunctionExpression.newBuilder()
      .setTypeDescriptor(JdtUtils.createTypeDescriptor(expression.resolveTypeBinding()))
      .setParameters(
          JdtUtils.<VariableDeclaration>asTypedList(expression.parameters()).stream()
              .map(this::convert)
              .collect(toImmutableList()))
      .setStatements(
          processEnclosedBy(
              functionalMethodDescriptor,
              () -> convertLambdaBody(expression.getBody()).getStatements()))
      .setSourcePosition(getSourcePosition(expression))
      .build();
}
 
Example #2
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static int getDimensions(VariableDeclaration declaration) {
	int dim= declaration.getExtraDimensions();
	if (declaration instanceof VariableDeclarationFragment && declaration.getParent() instanceof LambdaExpression) {
		LambdaExpression lambda= (LambdaExpression) declaration.getParent();
		IMethodBinding methodBinding= lambda.resolveMethodBinding();
		if (methodBinding != null) {
			ITypeBinding[] parameterTypes= methodBinding.getParameterTypes();
			int index= lambda.parameters().indexOf(declaration);
			ITypeBinding typeBinding= parameterTypes[index];
			return typeBinding.getDimensions();
		}
	} else {
		Type type= getType(declaration);
		if (type instanceof ArrayType) {
			dim+= ((ArrayType) type).getDimensions();
		}
	}
	return dim;
}
 
Example #3
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the type node for the given declaration.
 * 
 * @param declaration the declaration
 * @return the type node or <code>null</code> if the given declaration represents a type
 *         inferred parameter in lambda expression
 */
public static Type getType(VariableDeclaration declaration) {
	if (declaration instanceof SingleVariableDeclaration) {
		return ((SingleVariableDeclaration)declaration).getType();
	} else if (declaration instanceof VariableDeclarationFragment) {
		ASTNode parent= ((VariableDeclarationFragment)declaration).getParent();
		if (parent instanceof VariableDeclarationExpression)
			return ((VariableDeclarationExpression)parent).getType();
		else if (parent instanceof VariableDeclarationStatement)
			return ((VariableDeclarationStatement)parent).getType();
		else if (parent instanceof FieldDeclaration)
			return ((FieldDeclaration)parent).getType();
		else if (parent instanceof LambdaExpression)
			return null;
	}
	Assert.isTrue(false, "Unknown VariableDeclaration"); //$NON-NLS-1$
	return null;
}
 
Example #4
Source File: ExtractMethodAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean isVoidMethod() {
	ITypeBinding binding= null;
	LambdaExpression enclosingLambdaExpr= ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
	if (enclosingLambdaExpr != null) {
		IMethodBinding methodBinding= enclosingLambdaExpr.resolveMethodBinding();
		if (methodBinding != null) {
			binding= methodBinding.getReturnType();
		}
	} else {
		// if we have an initializer
		if (fEnclosingMethodBinding == null)
			return true;
		binding= fEnclosingMethodBinding.getReturnType();
	}
	if (fEnclosingBodyDeclaration.getAST().resolveWellKnownType("void").equals(binding)) //$NON-NLS-1$
		return true;
	return false;
}
 
Example #5
Source File: ASTNodeFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static Type newType(LambdaExpression lambdaExpression, VariableDeclarationFragment declaration, AST ast, ImportRewrite importRewrite, ImportRewriteContext context) {
	IMethodBinding method= lambdaExpression.resolveMethodBinding();
	if (method != null) {
		ITypeBinding[] parameterTypes= method.getParameterTypes();
		int index= lambdaExpression.parameters().indexOf(declaration);
		ITypeBinding typeBinding= parameterTypes[index];
		if (importRewrite != null) {
			return importRewrite.addImport(typeBinding, ast, context);
		} else {
			String qualifiedName= typeBinding.getQualifiedName();
			if (qualifiedName.length() > 0) {
				return newType(ast, qualifiedName);
			}
		}
	}
	// fall-back
	return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
}
 
Example #6
Source File: RenameLocalVariableProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
	initAST();
	if (fTempDeclarationNode == null || fTempDeclarationNode.resolveBinding() == null)
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_must_select_local);

	if (!Checks.isDeclaredIn(fTempDeclarationNode, MethodDeclaration.class)
			&& !Checks.isDeclaredIn(fTempDeclarationNode, Initializer.class)
			&& !Checks.isDeclaredIn(fTempDeclarationNode, LambdaExpression.class)) {
		if (JavaModelUtil.is18OrHigher(fCu.getJavaProject()))
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_only_in_methods_initializers_and_lambda);

		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_only_in_methods_and_initializers);
	}

	initNames();
	return new RefactoringStatus();
}
 
Example #7
Source File: ASTNodeFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the new type node representing the return type of <code>lambdaExpression</code>
 * including the extra dimensions.
 * 
 * @param lambdaExpression the lambda expression
 * @param ast the AST to create the return type with
 * @param importRewrite the import rewrite to use, or <code>null</code>
 * @param context the import rewrite context, or <code>null</code>
 * @return a new type node created with the given AST representing the return type of
 *         <code>lambdaExpression</code>
 * 
 * @since 3.10
 */
public static Type newReturnType(LambdaExpression lambdaExpression, AST ast, ImportRewrite importRewrite, ImportRewriteContext context) {
	IMethodBinding method= lambdaExpression.resolveMethodBinding();
	if (method != null) {
		ITypeBinding returnTypeBinding= method.getReturnType();
		if (importRewrite != null) {
			return importRewrite.addImport(returnTypeBinding, ast);
		} else {
			String qualifiedName= returnTypeBinding.getQualifiedName();
			if (qualifiedName.length() > 0) {
				return newType(ast, qualifiedName);
			}
		}
	}
	// fall-back
	return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
}
 
Example #8
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected String getFullTypeName() {
	ASTNode node= getNode();
	while (true) {
		node= node.getParent();
		if (node instanceof AbstractTypeDeclaration) {
			String typeName= ((AbstractTypeDeclaration) node).getName().getIdentifier();
			if (getNode() instanceof LambdaExpression) {
				return Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_lambda_expression, typeName);
			}
			return typeName;
		} else if (node instanceof ClassInstanceCreation) {
			ClassInstanceCreation cic= (ClassInstanceCreation) node;
			return Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_anonymous_subclass, BasicElementLabels.getJavaElementName(ASTNodes.asString(cic.getType())));
		} else if (node instanceof EnumConstantDeclaration) {
			EnumDeclaration ed= (EnumDeclaration) node.getParent();
			return Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_anonymous_subclass, BasicElementLabels.getJavaElementName(ASTNodes.asString(ed.getName())));
		}
	}
}
 
Example #9
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private OccurrenceUpdate<? extends ASTNode> createOccurrenceUpdate(ASTNode node, CompilationUnitRewrite cuRewrite, RefactoringStatus result) {
	if (BUG_89686 && node instanceof SimpleName && node.getParent() instanceof EnumConstantDeclaration)
		node= node.getParent();

	if (Invocations.isInvocationWithArguments(node))
		return new ReferenceUpdate(node, cuRewrite, result);

	else if (node instanceof SimpleName && node.getParent() instanceof MethodDeclaration)
		return new DeclarationUpdate((MethodDeclaration) node.getParent(), cuRewrite, result);

	else if (node instanceof MemberRef || node instanceof MethodRef)
		return new DocReferenceUpdate(node, cuRewrite, result);

	else if (ASTNodes.getParent(node, ImportDeclaration.class) != null)
		return new StaticImportUpdate((ImportDeclaration) ASTNodes.getParent(node, ImportDeclaration.class), cuRewrite, result);

	else if (node instanceof LambdaExpression)
		return new LambdaExpressionUpdate((LambdaExpression) node, cuRewrite, result);

	else if (node.getLocationInParent() == ExpressionMethodReference.NAME_PROPERTY)
		return new ExpressionMethodRefUpdate((ExpressionMethodReference) node.getParent(), cuRewrite, result);

	else
		return new NullOccurrenceUpdate(node, cuRewrite, result);
}
 
Example #10
Source File: ParFragmentExporter.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean preNext(MethodInvocation curElement) {
	List<LambdaExpression> params = (List<LambdaExpression>) curElement.arguments();
	compiler.println("par");
	boolean isFirst = true;
	for (LambdaExpression interaction : params) {
		if (isFirst) {
			isFirst = false;
		} else {
			compiler.println("else");
		}
		interaction.getBody().accept(compiler);
	}
	return false;
}
 
Example #11
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private ASTNode getEnclosingBodyNode() throws JavaModelException {
	ASTNode node = getSelectedExpression().getAssociatedNode();

	// expression must be in a method, lambda or initializer body.
	// make sure it is not in method or parameter annotation
	StructuralPropertyDescriptor location = null;
	while (node != null && !(node instanceof BodyDeclaration)) {
		location = node.getLocationInParent();
		node = node.getParent();
		if (node instanceof LambdaExpression) {
			break;
		}
	}
	if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY || (location == LambdaExpression.BODY_PROPERTY && ((LambdaExpression) node).resolveMethodBinding() != null)) {
		return (ASTNode) node.getStructuralProperty(location);
	}
	return null;
}
 
Example #12
Source File: ExtractMethodAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private boolean isVoidMethod() {
	ITypeBinding binding = null;
	LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
	if (enclosingLambdaExpr != null) {
		IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
		if (methodBinding != null) {
			binding = methodBinding.getReturnType();
		}
	} else {
		// if we have an initializer
		if (fEnclosingMethodBinding == null) {
			return true;
		}
		binding = fEnclosingMethodBinding.getReturnType();
	}
	if (fEnclosingBodyDeclaration.getAST().resolveWellKnownType("void").equals(binding)) {
		return true;
	}
	return false;
}
 
Example #13
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isLastStatementInEnclosingMethodOrLambda(Statement statement) {
	ASTNode currentStructure= statement;
	ASTNode currentParent= statement.getParent();
	while (!(currentParent instanceof MethodDeclaration || currentParent instanceof LambdaExpression)) {
		// should not be in a loop
		if (currentParent instanceof ForStatement || currentParent instanceof EnhancedForStatement
				|| currentParent instanceof WhileStatement || currentParent instanceof DoStatement) {
			return false;
		}
		if (currentParent instanceof Block) {
			Block parentBlock= (Block) currentParent;
			if (parentBlock.statements().indexOf(currentStructure) != parentBlock.statements().size() - 1) { // not last statement in the block
				return false;
			}
		}
		currentStructure= currentParent;
		currentParent= currentParent.getParent();
	}
	return true;
}
 
Example #14
Source File: RenameLocalVariableProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
	initAST();
	if (fTempDeclarationNode == null || fTempDeclarationNode.resolveBinding() == null) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_must_select_local);
	}

	if (!Checks.isDeclaredIn(fTempDeclarationNode, MethodDeclaration.class)
			&& !Checks.isDeclaredIn(fTempDeclarationNode, Initializer.class)
			&& !Checks.isDeclaredIn(fTempDeclarationNode, LambdaExpression.class)) {
		if (JavaModelUtil.is18OrHigher(fCu.getJavaProject())) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_only_in_methods_initializers_and_lambda);
		}

		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_only_in_methods_and_initializers);
	}

	initNames();
	return new RefactoringStatus();
}
 
Example #15
Source File: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private ASTNode getEnclosingBodyNode() throws JavaModelException {
	ASTNode node = getSelectedExpression().getAssociatedNode();

	// expression must be in a method, lambda or initializer body
	// make sure it is not in method or parameter annotation
	StructuralPropertyDescriptor location = null;
	while (node != null && !(node instanceof BodyDeclaration)) {
		location = node.getLocationInParent();
		node = node.getParent();
		if (node instanceof LambdaExpression) {
			break;
		}
	}
	if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY || (location == LambdaExpression.BODY_PROPERTY && ((LambdaExpression) node).resolveMethodBinding() != null)) {
		return (ASTNode) node.getStructuralProperty(location);
	}
	return null;
}
 
Example #16
Source File: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean shouldReplaceSelectedExpressionWithTempDeclaration() throws JavaModelException {
	IExpressionFragment selectedFragment = getSelectedExpression();
	IExpressionFragment firstExpression = getFirstReplacedExpression();
	if (firstExpression.getStartPosition() < selectedFragment.getStartPosition()) {
		return false;
	}
	ASTNode associatedNode = selectedFragment.getAssociatedNode();
	return (associatedNode.getParent() instanceof ExpressionStatement || associatedNode.getParent() instanceof LambdaExpression) && selectedFragment.matches(ASTFragmentFactory.createFragmentForFullSubtree(associatedNode));
}
 
Example #17
Source File: ExceptionAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(LambdaExpression node) {
	/*
	 * FIXME: Remove this method. It's just a workaround for bug 433426.
	 * ExceptionAnalyzer forces clients to on the wrong enclosing node (BodyDeclaration instead of LambdaExpression's body).
	 */
	return true;
}
 
Example #18
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example #19
Source File: ExtractMethodAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@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);
}
 
Example #20
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static IBinding resolveBinding(ASTNode node) {
	if (node instanceof SimpleName) {
		SimpleName simpleName = (SimpleName) node;
		// workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
		ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
		if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
			ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
			IMethodBinding constructorBinding = cic.resolveConstructorBinding();
			if (constructorBinding == null) {
				return null;
			}
			ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
			if (!declaringClass.isAnonymous()) {
				return constructorBinding;
			}
			ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
			return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
		}
		return simpleName.resolveBinding();

	} else if (node instanceof SuperConstructorInvocation) {
		return ((SuperConstructorInvocation) node).resolveConstructorBinding();
	} else if (node instanceof ConstructorInvocation) {
		return ((ConstructorInvocation) node).resolveConstructorBinding();
	} else if (node instanceof LambdaExpression) {
		return ((LambdaExpression) node).resolveMethodBinding();
	} else {
		return null;
	}
}
 
Example #21
Source File: FlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endVisit(LambdaExpression node) {
	if (skipNode(node))
		return;
	GenericSequentialFlowInfo info= createSequential(node);
	process(info, node.parameters());
	process(info, node.getBody());
	info.setNoReturn();
}
 
Example #22
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the lambda expression node which encloses the given <code>node</code>, or
 * <code>null</code> if none.
 * 
 * @param node the node
 * @return the enclosing lambda expression node for the given <code>node</code>, or
 *         <code>null</code> if none
 * 
 * @since 3.10
 */
public static LambdaExpression findEnclosingLambdaExpression(ASTNode node) {
	node= node.getParent();
	while (node != null) {
		if (node instanceof LambdaExpression) {
			return (LambdaExpression) node;
		}
		if (node instanceof BodyDeclaration || node instanceof AnonymousClassDeclaration) {
			return null;
		}
		node= node.getParent();
	}
	return null;
}
 
Example #23
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void endVisit(LambdaExpression node) {
	if (skipNode(node)) {
		return;
	}
	GenericSequentialFlowInfo info = createSequential(node);
	process(info, node.parameters());
	process(info, node.getBody());
	info.setNoReturn();
}
 
Example #24
Source File: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node = fragment.getAssociatedNode();
	ASTNode parent = node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName())) {
			return false;
		}
	}
	if (isMethodParameter(node)) {
		return false;
	}
	if (isThrowableInCatchBlock(node)) {
		return false;
	}
	if (parent instanceof ExpressionStatement) {
		return false;
	}
	if (parent instanceof LambdaExpression) {
		return false;
	}
	if (isLeftValue(node)) {
		return false;
	}
	if (isReferringToLocalVariableFromFor((Expression) node)) {
		return false;
	}
	if (isUsedInForInitializerOrUpdater((Expression) node)) {
		return false;
	}
	if (parent instanceof SwitchCase) {
		return false;
	}
	return true;
}
 
Example #25
Source File: LambdaExpressionObject.java    From RefactoringMiner with MIT License 5 votes vote down vote up
public LambdaExpressionObject(CompilationUnit cu, String filePath, LambdaExpression lambda) {
	this.locationInfo = new LocationInfo(cu, filePath, lambda, CodeElementType.LAMBDA_EXPRESSION);
	if(lambda.getBody() instanceof Block) {
		this.body = new OperationBody(cu, filePath, (Block)lambda.getBody());
	}
	else if(lambda.getBody() instanceof Expression) {
		this.expression = new AbstractExpression(cu, filePath, (Expression)lambda.getBody(), CodeElementType.LAMBDA_EXPRESSION_BODY);
	}
}
 
Example #26
Source File: Visitor.java    From RefactoringMiner with MIT License 5 votes vote down vote up
public boolean visit(LambdaExpression node) {
	LambdaExpressionObject lambda = new LambdaExpressionObject(cu, filePath, node);
	lambdas.add(lambda);
	if(current.getUserObject() != null) {
		AnonymousClassDeclarationObject anonymous = (AnonymousClassDeclarationObject)current.getUserObject();
		anonymous.getLambdas().add(lambda);
	}
	return false;
}
 
Example #27
Source File: Visitor.java    From RefactoringMiner with MIT License 5 votes vote down vote up
public boolean visit(VariableDeclarationFragment node) {
	if(!(node.getParent() instanceof LambdaExpression)) {
		VariableDeclaration variableDeclaration = new VariableDeclaration(cu, filePath, node);
		variableDeclarations.add(variableDeclaration);
		if(current.getUserObject() != null) {
			AnonymousClassDeclarationObject anonymous = (AnonymousClassDeclarationObject)current.getUserObject();
			anonymous.getVariableDeclarations().add(variableDeclaration);
		}
	}
	return super.visit(node);
}
 
Example #28
Source File: LambdaExpressionsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(LambdaExpression node) {
	ITypeBinding typeBinding= node.resolveTypeBinding();
	if (typeBinding != null && typeBinding.getFunctionalInterfaceMethod() != null) {
		fNodes.add(node);
	}
	return true;
}
 
Example #29
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * The node's enclosing method declaration or <code>null</code> if
 * the node is not inside a method and is not a method declaration itself.
 * 
 * @param node a node
 * @return the enclosing method declaration or <code>null</code>
 */
public static MethodDeclaration findParentMethodDeclaration(ASTNode node) {
	while (node != null) {
		if (node instanceof MethodDeclaration) {
			return (MethodDeclaration) node;
		} else if (node instanceof BodyDeclaration || node instanceof AnonymousClassDeclaration || node instanceof LambdaExpression) {
			return null;
		}
		node= node.getParent();
	}
	return null;
}
 
Example #30
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node = fragment.getAssociatedNode();
	ASTNode parent = node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName())) {
			return false;
		}
	}
	if (isMethodParameter(node)) {
		return false;
	}
	if (isThrowableInCatchBlock(node)) {
		return false;
	}
	if (parent instanceof ExpressionStatement) {
		return false;
	}
	if (parent instanceof LambdaExpression) {
		return false;
	}
	if (isLeftValue(node)) {
		return false;
	}
	if (isReferringToLocalVariableFromFor((Expression) node)) {
		return false;
	}
	if (isUsedInForInitializerOrUpdater((Expression) node)) {
		return false;
	}
	if (parent instanceof SwitchCase) {
		return false;
	}
	return true;
}