001    /*
002     * Copyright 2010-2013 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.jet.lang.psi;
018    
019    import com.google.common.collect.Lists;
020    import com.intellij.lang.ASTNode;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.jet.JetNodeTypes;
024    import org.jetbrains.jet.lang.psi.stubs.PsiJetUserTypeStub;
025    import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
026    import org.jetbrains.jet.lexer.JetTokens;
027    
028    import java.util.Collections;
029    import java.util.List;
030    
031    public class JetUserType extends JetElementImplStub<PsiJetUserTypeStub> implements JetTypeElement {
032        public JetUserType(@NotNull ASTNode node) {
033            super(node);
034        }
035    
036        public JetUserType(@NotNull PsiJetUserTypeStub stub) {
037            super(stub, JetStubElementTypes.USER_TYPE);
038        }
039    
040        public boolean isAbsoluteInRootPackage() {
041            PsiJetUserTypeStub 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 JetNameReferenceExpression getReferenceExpression() {
076            return getStubOrPsiChild(JetStubElementTypes.REFERENCE_EXPRESSION);
077        }
078    
079        @Nullable
080        public JetUserType getQualifier() {
081            return getStubOrPsiChild(JetStubElementTypes.USER_TYPE);
082        }
083    
084        @Nullable
085        public String getReferencedName() {
086            JetNameReferenceExpression referenceExpression = getReferenceExpression();
087            return referenceExpression == null ? null : referenceExpression.getReferencedName();
088        }
089    }