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.lexer.JetTokens;
025    
026    import java.util.Collections;
027    import java.util.List;
028    
029    public class JetUserType extends JetTypeElement {
030        public JetUserType(@NotNull ASTNode node) {
031            super(node);
032        }
033    
034        public boolean isAbsoluteInRootNamespace() {
035            return findChildByType(JetTokens.PACKAGE_KEYWORD) != null;
036        }
037    
038        @Override
039        public void accept(@NotNull JetVisitorVoid visitor) {
040            visitor.visitUserType(this);
041        }
042    
043        @Override
044        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
045            return visitor.visitUserType(this, data);
046        }
047    
048        public JetTypeArgumentList getTypeArgumentList() {
049            return (JetTypeArgumentList) findChildByType(JetNodeTypes.TYPE_ARGUMENT_LIST);
050        }
051    
052        @NotNull
053        public List<JetTypeProjection> getTypeArguments() {
054            // TODO: empty elements in PSI
055            JetTypeArgumentList typeArgumentList = getTypeArgumentList();
056            return typeArgumentList == null ? Collections.<JetTypeProjection>emptyList() : typeArgumentList.getArguments();
057        }
058    
059        @NotNull
060        @Override
061        public List<JetTypeReference> getTypeArgumentsAsTypes() {
062            List<JetTypeReference> result = Lists.newArrayList();
063            for (JetTypeProjection projection : getTypeArguments()) {
064                result.add(projection.getTypeReference());
065            }
066            return result;
067        }
068    
069        @Nullable @IfNotParsed
070        public JetSimpleNameExpression getReferenceExpression() {
071            return (JetSimpleNameExpression) findChildByType(JetNodeTypes.REFERENCE_EXPRESSION);
072        }
073    
074        @Nullable
075        public JetUserType getQualifier() {
076            return (JetUserType) findChildByType(JetNodeTypes.USER_TYPE);
077        }
078    
079        @Nullable
080        public String getReferencedName() {
081            JetSimpleNameExpression referenceExpression = getReferenceExpression();
082            return referenceExpression == null ? null : referenceExpression.getReferencedName();
083        }
084    }