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.KtTokens;
025 import org.jetbrains.kotlin.psi.stubs.KotlinUserTypeStub;
026 import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
027
028 import java.util.Collections;
029 import java.util.List;
030
031 public class KtUserType extends KtElementImplStub<KotlinUserTypeStub> implements KtTypeElement {
032 public KtUserType(@NotNull ASTNode node) {
033 super(node);
034 }
035
036 public KtUserType(@NotNull KotlinUserTypeStub stub) {
037 super(stub, KtStubElementTypes.USER_TYPE);
038 }
039
040 public boolean isAbsoluteInRootPackage() {
041 KotlinUserTypeStub stub = getStub();
042 if (stub != null) {
043 return stub.isAbsoluteInRootPackage();
044 }
045 return findChildByType(KtTokens.PACKAGE_KEYWORD) != null;
046 }
047
048 @Override
049 public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
050 return visitor.visitUserType(this, data);
051 }
052
053 public KtTypeArgumentList getTypeArgumentList() {
054 return getStubOrPsiChild(KtStubElementTypes.TYPE_ARGUMENT_LIST);
055 }
056
057 @NotNull
058 public List<KtTypeProjection> getTypeArguments() {
059 // TODO: empty elements in PSI
060 KtTypeArgumentList typeArgumentList = getTypeArgumentList();
061 return typeArgumentList == null ? Collections.<KtTypeProjection>emptyList() : typeArgumentList.getArguments();
062 }
063
064 @NotNull
065 @Override
066 public List<KtTypeReference> getTypeArgumentsAsTypes() {
067 List<KtTypeReference> result = Lists.newArrayList();
068 for (KtTypeProjection projection : getTypeArguments()) {
069 result.add(projection.getTypeReference());
070 }
071 return result;
072 }
073
074 @Nullable @IfNotParsed
075 public KtSimpleNameExpression getReferenceExpression() {
076 KtNameReferenceExpression nameRefExpr = getStubOrPsiChild(KtStubElementTypes.REFERENCE_EXPRESSION);
077 return nameRefExpr != null ? nameRefExpr : getStubOrPsiChild(KtStubElementTypes.ENUM_ENTRY_SUPERCLASS_REFERENCE_EXPRESSION);
078 }
079
080 @Nullable
081 public KtUserType getQualifier() {
082 return getStubOrPsiChild(KtStubElementTypes.USER_TYPE);
083 }
084
085 public boolean hasTypesWithTypeArgsInside() {
086 for (KtUserType type : getStubOrPsiChildrenAsList(KtStubElementTypes.USER_TYPE)) {
087 if (!type.getTypeArguments().isEmpty() || type.hasTypesWithTypeArgsInside()) {
088 return true;
089 }
090 }
091 return false;
092 }
093
094 public void deleteQualifier() {
095 KtUserType qualifier = getQualifier();
096 assert qualifier != null;
097 PsiElement dot = findChildByType(KtTokens.DOT);
098 assert dot != null;
099 qualifier.delete();
100 dot.delete();
101 }
102
103 @Nullable
104 public String getReferencedName() {
105 KtSimpleNameExpression referenceExpression = getReferenceExpression();
106 return referenceExpression == null ? null : referenceExpression.getReferencedName();
107 }
108 }