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 @Nullable
054 public KtTypeArgumentList getTypeArgumentList() {
055 return getStubOrPsiChild(KtStubElementTypes.TYPE_ARGUMENT_LIST);
056 }
057
058 @NotNull
059 public List<KtTypeProjection> getTypeArguments() {
060 // TODO: empty elements in PSI
061 KtTypeArgumentList typeArgumentList = getTypeArgumentList();
062 return typeArgumentList == null ? Collections.<KtTypeProjection>emptyList() : typeArgumentList.getArguments();
063 }
064
065 @NotNull
066 @Override
067 public List<KtTypeReference> getTypeArgumentsAsTypes() {
068 List<KtTypeReference> result = Lists.newArrayList();
069 for (KtTypeProjection projection : getTypeArguments()) {
070 result.add(projection.getTypeReference());
071 }
072 return result;
073 }
074
075 @Nullable @IfNotParsed
076 public KtSimpleNameExpression getReferenceExpression() {
077 KtNameReferenceExpression nameRefExpr = getStubOrPsiChild(KtStubElementTypes.REFERENCE_EXPRESSION);
078 return nameRefExpr != null ? nameRefExpr : getStubOrPsiChild(KtStubElementTypes.ENUM_ENTRY_SUPERCLASS_REFERENCE_EXPRESSION);
079 }
080
081 @Nullable
082 public KtUserType getQualifier() {
083 return getStubOrPsiChild(KtStubElementTypes.USER_TYPE);
084 }
085
086 public boolean hasTypesWithTypeArgsInside() {
087 for (KtUserType type : getStubOrPsiChildrenAsList(KtStubElementTypes.USER_TYPE)) {
088 if (!type.getTypeArguments().isEmpty() || type.hasTypesWithTypeArgsInside()) {
089 return true;
090 }
091 }
092 return false;
093 }
094
095 public void deleteQualifier() {
096 KtUserType qualifier = getQualifier();
097 assert qualifier != null;
098 PsiElement dot = findChildByType(KtTokens.DOT);
099 assert dot != null;
100 qualifier.delete();
101 dot.delete();
102 }
103
104 @Nullable
105 public String getReferencedName() {
106 KtSimpleNameExpression referenceExpression = getReferenceExpression();
107 return referenceExpression == null ? null : referenceExpression.getReferencedName();
108 }
109 }