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

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

private static ASTNode getInlineableMethodNode(ASTNode node, IJavaElement unit) {
	if (node == null) {
		return null;
	}
	switch (node.getNodeType()) {
		case ASTNode.SIMPLE_NAME:
			StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
			if (locationInParent == MethodDeclaration.NAME_PROPERTY) {
				return node.getParent();
			} else if (locationInParent == MethodInvocation.NAME_PROPERTY || locationInParent == SuperMethodInvocation.NAME_PROPERTY) {
				return unit instanceof ICompilationUnit ? node.getParent() : null; // don't start on invocations in binary
			}
			return null;
		case ASTNode.EXPRESSION_STATEMENT:
			node = ((ExpressionStatement) node).getExpression();
	}
	switch (node.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			return node;
		case ASTNode.METHOD_INVOCATION:
		case ASTNode.SUPER_METHOD_INVOCATION:
		case ASTNode.CONSTRUCTOR_INVOCATION:
			return unit instanceof ICompilationUnit ? node : null; // don't start on invocations in binary
	}
	return null;
}
 
private static ASTNode getInlineableMethodNode(ASTNode node, IJavaElement unit) {
	if (node == null)
		return null;
	switch (node.getNodeType()) {
		case ASTNode.SIMPLE_NAME:
			StructuralPropertyDescriptor locationInParent= node.getLocationInParent();
			if (locationInParent == MethodDeclaration.NAME_PROPERTY) {
				return node.getParent();
			} else if (locationInParent == MethodInvocation.NAME_PROPERTY
					|| locationInParent == SuperMethodInvocation.NAME_PROPERTY) {
				return unit instanceof ICompilationUnit ? node.getParent() : null; // don't start on invocations in binary
			}
			return null;
		case ASTNode.EXPRESSION_STATEMENT:
			node= ((ExpressionStatement)node).getExpression();
	}
	switch (node.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			return node;
		case ASTNode.METHOD_INVOCATION:
		case ASTNode.SUPER_METHOD_INVOCATION:
		case ASTNode.CONSTRUCTOR_INVOCATION:
			return unit instanceof ICompilationUnit ? node : null; // don't start on invocations in binary
	}
	return null;
}
 
private static ASTNode checkNode(ASTNode node) {
		if (node == null)
			return null;
		if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
			node= node.getParent();
		} else if (node.getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
			node= ((ExpressionStatement)node).getExpression();
		}
		switch(node.getNodeType()) {
			case ASTNode.METHOD_DECLARATION:
			case ASTNode.METHOD_INVOCATION:
// not yet...
//			case ASTNode.SUPER_METHOD_INVOCATION:
//			case ASTNode.CONSTRUCTOR_INVOCATION:
				return node;
		}
		return null;
	}
 
private static ASTNode checkNode(ASTNode node) {
	if (node == null)
		return null;
	if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
		node= node.getParent();
	} else if (node.getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
		node= ((ExpressionStatement) node).getExpression();
	}
	switch (node.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
		case ASTNode.METHOD_DECLARATION:
		case ASTNode.SUPER_METHOD_INVOCATION:
			return node;
	}
	return null;
}
 
private void initializeTargetNode() {
	ASTNode parent= fInvocation.getParent();
	int nodeType= parent.getNodeType();
	if (nodeType == ASTNode.EXPRESSION_STATEMENT || nodeType == ASTNode.RETURN_STATEMENT) {
		fTargetNode= parent;
	} else {
		fTargetNode= fInvocation;
	}
}
 
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);
}
 
源代码7 项目: 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());
  }
}
 
