001    /*
002     * Copyright 2010-2013 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.jet.lang.resolve.kotlin;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
021    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
022    import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor;
023    import org.jetbrains.jet.lang.resolve.java.JvmAbi;
024    import org.jetbrains.jet.lang.resolve.name.FqName;
025    import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
026    import org.jetbrains.jet.lang.resolve.name.Name;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    import static org.jetbrains.jet.lang.resolve.name.SpecialNames.isClassObjectName;
032    
033    public class DeserializedResolverUtils {
034        private DeserializedResolverUtils() {
035        }
036    
037        public static FqName kotlinFqNameToJavaFqName(@NotNull FqNameUnsafe kotlinFqName) {
038            return kotlinFqNameToJavaFqName(kotlinFqName, false);
039        }
040    
041        @NotNull
042        public static FqName kotlinFqNameToJavaFqName(@NotNull FqNameUnsafe kotlinFqName, boolean addTraitImplSuffix) {
043            List<Name> segments = kotlinFqName.pathSegments();
044            List<String> correctedSegments = new ArrayList<String>(segments.size());
045            for (Name segment : segments) {
046                correctedSegments.add(isClassObjectName(segment) ? JvmAbi.CLASS_OBJECT_CLASS_NAME : segment.getIdentifier());
047            }
048            if (addTraitImplSuffix) {
049                int lastIndex = correctedSegments.size() - 1;
050                correctedSegments.set(lastIndex, correctedSegments.get(lastIndex) + JvmAbi.TRAIT_IMPL_SUFFIX);
051            }
052            return FqName.fromSegments(correctedSegments);
053        }
054    
055        @NotNull
056        public static FqNameUnsafe naiveKotlinFqName(@NotNull ClassDescriptor descriptor) {
057            DeclarationDescriptor containing = descriptor.getContainingDeclaration();
058            if (containing instanceof ClassDescriptor) {
059                return naiveKotlinFqName((ClassDescriptor) containing).child(descriptor.getName());
060            }
061            else if (containing instanceof PackageFragmentDescriptor) {
062                return ((PackageFragmentDescriptor) containing).getFqName().child(descriptor.getName()).toUnsafe();
063            }
064            else {
065                throw new IllegalArgumentException("Class doesn't have a FQ name: " + descriptor);
066            }
067        }
068    }