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.codegen.binding;
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.codegen.AsmUtil;
024 import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
025 import org.jetbrains.kotlin.load.java.JvmAbi;
026 import org.jetbrains.kotlin.load.kotlin.PackagePartClassUtils;
027 import org.jetbrains.kotlin.name.Name;
028 import org.jetbrains.kotlin.psi.*;
029 import org.jetbrains.org.objectweb.asm.Type;
030
031 import static org.jetbrains.kotlin.resolve.DescriptorToSourceUtils.descriptorToDeclaration;
032
033 public final class PsiCodegenPredictor {
034 private PsiCodegenPredictor() {
035 }
036
037 public static boolean checkPredictedNameFromPsi(@NotNull DeclarationDescriptor descriptor, @Nullable Type nameFromDescriptors) {
038 PsiElement element = descriptorToDeclaration(descriptor);
039 if (element instanceof JetDeclaration) {
040 String classNameFromPsi = getPredefinedJvmInternalName((JetDeclaration) element);
041 assert classNameFromPsi == null || Type.getObjectType(classNameFromPsi).equals(nameFromDescriptors) :
042 String.format("Invalid algorithm for getting qualified name from psi! Predicted: %s, actual %s\n" +
043 "Element: %s", classNameFromPsi, nameFromDescriptors, element.getText());
044 }
045
046 return true;
047 }
048
049 /**
050 * @return null if no prediction can be done.
051 */
052 @Nullable
053 public static String getPredefinedJvmInternalName(@NotNull JetDeclaration declaration) {
054 // TODO: Method won't work for declarations inside companion objects
055 // TODO: Method won't give correct class name for traits implementations
056
057 JetDeclaration parentDeclaration = JetStubbedPsiUtil.getContainingDeclaration(declaration);
058
059 String parentInternalName;
060 if (parentDeclaration != null) {
061 parentInternalName = getPredefinedJvmInternalName(parentDeclaration);
062 if (parentInternalName == null) {
063 return null;
064 }
065 }
066 else {
067 JetFile containingFile = declaration.getContainingJetFile();
068
069 if (declaration instanceof JetNamedFunction) {
070 Name name = ((JetNamedFunction) declaration).getNameAsName();
071 return name == null ? null : PackagePartClassUtils.getPackagePartInternalName(containingFile) + "$" + name.asString();
072 }
073
074 parentInternalName = AsmUtil.internalNameByFqNameWithoutInnerClasses(containingFile.getPackageFqName());
075 }
076
077 if (!PsiTreeUtil.instanceOf(declaration, JetClass.class, JetObjectDeclaration.class, JetNamedFunction.class, JetProperty.class) ||
078 declaration instanceof JetEnumEntry) {
079 // Other subclasses are not valid for class name prediction.
080 // For example EnumEntry, JetFunctionLiteral
081 return null;
082 }
083
084 Name name = ((JetNamedDeclaration) declaration).getNameAsName();
085 if (name == null) {
086 return null;
087 }
088
089 if (declaration instanceof JetNamedFunction) {
090 if (!(parentDeclaration instanceof JetClass || parentDeclaration instanceof JetObjectDeclaration)) {
091 // Can't generate predefined name for internal functions
092 return null;
093 }
094 }
095
096 // NOTE: looks like a bug - for class in getter of top level property class name will be $propertyName$ClassName but not
097 // PackageClassName$propertyName$ClassName
098 if (declaration instanceof JetProperty) {
099 return parentInternalName + "$" + name.asString();
100 }
101
102 if (parentInternalName.isEmpty()) {
103 return name.asString();
104 }
105
106 return parentInternalName + (parentDeclaration == null ? "/" : "$") + name.asString();
107 }
108 }