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.*;
025
026 import java.util.ArrayList;
027 import java.util.List;
028
029 import static org.jetbrains.jet.lang.resolve.name.SpecialNames.isClassObjectName;
030
031 public class DeserializedResolverUtils {
032 private DeserializedResolverUtils() {
033 }
034
035 @NotNull
036 public static FqName kotlinFqNameToJavaFqName(@NotNull FqNameUnsafe kotlinFqName) {
037 List<Name> segments = kotlinFqName.pathSegments();
038 List<String> correctedSegments = new ArrayList<String>(segments.size());
039 for (Name segment : segments) {
040 correctedSegments.add(isClassObjectName(segment) ? JvmAbi.CLASS_OBJECT_CLASS_NAME : segment.getIdentifier());
041 }
042 return FqName.fromSegments(correctedSegments);
043 }
044
045 @NotNull
046 public static ClassId kotlinClassIdToJavaClassId(@NotNull ClassId kotlinClassId) {
047 return new ClassId(kotlinClassId.getPackageFqName(), kotlinFqNameToJavaFqName(kotlinClassId.getRelativeClassName()).toUnsafe());
048 }
049
050 @NotNull
051 public static FqNameUnsafe javaFqNameToKotlinFqName(@NotNull FqName javaFqName) {
052 if (javaFqName.isRoot()) {
053 return javaFqName.toUnsafe();
054 }
055 List<Name> segments = javaFqName.pathSegments();
056 List<Name> correctedSegments = new ArrayList<Name>(segments.size());
057 correctedSegments.add(segments.get(0));
058 for (int i = 1; i < segments.size(); i++) {
059 Name segment = segments.get(i);
060 boolean isClassObjectName = segment.asString().equals(JvmAbi.CLASS_OBJECT_CLASS_NAME);
061 Name correctedSegment = isClassObjectName ? SpecialNames.getClassObjectName(segments.get(i - 1)) : segment;
062 correctedSegments.add(correctedSegment);
063 }
064 return FqNameUnsafe.fromSegments(correctedSegments);
065 }
066
067 @NotNull
068 public static ClassId javaClassIdToKotlinClassId(@NotNull ClassId javaClassId) {
069 return new ClassId(javaClassId.getPackageFqName(), javaFqNameToKotlinFqName(javaClassId.getRelativeClassName().toSafe()));
070 }
071
072 @NotNull
073 public static ClassId getClassId(@NotNull ClassDescriptor descriptor) {
074 DeclarationDescriptor owner = descriptor.getContainingDeclaration();
075 if (owner instanceof PackageFragmentDescriptor) {
076 return new ClassId(((PackageFragmentDescriptor) owner).getFqName(), descriptor.getName());
077 }
078 return getClassId((ClassDescriptor) owner).createNestedClassId(descriptor.getName());
079 }
080 }