private void checkInvocationContext(RefactoringStatus result, int severity) {
	if (fInvocation.getNodeType() == ASTNode.METHOD_INVOCATION) {
		if (((MethodInvocation)fInvocation).resolveTypeBinding() == null) {
			addEntry(result, RefactoringCoreMessages.CallInliner_receiver_type,
				RefactoringStatusCodes.INLINE_METHOD_NULL_BINDING, severity);
			return;
		}
	}
	int nodeType= fTargetNode.getNodeType();
	if (nodeType == ASTNode.EXPRESSION_STATEMENT) {
		if (fSourceProvider.isExecutionFlowInterrupted()) {
			addEntry(result, RefactoringCoreMessages.CallInliner_execution_flow,
				RefactoringStatusCodes.INLINE_METHOD_EXECUTION_FLOW, severity);
			return;
		}
	} else if (nodeType == ASTNode.METHOD_INVOCATION) {
		ASTNode parent= fTargetNode.getParent();
		if (isReturnStatement(parent)) {
			//support inlining even if the execution flow is interrupted
			return;
		}
		if (fSourceProvider.isExecutionFlowInterrupted()) {
			addEntry(result, RefactoringCoreMessages.CallInliner_execution_flow,
				RefactoringStatusCodes.INLINE_METHOD_EXECUTION_FLOW, severity);
			return;
		}
		if (isAssignment(parent) || isSingleDeclaration(parent)) {
			// we support inlining expression in assigment and initializers as
			// long as the execution flow isn't interrupted.
			return;
		} else {
			boolean isFieldDeclaration= ASTNodes.getParent(fInvocation, FieldDeclaration.class) != null;
			if (!fSourceProvider.isSimpleFunction()) {
				if (isMultiDeclarationFragment(parent)) {
					addEntry(result, RefactoringCoreMessages.CallInliner_multiDeclaration,
						RefactoringStatusCodes.INLINE_METHOD_INITIALIZER_IN_FRAGEMENT, severity);
				} else if (isFieldDeclaration) {
					addEntry(result,
						RefactoringCoreMessages.CallInliner_field_initializer_simple,
						RefactoringStatusCodes.INLINE_METHOD_FIELD_INITIALIZER, severity);
				} else {
					addEntry(result, RefactoringCoreMessages.CallInliner_simple_functions,
						RefactoringStatusCodes.INLINE_METHOD_ONLY_SIMPLE_FUNCTIONS, severity);
				}
				return;
			}
			if (isFieldDeclaration) {
				int argumentsCount= fContext.arguments.length;
				for (int i= 0; i < argumentsCount; i++) {
					ParameterData parameter= fSourceProvider.getParameterData(i);
					if(parameter.isWrite()) {
						addEntry(result,
							RefactoringCoreMessages.CallInliner_field_initialize_write_parameter,
							RefactoringStatusCodes.INLINE_METHOD_FIELD_INITIALIZER, severity);
						return;
					}
				}
				if(fLocals.size() > 0) {
					addEntry(result,
						RefactoringCoreMessages.CallInliner_field_initialize_new_local,
						RefactoringStatusCodes.INLINE_METHOD_FIELD_INITIALIZER, severity);
					return;
				}
				// verify that the field is not referenced by the initializer method
				VariableDeclarationFragment variable= (VariableDeclarationFragment)ASTNodes.getParent(fInvocation, ASTNode.VARIABLE_DECLARATION_FRAGMENT);
				if(fSourceProvider.isVariableReferenced(variable.resolveBinding())) {
					addEntry(result,
						RefactoringCoreMessages.CallInliner_field_initialize_self_reference,
						RefactoringStatusCodes.INLINE_METHOD_FIELD_INITIALIZER, severity);
					return;
				}
			}
		}
	}
}
 
/**
 * Remove the field or variable declaration including the initializer.
 * @param rewrite the AST rewriter to use
 * @param reference a reference to the variable to remove
 * @param group the text edit group to use
 */
