001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.psi;
018
019 import com.intellij.lang.ASTNode;
020 import com.intellij.openapi.diagnostic.Logger;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.annotations.Nullable;
023 import org.jetbrains.kotlin.lexer.JetToken;
024 import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
025 import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
026
027 import static org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes.INSIDE_DIRECTIVE_EXPRESSIONS;
028
029 public class JetDotQualifiedExpression extends JetExpressionImplStub<KotlinPlaceHolderStub<JetDotQualifiedExpression>>
030 implements JetQualifiedExpression {
031
032 private static final Logger LOG = Logger.getInstance(JetDotQualifiedExpression.class);
033
034 public JetDotQualifiedExpression(@NotNull ASTNode node) {
035 super(node);
036 }
037
038 public JetDotQualifiedExpression(@NotNull KotlinPlaceHolderStub<JetDotQualifiedExpression> stub) {
039 super(stub, JetStubElementTypes.DOT_QUALIFIED_EXPRESSION);
040 }
041
042 @Override
043 public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
044 return visitor.visitDotQualifiedExpression(this, data);
045 }
046
047 @NotNull
048 @Override
049 public JetExpression getReceiverExpression() {
050 KotlinPlaceHolderStub<JetDotQualifiedExpression> stub = getStub();
051 if (stub != null) {
052 JetExpression[] childExpressionsByStub = getChildExpressionsByStub(stub);
053 if (childExpressionsByStub != null) {
054 return childExpressionsByStub[0];
055 }
056 }
057 return JetQualifiedExpressionImpl.INSTANCE$.getReceiverExpression(this);
058 }
059
060 @Nullable
061 @Override
062 public JetExpression getSelectorExpression() {
063 KotlinPlaceHolderStub<JetDotQualifiedExpression> stub = getStub();
064 if (stub != null) {
065 JetExpression[] childExpressionsByStub = getChildExpressionsByStub(stub);
066 if (childExpressionsByStub != null && childExpressionsByStub.length == 2) {
067 return childExpressionsByStub[1];
068 }
069 }
070 return JetQualifiedExpressionImpl.INSTANCE$.getSelectorExpression(this);
071 }
072
073
074 @Nullable
075 private JetExpression[] getChildExpressionsByStub(@NotNull KotlinPlaceHolderStub<JetDotQualifiedExpression> stub) {
076 if (stub.getParentStubOfType(JetImportDirective.class) == null && stub.getParentStubOfType(JetPackageDirective.class) == null) {
077 LOG.error("JetDotQualifiedExpression should only have stubs inside import or package directives.\n " +
078 "Stubs were created for:\n " + getText() +
079 "\nFile text:\n" + getContainingFile().getText());
080 return null;
081 }
082 else {
083 JetExpression[] expressions = stub.getChildrenByType(INSIDE_DIRECTIVE_EXPRESSIONS, JetExpression.ARRAY_FACTORY);
084 if (expressions.length < 1 || expressions.length > 2) {
085 LOG.error("Invalid stub structure. DOT_QUALIFIED_EXPRESSION must have one or two children. Was: " + expressions.length +
086 "\nFile text:\n" + getContainingFile().getText());
087 return null;
088 }
089 return expressions;
090 }
091 }
092
093 @NotNull
094 @Override
095 public ASTNode getOperationTokenNode() {
096 return JetQualifiedExpressionImpl.INSTANCE$.getOperationTokenNode(this);
097 }
098
099 @NotNull
100 @Override
101 public JetToken getOperationSign() {
102 return JetQualifiedExpressionImpl.INSTANCE$.getOperationSign(this);
103 }
104 }