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 @Override
041 public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
042 return visitor.visitUserType(this, data);
043 }
044
045 @Nullable
046 public KtTypeArgumentList getTypeArgumentList() {
047 return getStubOrPsiChild(KtStubElementTypes.TYPE_ARGUMENT_LIST);
048 }
049
050 @NotNull
051 public List<KtTypeProjection> getTypeArguments() {
052 // TODO: empty elements in PSI
053 KtTypeArgumentList typeArgumentList = getTypeArgumentList();
054 return typeArgumentList == null ? Collections.<KtTypeProjection>emptyList() : typeArgumentList.getArguments();
055 }
056
057 @NotNull
058 @Override
059 public List<KtTypeReference> getTypeArgumentsAsTypes() {
060 List<KtTypeReference> result = Lists.newArrayList();
061 for (KtTypeProjection projection : getTypeArguments()) {
062 result.add(projection.getTypeReference());
063 }
064 return result;
065 }
066
067 @Nullable @IfNotParsed
068 public KtSimpleNameExpression getReferenceExpression() {
069 KtNameReferenceExpression nameRefExpr = getStubOrPsiChild(KtStubElementTypes.REFERENCE_EXPRESSION);
070 return nameRefExpr != null ? nameRefExpr : getStubOrPsiChild(KtStubElementTypes.ENUM_ENTRY_SUPERCLASS_REFERENCE_EXPRESSION);
071 }
072
073 @Nullable
074 public KtUserType getQualifier() {
075 return getStubOrPsiChild(KtStubElementTypes.USER_TYPE);
076 }
077
078 public void deleteQualifier() {
079 KtUserType qualifier = getQualifier();
080 assert qualifier != null;
081 PsiElement dot = findChildByType(KtTokens.DOT);
082 assert dot != null;
083 qualifier.delete();
084 dot.delete();
085 }
086
087 @Nullable
088 public String getReferencedName() {
089 KtSimpleNameExpression referenceExpression = getReferenceExpression();
090 return referenceExpression == null ? null : referenceExpression.getReferencedName();
091 }
092 }