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.google.common.collect.Lists;
020    import com.intellij.lang.ASTNode;
021    import com.intellij.psi.PsiElement;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.annotations.Nullable;
024    import org.jetbrains.kotlin.lexer.JetTokens;
025    import org.jetbrains.kotlin.psi.stubs.KotlinUserTypeStub;
026    import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
027    
028    import java.util.Collections;
029    import java.util.List;
030    
031    public class JetUserType extends JetElementImplStub<KotlinUserTypeStub> implements JetTypeElement {
032        public JetUserType(@NotNull ASTNode node) {
033            super(node);
034        }
035    
036        public JetUserType(@NotNull KotlinUserTypeStub stub) {
037            super(stub, JetStubElementTypes.USER_TYPE);
038        }
039    
040        public boolean isAbsoluteInRootPackage() {
041            KotlinUserTypeStub stub = getStub();
042            if (stub != null) {
043                return stub.isAbsoluteInRootPackage();
044            }
045            return findChildByType(JetTokens.PACKAGE_KEYWORD) != null;
046        }
047    
048        @Override
049        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
050            return visitor.visitUserType(this, data);
051        }
052    
053        public JetTypeArgumentList getTypeArgumentList() {
054            return getStubOrPsiChild(JetStubElementTypes.TYPE_ARGUMENT_LIST);
055        }
056    
057        @NotNull
058        public List<JetTypeProjection> getTypeArguments() {
059            // TODO: empty elements in PSI
060            JetTypeArgumentList typeArgumentList = getTypeArgumentList();
061            return typeArgumentList == null ? Collections.<JetTypeProjection>emptyList() : typeArgumentList.getArguments();
062        }
063    
064        @NotNull
065        @Override
066        public List<JetTypeReference> getTypeArgumentsAsTypes() {
067            List<JetTypeReference> result = Lists.newArrayList();
068            for (JetTypeProjection projection : getTypeArguments()) {
069                result.add(projection.getTypeReference());
070            }
071            return result;
072        }
073    
074        @Nullable @IfNotParsed
075        public JetSimpleNameExpression getReferenceExpression() {
076            JetNameReferenceExpression nameRefExpr = getStubOrPsiChild(JetStubElementTypes.REFERENCE_EXPRESSION);
077            return nameRefExpr != null ? nameRefExpr : getStubOrPsiChild(JetStubElementTypes.ENUM_ENTRY_SUPERCLASS_REFERENCE_EXPRESSION);
078        }
079    
080        @Nullable
081        public JetUserType getQualifier() {
082            return getStubOrPsiChild(JetStubElementTypes.USER_TYPE);
083        }
084    
085        public void deleteQualifier() {
086            JetUserType qualifier = getQualifier();
087            assert qualifier != null;
088            PsiElement dot = findChildByType(JetTokens.DOT);
089            assert dot != null;
090            qualifier.delete();
091            dot.delete();
092        }
093    
094        @Nullable
095        public String getReferencedName() {
096            JetSimpleNameExpression referenceExpression = getReferenceExpression();
097            return referenceExpression == null ? null : referenceExpression.getReferencedName();
098        }
099    }