Java Code Examples for org.eclipse.jdt.core.dom.ASTNode#BLOCK

The following examples show how to use org.eclipse.jdt.core.dom.ASTNode#BLOCK . 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 Block convertLambdaBody(ASTNode lambdaBody) {
  Block body;
  if (lambdaBody.getNodeType() == ASTNode.BLOCK) {
    body = convert((org.eclipse.jdt.core.dom.Block) lambdaBody);
  } else {
    checkArgument(lambdaBody instanceof org.eclipse.jdt.core.dom.Expression);
    Expression lambdaMethodBody = convert((org.eclipse.jdt.core.dom.Expression) lambdaBody);
    Statement statement =
        AstUtils.createReturnOrExpressionStatement(
            getSourcePosition(lambdaBody),
            lambdaMethodBody,
            getEnclosingFunctional().getReturnTypeDescriptor());
    body =
        Block.newBuilder()
            .setSourcePosition(getSourcePosition(lambdaBody))
            .setStatements(statement)
            .build();
  }
  return body;
}
 
Example 2
Source File: NewVariableCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private ASTNode getDominantNode(SimpleName[] names) {
	ASTNode dominator= names[0]; //ASTResolvingUtil.findParentStatement(names[0]);
	for (int i= 1; i < names.length; i++) {
		ASTNode curr= names[i];// ASTResolvingUtil.findParentStatement(names[i]);
		if (curr != dominator) {
			ASTNode parent= getCommonParent(curr, dominator);

			if (curr.getStartPosition() < dominator.getStartPosition()) {
				dominator= curr;
			}
			while (dominator.getParent() != parent) {
				dominator= dominator.getParent();
			}
		}
	}
	int parentKind= dominator.getParent().getNodeType();
	if (parentKind != ASTNode.BLOCK && parentKind != ASTNode.FOR_STATEMENT) {
		return dominator.getParent();
	}
	return dominator;
}
 
Example 3
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public IBinding[] getDeclarationsAfter(int offset, int flags) {
	try {
		org.eclipse.jdt.core.dom.NodeFinder finder= new org.eclipse.jdt.core.dom.NodeFinder(fRoot, offset, 0);
		ASTNode node= finder.getCoveringNode();
		if (node == null) {
			return null;
		}

		ASTNode declaration= ASTResolving.findParentStatement(node);
		while (declaration instanceof Statement && declaration.getNodeType() != ASTNode.BLOCK) {
			declaration= declaration.getParent();
		}

		if (declaration instanceof Block) {
			DefaultBindingRequestor requestor= new DefaultBindingRequestor();
			DeclarationsAfterVisitor visitor= new DeclarationsAfterVisitor(node.getStartPosition(), flags, requestor);
			declaration.accept(visitor);
			List<IBinding> result= requestor.getResult();
			return result.toArray(new IBinding[result.size()]);
		}
		return NO_BINDING;
	} finally {
		clearLists();
	}
}
 
Example 4
Source File: JavaTextSelection.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public boolean resolveInMethodBody() {
	if (fInMethodBodyRequested)
		return fInMethodBody;
	fInMethodBodyRequested= true;
	resolveSelectedNodes();
	ASTNode node= getStartNode();
	if (node == null) {
		fInMethodBody= true;
	} else {
		while (node != null) {
			int nodeType= node.getNodeType();
			if (nodeType == ASTNode.BLOCK && node.getParent() instanceof BodyDeclaration) {
				fInMethodBody= node.getParent().getNodeType() == ASTNode.METHOD_DECLARATION;
				break;
			} else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
				fInMethodBody= false;
				break;
			}
			node= node.getParent();
		}
	}
	return fInMethodBody;
}
 
Example 5
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static ASTNode getCopyOfInner(ASTRewrite rewrite, ASTNode statement, boolean toControlStatementBody) {
	if (statement.getNodeType() == ASTNode.BLOCK) {
		Block block= (Block) statement;
		List<Statement> innerStatements= block.statements();
		int nStatements= innerStatements.size();
		if (nStatements == 1) {
			return rewrite.createCopyTarget(innerStatements.get(0));
		} else if (nStatements > 1) {
			if (toControlStatementBody) {
				return rewrite.createCopyTarget(block);
			}
			ListRewrite listRewrite= rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
			ASTNode first= innerStatements.get(0);
			ASTNode last= innerStatements.get(nStatements - 1);
			return listRewrite.createCopyTarget(first, last);
		}
		return null;
	} else {
		return rewrite.createCopyTarget(statement);
	}
}
 
