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        @NotNull
038        public static FqName kotlinFqNameToJavaFqName(@NotNull FqNameUnsafe kotlinFqName) {
039            List<Name> segments = kotlinFqName.pathSegments();
040            List<String> correctedSegments = new ArrayList<String>(segments.size());
041            for (Name segment : segments) {
042                correctedSegments.add(isClassObjectName(segment) ? JvmAbi.CLASS_OBJECT_CLASS_NAME : segment.getIdentifier());
043            }
044            return FqName.fromSegments(correctedSegments);
045        }
046    
047        @NotNull
048        public static FqNameUnsafe naiveKotlinFqName(@NotNull ClassDescriptor descriptor) {
049            DeclarationDescriptor containing = descriptor.getContainingDeclaration();
050            if (containing instanceof ClassDescriptor) {
051                return naiveKotlinFqName((ClassDescriptor) containing).child(descriptor.getName());
052            }
053            else if (containing instanceof PackageFragmentDescriptor) {
054                return ((PackageFragmentDescriptor) containing).getFqName().child(descriptor.getName()).toUnsafe();
055            }
056            else {
057                throw new IllegalArgumentException("Class doesn't have a FQ name: " + descriptor);
058            }
059        }
060    }