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.intellij.psi.PsiElement; 020 import com.intellij.psi.util.PsiTreeUtil; 021 import org.jetbrains.annotations.NotNull; 022 import org.jetbrains.annotations.Nullable; 023 import org.jetbrains.kotlin.name.FqName; 024 import org.jetbrains.kotlin.name.FqNameUnsafe; 025 import org.jetbrains.kotlin.name.Name; 026 027 public final class JetNamedDeclarationUtil { 028 @Nullable 029 public static FqNameUnsafe getUnsafeFQName(@NotNull JetNamedDeclaration namedDeclaration) { 030 FqName fqName = namedDeclaration.getFqName(); 031 return fqName != null ? fqName.toUnsafe() : null; 032 } 033 034 @Nullable 035 //NOTE: use JetNamedDeclaration#getFqName instead 036 /*package private*/ static FqName getFQName(@NotNull JetNamedDeclaration namedDeclaration) { 037 Name name = namedDeclaration.getNameAsName(); 038 if (name == null) { 039 return null; 040 } 041 042 FqName parentFqName = getParentFqName(namedDeclaration); 043 044 if (parentFqName == null) { 045 return null; 046 } 047 048 return parentFqName.child(name); 049 } 050 051 @Nullable 052 public static FqName getParentFqName(@NotNull JetNamedDeclaration namedDeclaration) { 053 PsiElement parent = namedDeclaration.getParent(); 054 if (parent instanceof JetClassBody) { 055 // One nesting to JetClassBody doesn't affect to qualified name 056 parent = parent.getParent(); 057 } 058 059 if (parent instanceof JetFile) { 060 return ((JetFile) parent).getPackageFqName(); 061 } 062 else if (parent instanceof JetNamedFunction || parent instanceof JetClass) { 063 return getFQName((JetNamedDeclaration) parent); 064 } 065 else if (namedDeclaration instanceof JetParameter) { 066 JetClass constructorClass = JetPsiUtil.getClassIfParameterIsProperty((JetParameter) namedDeclaration); 067 if (constructorClass != null) { 068 return getFQName(constructorClass); 069 } 070 } 071 else if (parent instanceof JetObjectDeclaration) { 072 return getFQName((JetNamedDeclaration) parent); 073 } 074 return null; 075 } 076 077 private JetNamedDeclarationUtil() { 078 } 079 }