private void removeVariableReferences(ASTRewrite rewrite, SimpleName reference, TextEditGroup group) {
	ASTNode parent= reference.getParent();
	while (parent instanceof QualifiedName) {
		parent= parent.getParent();
	}
	if (parent instanceof FieldAccess) {
		parent= parent.getParent();
	}

	int nameParentType= parent.getNodeType();
	if (nameParentType == ASTNode.ASSIGNMENT) {
		Assignment assignment= (Assignment) parent;
		Expression rightHand= assignment.getRightHandSide();

		ASTNode assignParent= assignment.getParent();
		if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT && rightHand.getNodeType() != ASTNode.ASSIGNMENT) {
			removeVariableWithInitializer(rewrite, rightHand, assignParent, group);
		}	else {
			rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), group);
		}
	} else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
		rewrite.remove(parent, group);
	} else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
		VariableDeclarationFragment frag= (VariableDeclarationFragment) parent;
		ASTNode varDecl= frag.getParent();
		List<VariableDeclarationFragment> fragments;
		if (varDecl instanceof VariableDeclarationExpression) {
			fragments= ((VariableDeclarationExpression) varDecl).fragments();
		} else if (varDecl instanceof FieldDeclaration) {
			fragments= ((FieldDeclaration) varDecl).fragments();
		} else {
			fragments= ((VariableDeclarationStatement) varDecl).fragments();
		}
		Expression initializer = frag.getInitializer();
		ArrayList<Expression> sideEffects= new ArrayList<Expression>();
		if (initializer != null) {
			initializer.accept(new SideEffectFinder(sideEffects));
		}
		boolean sideEffectInitializer= sideEffects.size() > 0;
		if (fragments.size() == fUnusedNames.length) {
			if (fForceRemove) {
				rewrite.remove(varDecl, group);
				return;
			}
			if (parent.getParent() instanceof FieldDeclaration) {
				rewrite.remove(varDecl, group);
				return;
			}
			if (sideEffectInitializer) {
				Statement[] wrapped= new Statement[sideEffects.size()];
				for (int i= 0; i < wrapped.length; i++) {
					Expression sideEffect= sideEffects.get(i);
					Expression movedInit= (Expression) rewrite.createMoveTarget(sideEffect);
					wrapped[i]= rewrite.getAST().newExpressionStatement(movedInit);
				}
				StatementRewrite statementRewrite= new StatementRewrite(rewrite, new ASTNode[] { varDecl });
				statementRewrite.replace(wrapped, group);
			} else {
				rewrite.remove(varDecl, group);
			}
		} else {
			if (fForceRemove) {
				rewrite.remove(frag, group);
				return;
			}
			//multiple declarations in one line
			ASTNode declaration = parent.getParent();
			if (declaration instanceof FieldDeclaration) {
				rewrite.remove(frag, group);
				return;
			}
			if (declaration instanceof VariableDeclarationStatement) {
				splitUpDeclarations(rewrite, group, frag, (VariableDeclarationStatement) declaration, sideEffects);
				rewrite.remove(frag, group);
				return;
			}
			if (declaration instanceof VariableDeclarationExpression) {
				//keep constructors and method invocations
				if (!sideEffectInitializer){
					rewrite.remove(frag, group);
				}
			}
		}
	} else if (nameParentType == ASTNode.POSTFIX_EXPRESSION || nameParentType == ASTNode.PREFIX_EXPRESSION) {
		Expression expression= (Expression)parent;
		ASTNode expressionParent= expression.getParent();
		if (expressionParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
			removeStatement(rewrite, expressionParent, group);
		} else {
			rewrite.remove(expression, group);
		}
	}
}
 
/**
 * Remove the field or variable declaration including the initializer.
 * @param rewrite the ast rewrite
 * @param reference the reference
 */
private void removeVariableReferences(ASTRewrite rewrite, SimpleName reference) {
	ASTNode parent= reference.getParent();
	while (parent instanceof QualifiedName) {
		parent= parent.getParent();
	}
	if (parent instanceof FieldAccess) {
		parent= parent.getParent();
	}

	int nameParentType= parent.getNodeType();
	if (nameParentType == ASTNode.ASSIGNMENT) {
		Assignment assignment= (Assignment) parent;
		Expression rightHand= assignment.getRightHandSide();

		ASTNode assignParent= assignment.getParent();
		if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT && rightHand.getNodeType() != ASTNode.ASSIGNMENT) {
			removeVariableWithInitializer(rewrite, rightHand, assignParent);
		}	else {
			rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), null);
		}
	} else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
		rewrite.remove(parent, null);
	} else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
		VariableDeclarationFragment frag= (VariableDeclarationFragment) parent;
		ASTNode varDecl= frag.getParent();
		List<VariableDeclarationFragment> fragments;
		if (varDecl instanceof VariableDeclarationExpression) {
			fragments= ((VariableDeclarationExpression) varDecl).fragments();
		} else if (varDecl instanceof FieldDeclaration) {
			fragments= ((FieldDeclaration) varDecl).fragments();
		} else {
			fragments= ((VariableDeclarationStatement) varDecl).fragments();
		}
		if (fragments.size() == 1) {
			rewrite.remove(varDecl, null);
		} else {
			rewrite.remove(frag, null); // don't try to preserve
		}
	}
}