update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / impl / source / tree / java / PsiExpressionStatementImpl.java
blobe1373dcb05567c90157ccfec25e3a2e0804dc742
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.psi.impl.source.tree.java;
18 import com.intellij.lang.ASTNode;
19 import com.intellij.openapi.diagnostic.Logger;
20 import com.intellij.psi.*;
21 import com.intellij.psi.impl.DebugUtil;
22 import com.intellij.psi.impl.source.SourceTreeToPsiMap;
23 import com.intellij.psi.impl.source.tree.*;
24 import com.intellij.psi.tree.ChildRoleBase;
25 import com.intellij.psi.tree.IElementType;
26 import org.jetbrains.annotations.NotNull;
28 public class PsiExpressionStatementImpl extends CompositePsiElement implements PsiExpressionStatement {
29 private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.tree.java.PsiExpressionStatementImpl");
31 public PsiExpressionStatementImpl() {
32 super(JavaElementType.EXPRESSION_STATEMENT);
35 @NotNull
36 public PsiExpression getExpression() {
37 PsiExpression expression = (PsiExpression)SourceTreeToPsiMap.treeElementToPsi(findChildByType(ElementType.EXPRESSION_BIT_SET));
38 if (expression != null) return expression;
39 LOG.error("Illegal PSI. Children: " + DebugUtil.psiToString(this, false));
40 return null;
43 public ASTNode findChildByRole(int role) {
44 LOG.assertTrue(ChildRole.isUnique(role));
45 switch(role){
46 default:
47 return null;
49 case ChildRole.EXPRESSION:
50 return findChildByType(ElementType.EXPRESSION_BIT_SET);
52 case ChildRole.CLOSING_SEMICOLON:
53 return TreeUtil.findChildBackward(this, JavaTokenType.SEMICOLON);
57 public int getChildRole(ASTNode child) {
58 LOG.assertTrue(child.getTreeParent() == this);
59 IElementType i = child.getElementType();
60 if (i == JavaTokenType.SEMICOLON) {
61 return ChildRole.CLOSING_SEMICOLON;
63 else {
64 if (ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())) {
65 return ChildRole.EXPRESSION;
67 return ChildRoleBase.NONE;
71 public void accept(@NotNull PsiElementVisitor visitor) {
72 if (visitor instanceof JavaElementVisitor) {
73 ((JavaElementVisitor)visitor).visitExpressionStatement(this);
75 else {
76 visitor.visitElement(this);
80 public String toString() {
81 return "PsiExpressionStatement";
84 public void deleteChildInternal(@NotNull ASTNode child) {
85 if (getChildRole(child) == ChildRole.EXPRESSION) {
86 getTreeParent().deleteChildInternal(this);
88 else {
89 super.deleteChildInternal(child);