Example 6
Source File: NewVariableCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ASTNode getDominantNode(SimpleName[] names) {
	ASTNode dominator= names[0]; //ASTResolving.findParentStatement(names[0]);
	for (int i= 1; i < names.length; i++) {
		ASTNode curr= names[i];// ASTResolving.findParentStatement(names[i]);
		if (curr != dominator) {
			ASTNode parent= getCommonParent(curr, dominator);

			if (curr.getStartPosition() < dominator.getStartPosition()) {
				dominator= curr;
			}
			while (dominator.getParent() != parent) {
				dominator= dominator.getParent();
			}
		}
	}
	int parentKind= dominator.getParent().getNodeType();
	if (parentKind != ASTNode.BLOCK && parentKind != ASTNode.FOR_STATEMENT) {
		return dominator.getParent();
	}
	return dominator;
}
 
Example 7
Source File: SnippetFinder.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public boolean hasCorrectNesting(ASTNode node) {
	if (fNodes.size() == 0) {
		return true;
	}
	ASTNode parent = node.getParent();
	if (fNodes.get(0).getParent() != parent) {
		return false;
	}
	// Here we know that we have two elements. In this case the
	// parent must be a block or a switch statement. Otherwise a
	// snippet like "if (true) foo(); else foo();" would match
	// the pattern "foo(); foo();"
	int nodeType = parent.getNodeType();
	return nodeType == ASTNode.BLOCK || nodeType == ASTNode.SWITCH_STATEMENT;
}
 
Example 8
Source File: SnippetFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean hasCorrectNesting(ASTNode node) {
	if (fNodes.size() == 0)
		return true;
	ASTNode parent= node.getParent();
	if(fNodes.get(0).getParent() != parent)
		return false;
	// Here we know that we have two elements. In this case the
	// parent must be a block or a switch statement. Otherwise a
	// snippet like "if (true) foo(); else foo();" would match
	// the pattern "foo(); foo();"
	int nodeType= parent.getNodeType();
	return nodeType == ASTNode.BLOCK || nodeType == ASTNode.SWITCH_STATEMENT;
}
 
Example 9
Source File: GetterSetterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isNotInBlock(ASTNode parent) {
	ASTNode statement= parent.getParent();
	boolean isStatement= statement.getNodeType() != ASTNode.EXPRESSION_STATEMENT;
	ASTNode block= statement.getParent();
	boolean isBlock= block.getNodeType() == ASTNode.BLOCK || block.getNodeType() == ASTNode.SWITCH_STATEMENT;
	boolean isControlStatemenBody= ASTNodes.isControlStatementBody(statement.getLocationInParent());
	return isStatement || !(isBlock || isControlStatemenBody);
}
 
Example 10
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 4 votes vote down vote up
private Statement convertStatement(org.eclipse.jdt.core.dom.Statement statement) {
  switch (statement.getNodeType()) {
    case ASTNode.ASSERT_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.AssertStatement) statement);
    case ASTNode.BLOCK:
      return convert((org.eclipse.jdt.core.dom.Block) statement);
    case ASTNode.BREAK_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.BreakStatement) statement);
    case ASTNode.CONSTRUCTOR_INVOCATION:
      return convert((org.eclipse.jdt.core.dom.ConstructorInvocation) statement);
    case ASTNode.CONTINUE_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.ContinueStatement) statement);
    case ASTNode.DO_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.DoStatement) statement);
    case ASTNode.EMPTY_STATEMENT:
      return new EmptyStatement(getSourcePosition(statement));
    case ASTNode.EXPRESSION_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.ExpressionStatement) statement);
    case ASTNode.FOR_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.ForStatement) statement);
    case ASTNode.ENHANCED_FOR_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.EnhancedForStatement) statement);
    case ASTNode.IF_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.IfStatement) statement);
    case ASTNode.LABELED_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.LabeledStatement) statement);
    case ASTNode.RETURN_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.ReturnStatement) statement);
    case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
      return convert((org.eclipse.jdt.core.dom.SuperConstructorInvocation) statement);
    case ASTNode.SWITCH_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.SwitchStatement) statement);
    case ASTNode.SYNCHRONIZED_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.SynchronizedStatement) statement);
    case ASTNode.THROW_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.ThrowStatement) statement);
    case ASTNode.TRY_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.TryStatement) statement);
    case ASTNode.TYPE_DECLARATION_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.TypeDeclarationStatement) statement);
    case ASTNode.VARIABLE_DECLARATION_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.VariableDeclarationStatement) statement);
    case ASTNode.WHILE_STATEMENT:
      return convert((org.eclipse.jdt.core.dom.WhileStatement) statement);
    default:
      throw internalCompilerError(
          "Unexpected type for Statement: %s", statement.getClass().getName());
  }
}