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.fileClasses.FileClasses;
026    import org.jetbrains.kotlin.fileClasses.JvmFileClassesProvider;
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(
038                @NotNull DeclarationDescriptor descriptor,
039                @Nullable Type nameFromDescriptors,
040                @NotNull JvmFileClassesProvider fileClassesManager
041        ) {
042            PsiElement element = descriptorToDeclaration(descriptor);
043            if (element instanceof KtDeclaration) {
044                String classNameFromPsi = getPredefinedJvmInternalName((KtDeclaration) element, fileClassesManager);
045                assert classNameFromPsi == null || Type.getObjectType(classNameFromPsi).equals(nameFromDescriptors) :
046                        String.format("Invalid algorithm for getting qualified name from psi! Predicted: %s, actual %s\n" +
047                                      "Element: %s", classNameFromPsi, nameFromDescriptors, element.getText());
048            }
049    
050            return true;
051        }
052    
053        /**
054         * @return null if no prediction can be done.
055         */
056        @Nullable
057        public static String getPredefinedJvmInternalName(
058                @NotNull KtDeclaration declaration,
059                @NotNull JvmFileClassesProvider fileClassesProvider
060        ) {
061            // TODO: Method won't work for declarations inside companion objects
062            // TODO: Method won't give correct class name for traits implementations
063    
064            KtDeclaration parentDeclaration = KtStubbedPsiUtil.getContainingDeclaration(declaration);
065    
066            String parentInternalName;
067            if (parentDeclaration != null) {
068                parentInternalName = getPredefinedJvmInternalName(parentDeclaration, fileClassesProvider);
069                if (parentInternalName == null) {
070                    return null;
071                }
072            }
073            else {
074                KtFile containingFile = declaration.getContainingJetFile();
075    
076                if (declaration instanceof KtNamedFunction) {
077                    Name name = ((KtNamedFunction) declaration).getNameAsName();
078                    return name == null ? null : FileClasses.getFileClassInternalName(fileClassesProvider, containingFile) + "$" + name.asString();
079                }
080    
081                parentInternalName = AsmUtil.internalNameByFqNameWithoutInnerClasses(containingFile.getPackageFqName());
082            }
083    
084            if (!PsiTreeUtil.instanceOf(declaration, KtClass.class, KtObjectDeclaration.class, KtNamedFunction.class, KtProperty.class) ||
085                isEnumEntryWithoutBody(declaration)) {
086                // Other subclasses are not valid for class name prediction.
087                // For example JetFunctionLiteral
088                return null;
089            }
090    
091            Name name = ((KtNamedDeclaration) declaration).getNameAsName();
092            if (name == null) {
093                return null;
094            }
095    
096            if (declaration instanceof KtNamedFunction) {
097                if (!(parentDeclaration instanceof KtClass || parentDeclaration instanceof KtObjectDeclaration)) {
098                    // Can't generate predefined name for internal functions
099                    return null;
100                }
101            }
102    
103            // NOTE: looks like a bug - for class in getter of top level property class name will be $propertyName$ClassName but not
104            // PackageClassName$propertyName$ClassName
105            if (declaration instanceof KtProperty) {
106                return parentInternalName + "$" + name.asString();
107            }
108    
109            if (parentInternalName.isEmpty()) {
110                return name.asString();
111            }
112    
113            return parentInternalName + (parentDeclaration == null ? "/" : "$") + name.asString();
114        }
115    
116        private static boolean isEnumEntryWithoutBody(KtDeclaration declaration) {
117            if (!(declaration instanceof KtEnumEntry)) {
118                return false;
119            }
120            KtClassBody body = ((KtEnumEntry) declaration).getBody();
121            return body == null || body.getDeclarations().size() == 0;
122        }
123    }