org.eclipse.jdt.core.dom.ASTNode#BLOCK源码实例Demo

下面列出了org.eclipse.jdt.core.dom.ASTNode#BLOCK 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: j2cl   文件: CompilationUnitBuilder.java
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;
}
 
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;
}
 
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();
	}
}
 
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;
}
 
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);
	}
}
 
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;
}
 
源代码7 项目: eclipse.jdt.ls   文件: SnippetFinder.java
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;
}
 
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;
}
 
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);
}
 
源代码10 项目: j2cl   文件: CompilationUnitBuilder.java
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());
  }
}